~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

Merge Monty.

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
 
 
23
22
  @defgroup Query_Optimizer  Query Optimizer
24
23
  @{
25
24
*/
26
 
 
27
 
#ifdef USE_PRAGMA_IMPLEMENTATION
28
 
#pragma implementation                          // gcc: Class implementation
29
 
#endif
30
 
 
31
 
#include "mysql_priv.h"
32
 
#include "sql_select.h"
33
 
 
34
 
#include <m_ctype.h>
35
 
#include <my_bit.h>
36
 
#include <hash.h>
37
 
 
38
 
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
39
 
                              "MAYBE_REF","ALL","range","index",
40
 
                              "ref_or_null","unique_subquery","index_subquery",
41
 
                              "index_merge"
42
 
};
43
 
 
44
 
struct st_sargable_param;
45
 
 
46
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
47
 
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
48
 
                                 DYNAMIC_ARRAY *keyuse);
49
 
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
50
 
                                JOIN_TAB *join_tab,
51
 
                                uint tables, COND *conds,
52
 
                                COND_EQUAL *cond_equal,
53
 
                                table_map table_map, SELECT_LEX *select_lex,
54
 
                                st_sargable_param **sargables);
55
 
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
56
 
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
57
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
58
 
                               table_map used_tables);
59
 
static bool choose_plan(JOIN *join,table_map join_tables);
60
 
 
61
 
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
62
 
                             table_map remaining_tables, uint idx,
63
 
                             double record_count, double read_time);
64
 
static void optimize_straight_join(JOIN *join, table_map join_tables);
65
 
static bool greedy_search(JOIN *join, table_map remaining_tables,
66
 
                             uint depth, uint prune_level);
67
 
static bool best_extension_by_limited_search(JOIN *join,
68
 
                                             table_map remaining_tables,
69
 
                                             uint idx, double record_count,
70
 
                                             double read_time, uint depth,
71
 
                                             uint prune_level);
72
 
static uint determine_search_depth(JOIN* join);
73
 
static int join_tab_cmp(const void* ptr1, const void* ptr2);
74
 
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
75
 
/*
76
 
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
77
 
  tested and approved.
78
 
*/
79
 
static bool find_best(JOIN *join,table_map rest_tables,uint index,
80
 
                      double record_count,double read_time);
81
 
static uint cache_record_length(JOIN *join,uint index);
82
 
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
83
 
static bool get_best_combination(JOIN *join);
84
 
static store_key *get_store_key(THD *thd,
85
 
                                KEYUSE *keyuse, table_map used_tables,
86
 
                                KEY_PART_INFO *key_part, uchar *key_buff,
87
 
                                uint maybe_null);
88
 
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
89
 
static void make_outerjoin_info(JOIN *join);
90
 
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
91
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
92
 
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
93
 
static void update_depend_map(JOIN *join);
94
 
static void update_depend_map(JOIN *join, ORDER *order);
95
 
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
96
 
                           bool change_list, bool *simple_order);
97
 
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
98
 
                            List<Item> &fields, bool send_row,
99
 
                            uint64_t select_options, const char *info,
100
 
                            Item *having);
101
 
static COND *build_equal_items(THD *thd, COND *cond,
 
25
#include "config.h"
 
26
 
 
27
#include <string>
 
28
#include <iostream>
 
29
#include <algorithm>
 
30
#include <vector>
 
31
 
 
32
#include "drizzled/sql_select.h" /* include join.h */
 
33
 
 
34
#include "drizzled/error.h"
 
35
#include "drizzled/gettext.h"
 
36
#include "drizzled/util/test.h"
 
37
#include "drizzled/name_resolution_context_state.h"
 
38
#include "drizzled/nested_join.h"
 
39
#include "drizzled/probes.h"
 
40
#include "drizzled/show.h"
 
41
#include "drizzled/plugin/info_schema_table.h"
 
42
#include "drizzled/item/cache.h"
 
43
#include "drizzled/item/cmpfunc.h"
 
44
#include "drizzled/item/copy_string.h"
 
45
#include "drizzled/item/uint.h"
 
46
#include "drizzled/cached_item.h"
 
47
#include "drizzled/sql_base.h"
 
48
#include "drizzled/field/blob.h"
 
49
#include "drizzled/check_stack_overrun.h"
 
50
#include "drizzled/lock.h"
 
51
#include "drizzled/item/outer_ref.h"
 
52
#include "drizzled/index_hint.h"
 
53
#include "drizzled/memory/multi_malloc.h"
 
54
#include "drizzled/records.h"
 
55
#include "drizzled/internal/iocache.h"
 
56
 
 
57
#include "drizzled/sql_union.h"
 
58
#include "drizzled/optimizer/key_field.h"
 
59
#include "drizzled/optimizer/position.h"
 
60
#include "drizzled/optimizer/sargable_param.h"
 
61
#include "drizzled/optimizer/key_use.h"
 
62
#include "drizzled/optimizer/range.h"
 
63
#include "drizzled/optimizer/quick_range_select.h"
 
64
#include "drizzled/optimizer/quick_ror_intersect_select.h"
 
65
 
 
66
using namespace std;
 
67
using namespace drizzled;
 
68
 
 
69
 
 
70
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b);
 
71
static COND *build_equal_items(Session *session, COND *cond,
102
72
                               COND_EQUAL *inherited,
103
 
                               List<TABLE_LIST> *join_list,
 
73
                               List<TableList> *join_list,
104
74
                               COND_EQUAL **cond_equal_ref);
105
 
static COND* substitute_for_best_equal_field(COND *cond,
106
 
                                             COND_EQUAL *cond_equal,
107
 
                                             void *table_join_idx);
108
 
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
109
 
                            COND *conds, bool top, bool in_sj);
110
 
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
111
 
static void restore_prev_nj_state(JOIN_TAB *last);
112
 
static void reset_nj_counters(List<TABLE_LIST> *join_list);
113
 
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
114
 
                                          uint first_unused);
115
 
 
116
 
static 
117
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
118
 
static void restore_prev_sj_state(const table_map remaining_tables, 
119
 
                                  const JOIN_TAB *tab);
120
 
 
121
 
static COND *optimize_cond(JOIN *join, COND *conds,
122
 
                           List<TABLE_LIST> *join_list,
123
 
                           Item::cond_result *cond_value);
124
 
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
125
 
static bool open_tmp_table(TABLE *table);
126
 
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
127
 
                                    MI_COLUMNDEF *start_recinfo,
128
 
                                    MI_COLUMNDEF **recinfo,
129
 
                                    uint64_t options);
130
 
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
131
 
 
132
 
static enum_nested_loop_state
133
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
134
 
                     int error);
135
 
static enum_nested_loop_state
136
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
137
 
static enum_nested_loop_state
138
 
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
139
 
static enum_nested_loop_state
140
 
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
141
 
static enum_nested_loop_state
142
 
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
143
 
static enum_nested_loop_state
144
 
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
145
 
static enum_nested_loop_state
146
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
147
 
 
148
 
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
149
 
static int join_read_system(JOIN_TAB *tab);
150
 
static int join_read_const(JOIN_TAB *tab);
151
 
static int join_read_key(JOIN_TAB *tab);
152
 
static int join_read_always_key(JOIN_TAB *tab);
153
 
static int join_read_last_key(JOIN_TAB *tab);
154
 
static int join_no_more_records(READ_RECORD *info);
155
 
static int join_read_next(READ_RECORD *info);
156
 
static int join_read_next_different(READ_RECORD *info);
157
 
static int join_init_quick_read_record(JOIN_TAB *tab);
158
 
static int test_if_quick_select(JOIN_TAB *tab);
159
 
static int join_init_read_record(JOIN_TAB *tab);
160
 
static int join_read_first(JOIN_TAB *tab);
161
 
static int join_read_next_same(READ_RECORD *info);
162
 
static int join_read_next_same_diff(READ_RECORD *info);
163
 
static int join_read_last(JOIN_TAB *tab);
164
 
static int join_read_prev_same(READ_RECORD *info);
165
 
static int join_read_prev(READ_RECORD *info);
166
 
int join_read_always_key_or_null(JOIN_TAB *tab);
167
 
int join_read_next_same_or_null(READ_RECORD *info);
168
 
static COND *make_cond_for_table(COND *cond,table_map table,
169
 
                                 table_map used_table,
170
 
                                 bool exclude_expensive_cond);
171
 
static Item* part_of_refkey(TABLE *form,Field *field);
172
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
173
 
                                    ha_rows select_limit, bool no_changes,
174
 
                                    const key_map *map);
175
 
static bool list_contains_unique_index(TABLE *table,
176
 
                          bool (*find_func) (Field *, void *), void *data);
177
 
static bool find_field_in_item_list (Field *field, void *data);
178
 
static bool find_field_in_order_list (Field *field, void *data);
179
 
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
180
 
                             ha_rows filesort_limit, ha_rows select_limit,
181
 
                             bool is_order_by);
182
 
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
183
 
                             Item *having);
184
 
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
185
 
                                   ulong offset,Item *having);
186
 
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
187
 
                                      uint field_count, Field **first_field,
188
 
 
189
 
                                      ulong key_length,Item *having);
190
 
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
191
 
static ulong used_blob_length(CACHE_FIELD **ptr);
192
 
static bool store_record_in_cache(JOIN_CACHE *cache);
193
 
static void reset_cache_read(JOIN_CACHE *cache);
194
 
static void reset_cache_write(JOIN_CACHE *cache);
195
 
static void read_cached_record(JOIN_TAB *tab);
196
 
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
197
 
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
198
 
                                    ORDER *order, List<Item> &fields,
199
 
                                    List<Item> &all_fields,
200
 
                                    bool *all_order_by_fields_used);
201
 
static bool test_if_subpart(ORDER *a,ORDER *b);
202
 
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
203
 
static void calc_group_buffer(JOIN *join,ORDER *group);
204
 
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
205
 
static bool alloc_group_fields(JOIN *join,ORDER *group);
206
 
// Create list for using with tempory table
207
 
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
208
 
                                     List<Item> &new_list1,
209
 
                                     List<Item> &new_list2,
210
 
                                     uint elements, List<Item> &items);
211
 
// Create list for using with tempory table
212
 
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
213
 
                                      List<Item> &new_list1,
214
 
                                      List<Item> &new_list2,
215
 
                                      uint elements, List<Item> &items);
216
 
static void init_tmptable_sum_functions(Item_sum **func);
217
 
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
218
 
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
219
 
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
220
 
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
221
 
static bool init_sum_functions(Item_sum **func, Item_sum **end);
222
 
static bool update_sum_func(Item_sum **func);
223
 
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
224
 
                            bool distinct, const char *message=NullS);
225
 
static Item *remove_additional_cond(Item* conds);
226
 
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
227
 
static bool test_if_ref(Item_field *left_item,Item *right_item);
228
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
229
 
                                       Item *new_cond, bool fix_fields);
 
75
 
 
76
static Item* part_of_refkey(Table *form,Field *field);
 
77
static bool cmp_buffer_with_ref(JoinTable *tab);
 
78
static void change_cond_ref_to_const(Session *session,
 
79
                                     vector<COND_CMP>& save_list,
 
80
                                     Item *and_father,
 
81
                                     Item *cond,
 
82
                                     Item *field,
 
83
                                     Item *value);
 
84
static bool copy_blobs(Field **ptr);
 
85
 
 
86
static bool eval_const_cond(COND *cond)
 
87
{
 
88
    return ((Item_func*) cond)->val_int() ? true : false;
 
89
}
230
90
 
231
91
/*
232
92
  This is used to mark equalities that were made from i-th IN-equality.
236
96
const char *subq_sj_cond_name=
237
97
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
238
98
 
239
 
static bool bitmap_covers(const table_map x, const table_map y)
 
99
static bool copy_blobs(Field **ptr)
240
100
{
241
 
  return !test(y & ~x);
 
101
  for (; *ptr ; ptr++)
 
102
  {
 
103
    if ((*ptr)->flags & BLOB_FLAG)
 
104
      if (((Field_blob *) (*ptr))->copy())
 
105
        return 1;                               // Error
 
106
  }
 
107
  return 0;
242
108
}
243
109
 
244
110
/**
245
111
  This handles SELECT with and without UNION.
246
112
*/
247
 
 
248
 
bool handle_select(THD *thd, LEX *lex, select_result *result,
249
 
                   ulong setup_tables_done_option)
 
113
bool handle_select(Session *session, LEX *lex, select_result *result,
 
114
                   uint64_t setup_tables_done_option)
250
115
{
251
116
  bool res;
252
 
  register SELECT_LEX *select_lex = &lex->select_lex;
253
 
  MYSQL_SELECT_START();
 
117
  register Select_Lex *select_lex= &lex->select_lex;
 
118
  DRIZZLE_SELECT_START(session->query);
254
119
 
255
 
  if (select_lex->master_unit()->is_union() || 
 
120
  if (select_lex->master_unit()->is_union() ||
256
121
      select_lex->master_unit()->fake_select_lex)
257
 
    res= mysql_union(thd, lex, result, &lex->unit, setup_tables_done_option);
 
122
    res= drizzle_union(session, lex, result, &lex->unit,
 
123
                       setup_tables_done_option);
258
124
  else
259
125
  {
260
 
    SELECT_LEX_UNIT *unit= &lex->unit;
 
126
    Select_Lex_Unit *unit= &lex->unit;
261
127
    unit->set_limit(unit->global_parameters);
262
 
    thd->thd_marker= 0;
 
128
    session->session_marker= 0;
263
129
    /*
264
130
      'options' of mysql_select will be set in JOIN, as far as JOIN for
265
 
      every PS/SP execution new, we will not need reset this flag if 
 
131
      every PS/SP execution new, we will not need reset this flag if
266
132
      setup_tables_done_option changed for next rexecution
267
133
    */
268
 
    res= mysql_select(thd, &select_lex->ref_pointer_array,
269
 
                      (TABLE_LIST*) select_lex->table_list.first,
 
134
    res= mysql_select(session, &select_lex->ref_pointer_array,
 
135
                      (TableList*) select_lex->table_list.first,
270
136
                      select_lex->with_wild, select_lex->item_list,
271
137
                      select_lex->where,
272
138
                      select_lex->order_list.elements +
273
139
                      select_lex->group_list.elements,
274
 
                      (ORDER*) select_lex->order_list.first,
275
 
                      (ORDER*) select_lex->group_list.first,
 
140
                      (order_st*) select_lex->order_list.first,
 
141
                      (order_st*) select_lex->group_list.first,
276
142
                      select_lex->having,
277
 
                      (ORDER*) lex->proc_list.first,
278
 
                      select_lex->options | thd->options |
 
143
                      select_lex->options | session->options |
279
144
                      setup_tables_done_option,
280
145
                      result, unit, select_lex);
281
146
  }
282
 
  res|= thd->is_error();
 
147
  res|= session->is_error();
283
148
  if (unlikely(res))
284
149
    result->abort();
285
150
 
286
 
  MYSQL_SELECT_END();
287
 
  return(res);
 
151
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
 
152
  return res;
288
153
}
289
154
 
290
 
 
291
155
/*
292
156
  Fix fields referenced from inner selects.
293
157
 
294
158
  SYNOPSIS
295
159
    fix_inner_refs()
296
 
    thd               Thread handle
 
160
    session               Thread handle
297
161
    all_fields        List of all fields used in select
298
162
    select            Current select
299
163
    ref_pointer_array Array of references to Items used in current select
328
192
    true  an error occured
329
193
    false ok
330
194
*/
331
 
 
332
 
bool
333
 
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
334
 
                 Item **ref_pointer_array)
 
195
bool fix_inner_refs(Session *session, 
 
196
                    List<Item> &all_fields, 
 
197
                    Select_Lex *select, 
 
198
                    Item **ref_pointer_array)
335
199
{
336
200
  Item_outer_ref *ref;
337
201
  bool res= false;
391
255
    ref->outer_ref= new_ref;
392
256
    ref->ref= &ref->outer_ref;
393
257
 
394
 
    if (!ref->fixed && ref->fix_fields(thd, 0))
 
258
    if (!ref->fixed && ref->fix_fields(session, 0))
395
259
      return true;
396
 
    thd->used_tables|= item->used_tables();
 
260
    session->used_tables|= item->used_tables();
397
261
  }
398
262
  return res;
399
263
}
400
264
 
401
 
#define MAGIC_IN_WHERE_TOP_LEVEL 10
402
 
/**
403
 
  Function to setup clauses without sum functions.
404
 
*/
405
 
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
406
 
                               TABLE_LIST *tables,
407
 
                               TABLE_LIST *leaves,
408
 
                               List<Item> &fields,
409
 
                               List<Item> &all_fields,
410
 
                               COND **conds,
411
 
                               ORDER *order,
412
 
                               ORDER *group, bool *hidden_group_fields)
413
 
{
414
 
  int res;
415
 
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
416
 
 
417
 
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
418
 
  res= setup_conds(thd, tables, leaves, conds);
419
 
 
420
 
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
421
 
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
422
 
                          order);
423
 
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
424
 
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
425
 
                          group, hidden_group_fields);
426
 
  thd->lex->allow_sum_func= save_allow_sum_func;
427
 
  return(res);
428
 
}
429
 
 
430
265
/*****************************************************************************
431
266
  Check fields, find best join, do the select and output fields.
432
267
  mysql_select assumes that all tables are already opened
433
268
*****************************************************************************/
434
269
 
435
 
/**
436
 
  Prepare of whole select (including sub queries in future).
437
 
 
438
 
  @todo
439
 
    Add check of calculation of GROUP functions and fields:
440
 
    SELECT COUNT(*)+table.col1 from table1;
441
 
 
442
 
  @retval
443
 
    -1   on error
444
 
  @retval
445
 
    0   on success
446
 
*/
447
 
int
448
 
JOIN::prepare(Item ***rref_pointer_array,
449
 
              TABLE_LIST *tables_init,
450
 
              uint wild_num, COND *conds_init, uint og_num,
451
 
              ORDER *order_init, ORDER *group_init,
452
 
              Item *having_init,
453
 
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
454
 
              SELECT_LEX_UNIT *unit_arg)
455
 
{
456
 
  // to prevent double initialization on EXPLAIN
457
 
  if (optimized)
458
 
    return(0);
459
 
 
460
 
  conds= conds_init;
461
 
  order= order_init;
462
 
  group_list= group_init;
463
 
  having= having_init;
464
 
  proc_param= proc_param_init;
465
 
  tables_list= tables_init;
466
 
  select_lex= select_lex_arg;
467
 
  select_lex->join= this;
468
 
  join_list= &select_lex->top_join_list;
469
 
  union_part= unit_arg->is_union();
470
 
 
471
 
  thd->lex->current_select->is_item_list_lookup= 1;
472
 
  /*
473
 
    If we have already executed SELECT, then it have not sense to prevent
474
 
    its table from update (see unique_table())
475
 
  */
476
 
  if (thd->derived_tables_processing)
477
 
    select_lex->exclude_from_table_unique_test= true;
478
 
 
479
 
  /* Check that all tables, fields, conds and order are ok */
480
 
 
481
 
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
482
 
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
483
 
                                    tables_list, &select_lex->leaf_tables,
484
 
                                    false))
485
 
      return(-1);
486
 
 
487
 
  TABLE_LIST *table_ptr;
488
 
  for (table_ptr= select_lex->leaf_tables;
489
 
       table_ptr;
490
 
       table_ptr= table_ptr->next_leaf)
491
 
    tables++;
492
 
 
493
 
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
494
 
      select_lex->setup_ref_array(thd, og_num) ||
495
 
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
496
 
                   &all_fields, 1) ||
497
 
      setup_without_group(thd, (*rref_pointer_array), tables_list,
498
 
                          select_lex->leaf_tables, fields_list,
499
 
                          all_fields, &conds, order, group_list,
500
 
                          &hidden_group_fields))
501
 
    return(-1);                         /* purecov: inspected */
502
 
 
503
 
  ref_pointer_array= *rref_pointer_array;
504
 
  
505
 
  if (having)
506
 
  {
507
 
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
508
 
    thd->where="having clause";
509
 
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
510
 
    select_lex->having_fix_field= 1;
511
 
    bool having_fix_rc= (!having->fixed &&
512
 
                         (having->fix_fields(thd, &having) ||
513
 
                          having->check_cols(1)));
514
 
    select_lex->having_fix_field= 0;
515
 
    if (having_fix_rc || thd->is_error())
516
 
      return(-1);                               /* purecov: inspected */
517
 
    thd->lex->allow_sum_func= save_allow_sum_func;
518
 
  }
519
 
 
520
 
  {
521
 
    Item_subselect *subselect;
522
 
    Item_in_subselect *in_subs= NULL;
523
 
    /*
524
 
      Are we in a subquery predicate?
525
 
      TODO: the block below will be executed for every PS execution without need.
526
 
    */
527
 
    if ((subselect= select_lex->master_unit()->item))
528
 
    {
529
 
      bool do_semijoin= !test(thd->variables.optimizer_switch &
530
 
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
531
 
      if (subselect->substype() == Item_subselect::IN_SUBS)
532
 
        in_subs= (Item_in_subselect*)subselect;
533
 
 
534
 
      /*
535
 
        Check if we're in subquery that is a candidate for flattening into a
536
 
        semi-join (which is done done in flatten_subqueries()). The
537
 
        requirements are:
538
 
          1. Subquery predicate is an IN/=ANY subq predicate
539
 
          2. Subquery is a single SELECT (not a UNION)
540
 
          3. Subquery does not have GROUP BY or ORDER BY
541
 
          4. Subquery does not use aggregate functions or HAVING
542
 
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
543
 
          6. No execution method was already chosen (by a prepared statement).
544
 
 
545
 
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
546
 
               doesn't have a JOIN (TODO: We should handle this at some
547
 
               point by switching to multi-table UPDATE/DELETE)
548
 
 
549
 
          (**). We're not in a confluent table-less subquery, like
550
 
                "SELECT 1". 
551
 
      */
552
 
      if (in_subs &&                                                    // 1
553
 
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
554
 
          !select_lex->group_list.elements && !order &&                 // 3
555
 
          !having && !select_lex->with_sum_func &&                      // 4
556
 
          thd->thd_marker &&                                            // 5
557
 
          select_lex->outer_select()->join &&                           // (*)
558
 
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
559
 
          do_semijoin &&
560
 
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
561
 
      {
562
 
        {
563
 
          if (!in_subs->left_expr->fixed &&
564
 
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
565
 
          {
566
 
            return(-1);
567
 
          }
568
 
          /*
569
 
            Check that the right part of the subselect contains no more than one
570
 
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
571
 
          */
572
 
          if (subselect->substype() == Item_subselect::IN_SUBS &&
573
 
             (select_lex->item_list.elements != 
574
 
              ((Item_in_subselect*)subselect)->left_expr->cols()))
575
 
          {
576
 
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
577
 
            return(-1);
578
 
          }
579
 
        }
580
 
 
581
 
        /* Register the subquery for further processing */
582
 
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
583
 
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
584
 
      }
585
 
      else
586
 
      {
587
 
        bool do_materialize= !test(thd->variables.optimizer_switch &
588
 
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
589
 
        /*
590
 
          Check if the subquery predicate can be executed via materialization.
591
 
          The required conditions are:
592
 
          1. Subquery predicate is an IN/=ANY subq predicate
593
 
          2. Subquery is a single SELECT (not a UNION)
594
 
          3. Subquery is not a table-less query. In this case there is no
595
 
             point in materializing.
596
 
          4. Subquery predicate is a top-level predicate
597
 
             (this implies it is not negated)
598
 
             TODO: this is a limitation that should be lifeted once we
599
 
             implement correct NULL semantics (WL#3830)
600
 
          5. Subquery is non-correlated
601
 
             TODO:
602
 
             This is an overly restrictive condition. It can be extended to:
603
 
             (Subquery is non-correlated ||
604
 
              Subquery is correlated to any query outer to IN predicate ||
605
 
              (Subquery is correlated to the immediate outer query &&
606
 
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
607
 
               aggregate functions) && subquery predicate is not under "NOT IN"))
608
 
          6. No execution method was already chosen (by a prepared statement).
609
 
 
610
 
          (*) The subquery must be part of a SELECT statement. The current
611
 
               condition also excludes multi-table update statements.
612
 
 
613
 
          We have to determine whether we will perform subquery materialization
614
 
          before calling the IN=>EXISTS transformation, so that we know whether to
615
 
          perform the whole transformation or only that part of it which wraps
616
 
          Item_in_subselect in an Item_in_optimizer.
617
 
        */
618
 
        if (do_materialize && 
619
 
            in_subs  &&                                                   // 1
620
 
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
621
 
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
622
 
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
623
 
        {
624
 
          if (in_subs->is_top_level_item() &&                             // 4
625
 
              !in_subs->is_correlated &&                                  // 5
626
 
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
627
 
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
628
 
        }
629
 
 
630
 
        Item_subselect::trans_res trans_res;
631
 
        if ((trans_res= subselect->select_transformer(this)) !=
632
 
            Item_subselect::RES_OK)
633
 
        {
634
 
          return((trans_res == Item_subselect::RES_ERROR));
635
 
        }
636
 
      }
637
 
    }
638
 
  }
639
 
 
640
 
  if (order)
641
 
  {
642
 
    ORDER *ord;
643
 
    for (ord= order; ord; ord= ord->next)
644
 
    {
645
 
      Item *item= *ord->item;
646
 
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
647
 
        item->split_sum_func(thd, ref_pointer_array, all_fields);
648
 
    }
649
 
  }
650
 
 
651
 
  if (having && having->with_sum_func)
652
 
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
653
 
                            &having, true);
654
 
  if (select_lex->inner_sum_func_list)
655
 
  {
656
 
    Item_sum *end=select_lex->inner_sum_func_list;
657
 
    Item_sum *item_sum= end;  
658
 
    do
659
 
    { 
660
 
      item_sum= item_sum->next;
661
 
      item_sum->split_sum_func2(thd, ref_pointer_array,
662
 
                                all_fields, item_sum->ref_by, false);
663
 
    } while (item_sum != end);
664
 
  }
665
 
 
666
 
  if (select_lex->inner_refs_list.elements &&
667
 
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
668
 
    return(-1);
669
 
 
670
 
  /*
671
 
    Check if there are references to un-aggregated columns when computing 
672
 
    aggregate functions with implicit grouping (there is no GROUP BY).
673
 
 
674
 
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
675
 
  */
676
 
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
677
 
  {
678
 
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
679
 
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
680
 
    return(-1);
681
 
  }
682
 
  {
683
 
    /* Caclulate the number of groups */
684
 
    send_group_parts= 0;
685
 
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
686
 
      send_group_parts++;
687
 
  }
688
 
  
689
 
  if (error)
690
 
    goto err;                                   /* purecov: inspected */
691
 
 
692
 
  if (result && result->prepare(fields_list, unit_arg))
693
 
    goto err;                                   /* purecov: inspected */
694
 
 
695
 
  /* Init join struct */
696
 
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
697
 
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
698
 
  this->group= group_list != 0;
699
 
  unit= unit_arg;
700
 
 
701
 
#ifdef RESTRICTED_GROUP
702
 
  if (sum_func_count && !group_list && (func_count || field_count))
703
 
  {
704
 
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
705
 
    goto err;
706
 
  }
707
 
#endif
708
 
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
709
 
    goto err;
710
 
  if (alloc_func_list())
711
 
    goto err;
712
 
 
713
 
  return(0); // All OK
714
 
 
715
 
err:
716
 
  return(-1);                           /* purecov: inspected */
717
 
}
718
 
 
719
 
 
720
 
/*
721
 
  Remove the predicates pushed down into the subquery
722
 
 
723
 
  SYNOPSIS
724
 
    JOIN::remove_subq_pushed_predicates()
725
 
      where   IN  Must be NULL
726
 
              OUT The remaining WHERE condition, or NULL
727
 
 
728
 
  DESCRIPTION
729
 
    Given that this join will be executed using (unique|index)_subquery,
730
 
    without "checking NULL", remove the predicates that were pushed down
731
 
    into the subquery.
732
 
 
733
 
    If the subquery compares scalar values, we can remove the condition that
734
 
    was wrapped into trig_cond (it will be checked when needed by the subquery
735
 
    engine)
736
 
 
737
 
    If the subquery compares row values, we need to keep the wrapped
738
 
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
739
 
    and non-NULL values, we'll do a full table scan and will rely on the
740
 
    equalities corresponding to non-NULL parts of left tuple to filter out
741
 
    non-matching records.
742
 
 
743
 
    TODO: We can remove the equalities that will be guaranteed to be true by the
744
 
    fact that subquery engine will be using index lookup. This must be done only
745
 
    for cases where there are no conversion errors of significance, e.g. 257
746
 
    that is searched in a byte. But this requires homogenization of the return 
747
 
    codes of all Field*::store() methods.
748
 
*/
749
 
 
750
 
void JOIN::remove_subq_pushed_predicates(Item **where)
751
 
{
752
 
  if (conds->type() == Item::FUNC_ITEM &&
753
 
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
754
 
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
755
 
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
756
 
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
757
 
                   ((Item_func *)conds)->arguments()[0]))
758
 
  {
759
 
    *where= 0;
760
 
    return;
761
 
  }
762
 
}
763
 
 
764
 
 
765
270
/*
766
271
  Index lookup-based subquery: save some flags for EXPLAIN output
767
272
 
774
279
  DESCRIPTION
775
280
    For index lookup-based subquery (i.e. one executed with
776
281
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
777
 
    check its EXPLAIN output row should contain 
778
 
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL) 
 
282
    check its EXPLAIN output row should contain
 
283
      "Using index" (TAB_INFO_FULL_SCAN_ON_NULL)
779
284
      "Using Where" (TAB_INFO_USING_WHERE)
780
285
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
781
286
    and set appropriate flags in join_tab->packed_info.
782
287
*/
783
 
 
784
 
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
 
288
void save_index_subquery_explain_info(JoinTable *join_tab, Item* where)
785
289
{
786
290
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
787
 
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
 
291
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
788
292
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
789
293
  if (where)
790
294
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
791
 
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
 
295
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
792
296
  {
793
297
    if (join_tab->ref.cond_guards[i])
794
298
    {
798
302
  }
799
303
}
800
304
 
801
 
 
802
 
 
803
 
 
804
 
/*
805
 
  Check if the table's rowid is included in the temptable
806
 
 
807
 
  SYNOPSIS
808
 
    sj_table_is_included()
809
 
      join      The join
810
 
      join_tab  The table to be checked
811
 
 
812
 
  DESCRIPTION
813
 
    SemiJoinDuplicateElimination: check the table's rowid should be included
814
 
    in the temptable. This is so if
815
 
 
816
 
    1. The table is not embedded within some semi-join nest
817
 
    2. The has been pulled out of a semi-join nest, or
818
 
 
819
 
    3. The table is functionally dependent on some previous table
820
 
 
821
 
    [4. This is also true for constant tables that can't be
822
 
        NULL-complemented but this function is not called for such tables]
823
 
 
824
 
  RETURN
825
 
    true  - Include table's rowid
826
 
    false - Don't
827
 
*/
828
 
 
829
 
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
830
 
{
831
 
  if (join_tab->emb_sj_nest)
832
 
    return false;
833
 
  
834
 
  /* Check if this table is functionally dependent on the tables that
835
 
     are within the same outer join nest
836
 
  */
837
 
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
838
 
  if (join_tab->type == JT_EQ_REF)
839
 
  {
840
 
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
841
 
    uint idx;
842
 
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
843
 
    {
844
 
      JOIN_TAB *ref_tab= join->join_tab + idx;
845
 
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
846
 
        return true;
847
 
    }
848
 
    /* Ok, functionally dependent */
849
 
    return false;
850
 
  }
851
 
  /* Not functionally dependent => need to include*/
852
 
  return true;
853
 
}
854
 
 
855
 
 
856
 
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
857
 
                                          SJ_TMP_TABLE *sjtbl);
858
 
 
859
 
 
860
 
/*
861
 
  Setup the strategies to eliminate semi-join duplicates.
862
 
  
863
 
  SYNOPSIS
864
 
    setup_semijoin_dups_elimination()
865
 
      join           Join to process
866
 
      options        Join options (needed to see if join buffering will be 
867
 
                     used or not)
868
 
      no_jbuf_after  Another bit of information re where join buffering will
869
 
                     be used.
870
 
 
871
 
  DESCRIPTION
872
 
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
873
 
    strategies:
874
 
 
875
 
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
876
 
                         of row combinations)
877
 
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
878
 
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
879
 
                  together and picking the 1st one)
880
 
    
881
 
    The join order has "duplicate-generating ranges", and every range is
882
 
    served by one strategy or a combination of FirstMatch with with some
883
 
    other strategy.
884
 
    
885
 
    "Duplicate-generating range" is defined as a range within the join order
886
 
    that contains all of the inner tables of a semi-join. All ranges must be
887
 
    disjoint, if tables of several semi-joins are interleaved, then the ranges
888
 
    are joined together, which is equivalent to converting
889
 
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
890
 
    to
891
 
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
892
 
    .
893
 
 
894
 
    Applicability conditions are as follows:
895
 
 
896
 
    DuplicateWeedout strategy
897
 
    ~~~~~~~~~~~~~~~~~~~~~~~~~
898
 
 
899
 
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
900
 
      +------+  +=========================+  +---+
901
 
        (1)                 (2)               (3)
902
 
 
903
 
       (1) - Prefix of OuterTables (those that participate in 
904
 
             IN-equality and/or are correlated with subquery) and outer 
905
 
             Noncorrelated Tables.
906
 
       (2) - The handled range. The range starts with the first sj-inner
907
 
             table, and covers all sj-inner and outer tables 
908
 
             Within the range,  Inner, Outer, outer Noncorrelated tables
909
 
             may follow in any order.
910
 
       (3) - The suffix of outer Noncorrelated tables.
911
 
    
912
 
    FirstMatch strategy
913
 
    ~~~~~~~~~~~~~~~~~~~
914
 
 
915
 
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
916
 
      +------+  +==================+  +---+
917
 
        (1)             (2)          (3)
918
 
 
919
 
      (1) - Prefix of outer and non-correlated tables
920
 
      (2) - The handled range, which may contain only inner and
921
 
            non-correlated tables.
922
 
      (3) - The suffix of outer Noncorrelated tables.
923
 
 
924
 
    InsideOut strategy 
925
 
    ~~~~~~~~~~~~~~~~~~
926
 
 
927
 
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
928
 
     +--------+   +===========+ +=============+   +------+
929
 
        (1)           (2)          (3)              (4)
930
 
     
931
 
      (1) - Prefix that may contain any outer tables. The prefix must contain
932
 
            all the non-trivially correlated outer tables. (non-trivially means
933
 
            that the correlation is not just through the IN-equality).
934
 
      
935
 
      (2) - Inner table for which the InsideOut scan is performed.
936
 
 
937
 
      (3) - The remainder of the duplicate-generating range. It is served by 
938
 
            application of FirstMatch strategy, with the exception that
939
 
            outer IN-correlated tables are considered to be non-correlated.
940
 
 
941
 
      (4) - THe suffix of outer and outer non-correlated tables.
942
 
 
943
 
    If several strategies are applicable, their relative priorities are:
944
 
      1. InsideOut
945
 
      2. FirstMatch 
946
 
      3. DuplicateWeedout
947
 
 
948
 
    This function walks over the join order and sets up the strategies by
949
 
    setting appropriate members in join_tab structures.
950
 
 
951
 
  RETURN
952
 
    false  OK 
953
 
    true   Out of memory error
954
 
*/
955
 
 
956
 
static
957
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
958
 
{
959
 
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
960
 
  struct {
961
 
    /* 
962
 
      0 - invalid (EOF marker), 
963
 
      1 - InsideOut, 
964
 
      2 - Temptable (maybe confluent),
965
 
      3 - Temptable with join buffering
966
 
    */
967
 
    uint strategy;
968
 
    uint start_idx; /* Left range bound */
969
 
    uint end_idx;   /* Right range bound */
970
 
    /* 
971
 
      For Temptable strategy: Bitmap of all outer and correlated tables from 
972
 
      all involved join nests.
973
 
    */
974
 
    table_map outer_tables;
975
 
  } dups_ranges [MAX_TABLES];
976
 
 
977
 
  TABLE_LIST *emb_insideout_nest= NULL;
978
 
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
979
 
                               tables) whose ranges we're in */
980
 
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
981
 
  table_map range_start_map= 0; /* table_map at current range start */
982
 
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
983
 
  int cur_range= 0;
984
 
  uint i;
985
 
 
986
 
  /*
987
 
    First pass: locate the duplicate-generating ranges and pick the strategies.
988
 
  */
989
 
  for (i=join->const_tables ; i < join->tables ; i++)
990
 
  {
991
 
    JOIN_TAB *tab=join->join_tab+i;
992
 
    TABLE *table=tab->table;
993
 
    cur_map |= table->map;
994
 
 
995
 
    if (tab->emb_sj_nest) // Encountered an sj-inner table
996
 
    {
997
 
      if (!emb_sj_map)
998
 
      {
999
 
        dups_ranges[cur_range].start_idx= i;
1000
 
        range_start_map= cur_map & ~table->map;
1001
 
        /*
1002
 
          Remember if this is a possible start of range that is covered by
1003
 
          the InsideOut strategy (the reason that it is not covered could
1004
 
          be that it overlaps with anther semi-join's range. we don't
1005
 
          support InsideOut for joined ranges)
1006
 
        */
1007
 
        if (join->best_positions[i].use_insideout_scan)
1008
 
          emb_insideout_nest= tab->emb_sj_nest;
1009
 
      }
1010
 
 
1011
 
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1012
 
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1013
 
 
1014
 
      if (tab->emb_sj_nest != emb_insideout_nest)
1015
 
      {
1016
 
        /*
1017
 
          Two different semi-joins interleave. This cannot be handled by
1018
 
          InsideOut strategy.
1019
 
        */
1020
 
        emb_insideout_nest= NULL;
1021
 
      }
1022
 
    }
1023
 
 
1024
 
    if (emb_sj_map) /* We're in duplicate-generating range */
1025
 
    {
1026
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1027
 
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1028
 
          i <= no_jbuf_after && !dealing_with_jbuf)
1029
 
      {
1030
 
        /*
1031
 
          This table uses join buffering, which makes use of FirstMatch or 
1032
 
          InsideOut strategies impossible for the current and (we assume) 
1033
 
          preceding duplicate-producing ranges.
1034
 
          That is, for the join order:
1035
 
 
1036
 
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
1037
 
                  |     |     |     |          | \
1038
 
                  +-----+     +-----+          |  join buffering use
1039
 
                     r1          r2         we're here
1040
 
 
1041
 
          we'll have to remove r1 and r2 and use duplicate-elimination
1042
 
          strategy that spans all the tables, starting from the very 1st
1043
 
          one.
1044
 
        */
1045
 
        dealing_with_jbuf= true;
1046
 
        emb_insideout_nest= false;
1047
 
 
1048
 
        /* 
1049
 
          Absorb all preceding duplicate-eliminating ranges. Their strategies
1050
 
          do not matter: 
1051
 
        */
1052
 
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
1053
 
        {
1054
 
          dups_ranges[cur_range].outer_tables |= 
1055
 
            dups_ranges[prev_range].outer_tables;
1056
 
        }
1057
 
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1058
 
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1059
 
        cur_range=  0;
1060
 
      }
1061
 
 
1062
 
      /*
1063
 
        Check if we are at the end of duplicate-producing range. We are if
1064
 
 
1065
 
        1. It's an InsideOut range (which presumes all correlated tables are
1066
 
           in the prefix), and all inner tables are in the join order prefix,
1067
 
           or
1068
 
        2. It's a DuplicateElimination range (possibly covering several
1069
 
           SJ-nests), and all inner, outer, and correlated tables of all 
1070
 
           sj-nests are in the join order prefix.
1071
 
      */
1072
 
      bool end_of_range= false;
1073
 
      if (emb_insideout_nest && 
1074
 
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1075
 
      {
1076
 
        /* Save that this range is handled with InsideOut: */
1077
 
        dups_ranges[cur_range].strategy= 1;
1078
 
        end_of_range= true;
1079
 
      }
1080
 
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1081
 
      {
1082
 
        /*
1083
 
          This is a complete range to be handled with either DuplicateWeedout 
1084
 
          or FirstMatch
1085
 
        */
1086
 
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1087
 
        /* 
1088
 
          This will hold tables from within the range that need to be put 
1089
 
          into the join buffer before we can use the FirstMatch on its tail.
1090
 
        */
1091
 
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
1092
 
                                             ~range_start_map;
1093
 
        end_of_range= true;
1094
 
      }
1095
 
 
1096
 
      if (end_of_range)
1097
 
      {
1098
 
        dups_ranges[cur_range].end_idx= i+1;
1099
 
        emb_sj_map= emb_outer_tables= 0;
1100
 
        emb_insideout_nest= NULL;
1101
 
        dealing_with_jbuf= false;
1102
 
        dups_ranges[++cur_range].strategy= 0;
1103
 
      }
1104
 
    }
1105
 
  }
1106
 
 
1107
 
  THD *thd= join->thd;
1108
 
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1109
 
  /*
1110
 
    Second pass: setup the chosen strategies    
1111
 
  */
1112
 
  for (int j= 0; j < cur_range; j++)
1113
 
  {
1114
 
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1115
 
    JOIN_TAB *jump_to;
1116
 
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
1117
 
    {
1118
 
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1119
 
      jump_to= tab++;
1120
 
    }
1121
 
    else // DuplicateWeedout strategy
1122
 
    {
1123
 
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1124
 
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1125
 
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1126
 
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1127
 
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1128
 
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1129
 
      JOIN_TAB *last_outer_tab= tab - 1;
1130
 
      /*
1131
 
        Walk through the range and remember
1132
 
         - tables that need their rowids to be put into temptable
1133
 
         - the last outer table
1134
 
      */
1135
 
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1136
 
      {
1137
 
        if (sj_table_is_included(join, tab))
1138
 
        {
1139
 
          last_tab->join_tab= tab;
1140
 
          last_tab->rowid_offset= jt_rowid_offset;
1141
 
          jt_rowid_offset += tab->table->file->ref_length;
1142
 
          if (tab->table->maybe_null)
1143
 
          {
1144
 
            last_tab->null_byte= jt_null_bits / 8;
1145
 
            last_tab->null_bit= jt_null_bits++;
1146
 
          }
1147
 
          last_tab++;
1148
 
          tab->table->prepare_for_position();
1149
 
          tab->rowid_keep_flags= rowid_keep_flags;
1150
 
        }
1151
 
        cur_map |= tab->table->map;
1152
 
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
1153
 
                                               dups_ranges[j].outer_tables))
1154
 
          last_outer_tab= tab;
1155
 
      }
1156
 
 
1157
 
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1158
 
      {
1159
 
        SJ_TMP_TABLE *sjtbl;
1160
 
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1161
 
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1162
 
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1163
 
          return(true);
1164
 
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1165
 
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1166
 
        sjtbl->rowid_len= jt_rowid_offset;
1167
 
        sjtbl->null_bits= jt_null_bits;
1168
 
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
1169
 
 
1170
 
        *next_sjtbl_ptr= sjtbl;
1171
 
        next_sjtbl_ptr= &(sjtbl->next);
1172
 
        sjtbl->next= NULL;
1173
 
 
1174
 
        sjtbl->tmp_table= 
1175
 
          create_duplicate_weedout_tmp_table(thd, 
1176
 
                                             sjtbl->rowid_len + 
1177
 
                                             sjtbl->null_bytes,
1178
 
                                             sjtbl);
1179
 
 
1180
 
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1181
 
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1182
 
      }
1183
 
      tab= last_outer_tab + 1;
1184
 
      jump_to= last_outer_tab;
1185
 
    }
1186
 
 
1187
 
    /* Create the FirstMatch tail */
1188
 
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1189
 
    {
1190
 
      if (tab->emb_sj_nest)
1191
 
        tab->do_firstmatch= jump_to; 
1192
 
      else
1193
 
        jump_to= tab;
1194
 
    }
1195
 
  }
1196
 
  return(false);
1197
 
}
1198
 
 
1199
 
 
1200
 
static void cleanup_sj_tmp_tables(JOIN *join)
1201
 
{
1202
 
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
1203
 
       sj_tbl= sj_tbl->next)
1204
 
  {
1205
 
    if (sj_tbl->tmp_table)
1206
 
    {
1207
 
      free_tmp_table(join->thd, sj_tbl->tmp_table);
1208
 
    }
1209
 
  }
1210
 
  join->sj_tmp_tables= NULL;
1211
 
}
1212
 
 
1213
 
uint make_join_orderinfo(JOIN *join);
1214
 
 
1215
 
/**
1216
 
  global select optimisation.
1217
 
 
1218
 
  @note
1219
 
    error code saved in field 'error'
1220
 
 
1221
 
  @retval
1222
 
    0   success
1223
 
  @retval
1224
 
    1   error
1225
 
*/
1226
 
 
1227
 
int
1228
 
JOIN::optimize()
1229
 
{
1230
 
  // to prevent double initialization on EXPLAIN
1231
 
  if (optimized)
1232
 
    return(0);
1233
 
  optimized= 1;
1234
 
 
1235
 
  thd_proc_info(thd, "optimizing");
1236
 
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1237
 
              unit->select_limit_cnt);
1238
 
  /* select_limit is used to decide if we are likely to scan the whole table */
1239
 
  select_limit= unit->select_limit_cnt;
1240
 
  if (having || (select_options & OPTION_FOUND_ROWS))
1241
 
    select_limit= HA_POS_ERROR;
1242
 
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1243
 
  // Ignore errors of execution if option IGNORE present
1244
 
  if (thd->lex->ignore)
1245
 
    thd->lex->current_select->no_error= 1;
1246
 
 
1247
 
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
1248
 
  /* Add HAVING to WHERE if possible */
1249
 
  if (having && !group_list && !sum_func_count)
1250
 
  {
1251
 
    if (!conds)
1252
 
    {
1253
 
      conds= having;
1254
 
      having= 0;
1255
 
    }
1256
 
    else if ((conds=new Item_cond_and(conds,having)))
1257
 
    {
1258
 
      /*
1259
 
        Item_cond_and can't be fixed after creation, so we do not check
1260
 
        conds->fixed
1261
 
      */
1262
 
      conds->fix_fields(thd, &conds);
1263
 
      conds->change_ref_to_fields(thd, tables_list);
1264
 
      conds->top_level_item();
1265
 
      having= 0;
1266
 
    }
1267
 
  }
1268
 
#endif
1269
 
  SELECT_LEX *sel= thd->lex->current_select;
1270
 
  if (sel->first_cond_optimization)
1271
 
  {
1272
 
    /*
1273
 
      The following code will allocate the new items in a permanent
1274
 
      MEMROOT for prepared statements and stored procedures.
1275
 
    */
1276
 
    sel->first_cond_optimization= 0;
1277
 
 
1278
 
    /* Convert all outer joins to inner joins if possible */
1279
 
    conds= simplify_joins(this, join_list, conds, true, false);
1280
 
    build_bitmap_for_nested_joins(join_list, 0);
1281
 
  }
1282
 
 
1283
 
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1284
 
  if (thd->is_error())
1285
 
  {
1286
 
    error= 1;
1287
 
    return(1);
1288
 
  }
1289
 
 
1290
 
  {
1291
 
    having= optimize_cond(this, having, join_list, &having_value);
1292
 
    if (thd->is_error())
1293
 
    {
1294
 
      error= 1;
1295
 
      return(1);
1296
 
    }
1297
 
    if (select_lex->where)
1298
 
      select_lex->cond_value= cond_value;
1299
 
    if (select_lex->having)
1300
 
      select_lex->having_value= having_value;
1301
 
 
1302
 
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
1303
 
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1304
 
    {                                           /* Impossible cond */
1305
 
      zero_result_cause=  having_value == Item::COND_FALSE ?
1306
 
                           "Impossible HAVING" : "Impossible WHERE";
1307
 
      error= 0;
1308
 
      return(0);
1309
 
    }
1310
 
  }
1311
 
 
1312
 
  /* Optimize count(*), min() and max() */
1313
 
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1314
 
  {
1315
 
    int res;
1316
 
    /*
1317
 
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1318
 
      to the WHERE conditions,
1319
 
      or 1 if all items were resolved,
1320
 
      or 0, or an error number HA_ERR_...
1321
 
    */
1322
 
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1323
 
    {
1324
 
      if (res == HA_ERR_KEY_NOT_FOUND)
1325
 
      {
1326
 
        zero_result_cause= "No matching min/max row";
1327
 
        error=0;
1328
 
        return(0);
1329
 
      }
1330
 
      if (res > 1)
1331
 
      {
1332
 
        error= res;
1333
 
        return(1);
1334
 
      }
1335
 
      if (res < 0)
1336
 
      {
1337
 
        zero_result_cause= "No matching min/max row";
1338
 
        error=0;
1339
 
        return(0);
1340
 
      }
1341
 
      zero_result_cause= "Select tables optimized away";
1342
 
      tables_list= 0;                           // All tables resolved
1343
 
      /*
1344
 
        Extract all table-independent conditions and replace the WHERE
1345
 
        clause with them. All other conditions were computed by opt_sum_query
1346
 
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
1347
 
        so there is no need to compute the whole WHERE clause again.
1348
 
        Notice that make_cond_for_table() will always succeed to remove all
1349
 
        computed conditions, because opt_sum_query() is applicable only to
1350
 
        conjunctions.
1351
 
        Preserve conditions for EXPLAIN.
1352
 
      */
1353
 
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1354
 
      {
1355
 
        COND *table_independent_conds=
1356
 
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1357
 
        conds= table_independent_conds;
1358
 
      }
1359
 
    }
1360
 
  }
1361
 
  if (!tables_list)
1362
 
  {
1363
 
    error= 0;
1364
 
    return(0);
1365
 
  }
1366
 
  error= -1;                                    // Error is sent to client
1367
 
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1368
 
 
1369
 
  /* Calculate how to do the join */
1370
 
  thd_proc_info(thd, "statistics");
1371
 
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1372
 
      thd->is_fatal_error)
1373
 
  {
1374
 
    return(1);
1375
 
  }
1376
 
 
1377
 
  /* Remove distinct if only const tables */
1378
 
  select_distinct= select_distinct && (const_tables != tables);
1379
 
  thd_proc_info(thd, "preparing");
1380
 
  if (result->initialize_tables(this))
1381
 
  {
1382
 
    return(1);                          // error == -1
1383
 
  }
1384
 
  if (const_table_map != found_const_table_map &&
1385
 
      !(select_options & SELECT_DESCRIBE) &&
1386
 
      (!conds ||
1387
 
       !(conds->used_tables() & RAND_TABLE_BIT) ||
1388
 
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1389
 
  {
1390
 
    zero_result_cause= "no matching row in const table";
1391
 
    error= 0;
1392
 
    return(0);
1393
 
  }
1394
 
  if (!(thd->options & OPTION_BIG_SELECTS) &&
1395
 
      best_read > (double) thd->variables.max_join_size &&
1396
 
      !(select_options & SELECT_DESCRIBE))
1397
 
  {                                             /* purecov: inspected */
1398
 
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1399
 
    error= -1;
1400
 
    return(1);
1401
 
  }
1402
 
  if (const_tables && !thd->locked_tables &&
1403
 
      !(select_options & SELECT_NO_UNLOCK))
1404
 
    mysql_unlock_some_tables(thd, table, const_tables);
1405
 
  if (!conds && outer_join)
1406
 
  {
1407
 
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1408
 
    conds=new Item_int((int64_t) 1,1);  // Always true
1409
 
  }
1410
 
  select= make_select(*table, const_table_map,
1411
 
                      const_table_map, conds, 1, &error);
1412
 
  if (error)
1413
 
  {                                             /* purecov: inspected */
1414
 
    error= -1;                                  /* purecov: inspected */
1415
 
    return(1);
1416
 
  }
1417
 
  
1418
 
  reset_nj_counters(join_list);
1419
 
  make_outerjoin_info(this);
1420
 
 
1421
 
  /*
1422
 
    Among the equal fields belonging to the same multiple equality
1423
 
    choose the one that is to be retrieved first and substitute
1424
 
    all references to these in where condition for a reference for
1425
 
    the selected field.
1426
 
  */
1427
 
  if (conds)
1428
 
  {
1429
 
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1430
 
    conds->update_used_tables();
1431
 
  }
1432
 
 
1433
 
  /*
1434
 
    Permorm the the optimization on fields evaluation mentioned above
1435
 
    for all on expressions.
1436
 
  */ 
1437
 
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1438
 
  {
1439
 
    if (*tab->on_expr_ref)
1440
 
    {
1441
 
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1442
 
                                                         tab->cond_equal,
1443
 
                                                         map2table);
1444
 
      (*tab->on_expr_ref)->update_used_tables();
1445
 
    }
1446
 
  }
1447
 
 
1448
 
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
1449
 
      (select_options & SELECT_DESCRIBE) &&
1450
 
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1451
 
  {
1452
 
    conds=new Item_int((int64_t) 0,1);  // Always false
1453
 
  }
1454
 
  if (make_join_select(this, select, conds))
1455
 
  {
1456
 
    zero_result_cause=
1457
 
      "Impossible WHERE noticed after reading const tables";
1458
 
    return(0);                          // error == 0
1459
 
  }
1460
 
 
1461
 
  error= -1;                                    /* if goto err */
1462
 
 
1463
 
  /* Optimize distinct away if possible */
1464
 
  {
1465
 
    ORDER *org_order= order;
1466
 
    order=remove_const(this, order,conds,1, &simple_order);
1467
 
    if (thd->is_error())
1468
 
    {
1469
 
      error= 1;
1470
 
      return(1);
1471
 
    }
1472
 
 
1473
 
    /*
1474
 
      If we are using ORDER BY NULL or ORDER BY const_expression,
1475
 
      return result in any order (even if we are using a GROUP BY)
1476
 
    */
1477
 
    if (!order && org_order)
1478
 
      skip_sort_order= 1;
1479
 
  }
1480
 
  /*
1481
 
     Check if we can optimize away GROUP BY/DISTINCT.
1482
 
     We can do that if there are no aggregate functions, the
1483
 
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
1484
 
     (if present) contain direct references to all key parts of
1485
 
     an unique index (in whatever order) and if the key parts of the
1486
 
     unique index cannot contain NULLs.
1487
 
     Note that the unique keys for DISTINCT and GROUP BY should not
1488
 
     be the same (as long as they are unique).
1489
 
 
1490
 
     The FROM clause must contain a single non-constant table.
1491
 
  */
1492
 
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
1493
 
      !tmp_table_param.sum_func_count &&
1494
 
      (!join_tab[const_tables].select ||
1495
 
       !join_tab[const_tables].select->quick ||
1496
 
       join_tab[const_tables].select->quick->get_type() != 
1497
 
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1498
 
  {
1499
 
    if (group_list &&
1500
 
       list_contains_unique_index(join_tab[const_tables].table,
1501
 
                                 find_field_in_order_list,
1502
 
                                 (void *) group_list))
1503
 
    {
1504
 
      /*
1505
 
        We have found that grouping can be removed since groups correspond to
1506
 
        only one row anyway, but we still have to guarantee correct result
1507
 
        order. The line below effectively rewrites the query from GROUP BY
1508
 
        <fields> to ORDER BY <fields>. There are two exceptions:
1509
 
        - if skip_sort_order is set (see above), then we can simply skip
1510
 
          GROUP BY;
1511
 
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1512
 
          with the GROUP BY ones, i.e. either one is a prefix of another.
1513
 
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
1514
 
          test_if_subpart() copies the ASC/DESC attributes from the original
1515
 
          ORDER BY fields.
1516
 
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1517
 
          'order' as is.
1518
 
       */
1519
 
      if (!order || test_if_subpart(group_list, order))
1520
 
          order= skip_sort_order ? 0 : group_list;
1521
 
      /*
1522
 
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
1523
 
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
1524
 
      */
1525
 
      join_tab->table->keys_in_use_for_order_by=
1526
 
        join_tab->table->keys_in_use_for_group_by;
1527
 
      group_list= 0;
1528
 
      group= 0;
1529
 
    }
1530
 
    if (select_distinct &&
1531
 
       list_contains_unique_index(join_tab[const_tables].table,
1532
 
                                 find_field_in_item_list,
1533
 
                                 (void *) &fields_list))
1534
 
    {
1535
 
      select_distinct= 0;
1536
 
    }
1537
 
  }
1538
 
  if (group_list || tmp_table_param.sum_func_count)
1539
 
  {
1540
 
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1541
 
      select_distinct=0;
1542
 
  }
1543
 
  else if (select_distinct && tables - const_tables == 1)
1544
 
  {
1545
 
    /*
1546
 
      We are only using one table. In this case we change DISTINCT to a
1547
 
      GROUP BY query if:
1548
 
      - The GROUP BY can be done through indexes (no sort) and the ORDER
1549
 
        BY only uses selected fields.
1550
 
        (In this case we can later optimize away GROUP BY and ORDER BY)
1551
 
      - We are scanning the whole table without LIMIT
1552
 
        This can happen if:
1553
 
        - We are using CALC_FOUND_ROWS
1554
 
        - We are using an ORDER BY that can't be optimized away.
1555
 
 
1556
 
      We don't want to use this optimization when we are using LIMIT
1557
 
      because in this case we can just create a temporary table that
1558
 
      holds LIMIT rows and stop when this table is full.
1559
 
    */
1560
 
    JOIN_TAB *tab= &join_tab[const_tables];
1561
 
    bool all_order_fields_used;
1562
 
    if (order)
1563
 
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
1564
 
        &tab->table->keys_in_use_for_order_by);
1565
 
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1566
 
                                          order, fields_list, all_fields,
1567
 
                                          &all_order_fields_used)))
1568
 
    {
1569
 
      bool skip_group= (skip_sort_order &&
1570
 
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
1571
 
                                &tab->table->keys_in_use_for_group_by) != 0);
1572
 
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1573
 
      if ((skip_group && all_order_fields_used) ||
1574
 
          select_limit == HA_POS_ERROR ||
1575
 
          (order && !skip_sort_order))
1576
 
      {
1577
 
        /*  Change DISTINCT to GROUP BY */
1578
 
        select_distinct= 0;
1579
 
        no_order= !order;
1580
 
        if (all_order_fields_used)
1581
 
        {
1582
 
          if (order && skip_sort_order)
1583
 
          {
1584
 
            /*
1585
 
              Force MySQL to read the table in sorted order to get result in
1586
 
              ORDER BY order.
1587
 
            */
1588
 
            tmp_table_param.quick_group=0;
1589
 
          }
1590
 
          order=0;
1591
 
        }
1592
 
        group=1;                                // For end_write_group
1593
 
      }
1594
 
      else
1595
 
        group_list= 0;
1596
 
    }
1597
 
    else if (thd->is_fatal_error)                       // End of memory
1598
 
      return(1);
1599
 
  }
1600
 
  simple_group= 0;
1601
 
  {
1602
 
    ORDER *old_group_list;
1603
 
    group_list= remove_const(this, (old_group_list= group_list), conds,
1604
 
                             rollup.state == ROLLUP::STATE_NONE,
1605
 
                             &simple_group);
1606
 
    if (thd->is_error())
1607
 
    {
1608
 
      error= 1;
1609
 
      return(1);
1610
 
    }
1611
 
    if (old_group_list && !group_list)
1612
 
      select_distinct= 0;
1613
 
  }
1614
 
  if (!group_list && group)
1615
 
  {
1616
 
    order=0;                                    // The output has only one row
1617
 
    simple_order=1;
1618
 
    select_distinct= 0;                       // No need in distinct for 1 row
1619
 
    group_optimized_away= 1;
1620
 
  }
1621
 
 
1622
 
  calc_group_buffer(this, group_list);
1623
 
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1624
 
 
1625
 
  if (test_if_subpart(group_list, order) ||
1626
 
      (!group_list && tmp_table_param.sum_func_count))
1627
 
    order=0;
1628
 
 
1629
 
  // Can't use sort on head table if using row cache
1630
 
  if (full_join)
1631
 
  {
1632
 
    if (group_list)
1633
 
      simple_group=0;
1634
 
    if (order)
1635
 
      simple_order=0;
1636
 
  }
1637
 
 
1638
 
  /*
1639
 
    Check if we need to create a temporary table.
1640
 
    This has to be done if all tables are not already read (const tables)
1641
 
    and one of the following conditions holds:
1642
 
    - We are using DISTINCT (simple distinct's are already optimized away)
1643
 
    - We are using an ORDER BY or GROUP BY on fields not in the first table
1644
 
    - We are using different ORDER BY and GROUP BY orders
1645
 
    - The user wants us to buffer the result.
1646
 
  */
1647
 
  need_tmp= (const_tables != tables &&
1648
 
             ((select_distinct || !simple_order || !simple_group) ||
1649
 
              (group_list && order) ||
1650
 
              test(select_options & OPTION_BUFFER_RESULT)));
1651
 
 
1652
 
  uint no_jbuf_after= make_join_orderinfo(this);
1653
 
  uint64_t select_opts_for_readinfo= 
1654
 
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1655
 
 
1656
 
  sj_tmp_tables= NULL;
1657
 
  if (!select_lex->sj_nests.is_empty())
1658
 
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1659
 
                                    no_jbuf_after);
1660
 
 
1661
 
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1662
 
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1663
 
    return(1);
1664
 
 
1665
 
  /* Create all structures needed for materialized subquery execution. */
1666
 
  if (setup_subquery_materialization())
1667
 
    return(1);
1668
 
 
1669
 
  /*
1670
 
    is this simple IN subquery?
1671
 
  */
1672
 
  if (!group_list && !order &&
1673
 
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1674
 
      tables == 1 && conds &&
1675
 
      !unit->is_union())
1676
 
  {
1677
 
    if (!having)
1678
 
    {
1679
 
      Item *where= conds;
1680
 
      if (join_tab[0].type == JT_EQ_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_UNIQUE_SUBQUERY;
1686
 
        error= 0;
1687
 
        return(unit->item->
1688
 
                    change_engine(new
1689
 
                                  subselect_uniquesubquery_engine(thd,
1690
 
                                                                  join_tab,
1691
 
                                                                  unit->item,
1692
 
                                                                  where)));
1693
 
      }
1694
 
      else if (join_tab[0].type == JT_REF &&
1695
 
               join_tab[0].ref.items[0]->name == in_left_expr_name)
1696
 
      {
1697
 
        remove_subq_pushed_predicates(&where);
1698
 
        save_index_subquery_explain_info(join_tab, where);
1699
 
        join_tab[0].type= JT_INDEX_SUBQUERY;
1700
 
        error= 0;
1701
 
        return(unit->item->
1702
 
                    change_engine(new
1703
 
                                  subselect_indexsubquery_engine(thd,
1704
 
                                                                 join_tab,
1705
 
                                                                 unit->item,
1706
 
                                                                 where,
1707
 
                                                                 NULL,
1708
 
                                                                 0)));
1709
 
      }
1710
 
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
1711
 
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
1712
 
               having->name == in_having_cond)
1713
 
    {
1714
 
      join_tab[0].type= JT_INDEX_SUBQUERY;
1715
 
      error= 0;
1716
 
      conds= remove_additional_cond(conds);
1717
 
      save_index_subquery_explain_info(join_tab, conds);
1718
 
      return(unit->item->
1719
 
                  change_engine(new subselect_indexsubquery_engine(thd,
1720
 
                                                                   join_tab,
1721
 
                                                                   unit->item,
1722
 
                                                                   conds,
1723
 
                                                                   having,
1724
 
                                                                   1)));
1725
 
    }
1726
 
 
1727
 
  }
1728
 
  /*
1729
 
    Need to tell handlers that to play it safe, it should fetch all
1730
 
    columns of the primary key of the tables: this is because MySQL may
1731
 
    build row pointers for the rows, and for all columns of the primary key
1732
 
    the read set has not necessarily been set by the server code.
1733
 
  */
1734
 
  if (need_tmp || select_distinct || group_list || order)
1735
 
  {
1736
 
    for (uint i = const_tables; i < tables; i++)
1737
 
      join_tab[i].table->prepare_for_position();
1738
 
  }
1739
 
 
1740
 
  if (const_tables != tables)
1741
 
  {
1742
 
    /*
1743
 
      Because filesort always does a full table scan or a quick range scan
1744
 
      we must add the removed reference to the select for the table.
1745
 
      We only need to do this when we have a simple_order or simple_group
1746
 
      as in other cases the join is done before the sort.
1747
 
    */
1748
 
    if ((order || group_list) &&
1749
 
        (join_tab[const_tables].type != JT_ALL) &&
1750
 
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
1751
 
        ((order && simple_order) || (group_list && simple_group)))
1752
 
    {
1753
 
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1754
 
        return(1);
1755
 
      }
1756
 
    }
1757
 
    
1758
 
    if (!(select_options & SELECT_BIG_RESULT) &&
1759
 
        ((group_list &&
1760
 
          (!simple_group ||
1761
 
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
1762
 
                                    unit->select_limit_cnt, 0, 
1763
 
                                    &join_tab[const_tables].table->
1764
 
                                    keys_in_use_for_group_by))) ||
1765
 
         select_distinct) &&
1766
 
        tmp_table_param.quick_group)
1767
 
    {
1768
 
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
1769
 
    }
1770
 
    if (order)
1771
 
    {
1772
 
      /*
1773
 
        Force using of tmp table if sorting by a SP or UDF function due to
1774
 
        their expensive and probably non-deterministic nature.
1775
 
      */
1776
 
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1777
 
      {
1778
 
        Item *item= *tmp_order->item;
1779
 
        if (item->is_expensive())
1780
 
        {
1781
 
          /* Force tmp table without sort */
1782
 
          need_tmp=1; simple_order=simple_group=0;
1783
 
          break;
1784
 
        }
1785
 
      }
1786
 
    }
1787
 
  }
1788
 
 
1789
 
  tmp_having= having;
1790
 
  if (select_options & SELECT_DESCRIBE)
1791
 
  {
1792
 
    error= 0;
1793
 
    return(0);
1794
 
  }
1795
 
  having= 0;
1796
 
 
1797
 
  /*
1798
 
    The loose index scan access method guarantees that all grouping or
1799
 
    duplicate row elimination (for distinct) is already performed
1800
 
    during data retrieval, and that all MIN/MAX functions are already
1801
 
    computed for each group. Thus all MIN/MAX functions should be
1802
 
    treated as regular functions, and there is no need to perform
1803
 
    grouping in the main execution loop.
1804
 
    Notice that currently loose index scan is applicable only for
1805
 
    single table queries, thus it is sufficient to test only the first
1806
 
    join_tab element of the plan for its access method.
1807
 
  */
1808
 
  if (join_tab->is_using_loose_index_scan())
1809
 
    tmp_table_param.precomputed_group_by= true;
1810
 
 
1811
 
  /* Create a tmp table if distinct or if the sort is too complicated */
1812
 
  if (need_tmp)
1813
 
  {
1814
 
    thd_proc_info(thd, "Creating tmp table");
1815
 
 
1816
 
    init_items_ref_array();
1817
 
 
1818
 
    tmp_table_param.hidden_field_count= (all_fields.elements -
1819
 
                                         fields_list.elements);
1820
 
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1821
 
                                                             (ORDER*) 0);
1822
 
    /*
1823
 
      Pushing LIMIT to the temporary table creation is not applicable
1824
 
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1825
 
      there are aggregate functions, because in all these cases we need
1826
 
      all result rows.
1827
 
    */
1828
 
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1829
 
                             !tmp_group &&
1830
 
                             !thd->lex->current_select->with_sum_func) ?
1831
 
                            select_limit : HA_POS_ERROR;
1832
 
 
1833
 
    if (!(exec_tmp_table1=
1834
 
          create_tmp_table(thd, &tmp_table_param, all_fields,
1835
 
                           tmp_group,
1836
 
                           group_list ? 0 : select_distinct,
1837
 
                           group_list && simple_group,
1838
 
                           select_options,
1839
 
                           tmp_rows_limit,
1840
 
                           (char *) "")))
1841
 
                {
1842
 
      return(1);
1843
 
    }
1844
 
 
1845
 
    /*
1846
 
      We don't have to store rows in temp table that doesn't match HAVING if:
1847
 
      - we are sorting the table and writing complete group rows to the
1848
 
        temp table.
1849
 
      - We are using DISTINCT without resolving the distinct as a GROUP BY
1850
 
        on all columns.
1851
 
      
1852
 
      If having is not handled here, it will be checked before the row
1853
 
      is sent to the client.
1854
 
    */    
1855
 
    if (tmp_having && 
1856
 
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1857
 
      having= tmp_having;
1858
 
 
1859
 
    /* if group or order on first table, sort first */
1860
 
    if (group_list && simple_group)
1861
 
    {
1862
 
      thd_proc_info(thd, "Sorting for group");
1863
 
      if (create_sort_index(thd, this, group_list,
1864
 
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1865
 
          alloc_group_fields(this, group_list) ||
1866
 
          make_sum_func_list(all_fields, fields_list, 1) ||
1867
 
          setup_sum_funcs(thd, sum_funcs))
1868
 
      {
1869
 
        return(1);
1870
 
      }
1871
 
      group_list=0;
1872
 
    }
1873
 
    else
1874
 
    {
1875
 
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1876
 
          setup_sum_funcs(thd, sum_funcs))
1877
 
      {
1878
 
        return(1);
1879
 
      }
1880
 
 
1881
 
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1882
 
      {
1883
 
        thd_proc_info(thd, "Sorting for order");
1884
 
        if (create_sort_index(thd, this, order,
1885
 
                              HA_POS_ERROR, HA_POS_ERROR, true))
1886
 
        {
1887
 
          return(1);
1888
 
        }
1889
 
        order=0;
1890
 
      }
1891
 
    }
1892
 
    
1893
 
    /*
1894
 
      Optimize distinct when used on some of the tables
1895
 
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1896
 
      In this case we can stop scanning t2 when we have found one t1.a
1897
 
    */
1898
 
 
1899
 
    if (exec_tmp_table1->distinct)
1900
 
    {
1901
 
      table_map used_tables= thd->used_tables;
1902
 
      JOIN_TAB *last_join_tab= join_tab+tables-1;
1903
 
      do
1904
 
      {
1905
 
        if (used_tables & last_join_tab->table->map)
1906
 
          break;
1907
 
        last_join_tab->not_used_in_distinct=1;
1908
 
      } while (last_join_tab-- != join_tab);
1909
 
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1910
 
      if (order && skip_sort_order)
1911
 
      {
1912
 
        /* Should always succeed */
1913
 
        if (test_if_skip_sort_order(&join_tab[const_tables],
1914
 
                                    order, unit->select_limit_cnt, 0, 
1915
 
                                    &join_tab[const_tables].table->
1916
 
                                      keys_in_use_for_order_by))
1917
 
          order=0;
1918
 
      }
1919
 
    }
1920
 
 
1921
 
    /* 
1922
 
      If this join belongs to an uncacheable subquery save 
1923
 
      the original join 
1924
 
    */
1925
 
    if (select_lex->uncacheable && !is_top_level_join() &&
1926
 
        init_save_join_tab())
1927
 
      return(-1);                         /* purecov: inspected */
1928
 
  }
1929
 
 
1930
 
  error= 0;
1931
 
  return(0);
1932
 
}
1933
 
 
1934
 
 
1935
 
/**
1936
 
  Restore values in temporary join.
1937
 
*/
1938
 
void JOIN::restore_tmp()
1939
 
{
1940
 
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1941
 
}
1942
 
 
1943
 
 
1944
 
int
1945
 
JOIN::reinit()
1946
 
{
1947
 
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1948
 
                                    select_lex->offset_limit->val_uint() :
1949
 
                                    0ULL);
1950
 
 
1951
 
  first_record= 0;
1952
 
 
1953
 
  if (exec_tmp_table1)
1954
 
  {
1955
 
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1956
 
    exec_tmp_table1->file->ha_delete_all_rows();
1957
 
    free_io_cache(exec_tmp_table1);
1958
 
    filesort_free_buffers(exec_tmp_table1,0);
1959
 
  }
1960
 
  if (exec_tmp_table2)
1961
 
  {
1962
 
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1963
 
    exec_tmp_table2->file->ha_delete_all_rows();
1964
 
    free_io_cache(exec_tmp_table2);
1965
 
    filesort_free_buffers(exec_tmp_table2,0);
1966
 
  }
1967
 
  if (items0)
1968
 
    set_items_ref_array(items0);
1969
 
 
1970
 
  if (join_tab_save)
1971
 
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1972
 
 
1973
 
  if (tmp_join)
1974
 
    restore_tmp();
1975
 
 
1976
 
  /* Reset of sum functions */
1977
 
  if (sum_funcs)
1978
 
  {
1979
 
    Item_sum *func, **func_ptr= sum_funcs;
1980
 
    while ((func= *(func_ptr++)))
1981
 
      func->clear();
1982
 
  }
1983
 
 
1984
 
  return(0);
1985
 
}
1986
 
 
1987
 
/**
1988
 
   @brief Save the original join layout
1989
 
      
1990
 
   @details Saves the original join layout so it can be reused in 
1991
 
   re-execution and for EXPLAIN.
1992
 
             
1993
 
   @return Operation status
1994
 
   @retval 0      success.
1995
 
   @retval 1      error occurred.
1996
 
*/
1997
 
 
1998
 
bool
1999
 
JOIN::init_save_join_tab()
2000
 
{
2001
 
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
2002
 
    return 1;                                  /* purecov: inspected */
2003
 
  error= 0;                                    // Ensure that tmp_join.error= 0
2004
 
  restore_tmp();
2005
 
  return 0;
2006
 
}
2007
 
 
2008
 
 
2009
 
bool
2010
 
JOIN::save_join_tab()
2011
 
{
2012
 
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
2013
 
  {
2014
 
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2015
 
                                                sizeof(JOIN_TAB) * tables)))
2016
 
      return 1;
2017
 
  }
2018
 
  return 0;
2019
 
}
2020
 
 
2021
 
 
2022
 
/**
2023
 
  Exec select.
2024
 
 
2025
 
  @todo
2026
 
    Note, that create_sort_index calls test_if_skip_sort_order and may
2027
 
    finally replace sorting with index scan if there is a LIMIT clause in
2028
 
    the query.  It's never shown in EXPLAIN!
2029
 
 
2030
 
  @todo
2031
 
    When can we have here thd->net.report_error not zero?
2032
 
*/
2033
 
void
2034
 
JOIN::exec()
2035
 
{
2036
 
  List<Item> *columns_list= &fields_list;
2037
 
  int      tmp_error;
2038
 
 
2039
 
  thd_proc_info(thd, "executing");
2040
 
  error= 0;
2041
 
  (void) result->prepare2(); // Currently, this cannot fail.
2042
 
 
2043
 
  if (!tables_list && (tables || !select_lex->with_sum_func))
2044
 
  {                                           // Only test of functions
2045
 
    if (select_options & SELECT_DESCRIBE)
2046
 
      select_describe(this, false, false, false,
2047
 
                      (zero_result_cause?zero_result_cause:"No tables used"));
2048
 
    else
2049
 
    {
2050
 
      result->send_fields(*columns_list,
2051
 
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2052
 
      /*
2053
 
        We have to test for 'conds' here as the WHERE may not be constant
2054
 
        even if we don't have any tables for prepared statements or if
2055
 
        conds uses something like 'rand()'.
2056
 
      */
2057
 
      if (cond_value != Item::COND_FALSE &&
2058
 
          (!conds || conds->val_int()) &&
2059
 
          (!having || having->val_int()))
2060
 
      {
2061
 
        if (do_send_rows && result->send_data(fields_list))
2062
 
          error= 1;
2063
 
        else
2064
 
        {
2065
 
          error= (int) result->send_eof();
2066
 
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2067
 
                         thd->sent_row_count);
2068
 
        }
2069
 
      }
2070
 
      else
2071
 
      {
2072
 
        error=(int) result->send_eof();
2073
 
        send_records= 0;
2074
 
      }
2075
 
    }
2076
 
    /* Single select (without union) always returns 0 or 1 row */
2077
 
    thd->limit_found_rows= send_records;
2078
 
    thd->examined_row_count= 0;
2079
 
    return;
2080
 
  }
2081
 
  /*
2082
 
    Don't reset the found rows count if there're no tables as
2083
 
    FOUND_ROWS() may be called. Never reset the examined row count here.
2084
 
    It must be accumulated from all join iterations of all join parts.
2085
 
  */
2086
 
  if (tables)
2087
 
    thd->limit_found_rows= 0;
2088
 
 
2089
 
  if (zero_result_cause)
2090
 
  {
2091
 
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
2092
 
                            *columns_list,
2093
 
                            send_row_on_empty_set(),
2094
 
                            select_options,
2095
 
                            zero_result_cause,
2096
 
                            having);
2097
 
    return;
2098
 
  }
2099
 
 
2100
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2101
 
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2102
 
    return;
2103
 
 
2104
 
  if (select_options & SELECT_DESCRIBE)
2105
 
  {
2106
 
    /*
2107
 
      Check if we managed to optimize ORDER BY away and don't use temporary
2108
 
      table to resolve ORDER BY: in that case, we only may need to do
2109
 
      filesort for GROUP BY.
2110
 
    */
2111
 
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2112
 
    {
2113
 
      /*
2114
 
        Reset 'order' to 'group_list' and reinit variables describing
2115
 
        'order'
2116
 
      */
2117
 
      order= group_list;
2118
 
      simple_order= simple_group;
2119
 
      skip_sort_order= 0;
2120
 
    }
2121
 
    if (order && 
2122
 
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2123
 
        (const_tables == tables ||
2124
 
         ((simple_order || skip_sort_order) &&
2125
 
          test_if_skip_sort_order(&join_tab[const_tables], order,
2126
 
                                  select_limit, 0, 
2127
 
                                  &join_tab[const_tables].table->
2128
 
                                    keys_in_use_for_query))))
2129
 
      order=0;
2130
 
    having= tmp_having;
2131
 
    select_describe(this, need_tmp,
2132
 
                    order != 0 && !skip_sort_order,
2133
 
                    select_distinct,
2134
 
                    !tables ? "No tables used" : NullS);
2135
 
    return;
2136
 
  }
2137
 
 
2138
 
  JOIN *curr_join= this;
2139
 
  List<Item> *curr_all_fields= &all_fields;
2140
 
  List<Item> *curr_fields_list= &fields_list;
2141
 
  TABLE *curr_tmp_table= 0;
2142
 
  /*
2143
 
    Initialize examined rows here because the values from all join parts
2144
 
    must be accumulated in examined_row_count. Hence every join
2145
 
    iteration must count from zero.
2146
 
  */
2147
 
  curr_join->examined_rows= 0;
2148
 
 
2149
 
  /* Create a tmp table if distinct or if the sort is too complicated */
2150
 
  if (need_tmp)
2151
 
  {
2152
 
    if (tmp_join)
2153
 
    {
2154
 
      /*
2155
 
        We are in a non cacheable sub query. Get the saved join structure
2156
 
        after optimization.
2157
 
        (curr_join may have been modified during last exection and we need
2158
 
        to reset it)
2159
 
      */
2160
 
      curr_join= tmp_join;
2161
 
    }
2162
 
    curr_tmp_table= exec_tmp_table1;
2163
 
 
2164
 
    /* Copy data to the temporary table */
2165
 
    thd_proc_info(thd, "Copying to tmp table");
2166
 
    if (!curr_join->sort_and_group &&
2167
 
        curr_join->const_tables != curr_join->tables)
2168
 
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2169
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2170
 
    {
2171
 
      error= tmp_error;
2172
 
      return;
2173
 
    }
2174
 
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2175
 
    
2176
 
    if (curr_join->having)
2177
 
      curr_join->having= curr_join->tmp_having= 0; // Allready done
2178
 
    
2179
 
    /* Change sum_fields reference to calculated fields in tmp_table */
2180
 
    curr_join->all_fields= *curr_all_fields;
2181
 
    if (!items1)
2182
 
    {
2183
 
      items1= items0 + all_fields.elements;
2184
 
      if (sort_and_group || curr_tmp_table->group)
2185
 
      {
2186
 
        if (change_to_use_tmp_fields(thd, items1,
2187
 
                                     tmp_fields_list1, tmp_all_fields1,
2188
 
                                     fields_list.elements, all_fields))
2189
 
          return;
2190
 
      }
2191
 
      else
2192
 
      {
2193
 
        if (change_refs_to_tmp_fields(thd, items1,
2194
 
                                      tmp_fields_list1, tmp_all_fields1,
2195
 
                                      fields_list.elements, all_fields))
2196
 
          return;
2197
 
      }
2198
 
      curr_join->tmp_all_fields1= tmp_all_fields1;
2199
 
      curr_join->tmp_fields_list1= tmp_fields_list1;
2200
 
      curr_join->items1= items1;
2201
 
    }
2202
 
    curr_all_fields= &tmp_all_fields1;
2203
 
    curr_fields_list= &tmp_fields_list1;
2204
 
    curr_join->set_items_ref_array(items1);
2205
 
    
2206
 
    if (sort_and_group || curr_tmp_table->group)
2207
 
    {
2208
 
      curr_join->tmp_table_param.field_count+= 
2209
 
        curr_join->tmp_table_param.sum_func_count+
2210
 
        curr_join->tmp_table_param.func_count;
2211
 
      curr_join->tmp_table_param.sum_func_count= 
2212
 
        curr_join->tmp_table_param.func_count= 0;
2213
 
    }
2214
 
    else
2215
 
    {
2216
 
      curr_join->tmp_table_param.field_count+= 
2217
 
        curr_join->tmp_table_param.func_count;
2218
 
      curr_join->tmp_table_param.func_count= 0;
2219
 
    }
2220
 
    
2221
 
    if (curr_tmp_table->group)
2222
 
    {                                           // Already grouped
2223
 
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2224
 
        curr_join->order= curr_join->group_list;  /* order by group */
2225
 
      curr_join->group_list= 0;
2226
 
    }
2227
 
    
2228
 
    /*
2229
 
      If we have different sort & group then we must sort the data by group
2230
 
      and copy it to another tmp table
2231
 
      This code is also used if we are using distinct something
2232
 
      we haven't been able to store in the temporary table yet
2233
 
      like SEC_TO_TIME(SUM(...)).
2234
 
    */
2235
 
 
2236
 
    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))
2237
 
    {                                   /* Must copy to another table */
2238
 
      /* Free first data from old join */
2239
 
      curr_join->join_free();
2240
 
      if (make_simple_join(curr_join, curr_tmp_table))
2241
 
        return;
2242
 
      calc_group_buffer(curr_join, group_list);
2243
 
      count_field_types(select_lex, &curr_join->tmp_table_param,
2244
 
                        curr_join->tmp_all_fields1,
2245
 
                        curr_join->select_distinct && !curr_join->group_list);
2246
 
      curr_join->tmp_table_param.hidden_field_count= 
2247
 
        (curr_join->tmp_all_fields1.elements-
2248
 
         curr_join->tmp_fields_list1.elements);
2249
 
      
2250
 
      
2251
 
      if (exec_tmp_table2)
2252
 
        curr_tmp_table= exec_tmp_table2;
2253
 
      else
2254
 
      {
2255
 
        /* group data to new table */
2256
 
 
2257
 
        /*
2258
 
          If the access method is loose index scan then all MIN/MAX
2259
 
          functions are precomputed, and should be treated as regular
2260
 
          functions. See extended comment in JOIN::exec.
2261
 
        */
2262
 
        if (curr_join->join_tab->is_using_loose_index_scan())
2263
 
          curr_join->tmp_table_param.precomputed_group_by= true;
2264
 
 
2265
 
        if (!(curr_tmp_table=
2266
 
              exec_tmp_table2= create_tmp_table(thd,
2267
 
                                                &curr_join->tmp_table_param,
2268
 
                                                *curr_all_fields,
2269
 
                                                (ORDER*) 0,
2270
 
                                                curr_join->select_distinct && 
2271
 
                                                !curr_join->group_list,
2272
 
                                                1, curr_join->select_options,
2273
 
                                                HA_POS_ERROR,
2274
 
                                                (char *) "")))
2275
 
          return;
2276
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
2277
 
      }
2278
 
      if (curr_join->group_list)
2279
 
      {
2280
 
        thd_proc_info(thd, "Creating sort index");
2281
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2282
 
        {
2283
 
          return;
2284
 
        }
2285
 
        if (create_sort_index(thd, curr_join, curr_join->group_list,
2286
 
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2287
 
            make_group_fields(this, curr_join))
2288
 
        {
2289
 
          return;
2290
 
        }
2291
 
        sortorder= curr_join->sortorder;
2292
 
      }
2293
 
      
2294
 
      thd_proc_info(thd, "Copying to group table");
2295
 
      tmp_error= -1;
2296
 
      if (curr_join != this)
2297
 
      {
2298
 
        if (sum_funcs2)
2299
 
        {
2300
 
          curr_join->sum_funcs= sum_funcs2;
2301
 
          curr_join->sum_funcs_end= sum_funcs_end2; 
2302
 
        }
2303
 
        else
2304
 
        {
2305
 
          curr_join->alloc_func_list();
2306
 
          sum_funcs2= curr_join->sum_funcs;
2307
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2308
 
        }
2309
 
      }
2310
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2311
 
                                        1, true))
2312
 
        return;
2313
 
      curr_join->group_list= 0;
2314
 
      if (!curr_join->sort_and_group &&
2315
 
          curr_join->const_tables != curr_join->tables)
2316
 
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2317
 
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2318
 
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2319
 
      {
2320
 
        error= tmp_error;
2321
 
        return;
2322
 
      }
2323
 
      end_read_record(&curr_join->join_tab->read_record);
2324
 
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2325
 
      curr_join->join_tab[0].table= 0;           // Table is freed
2326
 
      
2327
 
      // No sum funcs anymore
2328
 
      if (!items2)
2329
 
      {
2330
 
        items2= items1 + all_fields.elements;
2331
 
        if (change_to_use_tmp_fields(thd, items2,
2332
 
                                     tmp_fields_list2, tmp_all_fields2, 
2333
 
                                     fields_list.elements, tmp_all_fields1))
2334
 
          return;
2335
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2336
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
2337
 
      }
2338
 
      curr_fields_list= &curr_join->tmp_fields_list2;
2339
 
      curr_all_fields= &curr_join->tmp_all_fields2;
2340
 
      curr_join->set_items_ref_array(items2);
2341
 
      curr_join->tmp_table_param.field_count+= 
2342
 
        curr_join->tmp_table_param.sum_func_count;
2343
 
      curr_join->tmp_table_param.sum_func_count= 0;
2344
 
    }
2345
 
    if (curr_tmp_table->distinct)
2346
 
      curr_join->select_distinct=0;             /* Each row is unique */
2347
 
    
2348
 
    curr_join->join_free();                     /* Free quick selects */
2349
 
    if (curr_join->select_distinct && ! curr_join->group_list)
2350
 
    {
2351
 
      thd_proc_info(thd, "Removing duplicates");
2352
 
      if (curr_join->tmp_having)
2353
 
        curr_join->tmp_having->update_used_tables();
2354
 
      if (remove_duplicates(curr_join, curr_tmp_table,
2355
 
                            *curr_fields_list, curr_join->tmp_having))
2356
 
        return;
2357
 
      curr_join->tmp_having=0;
2358
 
      curr_join->select_distinct=0;
2359
 
    }
2360
 
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2361
 
    if (make_simple_join(curr_join, curr_tmp_table))
2362
 
      return;
2363
 
    calc_group_buffer(curr_join, curr_join->group_list);
2364
 
    count_field_types(select_lex, &curr_join->tmp_table_param, 
2365
 
                      *curr_all_fields, 0);
2366
 
    
2367
 
  }
2368
 
  
2369
 
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2370
 
  {
2371
 
    if (make_group_fields(this, curr_join))
2372
 
    {
2373
 
      return;
2374
 
    }
2375
 
    if (!items3)
2376
 
    {
2377
 
      if (!items0)
2378
 
        init_items_ref_array();
2379
 
      items3= ref_pointer_array + (all_fields.elements*4);
2380
 
      setup_copy_fields(thd, &curr_join->tmp_table_param,
2381
 
                        items3, tmp_fields_list3, tmp_all_fields3,
2382
 
                        curr_fields_list->elements, *curr_all_fields);
2383
 
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2384
 
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2385
 
      tmp_table_param.save_copy_field_end=
2386
 
        curr_join->tmp_table_param.copy_field_end;
2387
 
      curr_join->tmp_all_fields3= tmp_all_fields3;
2388
 
      curr_join->tmp_fields_list3= tmp_fields_list3;
2389
 
    }
2390
 
    else
2391
 
    {
2392
 
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2393
 
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2394
 
      curr_join->tmp_table_param.copy_field_end=
2395
 
        tmp_table_param.save_copy_field_end;
2396
 
    }
2397
 
    curr_fields_list= &tmp_fields_list3;
2398
 
    curr_all_fields= &tmp_all_fields3;
2399
 
    curr_join->set_items_ref_array(items3);
2400
 
 
2401
 
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2402
 
                                      1, true) || 
2403
 
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2404
 
        thd->is_fatal_error)
2405
 
      return;
2406
 
  }
2407
 
  if (curr_join->group_list || curr_join->order)
2408
 
  {
2409
 
    thd_proc_info(thd, "Sorting result");
2410
 
    /* If we have already done the group, add HAVING to sorted table */
2411
 
    if (curr_join->tmp_having && ! curr_join->group_list && 
2412
 
        ! curr_join->sort_and_group)
2413
 
    {
2414
 
      // Some tables may have been const
2415
 
      curr_join->tmp_having->update_used_tables();
2416
 
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2417
 
      table_map used_tables= (curr_join->const_table_map |
2418
 
                              curr_table->table->map);
2419
 
 
2420
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2421
 
                                                 used_tables,
2422
 
                                                 used_tables, 0);
2423
 
      if (sort_table_cond)
2424
 
      {
2425
 
        if (!curr_table->select)
2426
 
          if (!(curr_table->select= new SQL_SELECT))
2427
 
            return;
2428
 
        if (!curr_table->select->cond)
2429
 
          curr_table->select->cond= sort_table_cond;
2430
 
        else                                    // This should never happen
2431
 
        {
2432
 
          if (!(curr_table->select->cond=
2433
 
                new Item_cond_and(curr_table->select->cond,
2434
 
                                  sort_table_cond)))
2435
 
            return;
2436
 
          /*
2437
 
            Item_cond_and do not need fix_fields for execution, its parameters
2438
 
            are fixed or do not need fix_fields, too
2439
 
          */
2440
 
          curr_table->select->cond->quick_fix_field();
2441
 
        }
2442
 
        curr_table->select_cond= curr_table->select->cond;
2443
 
        curr_table->select_cond->top_level_item();
2444
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2445
 
                                                   ~ (table_map) 0,
2446
 
                                                   ~used_tables, 0);
2447
 
      }
2448
 
    }
2449
 
    {
2450
 
      if (group)
2451
 
        curr_join->select_limit= HA_POS_ERROR;
2452
 
      else
2453
 
      {
2454
 
        /*
2455
 
          We can abort sorting after thd->select_limit rows if we there is no
2456
 
          WHERE clause for any tables after the sorted one.
2457
 
        */
2458
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2459
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2460
 
        for (; curr_table < end_table ; curr_table++)
2461
 
        {
2462
 
          /*
2463
 
            table->keyuse is set in the case there was an original WHERE clause
2464
 
            on the table that was optimized away.
2465
 
          */
2466
 
          if (curr_table->select_cond ||
2467
 
              (curr_table->keyuse && !curr_table->first_inner))
2468
 
          {
2469
 
            /* We have to sort all rows */
2470
 
            curr_join->select_limit= HA_POS_ERROR;
2471
 
            break;
2472
 
          }
2473
 
        }
2474
 
      }
2475
 
      if (curr_join->join_tab == join_tab && save_join_tab())
2476
 
      {
2477
 
        return;
2478
 
      }
2479
 
      /*
2480
 
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2481
 
        chose FILESORT to be faster than INDEX SCAN or there is no 
2482
 
        suitable index present.
2483
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2484
 
        finally replace sorting with index scan if there is a LIMIT clause in
2485
 
        the query. XXX: it's never shown in EXPLAIN!
2486
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2487
 
      */
2488
 
      if (create_sort_index(thd, curr_join,
2489
 
                            curr_join->group_list ? 
2490
 
                            curr_join->group_list : curr_join->order,
2491
 
                            curr_join->select_limit,
2492
 
                            (select_options & OPTION_FOUND_ROWS ?
2493
 
                             HA_POS_ERROR : unit->select_limit_cnt),
2494
 
                            curr_join->group_list ? true : false))
2495
 
        return;
2496
 
      sortorder= curr_join->sortorder;
2497
 
      if (curr_join->const_tables != curr_join->tables &&
2498
 
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2499
 
      {
2500
 
        /*
2501
 
          If no IO cache exists for the first table then we are using an
2502
 
          INDEX SCAN and no filesort. Thus we should not remove the sorted
2503
 
          attribute on the INDEX SCAN.
2504
 
        */
2505
 
        skip_sort_order= 1;
2506
 
      }
2507
 
    }
2508
 
  }
2509
 
  /* XXX: When can we have here thd->is_error() not zero? */
2510
 
  if (thd->is_error())
2511
 
  {
2512
 
    error= thd->is_error();
2513
 
    return;
2514
 
  }
2515
 
  curr_join->having= curr_join->tmp_having;
2516
 
  curr_join->fields= curr_fields_list;
2517
 
 
2518
 
  {
2519
 
    thd_proc_info(thd, "Sending data");
2520
 
    result->send_fields(*curr_fields_list,
2521
 
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2522
 
    error= do_select(curr_join, curr_fields_list, NULL);
2523
 
    thd->limit_found_rows= curr_join->send_records;
2524
 
  }
2525
 
 
2526
 
  /* Accumulate the counts from all join iterations of all join parts. */
2527
 
  thd->examined_row_count+= curr_join->examined_rows;
2528
 
 
2529
 
  /* 
2530
 
    With EXPLAIN EXTENDED we have to restore original ref_array
2531
 
    for a derived table which is always materialized.
2532
 
    Otherwise we would not be able to print the query  correctly.
2533
 
  */ 
2534
 
  if (items0 &&
2535
 
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
2536
 
      select_lex->linkage == DERIVED_TABLE_TYPE)      
2537
 
    set_items_ref_array(items0);
2538
 
 
2539
 
  return;
2540
 
}
2541
 
 
2542
 
 
2543
 
/**
2544
 
  Clean up join.
2545
 
 
2546
 
  @return
2547
 
    Return error that hold JOIN.
2548
 
*/
2549
 
 
2550
 
int
2551
 
JOIN::destroy()
2552
 
{
2553
 
  select_lex->join= 0;
2554
 
 
2555
 
  if (tmp_join)
2556
 
  {
2557
 
    if (join_tab != tmp_join->join_tab)
2558
 
    {
2559
 
      JOIN_TAB *tab, *end;
2560
 
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2561
 
        tab->cleanup();
2562
 
    }
2563
 
    tmp_join->tmp_join= 0;
2564
 
    tmp_table_param.copy_field=0;
2565
 
    return(tmp_join->destroy());
2566
 
  }
2567
 
  cond_equal= 0;
2568
 
 
2569
 
  cleanup(1);
2570
 
  if (exec_tmp_table1)
2571
 
    free_tmp_table(thd, exec_tmp_table1);
2572
 
  if (exec_tmp_table2)
2573
 
    free_tmp_table(thd, exec_tmp_table2);
2574
 
  delete select;
2575
 
  delete_dynamic(&keyuse);
2576
 
  return(error);
2577
 
}
2578
 
 
2579
 
 
2580
 
 
2581
305
/**
2582
306
  An entry point to single-unit select (a select without UNION).
2583
307
 
2584
 
  @param thd                  thread handler
 
308
  @param session                  thread Cursor
2585
309
  @param rref_pointer_array   a reference to ref_pointer_array of
2586
310
                              the top-level select_lex for this query
2587
311
  @param tables               list of all tables used in this query.
2588
312
                              The tables have been pre-opened.
2589
 
  @param wild_num             number of wildcards used in the top level 
 
313
  @param wild_num             number of wildcards used in the top level
2590
314
                              select of this query.
2591
315
                              For example statement
2592
316
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2597
321
                              for a, b and c in this list.
2598
322
  @param conds                top level item of an expression representing
2599
323
                              WHERE clause of the top level select
2600
 
  @param og_num               total number of ORDER BY and GROUP BY clauses
 
324
  @param og_num               total number of order_st BY and GROUP BY clauses
2601
325
                              arguments
2602
 
  @param order                linked list of ORDER BY agruments
 
326
  @param order                linked list of order_st BY agruments
2603
327
  @param group                linked list of GROUP BY arguments
2604
328
  @param having               top level item of HAVING expression
2605
 
  @param proc_param           list of PROCEDUREs
2606
329
  @param select_options       select options (BIG_RESULT, etc)
2607
330
  @param result               an instance of result set handling class.
2608
331
                              This object is responsible for send result
2609
332
                              set rows to the client or inserting them
2610
333
                              into a table.
2611
 
  @param select_lex           the only SELECT_LEX of this query
 
334
  @param select_lex           the only Select_Lex of this query
2612
335
  @param unit                 top-level UNIT of this query
2613
336
                              UNIT is an artificial object created by the
2614
337
                              parser for every SELECT clause.
2621
344
  @retval
2622
345
    true   an error
2623
346
*/
2624
 
 
2625
 
bool
2626
 
mysql_select(THD *thd, Item ***rref_pointer_array,
2627
 
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
2628
 
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
2629
 
             Item *having, ORDER *proc_param, uint64_t select_options,
2630
 
             select_result *result, SELECT_LEX_UNIT *unit,
2631
 
             SELECT_LEX *select_lex)
 
347
bool mysql_select(Session *session,
 
348
                  Item ***rref_pointer_array,
 
349
                        TableList *tables, 
 
350
                  uint32_t wild_num, 
 
351
                  List<Item> &fields,
 
352
                        COND *conds, 
 
353
                  uint32_t og_num,  
 
354
                  order_st *order, 
 
355
                  order_st *group,
 
356
                        Item *having, 
 
357
                  uint64_t select_options,
 
358
                        select_result *result, 
 
359
                  Select_Lex_Unit *unit,
 
360
                        Select_Lex *select_lex)
2632
361
{
2633
362
  bool err;
2634
363
  bool free_join= 1;
2643
372
      creation
2644
373
    */
2645
374
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2646
 
        (select_options & SELECT_DESCRIBE))
 
375
        (select_options & SELECT_DESCRIBE))
2647
376
    {
2648
377
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2649
378
      {
2650
 
        //here is EXPLAIN of subselect or derived table
2651
 
        if (join->change_result(result))
2652
 
        {
2653
 
          return(true);
2654
 
        }
 
379
        //here is EXPLAIN of subselect or derived table
 
380
        if (join->change_result(result))
 
381
        {
 
382
          return(true);
 
383
        }
2655
384
      }
2656
385
      else
2657
386
      {
2658
387
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2659
 
                               conds, og_num, order, group, having, proc_param,
2660
 
                               select_lex, unit)))
2661
 
        {
2662
 
          goto err;
2663
 
        }
 
388
                               conds, og_num, order, group, having, select_lex, unit)))
 
389
        {
 
390
          goto err;
 
391
        }
2664
392
      }
2665
393
    }
2666
394
    free_join= 0;
2668
396
  }
2669
397
  else
2670
398
  {
2671
 
    if (!(join= new JOIN(thd, fields, select_options, result)))
2672
 
        return(true);
2673
 
    thd_proc_info(thd, "init");
2674
 
    thd->used_tables=0;                         // Updated by setup_fields
 
399
    if (!(join= new JOIN(session, fields, select_options, result)))
 
400
      return(true);
 
401
    session->set_proc_info("init");
 
402
    session->used_tables=0;                         // Updated by setup_fields
2675
403
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2676
 
                           conds, og_num, order, group, having, proc_param,
 
404
                           conds, og_num, order, group, having,
2677
405
                           select_lex, unit)) == true)
2678
406
    {
2679
407
      goto err;
2680
408
    }
2681
409
  }
2682
410
 
2683
 
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2684
 
  if (join->flatten_subqueries())
2685
 
  {
2686
 
    err= 1;
2687
 
    goto err;
2688
 
  }
2689
 
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2690
 
 
2691
 
  if ((err= join->optimize()))
2692
 
  {
2693
 
    goto err;                                   // 1
2694
 
  }
2695
 
 
2696
 
  if (thd->lex->describe & DESCRIBE_EXTENDED)
 
411
  err= join->optimize();
 
412
  if (err)
 
413
  {
 
414
    goto err; // 1
 
415
  }
 
416
 
 
417
  if (session->lex->describe & DESCRIBE_EXTENDED)
2697
418
  {
2698
419
    join->conds_history= join->conds;
2699
420
    join->having_history= (join->having?join->having:join->tmp_having);
2700
421
  }
2701
422
 
2702
 
  if (thd->is_error())
 
423
  if (session->is_error())
2703
424
    goto err;
2704
425
 
2705
426
  join->exec();
2706
427
 
2707
 
  if (thd->lex->describe & DESCRIBE_EXTENDED)
 
428
  if (session->lex->describe & DESCRIBE_EXTENDED)
2708
429
  {
2709
430
    select_lex->where= join->conds_history;
2710
431
    select_lex->having= join->having_history;
2713
434
err:
2714
435
  if (free_join)
2715
436
  {
2716
 
    thd_proc_info(thd, "end");
 
437
    session->set_proc_info("end");
2717
438
    err|= select_lex->cleanup();
2718
 
    return(err || thd->is_error());
 
439
    return(err || session->is_error());
2719
440
  }
2720
441
  return(join->error);
2721
442
}
2722
443
 
2723
 
 
2724
 
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
2725
 
                          Item_in_subselect* const *el2)
2726
 
{
2727
 
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
2728
 
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2729
 
}
2730
 
 
2731
 
 
2732
 
inline Item * and_items(Item* cond, Item *item)
 
444
inline Item *and_items(Item* cond, Item *item)
2733
445
{
2734
446
  return (cond? (new Item_cond_and(cond, item)) : item);
2735
447
}
2736
448
 
2737
 
 
2738
 
static TABLE_LIST *alloc_join_nest(THD *thd)
2739
 
{
2740
 
  TABLE_LIST *tbl;
2741
 
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2742
 
                                       sizeof(NESTED_JOIN))))
2743
 
    return NULL;
2744
 
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
2745
 
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
2746
 
  return tbl;
2747
 
}
2748
 
 
2749
 
 
2750
 
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2751
 
{
2752
 
  List_iterator<TABLE_LIST> it(*tlist);
2753
 
  TABLE_LIST *table;
 
449
static void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
 
450
{
 
451
  List_iterator<TableList> it(*tlist);
 
452
  TableList *table;
2754
453
  while ((table= it++))
2755
454
  {
2756
455
    if (table->on_expr)
2760
459
  }
2761
460
}
2762
461
 
2763
 
 
2764
 
/*
2765
 
  Convert a subquery predicate into a TABLE_LIST semi-join nest
2766
 
 
2767
 
  SYNOPSIS
2768
 
    convert_subq_to_sj()
2769
 
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
2770
 
                    clause
2771
 
       subq_pred    Subquery predicate to be converted
2772
 
  
2773
 
  DESCRIPTION
2774
 
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
2775
 
    prerequisites are already checked, so the conversion is always successfull.
2776
 
 
2777
 
    Prepared Statements: the transformation is permanent:
2778
 
     - Changes in TABLE_LIST structures are naturally permanent
2779
 
     - Item tree changes are performed on statement MEM_ROOT:
2780
 
        = we activate statement MEM_ROOT 
2781
 
        = this function is called before the first fix_prepare_information
2782
 
          call.
2783
 
 
2784
 
    This is intended because the criteria for subquery-to-sj conversion remain
2785
 
    constant for the lifetime of the Prepared Statement.
2786
 
 
2787
 
  RETURN
2788
 
    false  OK
2789
 
    true   Out of memory error
2790
 
*/
2791
 
 
2792
 
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2793
 
{
2794
 
  SELECT_LEX *parent_lex= parent_join->select_lex;
2795
 
  TABLE_LIST *emb_tbl_nest= NULL;
2796
 
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2797
 
  THD *thd= parent_join->thd;
2798
 
 
2799
 
  /*
2800
 
    1. Find out where to put the predicate into.
2801
 
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2802
 
  */
2803
 
  if ((void*)subq_pred->expr_join_nest != (void*)1)
2804
 
  {
2805
 
    if (subq_pred->expr_join_nest->nested_join)
2806
 
    {
2807
 
      /*
2808
 
        We're dealing with
2809
 
 
2810
 
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
2811
 
 
2812
 
        The sj-nest will be inserted into the brackets nest.
2813
 
      */
2814
 
      emb_tbl_nest=  subq_pred->expr_join_nest;
2815
 
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
2816
 
    }
2817
 
    else if (!subq_pred->expr_join_nest->outer_join)
2818
 
    {
2819
 
      /*
2820
 
        We're dealing with
2821
 
 
2822
 
          ... INNER JOIN tblX ON (subquery AND whatever) ...
2823
 
 
2824
 
        The sj-nest will be tblX's "sibling", i.e. another child of its
2825
 
        parent. This is ok because tblX is joined as an inner join.
2826
 
      */
2827
 
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2828
 
      if (emb_tbl_nest)
2829
 
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
2830
 
    }
2831
 
    else if (!subq_pred->expr_join_nest->nested_join)
2832
 
    {
2833
 
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
2834
 
      TABLE_LIST *wrap_nest;
2835
 
      /*
2836
 
        We're dealing with
2837
 
 
2838
 
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2839
 
 
2840
 
        we'll need to convert it into:
2841
 
 
2842
 
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2843
 
                        |                      |
2844
 
                        |<----- wrap_nest ---->|
2845
 
        
2846
 
        Q:  other subqueries may be pointing to this element. What to do?
2847
 
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2848
 
            But we'll need to fix other pointers.
2849
 
        A2: Another way: have TABLE_LIST::next_ptr so the following
2850
 
            subqueries know the table has been nested.
2851
 
        A3: changes in the TABLE_LIST::outer_join will make everything work
2852
 
            automatically.
2853
 
      */
2854
 
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2855
 
      {
2856
 
        return(true);
2857
 
      }
2858
 
      wrap_nest->embedding= outer_tbl->embedding;
2859
 
      wrap_nest->join_list= outer_tbl->join_list;
2860
 
      wrap_nest->alias= (char*) "(sj-wrap)";
2861
 
 
2862
 
      wrap_nest->nested_join->join_list.empty();
2863
 
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
2864
 
 
2865
 
      outer_tbl->embedding= wrap_nest;
2866
 
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2867
 
 
2868
 
      /*
2869
 
        wrap_nest will take place of outer_tbl, so move the outer join flag
2870
 
        and on_expr
2871
 
      */
2872
 
      wrap_nest->outer_join= outer_tbl->outer_join;
2873
 
      outer_tbl->outer_join= 0;
2874
 
 
2875
 
      wrap_nest->on_expr= outer_tbl->on_expr;
2876
 
      outer_tbl->on_expr= NULL;
2877
 
 
2878
 
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2879
 
      TABLE_LIST *tbl;
2880
 
      while ((tbl= li++))
2881
 
      {
2882
 
        if (tbl == outer_tbl)
2883
 
        {
2884
 
          li.replace(wrap_nest);
2885
 
          break;
2886
 
        }
2887
 
      }
2888
 
      /*
2889
 
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
2890
 
        semi-join nest into it
2891
 
      */
2892
 
      emb_join_list= &wrap_nest->nested_join->join_list;
2893
 
      emb_tbl_nest=  wrap_nest;
2894
 
    }
2895
 
  }
2896
 
 
2897
 
  TABLE_LIST *sj_nest;
2898
 
  NESTED_JOIN *nested_join;
2899
 
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2900
 
  {
2901
 
    return(true);
2902
 
  }
2903
 
  nested_join= sj_nest->nested_join;
2904
 
 
2905
 
  sj_nest->join_list= emb_join_list;
2906
 
  sj_nest->embedding= emb_tbl_nest;
2907
 
  sj_nest->alias= (char*) "(sj-nest)";
2908
 
  /* Nests do not participate in those 'chains', so: */
2909
 
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2910
 
  emb_join_list->push_back(sj_nest);
2911
 
 
2912
 
  /* 
2913
 
    nested_join->used_tables and nested_join->not_null_tables are
2914
 
    initialized in simplify_joins().
2915
 
  */
2916
 
  
2917
 
  /* 
2918
 
    2. Walk through subquery's top list and set 'embedding' to point to the
2919
 
       sj-nest.
2920
 
  */
2921
 
  st_select_lex *subq_lex= subq_pred->unit->first_select();
2922
 
  nested_join->join_list.empty();
2923
 
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2924
 
  TABLE_LIST *tl, *last_leaf;
2925
 
  while ((tl= li++))
2926
 
  {
2927
 
    tl->embedding= sj_nest;
2928
 
    tl->join_list= &nested_join->join_list;
2929
 
    nested_join->join_list.push_back(tl);
2930
 
  }
2931
 
  
2932
 
  /*
2933
 
    Reconnect the next_leaf chain.
2934
 
    TODO: Do we have to put subquery's tables at the end of the chain?
2935
 
          Inserting them at the beginning would be a bit faster.
2936
 
    NOTE: We actually insert them at the front! That's because the order is
2937
 
          reversed in this list.
2938
 
  */
2939
 
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2940
 
  tl->next_leaf= subq_lex->leaf_tables;
2941
 
  last_leaf= tl;
2942
 
 
2943
 
  /*
2944
 
    Same as above for next_local chain
2945
 
    (a theory: a next_local chain always starts with ::leaf_tables
2946
 
     because view's tables are inserted after the view)
2947
 
  */
2948
 
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2949
 
  tl->next_local= subq_lex->leaf_tables;
2950
 
 
2951
 
  /* A theory: no need to re-connect the next_global chain */
2952
 
 
2953
 
  /* 3. Remove the original subquery predicate from the WHERE/ON */
2954
 
 
2955
 
  // The subqueries were replaced for Item_int(1) earlier
2956
 
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2957
 
  /*TODO: also reset the 'with_subselect' there. */
2958
 
 
2959
 
  /* n. Adjust the parent_join->tables counter */
2960
 
  uint table_no= parent_join->tables;
2961
 
  /* n. Walk through child's tables and adjust table->map */
2962
 
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2963
 
  {
2964
 
    tl->table->tablenr= table_no;
2965
 
    tl->table->map= ((table_map)1) << table_no;
2966
 
    SELECT_LEX *old_sl= tl->select_lex;
2967
 
    tl->select_lex= parent_join->select_lex; 
2968
 
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2969
 
      emb->select_lex= parent_join->select_lex;
2970
 
  }
2971
 
  parent_join->tables += subq_lex->join->tables;
2972
 
 
2973
 
  /* 
2974
 
    Put the subquery's WHERE into semi-join's sj_on_expr
2975
 
    Add the subquery-induced equalities too.
2976
 
  */
2977
 
  SELECT_LEX *save_lex= thd->lex->current_select;
2978
 
  thd->lex->current_select=subq_lex;
2979
 
  if (!subq_pred->left_expr->fixed &&
2980
 
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2981
 
    return(true);
2982
 
  thd->lex->current_select=save_lex;
2983
 
 
2984
 
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2985
 
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
2986
 
                                        subq_pred->left_expr->used_tables();
2987
 
  sj_nest->sj_on_expr= subq_lex->where;
2988
 
 
2989
 
  /*
2990
 
    Create the IN-equalities and inject them into semi-join's ON expression.
2991
 
    Additionally, for InsideOut strategy
2992
 
     - Record the number of IN-equalities.
2993
 
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
2994
 
       see which of the expressions are bound and which are not (for those
2995
 
       we'll produce a distinct stream of (ie_i1,...ie_ik).
2996
 
 
2997
 
       (TODO: can we just create a list of pointers and hope the expressions
2998
 
       will not substitute themselves on fix_fields()? or we need to wrap
2999
 
       them into Item_direct_view_refs and store pointers to those. The
3000
 
       pointers to Item_direct_view_refs are guaranteed to be stable as 
3001
 
       Item_direct_view_refs doesn't substitute itself with anything in 
3002
 
       Item_direct_view_ref::fix_fields.
3003
 
  */
3004
 
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
3005
 
  sj_nest->nested_join->sj_outer_expr_list.empty();
3006
 
 
3007
 
  if (subq_pred->left_expr->cols() == 1)
3008
 
  {
3009
 
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3010
 
 
3011
 
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
3012
 
                                    subq_lex->ref_pointer_array[0]);
3013
 
    item_eq->name= (char*)subq_sj_cond_name;
3014
 
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3015
 
  }
3016
 
  else
3017
 
  {
3018
 
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3019
 
    {
3020
 
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3021
 
                                                element_index(i));
3022
 
      Item *item_eq= 
3023
 
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
3024
 
                         subq_lex->ref_pointer_array[i]);
3025
 
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3026
 
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3027
 
    }
3028
 
  }
3029
 
  /* Fix the created equality and AND */
3030
 
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3031
 
 
3032
 
  /*
3033
 
    Walk through sj nest's WHERE and ON expressions and call
3034
 
    item->fix_table_changes() for all items.
3035
 
  */
3036
 
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3037
 
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3038
 
 
3039
 
 
3040
 
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3041
 
  subq_lex->master_unit()->exclude_level();
3042
 
 
3043
 
  /* Inject sj_on_expr into the parent's WHERE or ON */
3044
 
  if (emb_tbl_nest)
3045
 
  {
3046
 
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
3047
 
                                     sj_nest->sj_on_expr);
3048
 
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3049
 
  }
3050
 
  else
3051
 
  {
3052
 
    /* Inject into the WHERE */
3053
 
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3054
 
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3055
 
    parent_join->select_lex->where= parent_join->conds;
3056
 
  }
3057
 
 
3058
 
  return(false);
3059
 
}
3060
 
 
3061
 
 
3062
 
/*
3063
 
  Convert candidate subquery predicates to semi-joins
3064
 
 
3065
 
  SYNOPSIS
3066
 
    JOIN::flatten_subqueries()
3067
 
 
3068
 
  DESCRIPTION
3069
 
    Convert candidate subquery predicates to semi-joins.
3070
 
 
3071
 
  RETURN 
3072
 
    false  OK
3073
 
    true   Error
3074
 
*/
3075
 
 
3076
 
bool JOIN::flatten_subqueries()
3077
 
{
3078
 
  Item_in_subselect **in_subq;
3079
 
  Item_in_subselect **in_subq_end;
3080
 
 
3081
 
  if (sj_subselects.elements() == 0)
3082
 
    return(false);
3083
 
 
3084
 
  /* 1. Fix children subqueries */
3085
 
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
3086
 
       in_subq != in_subq_end; in_subq++)
3087
 
  {
3088
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3089
 
    child_join->outer_tables = child_join->tables;
3090
 
    if (child_join->flatten_subqueries())
3091
 
      return(true);
3092
 
    (*in_subq)->sj_convert_priority= 
3093
 
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3094
 
  }
3095
 
 
3096
 
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3097
 
  /* 
3098
 
    2. Pick which subqueries to convert:
3099
 
      sort the subquery array
3100
 
      - prefer correlated subqueries over uncorrelated;
3101
 
      - prefer subqueries that have greater number of outer tables;
3102
 
  */
3103
 
  sj_subselects.sort(subq_sj_candidate_cmp);
3104
 
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3105
 
  /* Replace all subqueries to be flattened with Item_int(1) */
3106
 
  for (in_subq= sj_subselects.front(); 
3107
 
       in_subq != in_subq_end && 
3108
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3109
 
       in_subq++)
3110
 
  {
3111
 
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3112
 
      return(true);
3113
 
  }
3114
 
 
3115
 
  for (in_subq= sj_subselects.front(); 
3116
 
       in_subq != in_subq_end && 
3117
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3118
 
       in_subq++)
3119
 
  {
3120
 
    if (convert_subq_to_sj(this, *in_subq))
3121
 
      return(true);
3122
 
  }
3123
 
 
3124
 
  /* 3. Finalize those we didn't convert */
3125
 
  for (; in_subq!= in_subq_end; in_subq++)
3126
 
  {
3127
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3128
 
    Item_subselect::trans_res res;
3129
 
    (*in_subq)->changed= 0;
3130
 
    (*in_subq)->fixed= 0;
3131
 
    res= (*in_subq)->select_transformer(child_join);
3132
 
    if (res == Item_subselect::RES_ERROR)
3133
 
      return(true);
3134
 
 
3135
 
    (*in_subq)->changed= 1;
3136
 
    (*in_subq)->fixed= 1;
3137
 
 
3138
 
    Item *substitute= (*in_subq)->substitution;
3139
 
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3140
 
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3141
 
      return(true);
3142
 
 
3143
 
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3144
 
    //  return(true);
3145
 
  }
3146
 
  sj_subselects.clear();
3147
 
  return(false);
3148
 
}
3149
 
 
3150
 
 
3151
 
/**
3152
 
  Setup for execution all subqueries of a query, for which the optimizer
3153
 
  chose hash semi-join.
3154
 
 
3155
 
  @details Iterate over all subqueries of the query, and if they are under an
3156
 
  IN predicate, and the optimizer chose to compute it via hash semi-join:
3157
 
  - try to initialize all data structures needed for the materialized execution
3158
 
    of the IN predicate,
3159
 
  - if this fails, then perform the IN=>EXISTS transformation which was
3160
 
    previously blocked during JOIN::prepare.
3161
 
 
3162
 
  This method is part of the "code generation" query processing phase.
3163
 
 
3164
 
  This phase must be called after substitute_for_best_equal_field() because
3165
 
  that function may replace items with other items from a multiple equality,
3166
 
  and we need to reference the correct items in the index access method of the
3167
 
  IN predicate.
3168
 
 
3169
 
  @return Operation status
3170
 
  @retval false     success.
3171
 
  @retval true      error occurred.
3172
 
*/
3173
 
 
3174
 
bool JOIN::setup_subquery_materialization()
3175
 
{
3176
 
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3177
 
       un= un->next_unit())
3178
 
  {
3179
 
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3180
 
    {
3181
 
      Item_subselect *subquery_predicate= sl->master_unit()->item;
3182
 
      if (subquery_predicate &&
3183
 
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
3184
 
      {
3185
 
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3186
 
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3187
 
            in_subs->setup_engine())
3188
 
          return true;
3189
 
      }
3190
 
    }
3191
 
  }
3192
 
  return false;
3193
 
}
3194
 
 
3195
 
 
3196
 
/*
3197
 
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3198
 
 
3199
 
  SYNOPSIS
3200
 
    find_eq_ref_candidate()
3201
 
      table             Table to be checked
3202
 
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
3203
 
                        count.
3204
 
 
3205
 
  DESCRIPTION
3206
 
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3207
 
 
3208
 
  TODO
3209
 
    Check again if it is feasible to factor common parts with constant table
3210
 
    search
3211
 
 
3212
 
  RETURN
3213
 
    true  - There exists an eq_ref(outer-tables) candidate
3214
 
    false - Otherwise
3215
 
*/
3216
 
 
3217
 
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3218
 
{
3219
 
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3220
 
  uint key;
3221
 
 
3222
 
  if (keyuse)
3223
 
  {
3224
 
    while (1) /* For each key */
3225
 
    {
3226
 
      key= keyuse->key;
3227
 
      KEY *keyinfo= table->key_info + key;
3228
 
      key_part_map bound_parts= 0;
3229
 
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3230
 
      {
3231
 
        do  /* For all equalities on all key parts */
3232
 
        {
3233
 
          /* Check if this is "t.keypart = expr(outer_tables) */
3234
 
          if (!(keyuse->used_tables & sj_inner_tables) &&
3235
 
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3236
 
          {
3237
 
            bound_parts |= 1 << keyuse->keypart;
3238
 
          }
3239
 
          keyuse++;
3240
 
        } while (keyuse->key == key && keyuse->table == table);
3241
 
 
3242
 
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3243
 
          return true;
3244
 
        if (keyuse->table != table)
3245
 
          return false;
3246
 
      }
3247
 
      else
3248
 
      {
3249
 
        do
3250
 
        {
3251
 
          keyuse++;
3252
 
          if (keyuse->table != table)
3253
 
            return false;
3254
 
        }
3255
 
        while (keyuse->key == key);
3256
 
      }
3257
 
    }
3258
 
  }
3259
 
  return false;
3260
 
}
3261
 
 
3262
 
 
3263
 
/*
3264
 
  Pull tables out of semi-join nests, if possible
3265
 
 
3266
 
  SYNOPSIS
3267
 
    pull_out_semijoin_tables()
3268
 
      join  The join where to do the semi-join flattening
3269
 
 
3270
 
  DESCRIPTION
3271
 
    Try to pull tables out of semi-join nests.
3272
 
     
3273
 
    PRECONDITIONS
3274
 
    When this function is called, the join may have several semi-join nests
3275
 
    (possibly within different semi-join nests), but it is guaranteed that
3276
 
    one semi-join nest does not contain another.
3277
 
   
3278
 
    ACTION
3279
 
    A table can be pulled out of the semi-join nest if
3280
 
     - It is a constant table
3281
 
     - It is accessed 
3282
 
 
3283
 
    POSTCONDITIONS
3284
 
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3285
 
       tables)
3286
 
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3287
 
     * Semi-join nests TABLE_LIST::sj_inner_tables
3288
 
 
3289
 
    This operation is (and should be) performed at each PS execution since
3290
 
    tables may become/cease to be constant across PS reexecutions.
3291
 
 
3292
 
  RETURN 
3293
 
    0 - OK
3294
 
    1 - Out of memory error
3295
 
*/
3296
 
 
3297
 
int pull_out_semijoin_tables(JOIN *join)
3298
 
{
3299
 
  TABLE_LIST *sj_nest;
3300
 
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3301
 
   
3302
 
  /* Try pulling out of the each of the semi-joins */
3303
 
  while ((sj_nest= sj_list_it++))
3304
 
  {
3305
 
    /* Action #1: Mark the constant tables to be pulled out */
3306
 
    table_map pulled_tables= 0;
3307
 
     
3308
 
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3309
 
    TABLE_LIST *tbl;
3310
 
    while ((tbl= child_li++))
3311
 
    {
3312
 
      if (tbl->table)
3313
 
      {
3314
 
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3315
 
        if (tbl->table->map & join->const_table_map)
3316
 
        {
3317
 
          pulled_tables |= tbl->table->map;
3318
 
        }
3319
 
      }
3320
 
    }
3321
 
    
3322
 
    /*
3323
 
      Action #2: Find which tables we can pull out based on
3324
 
      update_ref_and_keys() data. Note that pulling one table out can allow
3325
 
      us to pull out some other tables too.
3326
 
    */
3327
 
    bool pulled_a_table;
3328
 
    do 
3329
 
    {
3330
 
      pulled_a_table= false;
3331
 
      child_li.rewind();
3332
 
      while ((tbl= child_li++))
3333
 
      {
3334
 
        if (tbl->table && !(pulled_tables & tbl->table->map))
3335
 
        {
3336
 
          if (find_eq_ref_candidate(tbl->table, 
3337
 
                                    sj_nest->nested_join->used_tables & 
3338
 
                                    ~pulled_tables))
3339
 
          {
3340
 
            pulled_a_table= true;
3341
 
            pulled_tables |= tbl->table->map;
3342
 
          }
3343
 
        }
3344
 
      }
3345
 
    } while (pulled_a_table);
3346
 
 
3347
 
    child_li.rewind();
3348
 
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3349
 
    {
3350
 
      (sj_nest)->sj_inner_tables= 0;
3351
 
      while ((tbl= child_li++))
3352
 
      {
3353
 
        if (tbl->table)
3354
 
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3355
 
      }
3356
 
    }
3357
 
    else
3358
 
    {
3359
 
      /* Record the bitmap of inner tables, mark the inner tables */
3360
 
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
3361
 
                             ~pulled_tables;
3362
 
      (sj_nest)->sj_inner_tables= inner_tables;
3363
 
      while ((tbl= child_li++))
3364
 
      {
3365
 
        if (tbl->table)
3366
 
        {
3367
 
          if (inner_tables & tbl->table->map)
3368
 
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3369
 
          else
3370
 
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3371
 
        }
3372
 
      }
3373
 
    }
3374
 
  }
3375
 
  return(0);
3376
 
}
3377
 
 
3378
462
/*****************************************************************************
3379
 
  Create JOIN_TABS, make a guess about the table types,
 
463
  Create JoinTableS, make a guess about the table types,
3380
464
  Approximate how many records will be used in each table
3381
465
*****************************************************************************/
3382
 
 
3383
 
 
3384
 
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3385
 
                                      TABLE *table,
3386
 
                                      const key_map *keys,ha_rows limit)
 
466
ha_rows get_quick_record_count(Session *session, optimizer::SqlSelect *select, Table *table, const key_map *keys,ha_rows limit)
3387
467
{
3388
468
  int error;
3389
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
 
469
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3390
470
    return(0);                           // Fatal error flag is set
3391
471
  if (select)
3392
472
  {
3393
473
    select->head=table;
3394
474
    table->reginfo.impossible_range=0;
3395
 
    if ((error= select->test_quick_select(thd, *(key_map *)keys,(table_map) 0,
 
475
    if ((error= select->test_quick_select(session, *(key_map *)keys,(table_map) 0,
3396
476
                                          limit, 0, false)) == 1)
3397
477
      return(select->quick->records);
3398
478
    if (error == -1)
3404
484
  return(HA_POS_ERROR);                 /* This shouldn't happend */
3405
485
}
3406
486
 
3407
 
/*
3408
 
   This structure is used to collect info on potentially sargable
3409
 
   predicates in order to check whether they become sargable after
3410
 
   reading const tables.
3411
 
   We form a bitmap of indexes that can be used for sargable predicates.
3412
 
   Only such indexes are involved in range analysis.
3413
 
*/
3414
 
typedef struct st_sargable_param
3415
 
{
3416
 
  Field *field;              /* field against which to check sargability */
3417
 
  Item **arg_value;          /* values of potential keys for lookups     */
3418
 
  uint num_values;           /* number of values in the above array      */
3419
 
} SARGABLE_PARAM;  
3420
 
 
3421
 
/**
3422
 
  Calculate the best possible join and initialize the join structure.
3423
 
 
3424
 
  @retval
3425
 
    0   ok
3426
 
  @retval
3427
 
    1   Fatal error
3428
 
*/
3429
 
 
3430
 
static bool
3431
 
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3432
 
                     DYNAMIC_ARRAY *keyuse_array)
3433
 
{
3434
 
  int error;
3435
 
  TABLE *table;
3436
 
  uint i,table_count,const_count,key;
3437
 
  table_map found_const_table_map, all_table_map, found_ref, refs;
3438
 
  key_map const_ref, eq_part;
3439
 
  TABLE **table_vector;
3440
 
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3441
 
  KEYUSE *keyuse,*start_keyuse;
3442
 
  table_map outer_join=0;
3443
 
  SARGABLE_PARAM *sargables= 0;
3444
 
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3445
 
 
3446
 
  table_count=join->tables;
3447
 
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3448
 
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3449
 
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3450
 
  if (!stat || !stat_ref || !table_vector)
3451
 
    return(1);                          // Eom /* purecov: inspected */
3452
 
 
3453
 
  join->best_ref=stat_vector;
3454
 
 
3455
 
  stat_end=stat+table_count;
3456
 
  found_const_table_map= all_table_map=0;
3457
 
  const_count=0;
3458
 
 
3459
 
  for (s= stat, i= 0;
3460
 
       tables;
3461
 
       s++, tables= tables->next_leaf, i++)
3462
 
  {
3463
 
    TABLE_LIST *embedding= tables->embedding;
3464
 
    stat_vector[i]=s;
3465
 
    s->keys.init();
3466
 
    s->const_keys.init();
3467
 
    s->checked_keys.init();
3468
 
    s->needed_reg.init();
3469
 
    table_vector[i]=s->table=table=tables->table;
3470
 
    table->pos_in_table_list= tables;
3471
 
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3472
 
    if(error)
3473
 
    {
3474
 
        table->file->print_error(error, MYF(0));
3475
 
        return(1);
3476
 
    }
3477
 
    table->quick_keys.clear_all();
3478
 
    table->reginfo.join_tab=s;
3479
 
    table->reginfo.not_exists_optimize=0;
3480
 
    bzero((char*) table->const_key_parts, sizeof(key_part_map)*table->s->keys);
3481
 
    all_table_map|= table->map;
3482
 
    s->join=join;
3483
 
    s->info=0;                                  // For describe
3484
 
 
3485
 
    s->dependent= tables->dep_tables;
3486
 
    s->key_dependent= 0;
3487
 
    if (tables->schema_table)
3488
 
      table->file->stats.records= 2;
3489
 
    table->quick_condition_rows= table->file->stats.records;
3490
 
 
3491
 
    s->on_expr_ref= &tables->on_expr;
3492
 
    if (*s->on_expr_ref)
3493
 
    {
3494
 
      /* s is the only inner table of an outer join */
3495
 
      if (!table->file->stats.records && !embedding)
3496
 
      {                                         // Empty table
3497
 
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
3498
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3499
 
        continue;
3500
 
      }
3501
 
      outer_join|= table->map;
3502
 
      s->embedding_map= 0;
3503
 
      for (;embedding; embedding= embedding->embedding)
3504
 
        s->embedding_map|= embedding->nested_join->nj_map;
3505
 
      continue;
3506
 
    }
3507
 
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3508
 
    {
3509
 
      /* s belongs to a nested join, maybe to several embedded joins */
3510
 
      s->embedding_map= 0;
3511
 
      do
3512
 
      {
3513
 
        NESTED_JOIN *nested_join= embedding->nested_join;
3514
 
        s->embedding_map|=nested_join->nj_map;
3515
 
        s->dependent|= embedding->dep_tables;
3516
 
        embedding= embedding->embedding;
3517
 
        outer_join|= nested_join->used_tables;
3518
 
      }
3519
 
      while (embedding);
3520
 
      continue;
3521
 
    }
3522
 
    if ((table->s->system || table->file->stats.records <= 1) &&
3523
 
        !s->dependent &&
3524
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3525
 
    {
3526
 
      set_position(join,const_count++,s,(KEYUSE*) 0);
3527
 
    }
3528
 
  }
3529
 
  stat_vector[i]=0;
3530
 
  join->outer_join=outer_join;
3531
 
 
3532
 
  if (join->outer_join)
3533
 
  {
3534
 
    /* 
3535
 
       Build transitive closure for relation 'to be dependent on'.
3536
 
       This will speed up the plan search for many cases with outer joins,
3537
 
       as well as allow us to catch illegal cross references/
3538
 
       Warshall's algorithm is used to build the transitive closure.
3539
 
       As we use bitmaps to represent the relation the complexity
3540
 
       of the algorithm is O((number of tables)^2). 
3541
 
    */
3542
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3543
 
    {
3544
 
      for (uint j= 0 ; j < table_count ; j++)
3545
 
      {
3546
 
        table= stat[j].table;
3547
 
        if (s->dependent & table->map)
3548
 
          s->dependent |= table->reginfo.join_tab->dependent;
3549
 
      }
3550
 
      if (s->dependent)
3551
 
        s->table->maybe_null= 1;
3552
 
    }
3553
 
    /* Catch illegal cross references for outer joins */
3554
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3555
 
    {
3556
 
      if (s->dependent & s->table->map)
3557
 
      {
3558
 
        join->tables=0;                 // Don't use join->table
3559
 
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3560
 
        return(1);
3561
 
      }
3562
 
      s->key_dependent= s->dependent;
3563
 
    }
3564
 
  }
3565
 
 
3566
 
  if (conds || outer_join)
3567
 
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3568
 
                            conds, join->cond_equal,
3569
 
                            ~outer_join, join->select_lex, &sargables))
3570
 
      return(1);
3571
 
 
3572
 
  /* Read tables with 0 or 1 rows (system tables) */
3573
 
  join->const_table_map= 0;
3574
 
 
3575
 
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3576
 
       p_pos < p_end ;
3577
 
       p_pos++)
3578
 
  {
3579
 
    int tmp;
3580
 
    s= p_pos->table;
3581
 
    s->type=JT_SYSTEM;
3582
 
    join->const_table_map|=s->table->map;
3583
 
    if ((tmp=join_read_const_table(s, p_pos)))
3584
 
    {
3585
 
      if (tmp > 0)
3586
 
        return(1);                      // Fatal error
3587
 
    }
3588
 
    else
3589
 
      found_const_table_map|= s->table->map;
3590
 
  }
3591
 
 
3592
 
  /* loop until no more const tables are found */
3593
 
  int ref_changed;
3594
 
  do
3595
 
  {
3596
 
  more_const_tables_found:
3597
 
    ref_changed = 0;
3598
 
    found_ref=0;
3599
 
 
3600
 
    /*
3601
 
      We only have to loop from stat_vector + const_count as
3602
 
      set_position() will move all const_tables first in stat_vector
3603
 
    */
3604
 
 
3605
 
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3606
 
    {
3607
 
      table=s->table;
3608
 
 
3609
 
      /* 
3610
 
        If equi-join condition by a key is null rejecting and after a
3611
 
        substitution of a const table the key value happens to be null
3612
 
        then we can state that there are no matches for this equi-join.
3613
 
      */  
3614
 
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3615
 
      {
3616
 
        /* 
3617
 
          When performing an outer join operation if there are no matching rows
3618
 
          for the single row of the outer table all the inner tables are to be
3619
 
          null complemented and thus considered as constant tables.
3620
 
          Here we apply this consideration to the case of outer join operations 
3621
 
          with a single inner table only because the case with nested tables
3622
 
          would require a more thorough analysis.
3623
 
          TODO. Apply single row substitution to null complemented inner tables
3624
 
          for nested outer join operations. 
3625
 
        */              
3626
 
        while (keyuse->table == table)
3627
 
        {
3628
 
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3629
 
              keyuse->val->is_null() && keyuse->null_rejecting)
3630
 
          {
3631
 
            s->type= JT_CONST;
3632
 
            mark_as_null_row(table);
3633
 
            found_const_table_map|= table->map;
3634
 
            join->const_table_map|= table->map;
3635
 
            set_position(join,const_count++,s,(KEYUSE*) 0);
3636
 
            goto more_const_tables_found;
3637
 
           }
3638
 
          keyuse++;
3639
 
        }
3640
 
      }
3641
 
 
3642
 
      if (s->dependent)                         // If dependent on some table
3643
 
      {
3644
 
        // All dep. must be constants
3645
 
        if (s->dependent & ~(found_const_table_map))
3646
 
          continue;
3647
 
        if (table->file->stats.records <= 1L &&
3648
 
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3649
 
            !table->pos_in_table_list->embedding)
3650
 
        {                                       // system table
3651
 
          int tmp= 0;
3652
 
          s->type=JT_SYSTEM;
3653
 
          join->const_table_map|=table->map;
3654
 
          set_position(join,const_count++,s,(KEYUSE*) 0);
3655
 
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3656
 
          {
3657
 
            if (tmp > 0)
3658
 
              return(1);                        // Fatal error
3659
 
          }
3660
 
          else
3661
 
            found_const_table_map|= table->map;
3662
 
          continue;
3663
 
        }
3664
 
      }
3665
 
      /* check if table can be read by key or table only uses const refs */
3666
 
      if ((keyuse=s->keyuse))
3667
 
      {
3668
 
        s->type= JT_REF;
3669
 
        while (keyuse->table == table)
3670
 
        {
3671
 
          start_keyuse=keyuse;
3672
 
          key=keyuse->key;
3673
 
          s->keys.set_bit(key);               // QQ: remove this ?
3674
 
 
3675
 
          refs=0;
3676
 
          const_ref.clear_all();
3677
 
          eq_part.clear_all();
3678
 
          do
3679
 
          {
3680
 
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3681
 
            {
3682
 
              if (!((~found_const_table_map) & keyuse->used_tables))
3683
 
                const_ref.set_bit(keyuse->keypart);
3684
 
              else
3685
 
                refs|=keyuse->used_tables;
3686
 
              eq_part.set_bit(keyuse->keypart);
3687
 
            }
3688
 
            keyuse++;
3689
 
          } while (keyuse->table == table && keyuse->key == key);
3690
 
 
3691
 
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3692
 
              !table->pos_in_table_list->embedding)
3693
 
          {
3694
 
            if ((table->key_info[key].flags & (HA_NOSAME))
3695
 
                 == HA_NOSAME)
3696
 
            {
3697
 
              if (const_ref == eq_part)
3698
 
              {                                 // Found everything for ref.
3699
 
                int tmp;
3700
 
                ref_changed = 1;
3701
 
                s->type= JT_CONST;
3702
 
                join->const_table_map|=table->map;
3703
 
                set_position(join,const_count++,s,start_keyuse);
3704
 
                if (create_ref_for_key(join, s, start_keyuse,
3705
 
                                       found_const_table_map))
3706
 
                  return(1);
3707
 
                if ((tmp=join_read_const_table(s,
3708
 
                                               join->positions+const_count-1)))
3709
 
                {
3710
 
                  if (tmp > 0)
3711
 
                    return(1);                  // Fatal error
3712
 
                }
3713
 
                else
3714
 
                  found_const_table_map|= table->map;
3715
 
                break;
3716
 
              }
3717
 
              else
3718
 
                found_ref|= refs;      // Table is const if all refs are const
3719
 
            }
3720
 
            else if (const_ref == eq_part)
3721
 
              s->const_keys.set_bit(key);
3722
 
          }
3723
 
        }
3724
 
      }
3725
 
    }
3726
 
  } while (join->const_table_map & found_ref && ref_changed);
3727
 
 
3728
 
  /* 
3729
 
    Update info on indexes that can be used for search lookups as
3730
 
    reading const tables may has added new sargable predicates. 
3731
 
  */
3732
 
  if (const_count && sargables)
3733
 
  {
3734
 
    for( ; sargables->field ; sargables++)
3735
 
    {
3736
 
      Field *field= sargables->field;
3737
 
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3738
 
      key_map possible_keys= field->key_start;
3739
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
3740
 
      bool is_const= 1;
3741
 
      for (uint j=0; j < sargables->num_values; j++)
3742
 
        is_const&= sargables->arg_value[j]->const_item();
3743
 
      if (is_const)
3744
 
        join_tab[0].const_keys.merge(possible_keys);
3745
 
    }
3746
 
  }
3747
 
 
3748
 
  if (pull_out_semijoin_tables(join))
3749
 
    return(true);
3750
 
 
3751
 
  /* Calc how many (possible) matched records in each table */
3752
 
 
3753
 
  for (s=stat ; s < stat_end ; s++)
3754
 
  {
3755
 
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
3756
 
    {
3757
 
      /* Only one matching row */
3758
 
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3759
 
      continue;
3760
 
    }
3761
 
    /* Approximate found rows and time to read them */
3762
 
    s->found_records=s->records=s->table->file->stats.records;
3763
 
    s->read_time=(ha_rows) s->table->file->scan_time();
3764
 
 
3765
 
    /*
3766
 
      Set a max range of how many seeks we can expect when using keys
3767
 
      This is can't be to high as otherwise we are likely to use
3768
 
      table scan.
3769
 
    */
3770
 
    s->worst_seeks= min((double) s->found_records / 10,
3771
 
                        (double) s->read_time*3);
3772
 
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3773
 
      s->worst_seeks=2.0;
3774
 
 
3775
 
    /*
3776
 
      Add to stat->const_keys those indexes for which all group fields or
3777
 
      all select distinct fields participate in one index.
3778
 
    */
3779
 
    add_group_and_distinct_keys(join, s);
3780
 
 
3781
 
    if (!s->const_keys.is_clear_all() &&
3782
 
        !s->table->pos_in_table_list->embedding)
3783
 
    {
3784
 
      ha_rows records;
3785
 
      SQL_SELECT *select;
3786
 
      select= make_select(s->table, found_const_table_map,
3787
 
                          found_const_table_map,
3788
 
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3789
 
                          1, &error);
3790
 
      if (!select)
3791
 
        return(1);
3792
 
      records= get_quick_record_count(join->thd, select, s->table,
3793
 
                                      &s->const_keys, join->row_limit);
3794
 
      s->quick=select->quick;
3795
 
      s->needed_reg=select->needed_reg;
3796
 
      select->quick=0;
3797
 
      if (records == 0 && s->table->reginfo.impossible_range)
3798
 
      {
3799
 
        /*
3800
 
          Impossible WHERE or ON expression
3801
 
          In case of ON, we mark that the we match one empty NULL row.
3802
 
          In case of WHERE, don't set found_const_table_map to get the
3803
 
          caller to abort with a zero row result.
3804
 
        */
3805
 
        join->const_table_map|= s->table->map;
3806
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3807
 
        s->type= JT_CONST;
3808
 
        if (*s->on_expr_ref)
3809
 
        {
3810
 
          /* Generate empty row */
3811
 
          s->info= "Impossible ON condition";
3812
 
          found_const_table_map|= s->table->map;
3813
 
          s->type= JT_CONST;
3814
 
          mark_as_null_row(s->table);           // All fields are NULL
3815
 
        }
3816
 
      }
3817
 
      if (records != HA_POS_ERROR)
3818
 
      {
3819
 
        s->found_records=records;
3820
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3821
 
      }
3822
 
      delete select;
3823
 
    }
3824
 
  }
3825
 
 
3826
 
  join->join_tab=stat;
3827
 
  join->map2table=stat_ref;
3828
 
  join->table= join->all_tables=table_vector;
3829
 
  join->const_tables=const_count;
3830
 
  join->found_const_table_map=found_const_table_map;
3831
 
 
3832
 
  /* Find an optimal join order of the non-constant tables. */
3833
 
  if (join->const_tables != join->tables)
3834
 
  {
3835
 
    optimize_keyuse(join, keyuse_array);
3836
 
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3837
 
      return(true);
3838
 
  }
3839
 
  else
3840
 
  {
3841
 
    memcpy((uchar*) join->best_positions,(uchar*) join->positions,
3842
 
           sizeof(POSITION)*join->const_tables);
3843
 
    join->best_read=1.0;
3844
 
  }
3845
 
  /* Generate an execution plan from the found optimal join order. */
3846
 
  return(join->thd->killed || get_best_combination(join));
3847
 
}
3848
 
 
3849
 
 
3850
487
/*****************************************************************************
3851
488
  Check with keys are used and with tables references with tables
3852
489
  Updates in stat:
3855
492
          keyuse     Pointer to possible keys
3856
493
*****************************************************************************/
3857
494
 
3858
 
/// Used when finding key fields
3859
 
typedef struct key_field_t {
3860
 
  Field         *field;
3861
 
  Item          *val;                   ///< May be empty if diff constant
3862
 
  uint          level;
3863
 
  uint          optimize; // KEY_OPTIMIZE_*
3864
 
  bool          eq_func;
3865
 
  /**
3866
 
    If true, the condition this struct represents will not be satisfied
3867
 
    when val IS NULL.
3868
 
  */
3869
 
  bool          null_rejecting; 
3870
 
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3871
 
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3872
 
} KEY_FIELD;
3873
 
 
3874
 
/**
3875
 
  Merge new key definitions to old ones, remove those not used in both.
3876
 
 
3877
 
  This is called for OR between different levels.
3878
 
 
3879
 
  To be able to do 'ref_or_null' we merge a comparison of a column
3880
 
  and 'column IS NULL' to one test.  This is useful for sub select queries
3881
 
  that are internally transformed to something like:.
3882
 
 
3883
 
  @code
3884
 
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
3885
 
  @endcode
3886
 
 
3887
 
  KEY_FIELD::null_rejecting is processed as follows: @n
3888
 
  result has null_rejecting=true if it is set for both ORed references.
3889
 
  for example:
3890
 
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
3891
 
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3892
 
 
3893
 
  @todo
3894
 
    The result of this is that we're missing some 'ref' accesses.
3895
 
    OptimizerTeam: Fix this
3896
 
*/
3897
 
 
3898
 
static KEY_FIELD *
3899
 
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3900
 
                 uint and_level)
3901
 
{
3902
 
  if (start == new_fields)
3903
 
    return start;                               // Impossible or
3904
 
  if (new_fields == end)
3905
 
    return start;                               // No new fields, skip all
3906
 
 
3907
 
  KEY_FIELD *first_free=new_fields;
3908
 
 
3909
 
  /* Mark all found fields in old array */
3910
 
  for (; new_fields != end ; new_fields++)
3911
 
  {
3912
 
    for (KEY_FIELD *old=start ; old != first_free ; old++)
3913
 
    {
3914
 
      if (old->field == new_fields->field)
3915
 
      {
3916
 
        /*
3917
 
          NOTE: below const_item() call really works as "!used_tables()", i.e.
3918
 
          it can return false where it is feasible to make it return true.
3919
 
          
3920
 
          The cause is as follows: Some of the tables are already known to be
3921
 
          const tables (the detection code is in make_join_statistics(),
3922
 
          above the update_ref_and_keys() call), but we didn't propagate 
3923
 
          information about this: TABLE::const_table is not set to true, and
3924
 
          Item::update_used_tables() hasn't been called for each item.
3925
 
          The result of this is that we're missing some 'ref' accesses.
3926
 
          TODO: OptimizerTeam: Fix this
3927
 
        */
3928
 
        if (!new_fields->val->const_item())
3929
 
        {
3930
 
          /*
3931
 
            If the value matches, we can use the key reference.
3932
 
            If not, we keep it until we have examined all new values
3933
 
          */
3934
 
          if (old->val->eq(new_fields->val, old->field->binary()))
3935
 
          {
3936
 
            old->level= and_level;
3937
 
            old->optimize= ((old->optimize & new_fields->optimize &
3938
 
                             KEY_OPTIMIZE_EXISTS) |
3939
 
                            ((old->optimize | new_fields->optimize) &
3940
 
                             KEY_OPTIMIZE_REF_OR_NULL));
3941
 
            old->null_rejecting= (old->null_rejecting &&
3942
 
                                  new_fields->null_rejecting);
3943
 
          }
3944
 
        }
3945
 
        else if (old->eq_func && new_fields->eq_func &&
3946
 
                 old->val->eq_by_collation(new_fields->val, 
3947
 
                                           old->field->binary(),
3948
 
                                           old->field->charset()))
3949
 
 
3950
 
        {
3951
 
          old->level= and_level;
3952
 
          old->optimize= ((old->optimize & new_fields->optimize &
3953
 
                           KEY_OPTIMIZE_EXISTS) |
3954
 
                          ((old->optimize | new_fields->optimize) &
3955
 
                           KEY_OPTIMIZE_REF_OR_NULL));
3956
 
          old->null_rejecting= (old->null_rejecting &&
3957
 
                                new_fields->null_rejecting);
3958
 
        }
3959
 
        else if (old->eq_func && new_fields->eq_func &&
3960
 
                 ((old->val->const_item() && old->val->is_null()) || 
3961
 
                  new_fields->val->is_null()))
3962
 
        {
3963
 
          /* field = expression OR field IS NULL */
3964
 
          old->level= and_level;
3965
 
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3966
 
          /*
3967
 
            Remember the NOT NULL value unless the value does not depend
3968
 
            on other tables.
3969
 
          */
3970
 
          if (!old->val->used_tables() && old->val->is_null())
3971
 
            old->val= new_fields->val;
3972
 
          /* The referred expression can be NULL: */ 
3973
 
          old->null_rejecting= 0;
3974
 
        }
3975
 
        else
3976
 
        {
3977
 
          /*
3978
 
            We are comparing two different const.  In this case we can't
3979
 
            use a key-lookup on this so it's better to remove the value
3980
 
            and let the range optimzier handle it
3981
 
          */
3982
 
          if (old == --first_free)              // If last item
3983
 
            break;
3984
 
          *old= *first_free;                    // Remove old value
3985
 
          old--;                                // Retry this value
3986
 
        }
3987
 
      }
3988
 
    }
3989
 
  }
3990
 
  /* Remove all not used items */
3991
 
  for (KEY_FIELD *old=start ; old != first_free ;)
3992
 
  {
3993
 
    if (old->level != and_level)
3994
 
    {                                           // Not used in all levels
3995
 
      if (old == --first_free)
3996
 
        break;
3997
 
      *old= *first_free;                        // Remove old value
3998
 
      continue;
3999
 
    }
4000
 
    old++;
4001
 
  }
4002
 
  return first_free;
4003
 
}
4004
 
 
4005
 
 
4006
 
/**
4007
 
  Add a possible key to array of possible keys if it's usable as a key
4008
 
 
4009
 
    @param key_fields      Pointer to add key, if usable
4010
 
    @param and_level       And level, to be stored in KEY_FIELD
4011
 
    @param cond            Condition predicate
4012
 
    @param field           Field used in comparision
4013
 
    @param eq_func         True if we used =, <=> or IS NULL
4014
 
    @param value           Value used for comparison with field
4015
 
    @param usable_tables   Tables which can be used for key optimization
4016
 
    @param sargables       IN/OUT Array of found sargable candidates
4017
 
 
4018
 
  @note
4019
 
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4020
 
    table, we store this to be able to do not exists optimization later.
4021
 
 
4022
 
  @returns
4023
 
    *key_fields is incremented if we stored a key in the array
4024
 
*/
4025
 
 
4026
 
static void
4027
 
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4028
 
              Field *field, bool eq_func, Item **value, uint num_values,
4029
 
              table_map usable_tables, SARGABLE_PARAM **sargables)
4030
 
{
4031
 
  uint exists_optimize= 0;
4032
 
  if (!(field->flags & PART_KEY_FLAG))
4033
 
  {
4034
 
    // Don't remove column IS NULL on a LEFT JOIN table
4035
 
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4036
 
        !field->table->maybe_null || field->null_ptr)
4037
 
      return;                                   // Not a key. Skip it
4038
 
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4039
 
    assert(num_values == 1);
4040
 
  }
4041
 
  else
4042
 
  {
4043
 
    table_map used_tables=0;
4044
 
    bool optimizable=0;
4045
 
    for (uint i=0; i<num_values; i++)
4046
 
    {
4047
 
      used_tables|=(value[i])->used_tables();
4048
 
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4049
 
        optimizable=1;
4050
 
    }
4051
 
    if (!optimizable)
4052
 
      return;
4053
 
    if (!(usable_tables & field->table->map))
4054
 
    {
4055
 
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4056
 
          !field->table->maybe_null || field->null_ptr)
4057
 
        return;                                 // Can't use left join optimize
4058
 
      exists_optimize= KEY_OPTIMIZE_EXISTS;
4059
 
    }
4060
 
    else
4061
 
    {
4062
 
      JOIN_TAB *stat=field->table->reginfo.join_tab;
4063
 
      key_map possible_keys=field->key_start;
4064
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
4065
 
      stat[0].keys.merge(possible_keys);             // Add possible keys
4066
 
 
4067
 
      /*
4068
 
        Save the following cases:
4069
 
        Field op constant
4070
 
        Field LIKE constant where constant doesn't start with a wildcard
4071
 
        Field = field2 where field2 is in a different table
4072
 
        Field op formula
4073
 
        Field IS NULL
4074
 
        Field IS NOT NULL
4075
 
         Field BETWEEN ...
4076
 
         Field IN ...
4077
 
      */
4078
 
      stat[0].key_dependent|=used_tables;
4079
 
 
4080
 
      bool is_const=1;
4081
 
      for (uint i=0; i<num_values; i++)
4082
 
      {
4083
 
        if (!(is_const&= value[i]->const_item()))
4084
 
          break;
4085
 
      }
4086
 
      if (is_const)
4087
 
        stat[0].const_keys.merge(possible_keys);
4088
 
      else if (!eq_func)
4089
 
      {
4090
 
        /* 
4091
 
          Save info to be able check whether this predicate can be 
4092
 
          considered as sargable for range analisis after reading const tables.
4093
 
          We do not save info about equalities as update_const_equal_items
4094
 
          will take care of updating info on keys from sargable equalities. 
4095
 
        */
4096
 
        (*sargables)--;
4097
 
        (*sargables)->field= field;
4098
 
        (*sargables)->arg_value= value;
4099
 
        (*sargables)->num_values= num_values;
4100
 
      }
4101
 
      /*
4102
 
        We can't always use indexes when comparing a string index to a
4103
 
        number. cmp_type() is checked to allow compare of dates to numbers.
4104
 
        eq_func is NEVER true when num_values > 1
4105
 
       */
4106
 
      if (!eq_func)
4107
 
      {
4108
 
        /* 
4109
 
          Additional optimization: if we're processing
4110
 
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
4111
 
          "t.key = c1".
4112
 
          TODO: This is a very limited fix. A more generic fix is possible. 
4113
 
          There are 2 options:
4114
 
          A) Make equality propagation code be able to handle BETWEEN
4115
 
             (including cases like t1.key BETWEEN t2.key AND t3.key)
4116
 
          B) Make range optimizer to infer additional "t.key = c" equalities
4117
 
             and use them in equality propagation process (see details in
4118
 
             OptimizerKBAndTodo)
4119
 
        */
4120
 
        if ((cond->functype() != Item_func::BETWEEN) ||
4121
 
            ((Item_func_between*) cond)->negated ||
4122
 
            !value[0]->eq(value[1], field->binary()))
4123
 
          return;
4124
 
        eq_func= true;
4125
 
      }
4126
 
 
4127
 
      if (field->result_type() == STRING_RESULT)
4128
 
      {
4129
 
        if ((*value)->result_type() != STRING_RESULT)
4130
 
        {
4131
 
          if (field->cmp_type() != (*value)->result_type())
4132
 
            return;
4133
 
        }
4134
 
        else
4135
 
        {
4136
 
          /*
4137
 
            We can't use indexes if the effective collation
4138
 
            of the operation differ from the field collation.
4139
 
          */
4140
 
          if (field->cmp_type() == STRING_RESULT &&
4141
 
              ((Field_str*)field)->charset() != cond->compare_collation())
4142
 
            return;
4143
 
        }
4144
 
      }
4145
 
    }
4146
 
  }
4147
 
  /*
4148
 
    For the moment eq_func is always true. This slot is reserved for future
4149
 
    extensions where we want to remembers other things than just eq comparisons
4150
 
  */
4151
 
  assert(eq_func);
4152
 
  /* Store possible eq field */
4153
 
  (*key_fields)->field=         field;
4154
 
  (*key_fields)->eq_func=       eq_func;
4155
 
  (*key_fields)->val=           *value;
4156
 
  (*key_fields)->level=         and_level;
4157
 
  (*key_fields)->optimize=      exists_optimize;
4158
 
  /*
4159
 
    If the condition has form "tbl.keypart = othertbl.field" and 
4160
 
    othertbl.field can be NULL, there will be no matches if othertbl.field 
4161
 
    has NULL value.
4162
 
    We use null_rejecting in add_not_null_conds() to add
4163
 
    'othertbl.field IS NOT NULL' to tab->select_cond.
4164
 
  */
4165
 
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4166
 
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4167
 
                                  ((*value)->type() == Item::FIELD_ITEM) &&
4168
 
                                  ((Item_field*)*value)->field->maybe_null());
4169
 
  (*key_fields)->cond_guard= NULL;
4170
 
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
4171
 
                              cond->name < subq_sj_cond_name + 64)? 
4172
 
                              cond->name - subq_sj_cond_name: UINT_MAX;
4173
 
  (*key_fields)++;
4174
 
}
4175
 
 
4176
 
/**
4177
 
  Add possible keys to array of possible keys originated from a simple
4178
 
  predicate.
4179
 
 
4180
 
    @param  key_fields     Pointer to add key, if usable
4181
 
    @param  and_level      And level, to be stored in KEY_FIELD
4182
 
    @param  cond           Condition predicate
4183
 
    @param  field          Field used in comparision
4184
 
    @param  eq_func        True if we used =, <=> or IS NULL
4185
 
    @param  value          Value used for comparison with field
4186
 
                           Is NULL for BETWEEN and IN    
4187
 
    @param  usable_tables  Tables which can be used for key optimization
4188
 
    @param  sargables      IN/OUT Array of found sargable candidates
4189
 
 
4190
 
  @note
4191
 
    If field items f1 and f2 belong to the same multiple equality and
4192
 
    a key is added for f1, the the same key is added for f2.
4193
 
 
4194
 
  @returns
4195
 
    *key_fields is incremented if we stored a key in the array
4196
 
*/
4197
 
 
4198
 
static void
4199
 
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4200
 
                     Item_func *cond, Item_field *field_item,
4201
 
                     bool eq_func, Item **val,
4202
 
                     uint num_values, table_map usable_tables,
4203
 
                     SARGABLE_PARAM **sargables)
4204
 
{
4205
 
  Field *field= field_item->field;
4206
 
  add_key_field(key_fields, and_level, cond, field,
4207
 
                eq_func, val, num_values, usable_tables, sargables);
4208
 
  Item_equal *item_equal= field_item->item_equal;
4209
 
  if (item_equal)
4210
 
  { 
4211
 
    /*
4212
 
      Add to the set of possible key values every substitution of
4213
 
      the field for an equal field included into item_equal
4214
 
    */
4215
 
    Item_equal_iterator it(*item_equal);
4216
 
    Item_field *item;
4217
 
    while ((item= it++))
4218
 
    {
4219
 
      if (!field->eq(item->field))
4220
 
      {
4221
 
        add_key_field(key_fields, and_level, cond, item->field,
4222
 
                      eq_func, val, num_values, usable_tables,
4223
 
                      sargables);
4224
 
      }
4225
 
    }
4226
 
  }
4227
 
}
4228
 
 
4229
 
static void
4230
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4231
 
               COND *cond, table_map usable_tables,
4232
 
               SARGABLE_PARAM **sargables)
4233
 
{
4234
 
  if (cond->type() == Item_func::COND_ITEM)
4235
 
  {
4236
 
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4237
 
    KEY_FIELD *org_key_fields= *key_fields;
4238
 
 
4239
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4240
 
    {
4241
 
      Item *item;
4242
 
      while ((item=li++))
4243
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4244
 
                       sargables);
4245
 
      for (; org_key_fields != *key_fields ; org_key_fields++)
4246
 
        org_key_fields->level= *and_level;
4247
 
    }
4248
 
    else
4249
 
    {
4250
 
      (*and_level)++;
4251
 
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
4252
 
                     sargables);
4253
 
      Item *item;
4254
 
      while ((item=li++))
4255
 
      {
4256
 
        KEY_FIELD *start_key_fields= *key_fields;
4257
 
        (*and_level)++;
4258
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4259
 
                       sargables);
4260
 
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
4261
 
                                     *key_fields,++(*and_level));
4262
 
      }
4263
 
    }
4264
 
    return;
4265
 
  }
4266
 
 
4267
 
  /* 
4268
 
    Subquery optimization: Conditions that are pushed down into subqueries
4269
 
    are wrapped into Item_func_trig_cond. We process the wrapped condition
4270
 
    but need to set cond_guard for KEYUSE elements generated from it.
4271
 
  */
4272
 
  {
4273
 
    if (cond->type() == Item::FUNC_ITEM &&
4274
 
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4275
 
    {
4276
 
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4277
 
      if (!join->group_list && !join->order &&
4278
 
          join->unit->item && 
4279
 
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
4280
 
          !join->unit->is_union())
4281
 
      {
4282
 
        KEY_FIELD *save= *key_fields;
4283
 
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4284
 
                       sargables);
4285
 
        // Indicate that this ref access candidate is for subquery lookup:
4286
 
        for (; save != *key_fields; save++)
4287
 
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4288
 
      }
4289
 
      return;
4290
 
    }
4291
 
  }
4292
 
 
4293
 
  /* If item is of type 'field op field/constant' add it to key_fields */
4294
 
  if (cond->type() != Item::FUNC_ITEM)
4295
 
    return;
4296
 
  Item_func *cond_func= (Item_func*) cond;
4297
 
  switch (cond_func->select_optimize()) {
4298
 
  case Item_func::OPTIMIZE_NONE:
4299
 
    break;
4300
 
  case Item_func::OPTIMIZE_KEY:
4301
 
  {
4302
 
    Item **values;
4303
 
    // BETWEEN, IN, NE
4304
 
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4305
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4306
 
    {
4307
 
      values= cond_func->arguments()+1;
4308
 
      if (cond_func->functype() == Item_func::NE_FUNC &&
4309
 
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4310
 
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4311
 
        values--;
4312
 
      assert(cond_func->functype() != Item_func::IN_FUNC ||
4313
 
                  cond_func->argument_count() != 2);
4314
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4315
 
                           (Item_field*) (cond_func->key_item()->real_item()),
4316
 
                           0, values, 
4317
 
                           cond_func->argument_count()-1,
4318
 
                           usable_tables, sargables);
4319
 
    }
4320
 
    if (cond_func->functype() == Item_func::BETWEEN)
4321
 
    {
4322
 
      values= cond_func->arguments();
4323
 
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4324
 
      {
4325
 
        Item_field *field_item;
4326
 
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4327
 
            &&
4328
 
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4329
 
        {
4330
 
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4331
 
          add_key_equal_fields(key_fields, *and_level, cond_func,
4332
 
                               field_item, 0, values, 1, usable_tables, 
4333
 
                               sargables);
4334
 
        }
4335
 
      }  
4336
 
    }
4337
 
    break;
4338
 
  }
4339
 
  case Item_func::OPTIMIZE_OP:
4340
 
  {
4341
 
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4342
 
                     cond_func->functype() == Item_func::EQUAL_FUNC);
4343
 
 
4344
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4345
 
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4346
 
    {
4347
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4348
 
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
4349
 
                           equal_func,
4350
 
                           cond_func->arguments()+1, 1, usable_tables,
4351
 
                           sargables);
4352
 
    }
4353
 
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4354
 
        cond_func->functype() != Item_func::LIKE_FUNC &&
4355
 
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4356
 
    {
4357
 
      add_key_equal_fields(key_fields, *and_level, cond_func, 
4358
 
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
4359
 
                           equal_func,
4360
 
                           cond_func->arguments(),1,usable_tables,
4361
 
                           sargables);
4362
 
    }
4363
 
    break;
4364
 
  }
4365
 
  case Item_func::OPTIMIZE_NULL:
4366
 
    /* column_name IS [NOT] NULL */
4367
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4368
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4369
 
    {
4370
 
      Item *tmp=new Item_null;
4371
 
      if (unlikely(!tmp))                       // Should never be true
4372
 
        return;
4373
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4374
 
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
4375
 
                    cond_func->functype() == Item_func::ISNULL_FUNC,
4376
 
                           &tmp, 1, usable_tables, sargables);
4377
 
    }
4378
 
    break;
4379
 
  case Item_func::OPTIMIZE_EQUAL:
4380
 
    Item_equal *item_equal= (Item_equal *) cond;
4381
 
    Item *const_item= item_equal->get_const();
4382
 
    Item_equal_iterator it(*item_equal);
4383
 
    Item_field *item;
4384
 
    if (const_item)
4385
 
    {
4386
 
      /*
4387
 
        For each field field1 from item_equal consider the equality 
4388
 
        field1=const_item as a condition allowing an index access of the table
4389
 
        with field1 by the keys value of field1.
4390
 
      */   
4391
 
      while ((item= it++))
4392
 
      {
4393
 
        add_key_field(key_fields, *and_level, cond_func, item->field,
4394
 
                      true, &const_item, 1, usable_tables, sargables);
4395
 
      }
4396
 
    }
4397
 
    else 
4398
 
    {
4399
 
      /*
4400
 
        Consider all pairs of different fields included into item_equal.
4401
 
        For each of them (field1, field1) consider the equality 
4402
 
        field1=field2 as a condition allowing an index access of the table
4403
 
        with field1 by the keys value of field2.
4404
 
      */   
4405
 
      Item_equal_iterator fi(*item_equal);
4406
 
      while ((item= fi++))
4407
 
      {
4408
 
        Field *field= item->field;
4409
 
        while ((item= it++))
4410
 
        {
4411
 
          if (!field->eq(item->field))
4412
 
          {
4413
 
            add_key_field(key_fields, *and_level, cond_func, field,
4414
 
                          true, (Item **) &item, 1, usable_tables,
4415
 
                          sargables);
4416
 
          }
4417
 
        }
4418
 
        it.rewind();
4419
 
      }
4420
 
    }
4421
 
    break;
4422
 
  }
4423
 
}
4424
495
 
4425
496
/**
4426
497
  Add all keys with uses 'field' for some keypart.
4427
498
 
4428
499
  If field->and_level != and_level then only mark key_part as const_part.
4429
500
*/
4430
 
 
4431
 
static uint
4432
 
max_part_bit(key_part_map bits)
 
501
uint32_t max_part_bit(key_part_map bits)
4433
502
{
4434
 
  uint found;
 
503
  uint32_t found;
4435
504
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4436
505
  return found;
4437
506
}
4438
507
 
4439
 
static void
4440
 
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4441
 
{
4442
 
  Field *field=key_field->field;
4443
 
  TABLE *form= field->table;
4444
 
  KEYUSE keyuse;
4445
 
 
4446
 
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4447
 
  {
4448
 
    for (uint key=0 ; key < form->s->keys ; key++)
4449
 
    {
4450
 
      if (!(form->keys_in_use_for_query.is_set(key)))
4451
 
        continue;
4452
 
 
4453
 
      uint key_parts= (uint) form->key_info[key].key_parts;
4454
 
      for (uint part=0 ; part <  key_parts ; part++)
4455
 
      {
4456
 
        if (field->eq(form->key_info[key].key_part[part].field))
4457
 
        {
4458
 
          keyuse.table= field->table;
4459
 
          keyuse.val =  key_field->val;
4460
 
          keyuse.key =  key;
4461
 
          keyuse.keypart=part;
4462
 
          keyuse.keypart_map= (key_part_map) 1 << part;
4463
 
          keyuse.used_tables=key_field->val->used_tables();
4464
 
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4465
 
          keyuse.null_rejecting= key_field->null_rejecting;
4466
 
          keyuse.cond_guard= key_field->cond_guard;
4467
 
          keyuse.sj_pred_no= key_field->sj_pred_no;
4468
 
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4469
 
        }
4470
 
      }
4471
 
    }
4472
 
  }
4473
 
}
4474
 
 
4475
 
static int
4476
 
sort_keyuse(KEYUSE *a,KEYUSE *b)
 
508
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4477
509
{
4478
510
  int res;
4479
 
  if (a->table->tablenr != b->table->tablenr)
4480
 
    return (int) (a->table->tablenr - b->table->tablenr);
4481
 
  if (a->key != b->key)
4482
 
    return (int) (a->key - b->key);
4483
 
  if (a->keypart != b->keypart)
4484
 
    return (int) (a->keypart - b->keypart);
 
511
  if (a->getTable()->tablenr != b->getTable()->tablenr)
 
512
    return static_cast<int>((a->getTable()->tablenr - b->getTable()->tablenr));
 
513
  if (a->getKey() != b->getKey())
 
514
    return static_cast<int>((a->getKey() - b->getKey()));
 
515
  if (a->getKeypart() != b->getKeypart())
 
516
    return static_cast<int>((a->getKeypart() - b->getKeypart()));
4485
517
  // Place const values before other ones
4486
 
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4487
 
       test((b->used_tables & ~OUTER_REF_TABLE_BIT))))
 
518
  if ((res= test((a->getUsedTables() & ~OUTER_REF_TABLE_BIT)) -
 
519
       test((b->getUsedTables() & ~OUTER_REF_TABLE_BIT))))
4488
520
    return res;
4489
521
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4490
 
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4491
 
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4492
 
}
4493
 
 
4494
 
 
4495
 
/*
4496
 
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
4497
 
 
4498
 
    This function populates KEY_FIELD array with entries generated from the 
4499
 
    ON condition of the given nested join, and does the same for nested joins 
4500
 
    contained within this nested join.
4501
 
 
4502
 
  @param[in]      nested_join_table   Nested join pseudo-table to process
4503
 
  @param[in,out]  end                 End of the key field array
4504
 
  @param[in,out]  and_level           And-level
4505
 
  @param[in,out]  sargables           Array of found sargable candidates
4506
 
 
4507
 
 
4508
 
  @note
4509
 
    We can add accesses to the tables that are direct children of this nested 
4510
 
    join (1), and are not inner tables w.r.t their neighbours (2).
4511
 
    
4512
 
    Example for #1 (outer brackets pair denotes nested join this function is 
4513
 
    invoked for):
4514
 
    @code
4515
 
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4516
 
    @endcode
4517
 
    Example for #2:
4518
 
    @code
4519
 
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4520
 
    @endcode
4521
 
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
4522
 
    t1 only.
4523
 
    Example #3:
4524
 
    @code
4525
 
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4526
 
    @endcode
4527
 
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4528
 
*/
4529
 
 
4530
 
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4531
 
                                  KEY_FIELD **end, uint *and_level,
4532
 
                                  SARGABLE_PARAM **sargables)
4533
 
{
4534
 
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4535
 
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4536
 
  bool have_another = false;
4537
 
  table_map tables= 0;
4538
 
  TABLE_LIST *table;
4539
 
  assert(nested_join_table->nested_join);
4540
 
 
4541
 
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4542
 
                                            (table= li++))))
4543
 
  {
4544
 
    if (table->nested_join)
4545
 
    {
4546
 
      if (!table->on_expr)
4547
 
      {
4548
 
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4549
 
        have_another= true;
4550
 
        li2= li;
4551
 
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
4552
 
      }
4553
 
      else
4554
 
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4555
 
    }
4556
 
    else
4557
 
      if (!table->on_expr)
4558
 
        tables |= table->table->map;
4559
 
  }
4560
 
  if (nested_join_table->on_expr)
4561
 
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
4562
 
                   sargables);
 
522
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
 
523
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4563
524
}
4564
525
 
4565
526
 
4566
527
/**
4567
528
  Update keyuse array with all possible keys we can use to fetch rows.
4568
 
  
4569
 
  @param       thd 
4570
 
  @param[out]  keyuse         Put here ordered array of KEYUSE structures
 
529
 
 
530
  @param       session
 
531
  @param[out]  keyuse         Put here ordered array of KeyUse structures
4571
532
  @param       join_tab       Array in tablenr_order
4572
533
  @param       tables         Number of tables in join
4573
534
  @param       cond           WHERE condition (note that the function analyzes
4576
537
                              for which we can make ref access based the WHERE
4577
538
                              clause)
4578
539
  @param       select_lex     current SELECT
4579
 
  @param[out]  sargables      Array of found sargable candidates
4580
 
      
 
540
  @param[out]  sargables      std::vector of found sargable candidates
 
541
 
4581
542
   @retval
4582
543
     0  OK
4583
544
   @retval
4584
545
     1  Out of memory.
4585
546
*/
4586
 
 
4587
 
static bool
4588
 
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4589
 
                    uint tables, COND *cond,
4590
 
                    COND_EQUAL *cond_equal __attribute__((unused)),
4591
 
                    table_map normal_tables, SELECT_LEX *select_lex,
4592
 
                    SARGABLE_PARAM **sargables)
 
547
bool update_ref_and_keys(Session *session,
 
548
                         DYNAMIC_ARRAY *keyuse,
 
549
                         JoinTable *join_tab,
 
550
                         uint32_t tables,
 
551
                         COND *cond, 
 
552
                         COND_EQUAL *,
 
553
                         table_map normal_tables,
 
554
                         Select_Lex *select_lex,
 
555
                         vector<optimizer::SargableParam> &sargables)
4593
556
{
4594
557
  uint  and_level,i,found_eq_constant;
4595
 
  KEY_FIELD *key_fields, *end, *field;
4596
 
  uint sz;
4597
 
  uint m= max(select_lex->max_equal_elems,1);
4598
 
  
4599
 
  /* 
4600
 
    We use the same piece of memory to store both  KEY_FIELD 
4601
 
    and SARGABLE_PARAM structure.
4602
 
    KEY_FIELD values are placed at the beginning this memory
4603
 
    while  SARGABLE_PARAM values are put at the end.
4604
 
    All predicates that are used to fill arrays of KEY_FIELD
4605
 
    and SARGABLE_PARAM structures have at most 2 arguments
4606
 
    except BETWEEN predicates that have 3 arguments and 
 
558
  optimizer::KeyField *key_fields, *end, *field;
 
559
  uint32_t sz;
 
560
  uint32_t m= max(select_lex->max_equal_elems,(uint32_t)1);
 
561
 
 
562
  /*
 
563
    All predicates that are used to fill arrays of KeyField
 
564
    and SargableParam classes have at most 2 arguments
 
565
    except BETWEEN predicates that have 3 arguments and
4607
566
    IN predicates.
4608
 
    This any predicate if it's not BETWEEN/IN can be used 
4609
 
    directly to fill at most 2 array elements, either of KEY_FIELD
4610
 
    or SARGABLE_PARAM type. For a BETWEEN predicate 3 elements
 
567
    This any predicate if it's not BETWEEN/IN can be used
 
568
    directly to fill at most 2 array elements, either of KeyField 
 
569
    or SargableParam type. For a BETWEEN predicate 3 elements
4611
570
    can be filled as this predicate is considered as
4612
571
    saragable with respect to each of its argument.
4613
572
    An IN predicate can require at most 1 element as currently
4614
573
    it is considered as sargable only for its first argument.
4615
574
    Multiple equality can add  elements that are filled after
4616
575
    substitution of field arguments by equal fields. There
4617
 
    can be not more than select_lex->max_equal_elems such 
 
576
    can be not more than select_lex->max_equal_elems such
4618
577
    substitutions.
4619
 
  */ 
4620
 
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4621
 
      (((thd->lex->current_select->cond_count+1)*2 +
4622
 
        thd->lex->current_select->between_count)*m+1);
4623
 
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4624
 
    return true; /* purecov: inspected */
 
578
  */
 
579
  sz= sizeof(optimizer::KeyField) *
 
580
      (((session->lex->current_select->cond_count+1) +
 
581
        session->lex->current_select->between_count)*m+1);
 
582
  if (! (key_fields= (optimizer::KeyField*) session->alloc(sz)))
 
583
    return true;
4625
584
  and_level= 0;
4626
585
  field= end= key_fields;
4627
 
  *sargables= (SARGABLE_PARAM *) key_fields + 
4628
 
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
4629
 
  /* set a barrier for the array of SARGABLE_PARAM */
4630
 
  (*sargables)[0].field= 0; 
4631
586
 
4632
 
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
 
587
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
4633
588
    return true;
4634
589
  if (cond)
4635
590
  {
4636
591
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
4637
592
                   sargables);
4638
 
    for (; field != end ; field++)
 
593
    for (; field != end; field++)
4639
594
    {
4640
 
      add_key_part(keyuse,field);
 
595
      add_key_part(keyuse, field);
4641
596
      /* Mark that we can optimize LEFT JOIN */
4642
 
      if (field->val->type() == Item::NULL_ITEM &&
4643
 
          !field->field->real_maybe_null())
4644
 
        field->field->table->reginfo.not_exists_optimize=1;
 
597
      if (field->getValue()->type() == Item::NULL_ITEM &&
 
598
          ! field->getField()->real_maybe_null())
 
599
      {
 
600
        field->getField()->table->reginfo.not_exists_optimize= 1;
 
601
      }
4645
602
    }
4646
603
  }
4647
 
  for (i=0 ; i < tables ; i++)
 
604
  for (i= 0; i < tables; i++)
4648
605
  {
4649
606
    /*
4650
607
      Block the creation of keys for inner tables of outer joins.
4654
611
      In the future when we introduce conditional accesses
4655
612
      for inner tables in outer joins these keys will be taken
4656
613
      into account as well.
4657
 
    */ 
 
614
    */
4658
615
    if (*join_tab[i].on_expr_ref)
4659
 
      add_key_fields(join_tab->join, &end, &and_level, 
 
616
      add_key_fields(join_tab->join, &end, &and_level,
4660
617
                     *join_tab[i].on_expr_ref,
4661
618
                     join_tab[i].table->map, sargables);
4662
619
  }
4663
620
 
4664
621
  /* Process ON conditions for the nested joins */
4665
622
  {
4666
 
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
4667
 
    TABLE_LIST *table;
 
623
    List_iterator<TableList> li(*join_tab->join->join_list);
 
624
    TableList *table;
4668
625
    while ((table= li++))
4669
626
    {
4670
627
      if (table->nested_join)
4671
 
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level, 
 
628
        add_key_fields_for_nj(join_tab->join, table, &end, &and_level,
4672
629
                              sargables);
4673
630
    }
4674
631
  }
4688
645
  */
4689
646
  if (keyuse->elements)
4690
647
  {
4691
 
    KEYUSE key_end,*prev,*save_pos,*use;
 
648
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
4692
649
 
4693
 
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
650
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
4694
651
          (qsort_cmp) sort_keyuse);
4695
652
 
4696
 
    bzero((char*) &key_end,sizeof(key_end));    /* Add for easy testing */
4697
 
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
 
653
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
 
654
    insert_dynamic(keyuse,(unsigned char*) &key_end);
4698
655
 
4699
 
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
 
656
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
4700
657
    prev= &key_end;
4701
 
    found_eq_constant=0;
4702
 
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
 
658
    found_eq_constant= 0;
 
659
    for (i= 0; i < keyuse->elements-1; i++, use++)
4703
660
    {
4704
 
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4705
 
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
661
      if (! use->getUsedTables() && use->getOptimizeFlags() != KEY_OPTIMIZE_REF_OR_NULL)
 
662
        use->getTable()->const_key_parts[use->getKey()]|= use->getKeypartMap();
 
663
      if (use->getKey() == prev->getKey() && use->getTable() == prev->getTable())
4706
664
      {
4707
 
        if (use->key == prev->key && use->table == prev->table)
4708
 
        {
4709
 
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4710
 
            continue;                           /* remove */
4711
 
        }
4712
 
        else if (use->keypart != 0)             // First found must be 0
4713
 
          continue;
 
665
        if (prev->getKeypart() + 1 < use->getKeypart() || 
 
666
            ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
 
667
          continue;                             /* remove */
4714
668
      }
 
669
      else if (use->getKeypart() != 0)          // First found must be 0
 
670
        continue;
4715
671
 
4716
672
#ifdef HAVE_purify
4717
673
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
4719
675
#endif
4720
676
        *save_pos= *use;
4721
677
      prev=use;
4722
 
      found_eq_constant= !use->used_tables;
 
678
      found_eq_constant= ! use->getUsedTables();
4723
679
      /* Save ptr to first use */
4724
 
      if (!use->table->reginfo.join_tab->keyuse)
4725
 
        use->table->reginfo.join_tab->keyuse=save_pos;
4726
 
      use->table->reginfo.join_tab->checked_keys.set_bit(use->key);
 
680
      if (! use->getTable()->reginfo.join_tab->keyuse)
 
681
        use->getTable()->reginfo.join_tab->keyuse= save_pos;
 
682
      use->getTable()->reginfo.join_tab->checked_keys.set(use->getKey());
4727
683
      save_pos++;
4728
684
    }
4729
 
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4730
 
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4731
 
    keyuse->elements=i;
 
685
    i= (uint32_t) (save_pos - (optimizer::KeyUse*) keyuse->buffer);
 
686
    set_dynamic(keyuse, (unsigned char*) &key_end, i);
 
687
    keyuse->elements= i;
4732
688
  }
4733
689
  return false;
4734
690
}
4736
692
/**
4737
693
  Update some values in keyuse for faster choose_plan() loop.
4738
694
*/
4739
 
 
4740
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
695
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
4741
696
{
4742
 
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
 
697
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
 
698
                                                  0, 
 
699
                                                  optimizer::KeyUse*);
4743
700
 
4744
701
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
4745
702
  {
4752
709
      Constant tables are ignored.
4753
710
      To avoid bad matches, we don't make ref_table_rows less than 100.
4754
711
    */
4755
 
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
4756
 
    if (keyuse->used_tables &
4757
 
        (map= (keyuse->used_tables & ~join->const_table_map &
4758
 
               ~OUTER_REF_TABLE_BIT)))
 
712
    keyuse->setTableRows(~(ha_rows) 0); // If no ref
 
713
    if (keyuse->getUsedTables() & (map= (keyuse->getUsedTables() & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
4759
714
    {
4760
 
      uint tablenr;
 
715
      uint32_t tablenr;
4761
716
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4762
717
      if (map == 1)                     // Only one table
4763
718
      {
4764
 
        TABLE *tmp_table=join->all_tables[tablenr];
4765
 
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, 100);
 
719
        Table *tmp_table=join->all_tables[tablenr];
 
720
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
4766
721
      }
4767
722
    }
4768
723
    /*
4769
724
      Outer reference (external field) is constant for single executing
4770
725
      of subquery
4771
726
    */
4772
 
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
4773
 
      keyuse->ref_table_rows= 1;
 
727
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
 
728
      keyuse->setTableRows(1);
4774
729
  }
4775
730
}
4776
731
 
4792
747
  @return
4793
748
    None
4794
749
*/
4795
 
 
4796
 
static void
4797
 
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
 
750
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab)
4798
751
{
4799
752
  List<Item_field> indexed_fields;
4800
753
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4801
 
  ORDER      *cur_group;
 
754
  order_st      *cur_group;
4802
755
  Item_field *cur_item;
4803
756
  key_map possible_keys(0);
4804
757
 
4806
759
  { /* Collect all query fields referenced in the GROUP clause. */
4807
760
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4808
761
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4809
 
                               (uchar*) &indexed_fields);
 
762
                               (unsigned char*) &indexed_fields);
4810
763
  }
4811
764
  else if (join->select_distinct)
4812
765
  { /* Collect all query fields referenced in the SELECT clause. */
4815
768
    Item *item;
4816
769
    while ((item= select_items_it++))
4817
770
      item->walk(&Item::collect_item_field_processor, 0,
4818
 
                 (uchar*) &indexed_fields);
 
771
                 (unsigned char*) &indexed_fields);
4819
772
  }
4820
773
  else
4821
774
    return;
4825
778
 
4826
779
  /* Intersect the keys of all group fields. */
4827
780
  cur_item= indexed_fields_it++;
4828
 
  possible_keys.merge(cur_item->field->part_of_key);
 
781
  possible_keys|= cur_item->field->part_of_key;
4829
782
  while ((cur_item= indexed_fields_it++))
4830
783
  {
4831
 
    possible_keys.intersect(cur_item->field->part_of_key);
4832
 
  }
4833
 
 
4834
 
  if (!possible_keys.is_clear_all())
4835
 
    join_tab->const_keys.merge(possible_keys);
4836
 
}
4837
 
 
4838
 
 
4839
 
/*****************************************************************************
4840
 
  Go through all combinations of not marked tables and find the one
4841
 
  which uses least records
4842
 
*****************************************************************************/
4843
 
 
4844
 
/** Save const tables first as used tables. */
4845
 
 
4846
 
static void
4847
 
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4848
 
{
4849
 
  join->positions[idx].table= table;
4850
 
  join->positions[idx].key=key;
4851
 
  join->positions[idx].records_read=1.0;        /* This is a const table */
4852
 
  join->positions[idx].ref_depend_map= 0;
4853
 
 
4854
 
  /* Move the const table as down as possible in best_ref */
4855
 
  JOIN_TAB **pos=join->best_ref+idx+1;
4856
 
  JOIN_TAB *next=join->best_ref[idx];
4857
 
  for (;next != table ; pos++)
4858
 
  {
4859
 
    JOIN_TAB *tmp=pos[0];
4860
 
    pos[0]=next;
4861
 
    next=tmp;
4862
 
  }
4863
 
  join->best_ref[idx]=table;
4864
 
}
4865
 
 
4866
 
 
4867
 
/*
4868
 
  Given a semi-join nest, find out which of the IN-equalities are bound
4869
 
 
4870
 
  SYNOPSIS
4871
 
    get_bound_sj_equalities()
4872
 
      sj_nest           Semi-join nest
4873
 
      remaining_tables  Tables that are not yet bound
4874
 
 
4875
 
  DESCRIPTION
4876
 
    Given a semi-join nest, find out which of the IN-equalities have their
4877
 
    left part expression bound (i.e. the said expression doesn't refer to
4878
 
    any of remaining_tables and can be evaluated).
4879
 
 
4880
 
  RETURN
4881
 
    Bitmap of bound IN-equalities.
4882
 
*/
4883
 
 
4884
 
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, 
4885
 
                                  table_map remaining_tables)
4886
 
{
4887
 
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4888
 
  Item *item;
4889
 
  uint i= 0;
4890
 
  uint64_t res= 0;
4891
 
  while ((item= li++))
4892
 
  {
4893
 
    /*
4894
 
      Q: should this take into account equality propagation and how?
4895
 
      A: If e->outer_side is an Item_field, walk over the equality
4896
 
         class and see if there is an element that is bound?
4897
 
      (this is an optional feature)
4898
 
    */
4899
 
    if (!(item->used_tables() & remaining_tables))
4900
 
    {
4901
 
      res |= 1ULL < i;
4902
 
    }
4903
 
  }
4904
 
  return res;
4905
 
}
4906
 
 
4907
 
 
4908
 
/**
4909
 
  Find the best access path for an extension of a partial execution
4910
 
  plan and add this path to the plan.
4911
 
 
4912
 
  The function finds the best access path to table 's' from the passed
4913
 
  partial plan where an access path is the general term for any means to
4914
 
  access the data in 's'. An access path may use either an index or a scan,
4915
 
  whichever is cheaper. The input partial plan is passed via the array
4916
 
  'join->positions' of length 'idx'. The chosen access method for 's' and its
4917
 
  cost are stored in 'join->positions[idx]'.
4918
 
 
4919
 
  @param join             pointer to the structure providing all context info
4920
 
                          for the query
4921
 
  @param s                the table to be joined by the function
4922
 
  @param thd              thread for the connection that submitted the query
4923
 
  @param remaining_tables set of tables not included into the partial plan yet
4924
 
  @param idx              the length of the partial plan
4925
 
  @param record_count     estimate for the number of records returned by the
4926
 
                          partial plan
4927
 
  @param read_time        the cost of the partial plan
4928
 
 
4929
 
  @return
4930
 
    None
4931
 
*/
4932
 
 
4933
 
static void
4934
 
best_access_path(JOIN      *join,
4935
 
                 JOIN_TAB  *s,
4936
 
                 THD       *thd,
4937
 
                 table_map remaining_tables,
4938
 
                 uint      idx,
4939
 
                 double    record_count,
4940
 
                 double    read_time __attribute__((unused)))
4941
 
{
4942
 
  KEYUSE *best_key=         0;
4943
 
  uint best_max_key_part=   0;
4944
 
  bool found_constraint= 0;
4945
 
  double best=              DBL_MAX;
4946
 
  double best_time=         DBL_MAX;
4947
 
  double records=           DBL_MAX;
4948
 
  table_map best_ref_depends_map= 0;
4949
 
  double tmp;
4950
 
  ha_rows rec;
4951
 
  uint best_is_sj_inside_out=    0;
4952
 
 
4953
 
  if (s->keyuse)
4954
 
  {                                            /* Use key if possible */
4955
 
    TABLE *table= s->table;
4956
 
    KEYUSE *keyuse,*start_key=0;
4957
 
    double best_records= DBL_MAX;
4958
 
    uint max_key_part=0;
4959
 
    uint64_t bound_sj_equalities= 0;
4960
 
    bool try_sj_inside_out= false;
4961
 
    /*
4962
 
      Discover the bound equalites. We need to do this, if
4963
 
        1. The next table is an SJ-inner table, and
4964
 
        2. It is the first table from that semijoin, and
4965
 
        3. We're not within a semi-join range (i.e. all semi-joins either have
4966
 
           all or none of their tables in join_table_map), except
4967
 
           s->emb_sj_nest (which we've just entered).
4968
 
        3. All correlation references from this sj-nest are bound
4969
 
    */
4970
 
    if (s->emb_sj_nest &&                                                 // (1)
4971
 
        s->emb_sj_nest->sj_in_exprs < 64 && 
4972
 
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
4973
 
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
4974
 
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
4975
 
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4976
 
    {
4977
 
      /* This table is an InsideOut scan candidate */
4978
 
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
4979
 
                                                   remaining_tables);
4980
 
      try_sj_inside_out= true;
4981
 
    }
4982
 
 
4983
 
    /* Test how we can use keys */
4984
 
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
4985
 
    for (keyuse=s->keyuse ; keyuse->table == table ;)
4986
 
    {
4987
 
      key_part_map found_part= 0;
4988
 
      table_map found_ref= 0;
4989
 
      uint key= keyuse->key;
4990
 
      KEY *keyinfo= table->key_info+key;
4991
 
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4992
 
      key_part_map const_part= 0;
4993
 
      /* The or-null keypart in ref-or-null access: */
4994
 
      key_part_map ref_or_null_part= 0;
4995
 
 
4996
 
      /* Calculate how many key segments of the current key we can use */
4997
 
      start_key= keyuse;
4998
 
      uint64_t handled_sj_equalities=0;
4999
 
      key_part_map sj_insideout_map= 0;
5000
 
 
5001
 
      do /* For each keypart */
5002
 
      {
5003
 
        uint keypart= keyuse->keypart;
5004
 
        table_map best_part_found_ref= 0;
5005
 
        double best_prev_record_reads= DBL_MAX;
5006
 
        
5007
 
        do /* For each way to access the keypart */
5008
 
        {
5009
 
 
5010
 
          /*
5011
 
            if 1. expression doesn't refer to forward tables
5012
 
               2. we won't get two ref-or-null's
5013
 
          */
5014
 
          if (!(remaining_tables & keyuse->used_tables) &&
5015
 
              !(ref_or_null_part && (keyuse->optimize &
5016
 
                                     KEY_OPTIMIZE_REF_OR_NULL)))
5017
 
          {
5018
 
            found_part|= keyuse->keypart_map;
5019
 
            if (!(keyuse->used_tables & ~join->const_table_map))
5020
 
              const_part|= keyuse->keypart_map;
5021
 
 
5022
 
            double tmp2= prev_record_reads(join, idx, (found_ref |
5023
 
                                                      keyuse->used_tables));
5024
 
            if (tmp2 < best_prev_record_reads)
5025
 
            {
5026
 
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5027
 
              best_prev_record_reads= tmp2;
5028
 
            }
5029
 
            if (rec > keyuse->ref_table_rows)
5030
 
              rec= keyuse->ref_table_rows;
5031
 
            /*
5032
 
              If there is one 'key_column IS NULL' expression, we can
5033
 
              use this ref_or_null optimisation of this field
5034
 
            */
5035
 
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5036
 
              ref_or_null_part |= keyuse->keypart_map;
5037
 
          }
5038
 
 
5039
 
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5040
 
          {
5041
 
            if (!(remaining_tables & keyuse->used_tables))
5042
 
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5043
 
            else
5044
 
            {
5045
 
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5046
 
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5047
 
            }
5048
 
          }
5049
 
 
5050
 
          keyuse++;
5051
 
        } while (keyuse->table == table && keyuse->key == key &&
5052
 
                 keyuse->keypart == keypart);
5053
 
        found_ref|= best_part_found_ref;
5054
 
      } while (keyuse->table == table && keyuse->key == key);
5055
 
 
5056
 
      /*
5057
 
        Assume that that each key matches a proportional part of table.
5058
 
      */
5059
 
      if (!found_part && !handled_sj_equalities)
5060
 
        continue;                               // Nothing usable found
5061
 
 
5062
 
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5063
 
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
5064
 
 
5065
 
      bool sj_inside_out_scan= false;
5066
 
      {
5067
 
        found_constraint= 1;
5068
 
        /*
5069
 
          Check if InsideOut scan is applicable:
5070
 
          1. All IN-equalities are either "bound" or "handled"
5071
 
          2. Index keyparts are 
5072
 
             ...
5073
 
        */
5074
 
        if (try_sj_inside_out && 
5075
 
            table->covering_keys.is_set(key) &&
5076
 
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5077
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5078
 
        {
5079
 
          uint n_fixed_parts= max_part_bit(found_part);
5080
 
          if (n_fixed_parts != keyinfo->key_parts &&
5081
 
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5082
 
               PREV_BITS(uint, keyinfo->key_parts))
5083
 
          {
5084
 
            /*
5085
 
              Not all parts are fixed. Produce bitmap of remaining bits and
5086
 
              check if all of them are covered.
5087
 
            */
5088
 
            sj_inside_out_scan= true;
5089
 
            if (!n_fixed_parts)
5090
 
            {
5091
 
              /*
5092
 
                It's a confluent ref scan.
5093
 
 
5094
 
                That is, all found KEYUSE elements refer to IN-equalities,
5095
 
                and there is really no ref access because there is no
5096
 
                  t.keypart0 = {bound expression}
5097
 
 
5098
 
                Calculate the cost of complete loose index scan.
5099
 
              */
5100
 
              records= (double)s->table->file->stats.records;
5101
 
 
5102
 
              /* The cost is entire index scan cost (divided by 2) */
5103
 
              best_time= s->table->file->index_only_read_time(key, records);
5104
 
 
5105
 
              /* Now figure how many different keys we will get */
5106
 
              ulong rpc;
5107
 
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5108
 
                records= records / rpc;
5109
 
              start_key= NULL;
5110
 
            }
5111
 
          }
5112
 
        }
5113
 
 
5114
 
        /*
5115
 
          Check if we found full key
5116
 
        */
5117
 
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5118
 
            !ref_or_null_part)
5119
 
        {                                         /* use eq key */
5120
 
          max_key_part= (uint) ~0;
5121
 
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5122
 
          {
5123
 
            tmp = prev_record_reads(join, idx, found_ref);
5124
 
            records=1.0;
5125
 
          }
5126
 
          else
5127
 
          {
5128
 
            if (!found_ref)
5129
 
            {                                     /* We found a const key */
5130
 
              /*
5131
 
                ReuseRangeEstimateForRef-1:
5132
 
                We get here if we've found a ref(const) (c_i are constants):
5133
 
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
5134
 
                
5135
 
                If range optimizer was able to construct a "range" 
5136
 
                access on this index, then its condition "quick_cond" was
5137
 
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5138
 
                from the range optimizer.
5139
 
                
5140
 
                Proof of (*): By properties of range and ref optimizers 
5141
 
                quick_cond will be equal or tighther than ref_const_cond. 
5142
 
                ref_const_cond already covers "smallest" possible interval - 
5143
 
                a singlepoint interval over all keyparts. Therefore, 
5144
 
                quick_cond is equivalent to ref_const_cond (if it was an 
5145
 
                empty interval we wouldn't have got here).
5146
 
              */
5147
 
              if (table->quick_keys.is_set(key))
5148
 
                records= (double) table->quick_rows[key];
5149
 
              else
5150
 
              {
5151
 
                /* quick_range couldn't use key! */
5152
 
                records= (double) s->records/rec;
5153
 
              }
5154
 
            }
5155
 
            else
5156
 
            {
5157
 
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5158
 
              {                                   /* Prefer longer keys */
5159
 
                records=
5160
 
                  ((double) s->records / (double) rec *
5161
 
                   (1.0 +
5162
 
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
5163
 
                     (double) table->s->max_key_length)));
5164
 
                if (records < 2.0)
5165
 
                  records=2.0;               /* Can't be as good as a unique */
5166
 
              }
5167
 
              /*
5168
 
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
5169
 
                E(#rows) from range optimizer. Make another try:
5170
 
                
5171
 
                If range optimizer produced E(#rows) for a prefix of the ref
5172
 
                access we're considering, and that E(#rows) is lower then our
5173
 
                current estimate, make an adjustment. The criteria of when we
5174
 
                can make an adjustment is a special case of the criteria used
5175
 
                in ReuseRangeEstimateForRef-3.
5176
 
              */
5177
 
              if (table->quick_keys.is_set(key) &&
5178
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5179
 
                  table->quick_n_ranges[key] == 1 &&
5180
 
                  records > (double) table->quick_rows[key])
5181
 
              {
5182
 
                records= (double) table->quick_rows[key];
5183
 
              }
5184
 
            }
5185
 
            /* Limit the number of matched rows */
5186
 
            tmp= records;
5187
 
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5188
 
            if (table->covering_keys.is_set(key))
5189
 
            {
5190
 
              /* we can use only index tree */
5191
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5192
 
            }
5193
 
            else
5194
 
              tmp= record_count*min(tmp,s->worst_seeks);
5195
 
          }
5196
 
        }
5197
 
        else
5198
 
        {
5199
 
          /*
5200
 
            Use as much key-parts as possible and a uniq key is better
5201
 
            than a not unique key
5202
 
            Set tmp to (previous record count) * (records / combination)
5203
 
          */
5204
 
          if ((found_part & 1) &&
5205
 
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5206
 
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
5207
 
          {
5208
 
            max_key_part= max_part_bit(found_part);
5209
 
            /*
5210
 
              ReuseRangeEstimateForRef-3:
5211
 
              We're now considering a ref[or_null] access via
5212
 
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
5213
 
              (same-as-above but with one cond replaced 
5214
 
               with "t.keypart_i IS NULL")]  (**)
5215
 
              
5216
 
              Try re-using E(#rows) from "range" optimizer:
5217
 
              We can do so if "range" optimizer used the same intervals as
5218
 
              in (**). The intervals used by range optimizer may be not 
5219
 
              available at this point (as "range" access might have choosen to
5220
 
              create quick select over another index), so we can't compare
5221
 
              them to (**). We'll make indirect judgements instead.
5222
 
              The sufficient conditions for re-use are:
5223
 
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5224
 
                   this is not satisfied we have no way to know which ranges
5225
 
                   will be actually scanned by 'ref' until we execute the 
5226
 
                   join)
5227
 
              (C2) max #key parts in 'range' access == K == max_key_part (this
5228
 
                   is apparently a necessary requirement)
5229
 
 
5230
 
              We also have a property that "range optimizer produces equal or 
5231
 
              tighter set of scan intervals than ref(const) optimizer". Each
5232
 
              of the intervals in (**) are "tightest possible" intervals when 
5233
 
              one limits itself to using keyparts 1..K (which we do in #2).              
5234
 
              From here it follows that range access used either one, or
5235
 
              both of the (I1) and (I2) intervals:
5236
 
              
5237
 
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
5238
 
               (same-as-above but with one cond replaced  
5239
 
                with "t.keypart_i IS NULL")               (I2)
5240
 
 
5241
 
              The remaining part is to exclude the situation where range
5242
 
              optimizer used one interval while we're considering
5243
 
              ref-or-null and looking for estimate for two intervals. This
5244
 
              is done by last limitation:
5245
 
 
5246
 
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5247
 
            */
5248
 
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5249
 
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5250
 
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5251
 
            {
5252
 
              tmp= records= (double) table->quick_rows[key];
5253
 
            }
5254
 
            else
5255
 
            {
5256
 
              /* Check if we have statistic about the distribution */
5257
 
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
5258
 
              {
5259
 
                /* 
5260
 
                  Fix for the case where the index statistics is too
5261
 
                  optimistic: If 
5262
 
                  (1) We're considering ref(const) and there is quick select
5263
 
                      on the same index, 
5264
 
                  (2) and that quick select uses more keyparts (i.e. it will
5265
 
                      scan equal/smaller interval then this ref(const))
5266
 
                  (3) and E(#rows) for quick select is higher then our
5267
 
                      estimate,
5268
 
                  Then 
5269
 
                    We'll use E(#rows) from quick select.
5270
 
 
5271
 
                  Q: Why do we choose to use 'ref'? Won't quick select be
5272
 
                  cheaper in some cases ?
5273
 
                  TODO: figure this out and adjust the plan choice if needed.
5274
 
                */
5275
 
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
5276
 
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5277
 
                    records < (double)table->quick_rows[key])         // (3)
5278
 
                  records= (double)table->quick_rows[key];
5279
 
 
5280
 
                tmp= records;
5281
 
              }
5282
 
              else
5283
 
              {
5284
 
                /*
5285
 
                  Assume that the first key part matches 1% of the file
5286
 
                  and that the whole key matches 10 (duplicates) or 1
5287
 
                  (unique) records.
5288
 
                  Assume also that more key matches proportionally more
5289
 
                  records
5290
 
                  This gives the formula:
5291
 
                  records = (x * (b-a) + a*c-b)/(c-1)
5292
 
 
5293
 
                  b = records matched by whole key
5294
 
                  a = records matched by first key part (1% of all records?)
5295
 
                  c = number of key parts in key
5296
 
                  x = used key parts (1 <= x <= c)
5297
 
                */
5298
 
                double rec_per_key;
5299
 
                if (!(rec_per_key=(double)
5300
 
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
5301
 
                  rec_per_key=(double) s->records/rec+1;
5302
 
 
5303
 
                if (!s->records)
5304
 
                  tmp = 0;
5305
 
                else if (rec_per_key/(double) s->records >= 0.01)
5306
 
                  tmp = rec_per_key;
5307
 
                else
5308
 
                {
5309
 
                  double a=s->records*0.01;
5310
 
                  if (keyinfo->key_parts > 1)
5311
 
                    tmp= (max_key_part * (rec_per_key - a) +
5312
 
                          a*keyinfo->key_parts - rec_per_key)/
5313
 
                         (keyinfo->key_parts-1);
5314
 
                  else
5315
 
                    tmp= a;
5316
 
                  set_if_bigger(tmp,1.0);
5317
 
                }
5318
 
                records = (ulong) tmp;
5319
 
              }
5320
 
 
5321
 
              if (ref_or_null_part)
5322
 
              {
5323
 
                /* We need to do two key searches to find key */
5324
 
                tmp *= 2.0;
5325
 
                records *= 2.0;
5326
 
              }
5327
 
 
5328
 
              /*
5329
 
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
5330
 
                E(#rows) from range optimizer. Make another try:
5331
 
                
5332
 
                If range optimizer produced E(#rows) for a prefix of the ref 
5333
 
                access we're considering, and that E(#rows) is lower then our
5334
 
                current estimate, make the adjustment.
5335
 
 
5336
 
                The decision whether we can re-use the estimate from the range
5337
 
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5338
 
                applied to first table->quick_key_parts[key] key parts.
5339
 
              */
5340
 
              if (table->quick_keys.is_set(key) &&
5341
 
                  table->quick_key_parts[key] <= max_key_part &&
5342
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5343
 
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5344
 
                                                         const_part) &&
5345
 
                  records > (double) table->quick_rows[key])
5346
 
              {
5347
 
                tmp= records= (double) table->quick_rows[key];
5348
 
              }
5349
 
            }
5350
 
 
5351
 
            /* Limit the number of matched rows */
5352
 
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5353
 
            if (table->covering_keys.is_set(key))
5354
 
            {
5355
 
              /* we can use only index tree */
5356
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5357
 
            }
5358
 
            else
5359
 
              tmp= record_count * min(tmp,s->worst_seeks);
5360
 
          }
5361
 
          else
5362
 
            tmp= best_time;                    // Do nothing
5363
 
        }
5364
 
 
5365
 
        if (sj_inside_out_scan && !start_key)
5366
 
        {
5367
 
          tmp= tmp/2;
5368
 
          if (records)
5369
 
            records= records/2;
5370
 
        }
5371
 
 
5372
 
      }
5373
 
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5374
 
      {
5375
 
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
5376
 
        best= tmp;
5377
 
        best_records= records;
5378
 
        best_key= start_key;
5379
 
        best_max_key_part= max_key_part;
5380
 
        best_ref_depends_map= found_ref;
5381
 
        best_is_sj_inside_out= sj_inside_out_scan;
5382
 
      }
5383
 
    }
5384
 
    records= best_records;
5385
 
  }
5386
 
 
5387
 
  /*
5388
 
    Don't test table scan if it can't be better.
5389
 
    Prefer key lookup if we would use the same key for scanning.
5390
 
 
5391
 
    Don't do a table scan on InnoDB tables, if we can read the used
5392
 
    parts of the row from any of the used index.
5393
 
    This is because table scans uses index and we would not win
5394
 
    anything by using a table scan.
5395
 
 
5396
 
    A word for word translation of the below if-statement in sergefp's
5397
 
    understanding: we check if we should use table scan if:
5398
 
    (1) The found 'ref' access produces more records than a table scan
5399
 
        (or index scan, or quick select), or 'ref' is more expensive than
5400
 
        any of them.
5401
 
    (2) This doesn't hold: the best way to perform table scan is to to perform
5402
 
        'range' access using index IDX, and the best way to perform 'ref' 
5403
 
        access is to use the same index IDX, with the same or more key parts.
5404
 
        (note: it is not clear how this rule is/should be extended to 
5405
 
        index_merge quick selects)
5406
 
    (3) See above note about InnoDB.
5407
 
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5408
 
             path, but there is no quick select)
5409
 
        If the condition in the above brackets holds, then the only possible
5410
 
        "table scan" access method is ALL/index (there is no quick select).
5411
 
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
5412
 
        choose it over ALL/index, there is no need to consider a full table
5413
 
        scan.
5414
 
  */
5415
 
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
5416
 
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5417
 
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5418
 
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5419
 
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5420
 
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5421
 
  {                                             // Check full join
5422
 
    ha_rows rnd_records= s->found_records;
5423
 
    /*
5424
 
      If there is a filtering condition on the table (i.e. ref analyzer found
5425
 
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5426
 
      preceding this table in the join order we're now considering), then 
5427
 
      assume that 25% of the rows will be filtered out by this condition.
5428
 
 
5429
 
      This heuristic is supposed to force tables used in exprZ to be before
5430
 
      this table in join order.
5431
 
    */
5432
 
    if (found_constraint)
5433
 
      rnd_records-= rnd_records/4;
5434
 
 
5435
 
    /*
5436
 
      If applicable, get a more accurate estimate. Don't use the two
5437
 
      heuristics at once.
5438
 
    */
5439
 
    if (s->table->quick_condition_rows != s->found_records)
5440
 
      rnd_records= s->table->quick_condition_rows;
5441
 
 
5442
 
    /*
5443
 
      Range optimizer never proposes a RANGE if it isn't better
5444
 
      than FULL: so if RANGE is present, it's always preferred to FULL.
5445
 
      Here we estimate its cost.
5446
 
    */
5447
 
    if (s->quick)
5448
 
    {
5449
 
      /*
5450
 
        For each record we:
5451
 
        - read record range through 'quick'
5452
 
        - skip rows which does not satisfy WHERE constraints
5453
 
        TODO: 
5454
 
        We take into account possible use of join cache for ALL/index
5455
 
        access (see first else-branch below), but we don't take it into 
5456
 
        account here for range/index_merge access. Find out why this is so.
5457
 
      */
5458
 
      tmp= record_count *
5459
 
        (s->quick->read_time +
5460
 
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5461
 
    }
5462
 
    else
5463
 
    {
5464
 
      /* Estimate cost of reading table. */
5465
 
      tmp= s->table->file->scan_time();
5466
 
      if (s->table->map & join->outer_join)     // Can't use join cache
5467
 
      {
5468
 
        /*
5469
 
          For each record we have to:
5470
 
          - read the whole table record 
5471
 
          - skip rows which does not satisfy join condition
5472
 
        */
5473
 
        tmp= record_count *
5474
 
          (tmp +
5475
 
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5476
 
      }
5477
 
      else
5478
 
      {
5479
 
        /* We read the table as many times as join buffer becomes full. */
5480
 
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5481
 
                           record_count /
5482
 
                           (double) thd->variables.join_buff_size));
5483
 
        /* 
5484
 
            We don't make full cartesian product between rows in the scanned
5485
 
           table and existing records because we skip all rows from the
5486
 
           scanned table, which does not satisfy join condition when 
5487
 
           we read the table (see flush_cached_records for details). Here we
5488
 
           take into account cost to read and skip these records.
5489
 
        */
5490
 
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5491
 
      }
5492
 
    }
5493
 
 
5494
 
    /*
5495
 
      We estimate the cost of evaluating WHERE clause for found records
5496
 
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5497
 
      tmp give us total cost of using TABLE SCAN
5498
 
    */
5499
 
    if (best == DBL_MAX ||
5500
 
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5501
 
         best + record_count/(double) TIME_FOR_COMPARE*records))
5502
 
    {
5503
 
      /*
5504
 
        If the table has a range (s->quick is set) make_join_select()
5505
 
        will ensure that this will be used
5506
 
      */
5507
 
      best= tmp;
5508
 
      records= rows2double(rnd_records);
5509
 
      best_key= 0;
5510
 
      /* range/index_merge/ALL/index access method are "independent", so: */
5511
 
      best_ref_depends_map= 0;
5512
 
      best_is_sj_inside_out= false;
5513
 
    }
5514
 
  }
5515
 
 
5516
 
  /* Update the cost information for the current partial plan */
5517
 
  join->positions[idx].records_read= records;
5518
 
  join->positions[idx].read_time=    best;
5519
 
  join->positions[idx].key=          best_key;
5520
 
  join->positions[idx].table=        s;
5521
 
  join->positions[idx].ref_depend_map= best_ref_depends_map;
5522
 
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5523
 
 
5524
 
  if (!best_key &&
5525
 
      idx == join->const_tables &&
5526
 
      s->table == join->sort_by_table &&
5527
 
      join->unit->select_limit_cnt >= records)
5528
 
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5529
 
 
5530
 
  return;
5531
 
}
5532
 
 
5533
 
 
5534
 
/**
5535
 
  Selects and invokes a search strategy for an optimal query plan.
5536
 
 
5537
 
  The function checks user-configurable parameters that control the search
5538
 
  strategy for an optimal plan, selects the search method and then invokes
5539
 
  it. Each specific optimization procedure stores the final optimal plan in
5540
 
  the array 'join->best_positions', and the cost of the plan in
5541
 
  'join->best_read'.
5542
 
 
5543
 
  @param join         pointer to the structure providing all context info for
5544
 
                      the query
5545
 
  @param join_tables  set of the tables in the query
5546
 
 
5547
 
  @todo
5548
 
    'MAX_TABLES+2' denotes the old implementation of find_best before
5549
 
    the greedy version. Will be removed when greedy_search is approved.
5550
 
 
5551
 
  @retval
5552
 
    false       ok
5553
 
  @retval
5554
 
    true        Fatal error
5555
 
*/
5556
 
 
5557
 
static bool
5558
 
choose_plan(JOIN *join, table_map join_tables)
5559
 
{
5560
 
  uint search_depth= join->thd->variables.optimizer_search_depth;
5561
 
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5562
 
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5563
 
 
5564
 
  join->cur_embedding_map= 0;
5565
 
  reset_nj_counters(join->join_list);
5566
 
  /*
5567
 
    if (SELECT_STRAIGHT_JOIN option is set)
5568
 
      reorder tables so dependent tables come after tables they depend 
5569
 
      on, otherwise keep tables in the order they were specified in the query 
5570
 
    else
5571
 
      Apply heuristic: pre-sort all access plans with respect to the number of
5572
 
      records accessed.
5573
 
  */
5574
 
  my_qsort(join->best_ref + join->const_tables,
5575
 
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
5576
 
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
5577
 
  join->cur_emb_sj_nests= 0;
5578
 
  if (straight_join)
5579
 
  {
5580
 
    optimize_straight_join(join, join_tables);
5581
 
  }
5582
 
  else
5583
 
  {
5584
 
    if (search_depth == MAX_TABLES+2)
5585
 
    { /*
5586
 
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5587
 
        the greedy version. Will be removed when greedy_search is approved.
5588
 
      */
5589
 
      join->best_read= DBL_MAX;
5590
 
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5591
 
        return(true);
5592
 
    } 
5593
 
    else
5594
 
    {
5595
 
      if (search_depth == 0)
5596
 
        /* Automatically determine a reasonable value for 'search_depth' */
5597
 
        search_depth= determine_search_depth(join);
5598
 
      if (greedy_search(join, join_tables, search_depth, prune_level))
5599
 
        return(true);
5600
 
    }
5601
 
  }
5602
 
 
5603
 
  /* 
5604
 
    Store the cost of this query into a user variable
5605
 
    Don't update last_query_cost for statements that are not "flat joins" :
5606
 
    i.e. they have subqueries, unions or call stored procedures.
5607
 
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
5608
 
  */
5609
 
  if (join->thd->lex->is_single_level_stmt())
5610
 
    join->thd->status_var.last_query_cost= join->best_read;
5611
 
  return(false);
5612
 
}
5613
 
 
5614
 
 
5615
 
/**
5616
 
  Compare two JOIN_TAB objects based on the number of accessed records.
5617
 
 
5618
 
  @param ptr1 pointer to first JOIN_TAB object
5619
 
  @param ptr2 pointer to second JOIN_TAB object
 
784
    possible_keys&= cur_item->field->part_of_key;
 
785
  }
 
786
 
 
787
  if (possible_keys.any())
 
788
    join_tab->const_keys|= possible_keys;
 
789
}
 
790
 
 
791
/**
 
792
  Compare two JoinTable objects based on the number of accessed records.
 
793
 
 
794
  @param ptr1 pointer to first JoinTable object
 
795
  @param ptr2 pointer to second JoinTable object
5620
796
 
5621
797
  NOTES
5622
798
    The order relation implemented by join_tab_cmp() is not transitive,
5628
804
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
5629
805
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
5630
806
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
5631
 
     
 
807
 
5632
808
  @retval
5633
809
    1  if first is bigger
5634
810
  @retval
5636
812
  @retval
5637
813
    0  if equal
5638
814
*/
5639
 
 
5640
 
static int
5641
 
join_tab_cmp(const void* ptr1, const void* ptr2)
 
815
int join_tab_cmp(const void* ptr1, const void* ptr2)
5642
816
{
5643
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5644
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
817
  JoinTable *jt1= *(JoinTable**) ptr1;
 
818
  JoinTable *jt2= *(JoinTable**) ptr2;
5645
819
 
5646
820
  if (jt1->dependent & jt2->table->map)
5647
821
    return 1;
5648
822
  if (jt2->dependent & jt1->table->map)
5649
 
    return -1;  
 
823
    return -1;
5650
824
  if (jt1->found_records > jt2->found_records)
5651
825
    return 1;
5652
826
  if (jt1->found_records < jt2->found_records)
5653
 
    return -1; 
 
827
    return -1;
5654
828
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5655
829
}
5656
830
 
5657
 
 
5658
831
/**
5659
832
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5660
833
*/
5661
 
 
5662
 
static int
5663
 
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
834
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5664
835
{
5665
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5666
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
836
  JoinTable *jt1= *(JoinTable**) ptr1;
 
837
  JoinTable *jt2= *(JoinTable**) ptr2;
5667
838
 
5668
839
  if (jt1->dependent & jt2->table->map)
5669
840
    return 1;
5673
844
}
5674
845
 
5675
846
/**
5676
 
  Heuristic procedure to automatically guess a reasonable degree of
5677
 
  exhaustiveness for the greedy search procedure.
5678
 
 
5679
 
  The procedure estimates the optimization time and selects a search depth
5680
 
  big enough to result in a near-optimal QEP, that doesn't take too long to
5681
 
  find. If the number of tables in the query exceeds some constant, then
5682
 
  search_depth is set to this constant.
5683
 
 
5684
 
  @param join   pointer to the structure providing all context info for
5685
 
                the query
5686
 
 
5687
 
  @note
5688
 
    This is an extremely simplistic implementation that serves as a stub for a
5689
 
    more advanced analysis of the join. Ideally the search depth should be
5690
 
    determined by learning from previous query optimizations, because it will
5691
 
    depend on the CPU power (and other factors).
5692
 
 
5693
 
  @todo
5694
 
    this value should be determined dynamically, based on statistics:
5695
 
    uint max_tables_for_exhaustive_opt= 7;
5696
 
 
5697
 
  @todo
5698
 
    this value could be determined by some mapping of the form:
5699
 
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5700
 
 
5701
 
  @return
5702
 
    A positive integer that specifies the search depth (and thus the
5703
 
    exhaustiveness) of the depth-first search algorithm used by
5704
 
    'greedy_search'.
5705
 
*/
5706
 
 
5707
 
static uint
5708
 
determine_search_depth(JOIN *join)
5709
 
{
5710
 
  uint table_count=  join->tables - join->const_tables;
5711
 
  uint search_depth;
5712
 
  /* TODO: this value should be determined dynamically, based on statistics: */
5713
 
  uint max_tables_for_exhaustive_opt= 7;
5714
 
 
5715
 
  if (table_count <= max_tables_for_exhaustive_opt)
5716
 
    search_depth= table_count+1; // use exhaustive for small number of tables
5717
 
  else
5718
 
    /*
5719
 
      TODO: this value could be determined by some mapping of the form:
5720
 
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5721
 
    */
5722
 
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
5723
 
 
5724
 
  return search_depth;
5725
 
}
5726
 
 
5727
 
 
5728
 
/**
5729
 
  Select the best ways to access the tables in a query without reordering them.
5730
 
 
5731
 
    Find the best access paths for each query table and compute their costs
5732
 
    according to their order in the array 'join->best_ref' (thus without
5733
 
    reordering the join tables). The function calls sequentially
5734
 
    'best_access_path' for each table in the query to select the best table
5735
 
    access method. The final optimal plan is stored in the array
5736
 
    'join->best_positions', and the corresponding cost in 'join->best_read'.
5737
 
 
5738
 
  @param join          pointer to the structure providing all context info for
5739
 
                       the query
5740
 
  @param join_tables   set of the tables in the query
5741
 
 
5742
 
  @note
5743
 
    This function can be applied to:
5744
 
    - queries with STRAIGHT_JOIN
5745
 
    - internally to compute the cost of an arbitrary QEP
5746
 
  @par
5747
 
    Thus 'optimize_straight_join' can be used at any stage of the query
5748
 
    optimization process to finalize a QEP as it is.
5749
 
*/
5750
 
 
5751
 
static void
5752
 
optimize_straight_join(JOIN *join, table_map join_tables)
5753
 
{
5754
 
  JOIN_TAB *s;
5755
 
  uint idx= join->const_tables;
5756
 
  double    record_count= 1.0;
5757
 
  double    read_time=    0.0;
5758
 
 
5759
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5760
 
  {
5761
 
    /* Find the best access method from 's' to the current partial plan */
5762
 
    advance_sj_state(join_tables, s);
5763
 
    best_access_path(join, s, join->thd, join_tables, idx,
5764
 
                     record_count, read_time);
5765
 
    /* compute the cost of the new plan extended with 's' */
5766
 
    record_count*= join->positions[idx].records_read;
5767
 
    read_time+=    join->positions[idx].read_time;
5768
 
    join_tables&= ~(s->table->map);
5769
 
    ++idx;
5770
 
  }
5771
 
 
5772
 
  read_time+= record_count / (double) TIME_FOR_COMPARE;
5773
 
  if (join->sort_by_table &&
5774
 
      join->sort_by_table != join->positions[join->const_tables].table->table)
5775
 
    read_time+= record_count;  // We have to make a temp table
5776
 
  memcpy((uchar*) join->best_positions, (uchar*) join->positions,
5777
 
         sizeof(POSITION)*idx);
5778
 
  join->best_read= read_time;
5779
 
}
5780
 
 
5781
 
 
5782
 
/**
5783
 
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5784
 
 
5785
 
    The search procedure uses a hybrid greedy/exhaustive search with controlled
5786
 
    exhaustiveness. The search is performed in N = card(remaining_tables)
5787
 
    steps. Each step evaluates how promising is each of the unoptimized tables,
5788
 
    selects the most promising table, and extends the current partial QEP with
5789
 
    that table.  Currenly the most 'promising' table is the one with least
5790
 
    expensive extension.\
5791
 
 
5792
 
    There are two extreme cases:
5793
 
    -# When (card(remaining_tables) < search_depth), the estimate finds the
5794
 
    best complete continuation of the partial QEP. This continuation can be
5795
 
    used directly as a result of the search.
5796
 
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
5797
 
    consideres the extension of the current QEP with each of the remaining
5798
 
    unoptimized tables.
5799
 
 
5800
 
    All other cases are in-between these two extremes. Thus the parameter
5801
 
    'search_depth' controlls the exhaustiveness of the search. The higher the
5802
 
    value, the longer the optimizaton time and possibly the better the
5803
 
    resulting plan. The lower the value, the fewer alternative plans are
5804
 
    estimated, but the more likely to get a bad QEP.
5805
 
 
5806
 
    All intermediate and final results of the procedure are stored in 'join':
5807
 
    - join->positions     : modified for every partial QEP that is explored
5808
 
    - join->best_positions: modified for the current best complete QEP
5809
 
    - join->best_read     : modified for the current best complete QEP
5810
 
    - join->best_ref      : might be partially reordered
5811
 
 
5812
 
    The final optimal plan is stored in 'join->best_positions', and its
5813
 
    corresponding cost in 'join->best_read'.
5814
 
 
5815
 
  @note
5816
 
    The following pseudocode describes the algorithm of 'greedy_search':
5817
 
 
5818
 
    @code
5819
 
    procedure greedy_search
5820
 
    input: remaining_tables
5821
 
    output: pplan;
5822
 
    {
5823
 
      pplan = <>;
5824
 
      do {
5825
 
        (t, a) = best_extension(pplan, remaining_tables);
5826
 
        pplan = concat(pplan, (t, a));
5827
 
        remaining_tables = remaining_tables - t;
5828
 
      } while (remaining_tables != {})
5829
 
      return pplan;
5830
 
    }
5831
 
 
5832
 
  @endcode
5833
 
    where 'best_extension' is a placeholder for a procedure that selects the
5834
 
    most "promising" of all tables in 'remaining_tables'.
5835
 
    Currently this estimate is performed by calling
5836
 
    'best_extension_by_limited_search' to evaluate all extensions of the
5837
 
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5838
 
    mainly depends on that of 'best_extension_by_limited_search'.
5839
 
 
5840
 
  @par
5841
 
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
5842
 
    worst-case complexity of this algorithm is <=
5843
 
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5844
 
    complexity of greedy_search is O(N!).
5845
 
 
5846
 
  @par
5847
 
    In the future, 'greedy_search' might be extended to support other
5848
 
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
5849
 
 
5850
 
  @param join             pointer to the structure providing all context info
5851
 
                          for the query
5852
 
  @param remaining_tables set of tables not included into the partial plan yet
5853
 
  @param search_depth     controlls the exhaustiveness of the search
5854
 
  @param prune_level      the pruning heuristics that should be applied during
5855
 
                          search
5856
 
 
5857
 
  @retval
5858
 
    false       ok
5859
 
  @retval
5860
 
    true        Fatal error
5861
 
*/
5862
 
 
5863
 
static bool
5864
 
greedy_search(JOIN      *join,
5865
 
              table_map remaining_tables,
5866
 
              uint      search_depth,
5867
 
              uint      prune_level)
5868
 
{
5869
 
  double    record_count= 1.0;
5870
 
  double    read_time=    0.0;
5871
 
  uint      idx= join->const_tables; // index into 'join->best_ref'
5872
 
  uint      best_idx;
5873
 
  uint      size_remain;    // cardinality of remaining_tables
5874
 
  POSITION  best_pos;
5875
 
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5876
 
 
5877
 
  /* number of tables that remain to be optimized */
5878
 
  size_remain= my_count_bits(remaining_tables);
5879
 
 
5880
 
  do {
5881
 
    /* Find the extension of the current QEP with the lowest cost */
5882
 
    join->best_read= DBL_MAX;
5883
 
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5884
 
                                         read_time, search_depth, prune_level))
5885
 
      return(true);
5886
 
 
5887
 
    if (size_remain <= search_depth)
5888
 
    {
5889
 
      /*
5890
 
        'join->best_positions' contains a complete optimal extension of the
5891
 
        current partial QEP.
5892
 
      */
5893
 
      return(false);
5894
 
    }
5895
 
 
5896
 
    /* select the first table in the optimal extension as most promising */
5897
 
    best_pos= join->best_positions[idx];
5898
 
    best_table= best_pos.table;
5899
 
    /*
5900
 
      Each subsequent loop of 'best_extension_by_limited_search' uses
5901
 
      'join->positions' for cost estimates, therefore we have to update its
5902
 
      value.
5903
 
    */
5904
 
    join->positions[idx]= best_pos;
5905
 
 
5906
 
    /* find the position of 'best_table' in 'join->best_ref' */
5907
 
    best_idx= idx;
5908
 
    JOIN_TAB *pos= join->best_ref[best_idx];
5909
 
    while (pos && best_table != pos)
5910
 
      pos= join->best_ref[++best_idx];
5911
 
    assert((pos != NULL)); // should always find 'best_table'
5912
 
    /* move 'best_table' at the first free position in the array of joins */
5913
 
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5914
 
 
5915
 
    /* compute the cost of the new plan extended with 'best_table' */
5916
 
    record_count*= join->positions[idx].records_read;
5917
 
    read_time+=    join->positions[idx].read_time;
5918
 
 
5919
 
    remaining_tables&= ~(best_table->table->map);
5920
 
    --size_remain;
5921
 
    ++idx;
5922
 
  } while (true);
5923
 
}
5924
 
 
5925
 
 
5926
 
/**
5927
 
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
5928
 
  exhaustive search.
5929
 
 
5930
 
    The procedure searches for the optimal ordering of the query tables in set
5931
 
    'remaining_tables' of size N, and the corresponding optimal access paths to
5932
 
    each table. The choice of a table order and an access path for each table
5933
 
    constitutes a query execution plan (QEP) that fully specifies how to
5934
 
    execute the query.
5935
 
   
5936
 
    The maximal size of the found plan is controlled by the parameter
5937
 
    'search_depth'. When search_depth == N, the resulting plan is complete and
5938
 
    can be used directly as a QEP. If search_depth < N, the found plan consists
5939
 
    of only some of the query tables. Such "partial" optimal plans are useful
5940
 
    only as input to query optimization procedures, and cannot be used directly
5941
 
    to execute a query.
5942
 
 
5943
 
    The algorithm begins with an empty partial plan stored in 'join->positions'
5944
 
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
5945
 
    evaluates the cost of the partial plan extended by all access plans for
5946
 
    each of the relations in 'remaining_tables', expands the current partial
5947
 
    plan with the access plan that results in lowest cost of the expanded
5948
 
    partial plan, and removes the corresponding relation from
5949
 
    'remaining_tables'. The algorithm continues until it either constructs a
5950
 
    complete optimal plan, or constructs an optimal plartial plan with size =
5951
 
    search_depth.
5952
 
 
5953
 
    The final optimal plan is stored in 'join->best_positions'. The
5954
 
    corresponding cost of the optimal plan is in 'join->best_read'.
5955
 
 
5956
 
  @note
5957
 
    The procedure uses a recursive depth-first search where the depth of the
5958
 
    recursion (and thus the exhaustiveness of the search) is controlled by the
5959
 
    parameter 'search_depth'.
5960
 
 
5961
 
  @note
5962
 
    The pseudocode below describes the algorithm of
5963
 
    'best_extension_by_limited_search'. The worst-case complexity of this
5964
 
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5965
 
    the complexity of greedy_search is O(N!).
5966
 
 
5967
 
    @code
5968
 
    procedure best_extension_by_limited_search(
5969
 
      pplan in,             // in, partial plan of tables-joined-so-far
5970
 
      pplan_cost,           // in, cost of pplan
5971
 
      remaining_tables,     // in, set of tables not referenced in pplan
5972
 
      best_plan_so_far,     // in/out, best plan found so far
5973
 
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5974
 
      search_depth)         // in, maximum size of the plans being considered
5975
 
    {
5976
 
      for each table T from remaining_tables
5977
 
      {
5978
 
        // Calculate the cost of using table T as above
5979
 
        cost = complex-series-of-calculations;
5980
 
 
5981
 
        // Add the cost to the cost so far.
5982
 
        pplan_cost+= cost;
5983
 
 
5984
 
        if (pplan_cost >= best_plan_so_far_cost)
5985
 
          // pplan_cost already too great, stop search
5986
 
          continue;
5987
 
 
5988
 
        pplan= expand pplan by best_access_method;
5989
 
        remaining_tables= remaining_tables - table T;
5990
 
        if (remaining_tables is not an empty set
5991
 
            and
5992
 
            search_depth > 1)
5993
 
        {
5994
 
          best_extension_by_limited_search(pplan, pplan_cost,
5995
 
                                           remaining_tables,
5996
 
                                           best_plan_so_far,
5997
 
                                           best_plan_so_far_cost,
5998
 
                                           search_depth - 1);
5999
 
        }
6000
 
        else
6001
 
        {
6002
 
          best_plan_so_far_cost= pplan_cost;
6003
 
          best_plan_so_far= pplan;
6004
 
        }
6005
 
      }
6006
 
    }
6007
 
    @endcode
6008
 
 
6009
 
  @note
6010
 
    When 'best_extension_by_limited_search' is called for the first time,
6011
 
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6012
 
    The actual implementation provides a way to optionally use pruning
6013
 
    heuristic (controlled by the parameter 'prune_level') to reduce the search
6014
 
    space by skipping some partial plans.
6015
 
 
6016
 
  @note
6017
 
    The parameter 'search_depth' provides control over the recursion
6018
 
    depth, and thus the size of the resulting optimal plan.
6019
 
 
6020
 
  @param join             pointer to the structure providing all context info
6021
 
                          for the query
6022
 
  @param remaining_tables set of tables not included into the partial plan yet
6023
 
  @param idx              length of the partial QEP in 'join->positions';
6024
 
                          since a depth-first search is used, also corresponds
6025
 
                          to the current depth of the search tree;
6026
 
                          also an index in the array 'join->best_ref';
6027
 
  @param record_count     estimate for the number of records returned by the
6028
 
                          best partial plan
6029
 
  @param read_time        the cost of the best partial plan
6030
 
  @param search_depth     maximum depth of the recursion and thus size of the
6031
 
                          found optimal plan
6032
 
                          (0 < search_depth <= join->tables+1).
6033
 
  @param prune_level      pruning heuristics that should be applied during
6034
 
                          optimization
6035
 
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6036
 
 
6037
 
  @retval
6038
 
    false       ok
6039
 
  @retval
6040
 
    true        Fatal error
6041
 
*/
6042
 
 
6043
 
static bool
6044
 
best_extension_by_limited_search(JOIN      *join,
6045
 
                                 table_map remaining_tables,
6046
 
                                 uint      idx,
6047
 
                                 double    record_count,
6048
 
                                 double    read_time,
6049
 
                                 uint      search_depth,
6050
 
                                 uint      prune_level)
6051
 
{
6052
 
  THD *thd= join->thd;
6053
 
  if (thd->killed)  // Abort
6054
 
    return(true);
6055
 
 
6056
 
  /* 
6057
 
     'join' is a partial plan with lower cost than the best plan so far,
6058
 
     so continue expanding it further with the tables in 'remaining_tables'.
6059
 
  */
6060
 
  JOIN_TAB *s;
6061
 
  double best_record_count= DBL_MAX;
6062
 
  double best_read_time=    DBL_MAX;
6063
 
 
6064
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6065
 
  {
6066
 
    table_map real_table_bit= s->table->map;
6067
 
    if ((remaining_tables & real_table_bit) && 
6068
 
        !(remaining_tables & s->dependent) && 
6069
 
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6070
 
    {
6071
 
      double current_record_count, current_read_time;
6072
 
      advance_sj_state(remaining_tables, s);
6073
 
 
6074
 
      /*
6075
 
        psergey-insideout-todo: 
6076
 
          when best_access_path() detects it could do an InsideOut scan or 
6077
 
          some other scan, have it return an insideout scan and a flag that 
6078
 
          requests to "fork" this loop iteration. (Q: how does that behave 
6079
 
          when the depth is insufficient??)
6080
 
      */
6081
 
      /* Find the best access method from 's' to the current partial plan */
6082
 
      best_access_path(join, s, thd, remaining_tables, idx,
6083
 
                       record_count, read_time);
6084
 
      /* Compute the cost of extending the plan with 's' */
6085
 
      current_record_count= record_count * join->positions[idx].records_read;
6086
 
      current_read_time=    read_time + join->positions[idx].read_time;
6087
 
 
6088
 
      /* Expand only partial plans with lower cost than the best QEP so far */
6089
 
      if ((current_read_time +
6090
 
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6091
 
      {
6092
 
        restore_prev_nj_state(s);
6093
 
        restore_prev_sj_state(remaining_tables, s);
6094
 
        continue;
6095
 
      }
6096
 
 
6097
 
      /*
6098
 
        Prune some less promising partial plans. This heuristic may miss
6099
 
        the optimal QEPs, thus it results in a non-exhaustive search.
6100
 
      */
6101
 
      if (prune_level == 1)
6102
 
      {
6103
 
        if (best_record_count > current_record_count ||
6104
 
            best_read_time > current_read_time ||
6105
 
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6106
 
        {
6107
 
          if (best_record_count >= current_record_count &&
6108
 
              best_read_time >= current_read_time &&
6109
 
              /* TODO: What is the reasoning behind this condition? */
6110
 
              (!(s->key_dependent & remaining_tables) ||
6111
 
               join->positions[idx].records_read < 2.0))
6112
 
          {
6113
 
            best_record_count= current_record_count;
6114
 
            best_read_time=    current_read_time;
6115
 
          }
6116
 
        }
6117
 
        else
6118
 
        {
6119
 
          restore_prev_nj_state(s);
6120
 
          restore_prev_sj_state(remaining_tables, s);
6121
 
          continue;
6122
 
        }
6123
 
      }
6124
 
 
6125
 
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6126
 
      { /* Recursively expand the current partial plan */
6127
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6128
 
        if (best_extension_by_limited_search(join,
6129
 
                                             remaining_tables & ~real_table_bit,
6130
 
                                             idx + 1,
6131
 
                                             current_record_count,
6132
 
                                             current_read_time,
6133
 
                                             search_depth - 1,
6134
 
                                             prune_level))
6135
 
          return(true);
6136
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6137
 
      }
6138
 
      else
6139
 
      { /*
6140
 
          'join' is either the best partial QEP with 'search_depth' relations,
6141
 
          or the best complete QEP so far, whichever is smaller.
6142
 
        */
6143
 
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6144
 
        if (join->sort_by_table &&
6145
 
            join->sort_by_table !=
6146
 
            join->positions[join->const_tables].table->table)
6147
 
          /* We have to make a temp table */
6148
 
          current_read_time+= current_record_count;
6149
 
        if ((search_depth == 1) || (current_read_time < join->best_read))
6150
 
        {
6151
 
          memcpy((uchar*) join->best_positions, (uchar*) join->positions,
6152
 
                 sizeof(POSITION) * (idx + 1));
6153
 
          join->best_read= current_read_time - 0.001;
6154
 
        }
6155
 
      }
6156
 
      restore_prev_nj_state(s);
6157
 
      restore_prev_sj_state(remaining_tables, s);
6158
 
    }
6159
 
  }
6160
 
  return(false);
6161
 
}
6162
 
 
6163
 
 
6164
 
/**
6165
 
  @todo
6166
 
  - TODO: this function is here only temporarily until 'greedy_search' is
6167
 
  tested and accepted.
6168
 
 
6169
 
  RETURN VALUES
6170
 
    false       ok
6171
 
    true        Fatal error
6172
 
*/
6173
 
static bool
6174
 
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6175
 
          double read_time)
6176
 
{
6177
 
  THD *thd= join->thd;
6178
 
  if (thd->killed)
6179
 
    return(true);
6180
 
  if (!rest_tables)
6181
 
  {
6182
 
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6183
 
    if (join->sort_by_table &&
6184
 
        join->sort_by_table !=
6185
 
        join->positions[join->const_tables].table->table)
6186
 
      read_time+=record_count;                  // We have to make a temp table
6187
 
    if (read_time < join->best_read)
6188
 
    {
6189
 
      memcpy((uchar*) join->best_positions,(uchar*) join->positions,
6190
 
             sizeof(POSITION)*idx);
6191
 
      join->best_read= read_time - 0.001;
6192
 
    }
6193
 
    return(false);
6194
 
  }
6195
 
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6196
 
    return(false);                                      /* Found better before */
6197
 
 
6198
 
  JOIN_TAB *s;
6199
 
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6200
 
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6201
 
  {
6202
 
    table_map real_table_bit=s->table->map;
6203
 
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6204
 
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6205
 
    {
6206
 
      double records, best;
6207
 
      advance_sj_state(rest_tables, s);
6208
 
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
6209
 
                       read_time);
6210
 
      records= join->positions[idx].records_read;
6211
 
      best= join->positions[idx].read_time;
6212
 
      /*
6213
 
        Go to the next level only if there hasn't been a better key on
6214
 
        this level! This will cut down the search for a lot simple cases!
6215
 
      */
6216
 
      double current_record_count=record_count*records;
6217
 
      double current_read_time=read_time+best;
6218
 
      if (best_record_count > current_record_count ||
6219
 
          best_read_time > current_read_time ||
6220
 
          (idx == join->const_tables && s->table == join->sort_by_table))
6221
 
      {
6222
 
        if (best_record_count >= current_record_count &&
6223
 
            best_read_time >= current_read_time &&
6224
 
            (!(s->key_dependent & rest_tables) || records < 2.0))
6225
 
        {
6226
 
          best_record_count=current_record_count;
6227
 
          best_read_time=current_read_time;
6228
 
        }
6229
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6230
 
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6231
 
                      current_record_count,current_read_time))
6232
 
          return(true);
6233
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6234
 
      }
6235
 
      restore_prev_nj_state(s);
6236
 
      restore_prev_sj_state(rest_tables, s);
6237
 
      if (join->select_options & SELECT_STRAIGHT_JOIN)
6238
 
        break;                          // Don't test all combinations
6239
 
    }
6240
 
  }
6241
 
  return(false);
6242
 
}
6243
 
 
6244
 
 
6245
 
/**
6246
847
  Find how much space the prevous read not const tables takes in cache.
6247
848
*/
6248
 
 
6249
 
static void calc_used_field_length(THD *thd __attribute__((unused)),
6250
 
                                   JOIN_TAB *join_tab)
 
849
void calc_used_field_length(Session *, JoinTable *join_tab)
6251
850
{
6252
 
  uint null_fields,blobs,fields,rec_length;
 
851
  uint32_t null_fields,blobs,fields,rec_length;
6253
852
  Field **f_ptr,*field;
6254
 
  MY_BITMAP *read_set= join_tab->table->read_set;;
6255
853
 
6256
854
  null_fields= blobs= fields= rec_length=0;
6257
855
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6258
856
  {
6259
 
    if (bitmap_is_set(read_set, field->field_index))
 
857
    if (field->isReadSet())
6260
858
    {
6261
 
      uint flags=field->flags;
 
859
      uint32_t flags=field->flags;
6262
860
      fields++;
6263
861
      rec_length+=field->pack_length();
6264
862
      if (flags & BLOB_FLAG)
6265
 
        blobs++;
 
863
        blobs++;
6266
864
      if (!(flags & NOT_NULL_FLAG))
6267
 
        null_fields++;
 
865
        null_fields++;
6268
866
    }
6269
867
  }
6270
868
  if (null_fields)
6271
 
    rec_length+=(join_tab->table->s->null_fields+7)/8;
 
869
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
6272
870
  if (join_tab->table->maybe_null)
6273
871
    rec_length+=sizeof(bool);
6274
872
  if (blobs)
6275
873
  {
6276
 
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6277
 
                             (join_tab->table->s->reclength- rec_length));
6278
 
    rec_length+=(uint) max(4,blob_length);
6279
 
  }
6280
 
  join_tab->used_fields=fields;
6281
 
  join_tab->used_fieldlength=rec_length;
6282
 
  join_tab->used_blobs=blobs;
6283
 
}
6284
 
 
6285
 
 
6286
 
static uint
6287
 
cache_record_length(JOIN *join,uint idx)
6288
 
{
6289
 
  uint length=0;
6290
 
  JOIN_TAB **pos,**end;
6291
 
  THD *thd=join->thd;
6292
 
 
6293
 
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6294
 
       pos != end ;
6295
 
       pos++)
6296
 
  {
6297
 
    JOIN_TAB *join_tab= *pos;
6298
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
6299
 
      calc_used_field_length(thd, join_tab);
6300
 
    length+=join_tab->used_fieldlength;
6301
 
  }
6302
 
  return length;
6303
 
}
6304
 
 
6305
 
 
6306
 
/*
6307
 
  Get the number of different row combinations for subset of partial join
6308
 
 
6309
 
  SYNOPSIS
6310
 
    prev_record_reads()
6311
 
      join       The join structure
6312
 
      idx        Number of tables in the partial join order (i.e. the
6313
 
                 partial join order is in join->positions[0..idx-1])
6314
 
      found_ref  Bitmap of tables for which we need to find # of distinct
6315
 
                 row combinations.
6316
 
 
6317
 
  DESCRIPTION
6318
 
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
6319
 
    tables within that join order (specified in found_ref), find out how many
6320
 
    distinct row combinations of subset tables will be in the result of the
6321
 
    partial join order.
6322
 
     
6323
 
    This is used as follows: Suppose we have a table accessed with a ref-based
6324
 
    method. The ref access depends on current rows of tables in found_ref.
6325
 
    We want to count # of different ref accesses. We assume two ref accesses
6326
 
    will be different if at least one of access parameters is different.
6327
 
    Example: consider a query
6328
 
 
6329
 
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6330
 
 
6331
 
    and a join order:
6332
 
      t1,  ref access on t1.key=c1
6333
 
      t2,  ref access on t2.key=c2       
6334
 
      t3,  ref access on t3.key=t1.field 
6335
 
    
6336
 
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6337
 
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6338
 
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
6339
 
            n_distinct_ref_scans = #records_read(t1)
6340
 
    
6341
 
    The reason for having this function (at least the latest version of it)
6342
 
    is that we need to account for buffering in join execution. 
6343
 
    
6344
 
    An edge-case example: if we have a non-first table in join accessed via
6345
 
    ref(const) or ref(param) where there is a small number of different
6346
 
    values of param, then the access will likely hit the disk cache and will
6347
 
    not require any disk seeks.
6348
 
    
6349
 
    The proper solution would be to assume an LRU disk cache of some size,
6350
 
    calculate probability of cache hits, etc. For now we just count
6351
 
    identical ref accesses as one.
6352
 
 
6353
 
  RETURN 
6354
 
    Expected number of row combinations
6355
 
*/
6356
 
 
6357
 
static double
6358
 
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6359
 
{
6360
 
  double found=1.0;
6361
 
  POSITION *pos_end= join->positions - 1;
6362
 
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6363
 
  {
6364
 
    if (pos->table->table->map & found_ref)
6365
 
    {
6366
 
      found_ref|= pos->ref_depend_map;
6367
 
      /* 
6368
 
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
6369
 
        with no matching row we will get position[t2].records_read==0. 
6370
 
        Actually the size of output is one null-complemented row, therefore 
6371
 
        we will use value of 1 whenever we get records_read==0.
6372
 
 
6373
 
        Note
6374
 
        - the above case can't occur if inner part of outer join has more 
6375
 
          than one table: table with no matches will not be marked as const.
6376
 
 
6377
 
        - Ideally we should add 1 to records_read for every possible null-
6378
 
          complemented row. We're not doing it because: 1. it will require
6379
 
          non-trivial code and add overhead. 2. The value of records_read
6380
 
          is an inprecise estimate and adding 1 (or, in the worst case,
6381
 
          #max_nested_outer_joins=64-1) will not make it any more precise.
6382
 
      */
6383
 
      if (pos->records_read > DBL_EPSILON)
6384
 
        found*= pos->records_read;
6385
 
    }
6386
 
  }
6387
 
  return found;
6388
 
}
6389
 
 
 
874
    uint32_t blob_length=(uint32_t) (join_tab->table->cursor->stats.mean_rec_length-
 
875
                                     (join_tab->table->getRecordLength()- rec_length));
 
876
    rec_length+= max((uint32_t)4,blob_length);
 
877
  }
 
878
  join_tab->used_fields= fields;
 
879
  join_tab->used_fieldlength= rec_length;
 
880
  join_tab->used_blobs= blobs;
 
881
}
 
882
 
 
883
StoredKey *get_store_key(Session *session,
 
884
                         optimizer::KeyUse *keyuse,
 
885
                         table_map used_tables,
 
886
                         KEY_PART_INFO *key_part,
 
887
                         unsigned char *key_buff,
 
888
                         uint32_t maybe_null)
 
889
{
 
890
  Item_ref *key_use_val= static_cast<Item_ref *>(keyuse->getVal());
 
891
  if (! ((~used_tables) & keyuse->getUsedTables())) // if const item
 
892
  {
 
893
    return new store_key_const_item(session,
 
894
                                    key_part->field,
 
895
                                    key_buff + maybe_null,
 
896
                                    maybe_null ? key_buff : 0,
 
897
                                    key_part->length,
 
898
                                    key_use_val);
 
899
  }
 
900
  else if (key_use_val->type() == Item::FIELD_ITEM ||
 
901
           (key_use_val->type() == Item::REF_ITEM &&
 
902
            key_use_val->ref_type() == Item_ref::OUTER_REF &&
 
903
            (*(Item_ref**)((Item_ref*)key_use_val)->ref)->ref_type() == Item_ref::DIRECT_REF &&
 
904
            key_use_val->real_item()->type() == Item::FIELD_ITEM))
 
905
  {
 
906
    return new store_key_field(session,
 
907
                               key_part->field,
 
908
                               key_buff + maybe_null,
 
909
                               maybe_null ? key_buff : 0,
 
910
                               key_part->length,
 
911
                               ((Item_field*) key_use_val->real_item())->field,
 
912
                               key_use_val->full_name());
 
913
  }
 
914
  return new store_key_item(session,
 
915
                            key_part->field,
 
916
                            key_buff + maybe_null,
 
917
                            maybe_null ? key_buff : 0,
 
918
                            key_part->length,
 
919
                            key_use_val);
 
920
}
6390
921
 
6391
922
/**
6392
 
  Set up join struct according to best position.
 
923
  This function is only called for const items on fields which are keys.
 
924
 
 
925
  @return
 
926
    returns 1 if there was some conversion made when the field was stored.
6393
927
*/
6394
 
 
6395
 
static bool
6396
 
get_best_combination(JOIN *join)
6397
 
{
6398
 
  uint i,tablenr;
6399
 
  table_map used_tables;
6400
 
  JOIN_TAB *join_tab,*j;
6401
 
  KEYUSE *keyuse;
6402
 
  uint table_count;
6403
 
  THD *thd=join->thd;
6404
 
 
6405
 
  table_count=join->tables;
6406
 
  if (!(join->join_tab=join_tab=
6407
 
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6408
 
    return(true);
6409
 
 
6410
 
  join->full_join=0;
6411
 
 
6412
 
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6413
 
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
928
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
929
{
 
930
  bool error;
 
931
  Table *table= field->table;
 
932
  Session *session= table->in_use;
 
933
  ha_rows cuted_fields=session->cuted_fields;
 
934
 
 
935
  /*
 
936
    we should restore old value of count_cuted_fields because
 
937
    store_val_in_field can be called from mysql_insert
 
938
    with select_insert, which make count_cuted_fields= 1
 
939
   */
 
940
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
 
941
  session->count_cuted_fields= check_flag;
 
942
  error= item->save_in_field(field, 1);
 
943
  session->count_cuted_fields= old_count_cuted_fields;
 
944
  return error || cuted_fields != session->cuted_fields;
 
945
}
 
946
 
 
947
inline void add_cond_and_fix(Item **e1, Item *e2)
 
948
{
 
949
  if (*e1)
6414
950
  {
6415
 
    TABLE *form;
6416
 
    *j= *join->best_positions[tablenr].table;
6417
 
    form=join->table[tablenr]=j->table;
6418
 
    used_tables|= form->map;
6419
 
    form->reginfo.join_tab=j;
6420
 
    if (!*j->on_expr_ref)
6421
 
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
6422
 
    if (j->type == JT_CONST)
6423
 
      continue;                                 // Handled in make_join_stat..
6424
 
 
6425
 
    j->ref.key = -1;
6426
 
    j->ref.key_parts=0;
6427
 
 
6428
 
    if (j->type == JT_SYSTEM)
6429
 
      continue;
6430
 
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
951
    Item *res;
 
952
    if ((res= new Item_cond_and(*e1, e2)))
6431
953
    {
6432
 
      j->type=JT_ALL;
6433
 
      if (tablenr != join->const_tables)
6434
 
        join->full_join=1;
 
954
      *e1= res;
 
955
      res->quick_fix_field();
6435
956
    }
6436
 
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6437
 
      return(true);                        // Something went wrong
6438
957
  }
6439
 
 
6440
 
  for (i=0 ; i < table_count ; i++)
6441
 
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6442
 
  update_depend_map(join);
6443
 
  return(0);
 
958
  else
 
959
    *e1= e2;
6444
960
}
6445
961
 
6446
 
 
6447
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6448
 
                               table_map used_tables)
 
962
bool create_ref_for_key(JOIN *join, 
 
963
                        JoinTable *j, 
 
964
                        optimizer::KeyUse *org_keyuse,
 
965
                        table_map used_tables)
6449
966
{
6450
 
  KEYUSE *keyuse=org_keyuse;
6451
 
  THD  *thd= join->thd;
6452
 
  uint keyparts,length,key;
6453
 
  TABLE *table;
6454
 
  KEY *keyinfo;
 
967
  optimizer::KeyUse *keyuse= org_keyuse;
 
968
  Session  *session= join->session;
 
969
  uint32_t keyparts;
 
970
  uint32_t length;
 
971
  uint32_t key;
 
972
  Table *table= NULL;
 
973
  KEY *keyinfo= NULL;
6455
974
 
6456
975
  /*  Use best key from find_best */
6457
 
  table=j->table;
6458
 
  key=keyuse->key;
6459
 
  keyinfo=table->key_info+key;
 
976
  table= j->table;
 
977
  key= keyuse->getKey();
 
978
  keyinfo= table->key_info + key;
6460
979
 
6461
980
  {
6462
 
    keyparts=length=0;
6463
 
    uint found_part_ref_or_null= 0;
 
981
    keyparts= length= 0;
 
982
    uint32_t found_part_ref_or_null= 0;
6464
983
    /*
6465
984
      Calculate length for the used key
6466
985
      Stop if there is a missing key part or when we find second key_part
6468
987
    */
6469
988
    do
6470
989
    {
6471
 
      if (!(~used_tables & keyuse->used_tables))
 
990
      if (! (~used_tables & keyuse->getUsedTables()))
6472
991
      {
6473
 
        if (keyparts == keyuse->keypart &&
6474
 
            !(found_part_ref_or_null & keyuse->optimize))
6475
 
        {
6476
 
          keyparts++;
6477
 
          length+= keyinfo->key_part[keyuse->keypart].store_length;
6478
 
          found_part_ref_or_null|= keyuse->optimize;
6479
 
        }
 
992
        if (keyparts == keyuse->getKeypart() &&
 
993
            ! (found_part_ref_or_null & keyuse->getOptimizeFlags()))
 
994
        {
 
995
          keyparts++;
 
996
          length+= keyinfo->key_part[keyuse->getKeypart()].store_length;
 
997
          found_part_ref_or_null|= keyuse->getOptimizeFlags();
 
998
        }
6480
999
      }
6481
1000
      keyuse++;
6482
 
    } while (keyuse->table == table && keyuse->key == key);
 
1001
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
6483
1002
  }
6484
1003
 
6485
1004
  /* set up fieldref */
6487
1006
  j->ref.key_parts=keyparts;
6488
1007
  j->ref.key_length=length;
6489
1008
  j->ref.key=(int) key;
6490
 
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6491
 
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6492
 
                                                   (keyparts+1)))) ||
6493
 
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6494
 
      !(j->ref.cond_guards= (bool**) thd->alloc(sizeof(uint*)*keyparts)))
 
1009
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
 
1010
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
 
1011
               (keyparts+1)))) ||
 
1012
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
 
1013
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
6495
1014
  {
6496
1015
    return(true);
6497
1016
  }
6501
1020
  j->ref.disable_cache= false;
6502
1021
  keyuse=org_keyuse;
6503
1022
 
6504
 
  store_key **ref_key= j->ref.key_copy;
6505
 
  uchar *key_buff=j->ref.key_buff, *null_ref_key= 0;
 
1023
  StoredKey **ref_key= j->ref.key_copy;
 
1024
  unsigned char *key_buff= j->ref.key_buff, *null_ref_key= 0;
6506
1025
  bool keyuse_uses_no_tables= true;
6507
1026
  {
6508
 
    uint i;
6509
 
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
1027
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
6510
1028
    {
6511
 
      while (keyuse->keypart != i ||
6512
 
             ((~used_tables) & keyuse->used_tables))
6513
 
        keyuse++;                               /* Skip other parts */
 
1029
      while (keyuse->getKeypart() != i ||
 
1030
             ((~used_tables) & keyuse->getUsedTables()))
 
1031
        keyuse++;       /* Skip other parts */
6514
1032
 
6515
 
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6516
 
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6517
 
      j->ref.cond_guards[i]= keyuse->cond_guard;
6518
 
      if (keyuse->null_rejecting) 
 
1033
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
 
1034
      j->ref.items[i]= keyuse->getVal();    // Save for cond removal
 
1035
      j->ref.cond_guards[i]= keyuse->getConditionalGuard();
 
1036
      if (keyuse->isNullRejected())
6519
1037
        j->ref.null_rejecting |= 1 << i;
6520
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6521
 
      if (!keyuse->used_tables &&
6522
 
          !(join->select_options & SELECT_DESCRIBE))
6523
 
      {                                 // Compare against constant
6524
 
        store_key_item tmp(thd, keyinfo->key_part[i].field,
 
1038
      keyuse_uses_no_tables= keyuse_uses_no_tables && ! keyuse->getUsedTables();
 
1039
      if (! keyuse->getUsedTables() &&  !(join->select_options & SELECT_DESCRIBE))
 
1040
      {         // Compare against constant
 
1041
        store_key_item tmp(session, keyinfo->key_part[i].field,
6525
1042
                           key_buff + maybe_null,
6526
1043
                           maybe_null ?  key_buff : 0,
6527
 
                           keyinfo->key_part[i].length, keyuse->val);
6528
 
        if (thd->is_fatal_error)
6529
 
          return(true);
6530
 
        tmp.copy();
 
1044
                           keyinfo->key_part[i].length, keyuse->getVal());
 
1045
        if (session->is_fatal_error)
 
1046
          return(true);
 
1047
        tmp.copy();
6531
1048
      }
6532
1049
      else
6533
 
        *ref_key++= get_store_key(thd,
6534
 
                                  keyuse,join->const_table_map,
6535
 
                                  &keyinfo->key_part[i],
6536
 
                                  key_buff, maybe_null);
 
1050
        *ref_key++= get_store_key(session,
 
1051
          keyuse,join->const_table_map,
 
1052
          &keyinfo->key_part[i],
 
1053
          key_buff, maybe_null);
6537
1054
      /*
6538
 
        Remember if we are going to use REF_OR_NULL
6539
 
        But only if field _really_ can be null i.e. we force JT_REF
6540
 
        instead of JT_REF_OR_NULL in case if field can't be null
 
1055
        Remember if we are going to use REF_OR_NULL
 
1056
        But only if field _really_ can be null i.e. we force AM_REF
 
1057
        instead of AM_REF_OR_NULL in case if field can't be null
6541
1058
      */
6542
 
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6543
 
        null_ref_key= key_buff;
 
1059
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
1060
        null_ref_key= key_buff;
6544
1061
      key_buff+=keyinfo->key_part[i].store_length;
6545
1062
    }
6546
1063
  }
6547
 
  *ref_key=0;                           // end_marker
6548
 
  if (j->type == JT_CONST)
 
1064
  *ref_key= 0;       // end_marker
 
1065
  if (j->type == AM_CONST)
6549
1066
    j->table->const_table= 1;
6550
1067
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
6551
1068
           keyparts != keyinfo->key_parts || null_ref_key)
6552
1069
  {
6553
1070
    /* Must read with repeat */
6554
 
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
1071
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
6555
1072
    j->ref.null_ref_key= null_ref_key;
6556
1073
  }
6557
1074
  else if (keyuse_uses_no_tables)
6563
1080
      Here we should not mark the table as a 'const' as a field may
6564
1081
      have a 'normal' value or a NULL value.
6565
1082
    */
6566
 
    j->type=JT_CONST;
6567
 
  }
6568
 
  else
6569
 
    j->type=JT_EQ_REF;
6570
 
  return(0);
6571
 
}
6572
 
 
6573
 
 
6574
 
 
6575
 
static store_key *
6576
 
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6577
 
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6578
 
{
6579
 
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6580
 
  {
6581
 
    return new store_key_const_item(thd,
6582
 
                                    key_part->field,
6583
 
                                    key_buff + maybe_null,
6584
 
                                    maybe_null ? key_buff : 0,
6585
 
                                    key_part->length,
6586
 
                                    keyuse->val);
6587
 
  }
6588
 
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
6589
 
           (keyuse->val->type() == Item::REF_ITEM &&
6590
 
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6591
 
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6592
 
             Item_ref::DIRECT_REF && 
6593
 
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6594
 
    return new store_key_field(thd,
6595
 
                               key_part->field,
6596
 
                               key_buff + maybe_null,
6597
 
                               maybe_null ? key_buff : 0,
6598
 
                               key_part->length,
6599
 
                               ((Item_field*) keyuse->val->real_item())->field,
6600
 
                               keyuse->val->full_name());
6601
 
  return new store_key_item(thd,
6602
 
                            key_part->field,
6603
 
                            key_buff + maybe_null,
6604
 
                            maybe_null ? key_buff : 0,
6605
 
                            key_part->length,
6606
 
                            keyuse->val);
6607
 
}
6608
 
 
6609
 
/**
6610
 
  This function is only called for const items on fields which are keys.
6611
 
 
6612
 
  @return
6613
 
    returns 1 if there was some conversion made when the field was stored.
6614
 
*/
6615
 
 
6616
 
bool
6617
 
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6618
 
{
6619
 
  bool error;
6620
 
  TABLE *table= field->table;
6621
 
  THD *thd= table->in_use;
6622
 
  ha_rows cuted_fields=thd->cuted_fields;
6623
 
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
6624
 
                                                   table->write_set);
6625
 
 
6626
 
  /*
6627
 
    we should restore old value of count_cuted_fields because
6628
 
    store_val_in_field can be called from mysql_insert 
6629
 
    with select_insert, which make count_cuted_fields= 1
6630
 
   */
6631
 
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
6632
 
  thd->count_cuted_fields= check_flag;
6633
 
  error= item->save_in_field(field, 1);
6634
 
  thd->count_cuted_fields= old_count_cuted_fields;
6635
 
  dbug_tmp_restore_column_map(table->write_set, old_map);
6636
 
  return error || cuted_fields != thd->cuted_fields;
6637
 
}
6638
 
 
6639
 
 
6640
 
static bool
6641
 
make_simple_join(JOIN *join,TABLE *tmp_table)
6642
 
{
6643
 
  TABLE **tableptr;
6644
 
  JOIN_TAB *join_tab;
6645
 
 
6646
 
  /*
6647
 
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6648
 
    to this function through JOIN::exec (may happen for sub-queries).
6649
 
  */
6650
 
  if (!join->table_reexec)
6651
 
  {
6652
 
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6653
 
      return(true);                        /* purecov: inspected */
6654
 
    if (join->tmp_join)
6655
 
      join->tmp_join->table_reexec= join->table_reexec;
6656
 
  }
6657
 
  if (!join->join_tab_reexec)
6658
 
  {
6659
 
    if (!(join->join_tab_reexec=
6660
 
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6661
 
      return(true);                        /* purecov: inspected */
6662
 
    if (join->tmp_join)
6663
 
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6664
 
  }
6665
 
  tableptr= join->table_reexec;
6666
 
  join_tab= join->join_tab_reexec;
6667
 
 
6668
 
  join->join_tab=join_tab;
6669
 
  join->table=tableptr; tableptr[0]=tmp_table;
6670
 
  join->tables=1;
6671
 
  join->const_tables=0;
6672
 
  join->const_table_map=0;
6673
 
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6674
 
    join->tmp_table_param.func_count=0;
6675
 
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6676
 
  join->first_record=join->sort_and_group=0;
6677
 
  join->send_records=(ha_rows) 0;
6678
 
  join->group=0;
6679
 
  join->row_limit=join->unit->select_limit_cnt;
6680
 
  join->do_send_rows = (join->row_limit) ? 1 : 0;
6681
 
 
6682
 
  join_tab->cache.buff=0;                       /* No caching */
6683
 
  join_tab->table=tmp_table;
6684
 
  join_tab->select=0;
6685
 
  join_tab->select_cond=0;
6686
 
  join_tab->quick=0;
6687
 
  join_tab->type= JT_ALL;                       /* Map through all records */
6688
 
  join_tab->keys.init();
6689
 
  join_tab->keys.set_all();                     /* test everything in quick */
6690
 
  join_tab->info=0;
6691
 
  join_tab->on_expr_ref=0;
6692
 
  join_tab->last_inner= 0;
6693
 
  join_tab->first_unmatched= 0;
6694
 
  join_tab->ref.key = -1;
6695
 
  join_tab->not_used_in_distinct=0;
6696
 
  join_tab->read_first_record= join_init_read_record;
6697
 
  join_tab->join=join;
6698
 
  join_tab->ref.key_parts= 0;
6699
 
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6700
 
  join_tab->do_firstmatch= NULL;
6701
 
  bzero((char*) &join_tab->read_record,sizeof(join_tab->read_record));
6702
 
  tmp_table->status=0;
6703
 
  tmp_table->null_row=0;
6704
 
  return(false);
6705
 
}
6706
 
 
6707
 
 
6708
 
inline void add_cond_and_fix(Item **e1, Item *e2)
6709
 
{
6710
 
  if (*e1)
6711
 
  {
6712
 
    Item *res;
6713
 
    if ((res= new Item_cond_and(*e1, e2)))
6714
 
    {
6715
 
      *e1= res;
6716
 
      res->quick_fix_field();
6717
 
    }
6718
 
  }
6719
 
  else
6720
 
    *e1= e2;
6721
 
}
6722
 
 
 
1083
    j->type= AM_CONST;
 
1084
  }
 
1085
  else
 
1086
    j->type= AM_EQ_REF;
 
1087
  return 0;
 
1088
}
6723
1089
 
6724
1090
/**
6725
1091
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6734
1100
    add "t1.field IS NOT NULL" to t1's table condition. @n
6735
1101
 
6736
1102
    Description of the optimization:
6737
 
    
 
1103
 
6738
1104
      We look through equalities choosen to perform ref/eq_ref access,
6739
1105
      pick equalities that have form "tbl.part_of_key = othertbl.field"
6740
1106
      (where othertbl is a non-const table and othertbl.field may be NULL)
6762
1128
      This optimization doesn't affect the choices that ref, range, or join
6763
1129
      optimizer make. This was intentional because this was added after 4.1
6764
1130
      was GA.
6765
 
      
 
1131
 
6766
1132
    Implementation overview
6767
1133
      1. update_ref_and_keys() accumulates info about null-rejecting
6768
 
         predicates in in KEY_FIELD::null_rejecting
6769
 
      1.1 add_key_part saves these to KEYUSE.
6770
 
      2. create_ref_for_key copies them to TABLE_REF.
 
1134
         predicates in in KeyField::null_rejecting
 
1135
      1.1 add_key_part saves these to KeyUse.
 
1136
      2. create_ref_for_key copies them to table_reference_st.
6771
1137
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6772
 
         appropiate JOIN_TAB members.
 
1138
         appropiate JoinTable members.
6773
1139
*/
6774
 
 
6775
 
static void add_not_null_conds(JOIN *join)
 
1140
void add_not_null_conds(JOIN *join)
6776
1141
{
6777
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
1142
  for (uint32_t i= join->const_tables; i < join->tables; i++)
6778
1143
  {
6779
 
    JOIN_TAB *tab=join->join_tab+i;
6780
 
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6781
 
         tab->type == JT_REF_OR_NULL) &&
 
1144
    JoinTable *tab=join->join_tab+i;
 
1145
    if ((tab->type == AM_REF || tab->type == AM_EQ_REF ||
 
1146
         tab->type == AM_REF_OR_NULL) &&
6782
1147
        !tab->table->maybe_null)
6783
1148
    {
6784
 
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
1149
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
6785
1150
      {
6786
1151
        if (tab->ref.null_rejecting & (1 << keypart))
6787
1152
        {
6789
1154
          Item *notnull;
6790
1155
          assert(item->type() == Item::FIELD_ITEM);
6791
1156
          Item_field *not_null_item= (Item_field*)item;
6792
 
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
1157
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
6793
1158
          /*
6794
1159
            For UPDATE queries such as:
6795
1160
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
6801
1166
            return;
6802
1167
          /*
6803
1168
            We need to do full fix_fields() call here in order to have correct
6804
 
            notnull->const_item(). This is needed e.g. by test_quick_select 
6805
 
            when it is called from make_join_select after this function is 
 
1169
            notnull->const_item(). This is needed e.g. by test_quick_select
 
1170
            when it is called from make_join_select after this function is
6806
1171
            called.
6807
1172
          */
6808
 
          if (notnull->fix_fields(join->thd, &notnull))
 
1173
          if (notnull->fix_fields(join->session, &notnull))
6809
1174
            return;
6810
1175
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6811
1176
        }
6830
1195
    -  pointer to the guarded predicate, if success
6831
1196
    -  0, otherwise
6832
1197
*/
6833
 
 
6834
 
static COND*
6835
 
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
 
1198
COND *add_found_match_trig_cond(JoinTable *tab, COND *cond, JoinTable *root_tab)
6836
1199
{
6837
1200
  COND *tmp;
6838
1201
  assert(cond != 0);
6848
1211
  return tmp;
6849
1212
}
6850
1213
 
6851
 
 
6852
 
/**
6853
 
  Fill in outer join related info for the execution plan structure.
6854
 
 
6855
 
    For each outer join operation left after simplification of the
6856
 
    original query the function set up the following pointers in the linear
6857
 
    structure join->join_tab representing the selected execution plan.
6858
 
    The first inner table t0 for the operation is set to refer to the last
6859
 
    inner table tk through the field t0->last_inner.
6860
 
    Any inner table ti for the operation are set to refer to the first
6861
 
    inner table ti->first_inner.
6862
 
    The first inner table t0 for the operation is set to refer to the
6863
 
    first inner table of the embedding outer join operation, if there is any,
6864
 
    through the field t0->first_upper.
6865
 
    The on expression for the outer join operation is attached to the
6866
 
    corresponding first inner table through the field t0->on_expr_ref.
6867
 
    Here ti are structures of the JOIN_TAB type.
6868
 
 
6869
 
  EXAMPLE. For the query: 
6870
 
  @code
6871
 
        SELECT * FROM t1
6872
 
                      LEFT JOIN
6873
 
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6874
 
                      ON (t1.a=t2.a AND t1.b=t3.b)
6875
 
          WHERE t1.c > 5,
6876
 
  @endcode
6877
 
 
6878
 
    given the execution plan with the table order t1,t2,t3,t4
6879
 
    is selected, the following references will be set;
6880
 
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6881
 
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6882
 
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
6883
 
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6884
 
 
6885
 
  @param join   reference to the info fully describing the query
6886
 
 
6887
 
  @note
6888
 
    The function assumes that the simplification procedure has been
6889
 
    already applied to the join query (see simplify_joins).
6890
 
    This function can be called only after the execution plan
6891
 
    has been chosen.
6892
 
*/
6893
 
 
6894
 
static void
6895
 
make_outerjoin_info(JOIN *join)
6896
 
{
6897
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
6898
 
  {
6899
 
    JOIN_TAB *tab=join->join_tab+i;
6900
 
    TABLE *table=tab->table;
6901
 
    TABLE_LIST *tbl= table->pos_in_table_list;
6902
 
    TABLE_LIST *embedding= tbl->embedding;
6903
 
 
6904
 
    if (tbl->outer_join)
6905
 
    {
6906
 
      /* 
6907
 
        Table tab is the only one inner table for outer join.
6908
 
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6909
 
        is in the query above.)
6910
 
      */
6911
 
      tab->last_inner= tab->first_inner= tab;
6912
 
      tab->on_expr_ref= &tbl->on_expr;
6913
 
      tab->cond_equal= tbl->cond_equal;
6914
 
      if (embedding)
6915
 
        tab->first_upper= embedding->nested_join->first_nested;
6916
 
    }    
6917
 
    for ( ; embedding ; embedding= embedding->embedding)
6918
 
    {
6919
 
      /* Ignore sj-nests: */
6920
 
      if (!embedding->on_expr)
6921
 
        continue;
6922
 
      NESTED_JOIN *nested_join= embedding->nested_join;
6923
 
      if (!nested_join->counter_)
6924
 
      {
6925
 
        /* 
6926
 
          Table tab is the first inner table for nested_join.
6927
 
          Save reference to it in the nested join structure.
6928
 
        */ 
6929
 
        nested_join->first_nested= tab;
6930
 
        tab->on_expr_ref= &embedding->on_expr;
6931
 
        tab->cond_equal= tbl->cond_equal;
6932
 
        if (embedding->embedding)
6933
 
          tab->first_upper= embedding->embedding->nested_join->first_nested;
6934
 
      }
6935
 
      if (!tab->first_inner)  
6936
 
        tab->first_inner= nested_join->first_nested;
6937
 
      if (++nested_join->counter_ < nested_join->join_list.elements)
6938
 
        break;
6939
 
      /* Table tab is the last inner table for nested join. */
6940
 
      nested_join->first_nested->last_inner= tab;
6941
 
    }
6942
 
  }
6943
 
  return;
6944
 
}
6945
 
 
6946
 
 
6947
 
static bool
6948
 
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6949
 
{
6950
 
  THD *thd= join->thd;
6951
 
  if (select)
6952
 
  {
6953
 
    add_not_null_conds(join);
6954
 
    table_map used_tables;
6955
 
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6956
 
    {                        /* there may be a select without a cond. */    
6957
 
      if (join->tables > 1)
6958
 
        cond->update_used_tables();             // Tablenr may have changed
6959
 
      if (join->const_tables == join->tables &&
6960
 
          thd->lex->current_select->master_unit() ==
6961
 
          &thd->lex->unit)              // not upper level SELECT
6962
 
        join->const_table_map|=RAND_TABLE_BIT;
6963
 
      {                                         // Check const tables
6964
 
        COND *const_cond=
6965
 
          make_cond_for_table(cond,
6966
 
                              join->const_table_map,
6967
 
                              (table_map) 0, 1);
6968
 
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6969
 
             tab < join->join_tab+join->tables ; tab++)
6970
 
        {
6971
 
          if (*tab->on_expr_ref)
6972
 
          {
6973
 
            JOIN_TAB *cond_tab= tab->first_inner;
6974
 
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6975
 
                                           join->const_table_map,
6976
 
                                           (  table_map) 0, 0);
6977
 
            if (!tmp)
6978
 
              continue;
6979
 
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6980
 
            if (!tmp)
6981
 
              return(1);
6982
 
            tmp->quick_fix_field();
6983
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6984
 
                                    new Item_cond_and(cond_tab->select_cond,
6985
 
                                                      tmp);
6986
 
            if (!cond_tab->select_cond)
6987
 
              return(1);
6988
 
            cond_tab->select_cond->quick_fix_field();
6989
 
          }       
6990
 
        }
6991
 
        if (const_cond && !const_cond->val_int())
6992
 
        {
6993
 
          return(1);     // Impossible const condition
6994
 
        }
6995
 
      }
6996
 
    }
6997
 
    used_tables=((select->const_tables=join->const_table_map) |
6998
 
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6999
 
    for (uint i=join->const_tables ; i < join->tables ; i++)
7000
 
    {
7001
 
      JOIN_TAB *tab=join->join_tab+i;
7002
 
      /*
7003
 
        first_inner is the X in queries like:
7004
 
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
7005
 
      */
7006
 
      JOIN_TAB *first_inner_tab= tab->first_inner; 
7007
 
      table_map current_map= tab->table->map;
7008
 
      bool use_quick_range=0;
7009
 
      COND *tmp;
7010
 
 
7011
 
      /*
7012
 
        Following force including random expression in last table condition.
7013
 
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7014
 
      */
7015
 
      if (i == join->tables-1)
7016
 
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7017
 
      used_tables|=current_map;
7018
 
 
7019
 
      if (tab->type == JT_REF && tab->quick &&
7020
 
          (uint) tab->ref.key == tab->quick->index &&
7021
 
          tab->ref.key_length < tab->quick->max_used_key_length)
7022
 
      {
7023
 
        /* Range uses longer key;  Use this instead of ref on key */
7024
 
        tab->type=JT_ALL;
7025
 
        use_quick_range=1;
7026
 
        tab->use_quick=1;
7027
 
        tab->ref.key= -1;
7028
 
        tab->ref.key_parts=0;           // Don't use ref key.
7029
 
        join->best_positions[i].records_read= rows2double(tab->quick->records);
7030
 
        /* 
7031
 
          We will use join cache here : prevent sorting of the first
7032
 
          table only and sort at the end.
7033
 
        */
7034
 
        if (i != join->const_tables && join->tables > join->const_tables + 1)
7035
 
          join->full_join= 1;
7036
 
      }
7037
 
 
7038
 
      tmp= NULL;
7039
 
      if (cond)
7040
 
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7041
 
      if (cond && !tmp && tab->quick)
7042
 
      {                                         // Outer join
7043
 
        if (tab->type != JT_ALL)
7044
 
        {
7045
 
          /*
7046
 
            Don't use the quick method
7047
 
            We come here in the case where we have 'key=constant' and
7048
 
            the test is removed by make_cond_for_table()
7049
 
          */
7050
 
          delete tab->quick;
7051
 
          tab->quick= 0;
7052
 
        }
7053
 
        else
7054
 
        {
7055
 
          /*
7056
 
            Hack to handle the case where we only refer to a table
7057
 
            in the ON part of an OUTER JOIN. In this case we want the code
7058
 
            below to check if we should use 'quick' instead.
7059
 
          */
7060
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
7061
 
        }
7062
 
 
7063
 
      }
7064
 
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7065
 
          tab->type == JT_EQ_REF)
7066
 
      {
7067
 
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7068
 
                                       thd->memdup((uchar*) select,
7069
 
                                                   sizeof(*select)));
7070
 
        if (!sel)
7071
 
          return(1);                    // End of memory
7072
 
        /*
7073
 
          If tab is an inner table of an outer join operation,
7074
 
          add a match guard to the pushed down predicate.
7075
 
          The guard will turn the predicate on only after
7076
 
          the first match for outer tables is encountered.
7077
 
        */        
7078
 
        if (cond && tmp)
7079
 
        {
7080
 
          /*
7081
 
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7082
 
            a cond, so neutralize the hack above.
7083
 
          */
7084
 
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7085
 
            return(1);
7086
 
          tab->select_cond=sel->cond=tmp;
7087
 
          /* Push condition to storage engine if this is enabled
7088
 
             and the condition is not guarded */
7089
 
          tab->table->file->pushed_cond= NULL;
7090
 
          if (thd->variables.engine_condition_pushdown)
7091
 
          {
7092
 
            COND *push_cond= 
7093
 
              make_cond_for_table(tmp, current_map, current_map, 0);
7094
 
            if (push_cond)
7095
 
            {
7096
 
              /* Push condition to handler */
7097
 
              if (!tab->table->file->cond_push(push_cond))
7098
 
                tab->table->file->pushed_cond= push_cond;
7099
 
            }
7100
 
          }
7101
 
        }
7102
 
        else
7103
 
          tab->select_cond= sel->cond= NULL;
7104
 
 
7105
 
        sel->head=tab->table;
7106
 
        if (tab->quick)
7107
 
        {
7108
 
          /* Use quick key read if it's a constant and it's not used
7109
 
             with key reading */
7110
 
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7111
 
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7112
 
          {
7113
 
            sel->quick=tab->quick;              // Use value from get_quick_...
7114
 
            sel->quick_keys.clear_all();
7115
 
            sel->needed_reg.clear_all();
7116
 
          }
7117
 
          else
7118
 
          {
7119
 
            delete tab->quick;
7120
 
          }
7121
 
          tab->quick=0;
7122
 
        }
7123
 
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7124
 
        if (i == join->const_tables && ref_key)
7125
 
        {
7126
 
          if (!tab->const_keys.is_clear_all() &&
7127
 
              tab->table->reginfo.impossible_range)
7128
 
            return(1);
7129
 
        }
7130
 
        else if (tab->type == JT_ALL && ! use_quick_range)
7131
 
        {
7132
 
          if (!tab->const_keys.is_clear_all() &&
7133
 
              tab->table->reginfo.impossible_range)
7134
 
            return(1);                          // Impossible range
7135
 
          /*
7136
 
            We plan to scan all rows.
7137
 
            Check again if we should use an index.
7138
 
            We could have used an column from a previous table in
7139
 
            the index if we are using limit and this is the first table
7140
 
          */
7141
 
 
7142
 
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7143
 
              (!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)))
7144
 
          {
7145
 
            /* Join with outer join condition */
7146
 
            COND *orig_cond=sel->cond;
7147
 
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7148
 
 
7149
 
            /*
7150
 
              We can't call sel->cond->fix_fields,
7151
 
              as it will break tab->on_expr if it's AND condition
7152
 
              (fix_fields currently removes extra AND/OR levels).
7153
 
              Yet attributes of the just built condition are not needed.
7154
 
              Thus we call sel->cond->quick_fix_field for safety.
7155
 
            */
7156
 
            if (sel->cond && !sel->cond->fixed)
7157
 
              sel->cond->quick_fix_field();
7158
 
 
7159
 
            if (sel->test_quick_select(thd, tab->keys,
7160
 
                                       used_tables & ~ current_map,
7161
 
                                       (join->select_options &
7162
 
                                        OPTION_FOUND_ROWS ?
7163
 
                                        HA_POS_ERROR :
7164
 
                                        join->unit->select_limit_cnt), 0,
7165
 
                                        false) < 0)
7166
 
            {
7167
 
              /*
7168
 
                Before reporting "Impossible WHERE" for the whole query
7169
 
                we have to check isn't it only "impossible ON" instead
7170
 
              */
7171
 
              sel->cond=orig_cond;
7172
 
              if (!*tab->on_expr_ref ||
7173
 
                  sel->test_quick_select(thd, tab->keys,
7174
 
                                         used_tables & ~ current_map,
7175
 
                                         (join->select_options &
7176
 
                                          OPTION_FOUND_ROWS ?
7177
 
                                          HA_POS_ERROR :
7178
 
                                          join->unit->select_limit_cnt),0,
7179
 
                                          false) < 0)
7180
 
                return(1);                      // Impossible WHERE
7181
 
            }
7182
 
            else
7183
 
              sel->cond=orig_cond;
7184
 
 
7185
 
            /* Fix for EXPLAIN */
7186
 
            if (sel->quick)
7187
 
              join->best_positions[i].records_read= (double)sel->quick->records;
7188
 
          }
7189
 
          else
7190
 
          {
7191
 
            sel->needed_reg=tab->needed_reg;
7192
 
            sel->quick_keys.clear_all();
7193
 
          }
7194
 
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7195
 
              !sel->needed_reg.is_subset(tab->checked_keys))
7196
 
          {
7197
 
            tab->keys=sel->quick_keys;
7198
 
            tab->keys.merge(sel->needed_reg);
7199
 
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7200
 
                             (select->quick_keys.is_clear_all() ||
7201
 
                              (select->quick &&
7202
 
                               (select->quick->records >= 100L)))) ?
7203
 
              2 : 1;
7204
 
            sel->read_tables= used_tables & ~current_map;
7205
 
          }
7206
 
          if (i != join->const_tables && tab->use_quick != 2)
7207
 
          {                                     /* Read with cache */
7208
 
            if (cond &&
7209
 
                (tmp=make_cond_for_table(cond,
7210
 
                                         join->const_table_map |
7211
 
                                         current_map,
7212
 
                                         current_map, 0)))
7213
 
            {
7214
 
              tab->cache.select=(SQL_SELECT*)
7215
 
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7216
 
              tab->cache.select->cond=tmp;
7217
 
              tab->cache.select->read_tables=join->const_table_map;
7218
 
            }
7219
 
          }
7220
 
        }
7221
 
      }
7222
 
      
7223
 
      /* 
7224
 
        Push down conditions from all on expressions.
7225
 
        Each of these conditions are guarded by a variable
7226
 
        that turns if off just before null complemented row for
7227
 
        outer joins is formed. Thus, the condition from an
7228
 
        'on expression' are guaranteed not to be checked for
7229
 
        the null complemented row.
7230
 
      */ 
7231
 
 
7232
 
      /* First push down constant conditions from on expressions */
7233
 
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7234
 
           join_tab < join->join_tab+join->tables ; join_tab++)
7235
 
      {
7236
 
        if (*join_tab->on_expr_ref)
7237
 
        {
7238
 
          JOIN_TAB *cond_tab= join_tab->first_inner;
7239
 
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7240
 
                                         join->const_table_map,
7241
 
                                         (table_map) 0, 0);
7242
 
          if (!tmp)
7243
 
            continue;
7244
 
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7245
 
          if (!tmp)
7246
 
            return(1);
7247
 
          tmp->quick_fix_field();
7248
 
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7249
 
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7250
 
          if (!cond_tab->select_cond)
7251
 
            return(1);
7252
 
          cond_tab->select_cond->quick_fix_field();
7253
 
        }       
7254
 
      }
7255
 
 
7256
 
      /* Push down non-constant conditions from on expressions */
7257
 
      JOIN_TAB *last_tab= tab;
7258
 
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7259
 
      {  
7260
 
        /* 
7261
 
          Table tab is the last inner table of an outer join.
7262
 
          An on expression is always attached to it.
7263
 
        */     
7264
 
        COND *on_expr= *first_inner_tab->on_expr_ref;
7265
 
 
7266
 
        table_map used_tables2= (join->const_table_map |
7267
 
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7268
 
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7269
 
        {
7270
 
          current_map= tab->table->map;
7271
 
          used_tables2|= current_map;
7272
 
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7273
 
                                              current_map, 0);
7274
 
          if (tmp_cond)
7275
 
          {
7276
 
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7277
 
            /*
7278
 
              First add the guards for match variables of
7279
 
              all embedding outer join operations.
7280
 
            */
7281
 
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7282
 
                                                     tmp_cond,
7283
 
                                                     first_inner_tab)))
7284
 
              return(1);
7285
 
            /* 
7286
 
              Now add the guard turning the predicate off for 
7287
 
              the null complemented row.
7288
 
            */ 
7289
 
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7290
 
                                              &first_inner_tab->
7291
 
                                              not_null_compl);
7292
 
            if (tmp_cond)
7293
 
              tmp_cond->quick_fix_field();
7294
 
            /* Add the predicate to other pushed down predicates */
7295
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7296
 
                                  new Item_cond_and(cond_tab->select_cond,
7297
 
                                                    tmp_cond);
7298
 
            if (!cond_tab->select_cond)
7299
 
              return(1);
7300
 
            cond_tab->select_cond->quick_fix_field();
7301
 
          }              
7302
 
        }
7303
 
        first_inner_tab= first_inner_tab->first_upper;       
7304
 
      }
7305
 
    }
7306
 
  }
7307
 
  return(0);
7308
 
}
7309
 
 
7310
 
 
7311
 
/* 
 
1214
/*
7312
1215
  Check if given expression uses only table fields covered by the given index
7313
1216
 
7314
1217
  SYNOPSIS
7321
1224
  DESCRIPTION
7322
1225
    Check if given expression only uses fields covered by index #keyno in the
7323
1226
    table tbl. The expression can use any fields in any other tables.
7324
 
    
7325
 
    The expression is guaranteed not to be AND or OR - those constructs are 
 
1227
 
 
1228
    The expression is guaranteed not to be AND or OR - those constructs are
7326
1229
    handled outside of this function.
7327
1230
 
7328
1231
  RETURN
7329
1232
    true   Yes
7330
1233
    false  No
7331
1234
*/
7332
 
 
7333
 
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
7334
 
                            bool other_tbls_ok)
 
1235
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
7335
1236
{
7336
1237
  if (item->const_item())
7337
1238
    return true;
7338
1239
 
7339
 
  /* 
7340
 
    Don't push down the triggered conditions. Nested outer joins execution 
 
1240
  /*
 
1241
    Don't push down the triggered conditions. Nested outer joins execution
7341
1242
    code may need to evaluate a condition several times (both triggered and
7342
1243
    untriggered), and there is no way to put thi
7343
1244
    TODO: Consider cloning the triggered condition and using the copies for:
7344
1245
      1. push the first copy down, to have most restrictive index condition
7345
1246
         possible
7346
 
      2. Put the second copy into tab->select_cond. 
 
1247
      2. Put the second copy into tab->select_cond.
7347
1248
  */
7348
 
  if (item->type() == Item::FUNC_ITEM && 
 
1249
  if (item->type() == Item::FUNC_ITEM &&
7349
1250
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7350
1251
    return false;
7351
1252
 
7371
1272
    {
7372
1273
      /* This is a function, apply condition recursively to arguments */
7373
1274
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7374
 
      Item *item;
7375
 
      while ((item=li++))
 
1275
      Item *list_item;
 
1276
      while ((list_item=li++))
7376
1277
      {
7377
1278
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7378
1279
          return false;
7382
1283
  case Item::FIELD_ITEM:
7383
1284
    {
7384
1285
      Item_field *item_field= (Item_field*)item;
7385
 
      if (item_field->field->table != tbl) 
 
1286
      if (item_field->field->table != tbl)
7386
1287
        return true;
7387
 
      return item_field->field->part_of_key.is_set(keyno);
 
1288
      return item_field->field->part_of_key.test(keyno);
7388
1289
    }
7389
1290
  case Item::REF_ITEM:
7390
1291
    return uses_index_fields_only(item->real_item(), tbl, keyno,
7394
1295
  }
7395
1296
}
7396
1297
 
7397
 
 
7398
1298
#define ICP_COND_USES_INDEX_ONLY 10
7399
1299
 
7400
1300
/*
7409
1309
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7410
1310
 
7411
1311
  DESCRIPTION
7412
 
    Get a part of the condition that can be checked when for the given table 
 
1312
    Get a part of the condition that can be checked when for the given table
7413
1313
    we have values only of fields covered by some index. The condition may
7414
 
    refer to other tables, it is assumed that we have values of all of their 
 
1314
    refer to other tables, it is assumed that we have values of all of their
7415
1315
    fields.
7416
1316
 
7417
1317
    Example:
7418
1318
      make_cond_for_index(
7419
1319
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7420
 
          t2, keyno(t2.key1)) 
 
1320
          t2, keyno(t2.key1))
7421
1321
      will return
7422
1322
        "cond(t1.field) AND cond(t2.key2)"
7423
1323
 
7424
1324
  RETURN
7425
1325
    Index condition, or NULL if no condition could be inferred.
7426
1326
*/
7427
 
 
7428
 
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7429
 
                          bool other_tbls_ok)
 
1327
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
7430
1328
{
7431
1329
  if (!cond)
7432
1330
    return NULL;
7433
1331
  if (cond->type() == Item::COND_ITEM)
7434
1332
  {
7435
 
    uint n_marked= 0;
 
1333
    uint32_t n_marked= 0;
7436
1334
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7437
1335
    {
7438
1336
      Item_cond_and *new_cond=new Item_cond_and;
7439
1337
      if (!new_cond)
7440
 
        return (COND*) 0;
 
1338
        return (COND*) 0;
7441
1339
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7442
1340
      Item *item;
7443
1341
      while ((item=li++))
7444
1342
      {
7445
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7446
 
        if (fix)
7447
 
          new_cond->argument_list()->push_back(fix);
 
1343
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1344
        if (fix)
 
1345
          new_cond->argument_list()->push_back(fix);
7448
1346
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7449
1347
      }
7450
1348
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7451
1349
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7452
1350
      switch (new_cond->argument_list()->elements) {
7453
1351
      case 0:
7454
 
        return (COND*) 0;
 
1352
        return (COND*) 0;
7455
1353
      case 1:
7456
 
        return new_cond->argument_list()->head();
 
1354
        return new_cond->argument_list()->head();
7457
1355
      default:
7458
 
        new_cond->quick_fix_field();
7459
 
        return new_cond;
 
1356
        new_cond->quick_fix_field();
 
1357
        return new_cond;
7460
1358
      }
7461
1359
    }
7462
1360
    else /* It's OR */
7463
1361
    {
7464
1362
      Item_cond_or *new_cond=new Item_cond_or;
7465
1363
      if (!new_cond)
7466
 
        return (COND*) 0;
 
1364
        return (COND*) 0;
7467
1365
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7468
1366
      Item *item;
7469
1367
      while ((item=li++))
7470
1368
      {
7471
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7472
 
        if (!fix)
7473
 
          return (COND*) 0;
7474
 
        new_cond->argument_list()->push_back(fix);
 
1369
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
1370
        if (!fix)
 
1371
          return (COND*) 0;
 
1372
        new_cond->argument_list()->push_back(fix);
7475
1373
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7476
1374
      }
7477
1375
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7489
1387
}
7490
1388
 
7491
1389
 
7492
 
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
1390
static Item *make_cond_remainder(Item *cond, bool exclude_index)
7493
1391
{
7494
1392
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7495
1393
    return 0; /* Already checked */
7502
1400
      /* Create new top level AND item */
7503
1401
      Item_cond_and *new_cond=new Item_cond_and;
7504
1402
      if (!new_cond)
7505
 
        return (COND*) 0;
 
1403
        return (COND*) 0;
7506
1404
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7507
1405
      Item *item;
7508
1406
      while ((item=li++))
7509
1407
      {
7510
 
        Item *fix= make_cond_remainder(item, exclude_index);
7511
 
        if (fix)
 
1408
        Item *fix= make_cond_remainder(item, exclude_index);
 
1409
        if (fix)
7512
1410
        {
7513
 
          new_cond->argument_list()->push_back(fix);
 
1411
          new_cond->argument_list()->push_back(fix);
7514
1412
          tbl_map |= fix->used_tables();
7515
1413
        }
7516
1414
      }
7517
1415
      switch (new_cond->argument_list()->elements) {
7518
1416
      case 0:
7519
 
        return (COND*) 0;
 
1417
        return (COND*) 0;
7520
1418
      case 1:
7521
 
        return new_cond->argument_list()->head();
 
1419
        return new_cond->argument_list()->head();
7522
1420
      default:
7523
 
        new_cond->quick_fix_field();
 
1421
        new_cond->quick_fix_field();
7524
1422
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7525
 
        return new_cond;
 
1423
        return new_cond;
7526
1424
      }
7527
1425
    }
7528
1426
    else /* It's OR */
7529
1427
    {
7530
1428
      Item_cond_or *new_cond=new Item_cond_or;
7531
1429
      if (!new_cond)
7532
 
        return (COND*) 0;
 
1430
        return (COND*) 0;
7533
1431
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7534
1432
      Item *item;
7535
1433
      while ((item=li++))
7536
1434
      {
7537
 
        Item *fix= make_cond_remainder(item, false);
7538
 
        if (!fix)
7539
 
          return (COND*) 0;
7540
 
        new_cond->argument_list()->push_back(fix);
 
1435
        Item *fix= make_cond_remainder(item, false);
 
1436
        if (!fix)
 
1437
          return (COND*) 0;
 
1438
        new_cond->argument_list()->push_back(fix);
7541
1439
        tbl_map |= fix->used_tables();
7542
1440
      }
7543
1441
      new_cond->quick_fix_field();
7549
1447
  return cond;
7550
1448
}
7551
1449
 
7552
 
 
7553
 
/*
7554
 
  Try to extract and push the index condition
7555
 
 
7556
 
  SYNOPSIS
7557
 
    push_index_cond()
7558
 
      tab            A join tab that has tab->table->file and its condition
7559
 
                     in tab->select_cond
7560
 
      keyno          Index for which extract and push the condition
7561
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7562
 
 
7563
 
  DESCRIPTION
7564
 
    Try to extract and push the index condition down to table handler
7565
 
*/
7566
 
 
7567
 
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7568
 
{
7569
 
  Item *idx_cond;
7570
 
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7571
 
      tab->join->thd->variables.engine_condition_pushdown)
7572
 
  {
7573
 
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7574
 
                                  other_tbls_ok);
7575
 
 
7576
 
    if (idx_cond)
7577
 
    {
7578
 
      tab->pre_idx_push_select_cond= tab->select_cond;
7579
 
      Item *idx_remainder_cond= 
7580
 
        tab->table->file->idx_cond_push(keyno, idx_cond);
7581
 
 
7582
 
      /*
7583
 
        Disable eq_ref's "lookup cache" if we've pushed down an index
7584
 
        condition. 
7585
 
        TODO: This check happens to work on current ICP implementations, but
7586
 
        there may exist a compliant implementation that will not work 
7587
 
        correctly with it. Sort this out when we stabilize the condition
7588
 
        pushdown APIs.
7589
 
      */
7590
 
      if (idx_remainder_cond != idx_cond)
7591
 
        tab->ref.disable_cache= true;
7592
 
 
7593
 
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
7594
 
 
7595
 
      if (row_cond)
7596
 
      {
7597
 
        if (!idx_remainder_cond)
7598
 
          tab->select_cond= row_cond;
7599
 
        else
7600
 
        {
7601
 
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7602
 
          tab->select_cond->quick_fix_field();
7603
 
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
7604
 
            row_cond->used_tables() | idx_remainder_cond->used_tables();
7605
 
        }
7606
 
      }
7607
 
      else
7608
 
        tab->select_cond= idx_remainder_cond;
7609
 
      if (tab->select)
7610
 
      {
7611
 
        tab->select->cond= tab->select_cond;
7612
 
      }
7613
 
    }
7614
 
  }
7615
 
  return;
7616
 
}
7617
 
 
7618
 
 
7619
 
 
7620
 
    /*
7621
 
      Determine if the set is already ordered for ORDER BY, so it can 
7622
 
      disable join cache because it will change the ordering of the results.
7623
 
      Code handles sort table that is at any location (not only first after 
7624
 
      the const tables) despite the fact that it's currently prohibited.
7625
 
      We must disable join cache if the first non-const table alone is
7626
 
      ordered. If there is a temp table the ordering is done as a last
7627
 
      operation and doesn't prevent join cache usage.
7628
 
    */
7629
 
uint make_join_orderinfo(JOIN *join)
7630
 
{
7631
 
  uint i;
7632
 
  if (join->need_tmp)
7633
 
    return join->tables;
7634
 
 
7635
 
  for (i=join->const_tables ; i < join->tables ; i++)
7636
 
  {
7637
 
    JOIN_TAB *tab=join->join_tab+i;
7638
 
    TABLE *table=tab->table;
7639
 
    if ((table == join->sort_by_table && 
7640
 
         (!join->order || join->skip_sort_order)) ||
7641
 
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7642
 
    {
7643
 
      break;
7644
 
    }
7645
 
  }
7646
 
  return i;
7647
 
}
7648
 
 
7649
 
 
7650
 
/*
7651
 
  Plan refinement stage: do various set ups for the executioner
7652
 
 
7653
 
  SYNOPSIS
7654
 
    make_join_readinfo()
7655
 
      join           Join being processed
7656
 
      options        Join's options (checking for SELECT_DESCRIBE, 
7657
 
                     SELECT_NO_JOIN_CACHE)
7658
 
      no_jbuf_after  Don't use join buffering after table with this number.
7659
 
 
7660
 
  DESCRIPTION
7661
 
    Plan refinement stage: do various set ups for the executioner
7662
 
      - set up use of join buffering
7663
 
      - push index conditions
7664
 
      - increment counters
7665
 
      - etc
7666
 
 
7667
 
  RETURN 
7668
 
    false - OK
7669
 
    true  - Out of memory
7670
 
*/
7671
 
 
7672
 
static bool
7673
 
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
7674
 
{
7675
 
  uint i;
7676
 
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7677
 
  bool sorted= 1;
7678
 
 
7679
 
  for (i=join->const_tables ; i < join->tables ; i++)
7680
 
  {
7681
 
    JOIN_TAB *tab=join->join_tab+i;
7682
 
    TABLE *table=tab->table;
7683
 
    bool using_join_cache;
7684
 
    tab->read_record.table= table;
7685
 
    tab->read_record.file=table->file;
7686
 
    tab->next_select=sub_select;                /* normal select */
7687
 
    /* 
7688
 
      TODO: don't always instruct first table's ref/range access method to 
7689
 
      produce sorted output.
7690
 
    */
7691
 
    tab->sorted= sorted;
7692
 
    sorted= 0;                                  // only first must be sorted
7693
 
    if (tab->insideout_match_tab)
7694
 
    {
7695
 
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7696
 
                                                         [tab->index].
7697
 
                                                         key_length)))
7698
 
        return true;
7699
 
    }
7700
 
    switch (tab->type) {
7701
 
    case JT_SYSTEM:                             // Only happens with left join
7702
 
      table->status=STATUS_NO_RECORD;
7703
 
      tab->read_first_record= join_read_system;
7704
 
      tab->read_record.read_record= join_no_more_records;
7705
 
      break;
7706
 
    case JT_CONST:                              // Only happens with left join
7707
 
      table->status=STATUS_NO_RECORD;
7708
 
      tab->read_first_record= join_read_const;
7709
 
      tab->read_record.read_record= join_no_more_records;
7710
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7711
 
          !table->no_keyread)
7712
 
      {
7713
 
        table->key_read=1;
7714
 
        table->file->extra(HA_EXTRA_KEYREAD);
7715
 
      }
7716
 
      break;
7717
 
    case JT_EQ_REF:
7718
 
      table->status=STATUS_NO_RECORD;
7719
 
      if (tab->select)
7720
 
      {
7721
 
        delete tab->select->quick;
7722
 
        tab->select->quick=0;
7723
 
      }
7724
 
      delete tab->quick;
7725
 
      tab->quick=0;
7726
 
      tab->read_first_record= join_read_key;
7727
 
      tab->read_record.read_record= join_no_more_records;
7728
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7729
 
          !table->no_keyread)
7730
 
      {
7731
 
        table->key_read=1;
7732
 
        table->file->extra(HA_EXTRA_KEYREAD);
7733
 
      }
7734
 
      else
7735
 
        push_index_cond(tab, tab->ref.key, true);
7736
 
      break;
7737
 
    case JT_REF_OR_NULL:
7738
 
    case JT_REF:
7739
 
      table->status=STATUS_NO_RECORD;
7740
 
      if (tab->select)
7741
 
      {
7742
 
        delete tab->select->quick;
7743
 
        tab->select->quick=0;
7744
 
      }
7745
 
      delete tab->quick;
7746
 
      tab->quick=0;
7747
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7748
 
          !table->no_keyread)
7749
 
      {
7750
 
        table->key_read=1;
7751
 
        table->file->extra(HA_EXTRA_KEYREAD);
7752
 
      }
7753
 
      else
7754
 
        push_index_cond(tab, tab->ref.key, true);
7755
 
      if (tab->type == JT_REF)
7756
 
      {
7757
 
        tab->read_first_record= join_read_always_key;
7758
 
        tab->read_record.read_record= tab->insideout_match_tab? 
7759
 
           join_read_next_same_diff : join_read_next_same;
7760
 
      }
7761
 
      else
7762
 
      {
7763
 
        tab->read_first_record= join_read_always_key_or_null;
7764
 
        tab->read_record.read_record= join_read_next_same_or_null;
7765
 
      }
7766
 
      break;
7767
 
    case JT_ALL:
7768
 
      /*
7769
 
        If previous table use cache
7770
 
        If the incoming data set is already sorted don't use cache.
7771
 
      */
7772
 
      table->status=STATUS_NO_RECORD;
7773
 
      using_join_cache= false;
7774
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7775
 
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7776
 
          !tab->insideout_match_tab)
7777
 
      {
7778
 
        if ((options & SELECT_DESCRIBE) ||
7779
 
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
7780
 
                             i-join->const_tables))
7781
 
        {
7782
 
          using_join_cache= true;
7783
 
          tab[-1].next_select=sub_select_cache; /* Patch previous */
7784
 
        }
7785
 
      }
7786
 
      /* These init changes read_record */
7787
 
      if (tab->use_quick == 2)
7788
 
      {
7789
 
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7790
 
        tab->read_first_record= join_init_quick_read_record;
7791
 
        if (statistics)
7792
 
          status_var_increment(join->thd->status_var.select_range_check_count);
7793
 
      }
7794
 
      else
7795
 
      {
7796
 
        tab->read_first_record= join_init_read_record;
7797
 
        if (i == join->const_tables)
7798
 
        {
7799
 
          if (tab->select && tab->select->quick)
7800
 
          {
7801
 
            if (statistics)
7802
 
              status_var_increment(join->thd->status_var.select_range_count);
7803
 
          }
7804
 
          else
7805
 
          {
7806
 
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7807
 
            if (statistics)
7808
 
              status_var_increment(join->thd->status_var.select_scan_count);
7809
 
          }
7810
 
        }
7811
 
        else
7812
 
        {
7813
 
          if (tab->select && tab->select->quick)
7814
 
          {
7815
 
            if (statistics)
7816
 
              status_var_increment(join->thd->status_var.select_full_range_join_count);
7817
 
          }
7818
 
          else
7819
 
          {
7820
 
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7821
 
            if (statistics)
7822
 
              status_var_increment(join->thd->status_var.select_full_join_count);
7823
 
          }
7824
 
        }
7825
 
        if (!table->no_keyread)
7826
 
        {
7827
 
          if (tab->select && tab->select->quick &&
7828
 
              tab->select->quick->index != MAX_KEY && //not index_merge
7829
 
              table->covering_keys.is_set(tab->select->quick->index))
7830
 
          {
7831
 
            table->key_read=1;
7832
 
            table->file->extra(HA_EXTRA_KEYREAD);
7833
 
          }
7834
 
          else if (!table->covering_keys.is_clear_all() &&
7835
 
                   !(tab->select && tab->select->quick))
7836
 
          {                                     // Only read index tree
7837
 
            if (!tab->insideout_match_tab)
7838
 
            {
7839
 
              /*
7840
 
                See bug #26447: "Using the clustered index for a table scan
7841
 
                is always faster than using a secondary index".
7842
 
              */
7843
 
              if (table->s->primary_key != MAX_KEY &&
7844
 
                  table->file->primary_key_is_clustered())
7845
 
                tab->index= table->s->primary_key;
7846
 
              else
7847
 
                tab->index=find_shortest_key(table, & table->covering_keys);
7848
 
            }
7849
 
            tab->read_first_record= join_read_first;
7850
 
            tab->type=JT_NEXT;          // Read with index_first / index_next
7851
 
          }
7852
 
        }
7853
 
        if (tab->select && tab->select->quick &&
7854
 
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7855
 
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7856
 
      }
7857
 
      break;
7858
 
    default:
7859
 
      break;                                    /* purecov: deadcode */
7860
 
    case JT_UNKNOWN:
7861
 
    case JT_MAYBE_REF:
7862
 
      abort();                                  /* purecov: deadcode */
7863
 
    }
7864
 
  }
7865
 
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7866
 
  return(false);
7867
 
}
7868
 
 
7869
 
 
7870
 
/**
7871
 
  Give error if we some tables are done with a full join.
7872
 
 
7873
 
  This is used by multi_table_update and multi_table_delete when running
7874
 
  in safe mode.
7875
 
 
7876
 
  @param join           Join condition
7877
 
 
7878
 
  @retval
7879
 
    0   ok
7880
 
  @retval
7881
 
    1   Error (full join used)
7882
 
*/
7883
 
 
7884
 
bool error_if_full_join(JOIN *join)
7885
 
{
7886
 
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7887
 
       tab < end;
7888
 
       tab++)
7889
 
  {
7890
 
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7891
 
    {
7892
 
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7893
 
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7894
 
      return(1);
7895
 
    }
7896
 
  }
7897
 
  return(0);
7898
 
}
7899
 
 
7900
 
 
7901
 
/**
7902
 
  cleanup JOIN_TAB.
7903
 
*/
7904
 
 
7905
 
void JOIN_TAB::cleanup()
 
1450
/**
 
1451
  cleanup JoinTable.
 
1452
*/
 
1453
void JoinTable::cleanup()
7906
1454
{
7907
1455
  delete select;
7908
1456
  select= 0;
7909
1457
  delete quick;
7910
1458
  quick= 0;
7911
 
  x_free(cache.buff);
 
1459
  if (cache.buff)
 
1460
    free(cache.buff);
7912
1461
  cache.buff= 0;
7913
1462
  limit= 0;
7914
1463
  if (table)
7916
1465
    if (table->key_read)
7917
1466
    {
7918
1467
      table->key_read= 0;
7919
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
1468
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
7920
1469
    }
7921
 
    table->file->ha_index_or_rnd_end();
 
1470
    table->cursor->ha_index_or_rnd_end();
7922
1471
    /*
7923
1472
      We need to reset this for next select
7924
1473
      (Tested in part_of_refkey)
7928
1477
  end_read_record(&read_record);
7929
1478
}
7930
1479
 
7931
 
 
7932
 
/**
7933
 
  Partially cleanup JOIN after it has executed: close index or rnd read
7934
 
  (table cursors), free quick selects.
7935
 
 
7936
 
    This function is called in the end of execution of a JOIN, before the used
7937
 
    tables are unlocked and closed.
7938
 
 
7939
 
    For a join that is resolved using a temporary table, the first sweep is
7940
 
    performed against actual tables and an intermediate result is inserted
7941
 
    into the temprorary table.
7942
 
    The last sweep is performed against the temporary table. Therefore,
7943
 
    the base tables and associated buffers used to fill the temporary table
7944
 
    are no longer needed, and this function is called to free them.
7945
 
 
7946
 
    For a join that is performed without a temporary table, this function
7947
 
    is called after all rows are sent, but before EOF packet is sent.
7948
 
 
7949
 
    For a simple SELECT with no subqueries this function performs a full
7950
 
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
7951
 
    tables.
7952
 
 
7953
 
    If a JOIN is executed for a subquery or if it has a subquery, we can't
7954
 
    do the full cleanup and need to do a partial cleanup only.
7955
 
    - If a JOIN is not the top level join, we must not unlock the tables
7956
 
    because the outer select may not have been evaluated yet, and we
7957
 
    can't unlock only selected tables of a query.
7958
 
    - Additionally, if this JOIN corresponds to a correlated subquery, we
7959
 
    should not free quick selects and join buffers because they will be
7960
 
    needed for the next execution of the correlated subquery.
7961
 
    - However, if this is a JOIN for a [sub]select, which is not
7962
 
    a correlated subquery itself, but has subqueries, we can free it
7963
 
    fully and also free JOINs of all its subqueries. The exception
7964
 
    is a subquery in SELECT list, e.g: @n
7965
 
    SELECT a, (select max(b) from t1) group by c @n
7966
 
    This subquery will not be evaluated at first sweep and its value will
7967
 
    not be inserted into the temporary table. Instead, it's evaluated
7968
 
    when selecting from the temporary table. Therefore, it can't be freed
7969
 
    here even though it's not correlated.
7970
 
 
7971
 
  @todo
7972
 
    Unlock tables even if the join isn't top level select in the tree
7973
 
*/
7974
 
 
7975
 
void JOIN::join_free()
7976
 
{
7977
 
  SELECT_LEX_UNIT *tmp_unit;
7978
 
  SELECT_LEX *sl;
7979
 
  /*
7980
 
    Optimization: if not EXPLAIN and we are done with the JOIN,
7981
 
    free all tables.
7982
 
  */
7983
 
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
7984
 
  bool can_unlock= full;
7985
 
 
7986
 
  cleanup(full);
7987
 
 
7988
 
  for (tmp_unit= select_lex->first_inner_unit();
7989
 
       tmp_unit;
7990
 
       tmp_unit= tmp_unit->next_unit())
7991
 
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
7992
 
    {
7993
 
      Item_subselect *subselect= sl->master_unit()->item;
7994
 
      bool full_local= full && (!subselect || subselect->is_evaluated());
7995
 
      /*
7996
 
        If this join is evaluated, we can fully clean it up and clean up all
7997
 
        its underlying joins even if they are correlated -- they will not be
7998
 
        used any more anyway.
7999
 
        If this join is not yet evaluated, we still must clean it up to
8000
 
        close its table cursors -- it may never get evaluated, as in case of
8001
 
        ... HAVING false OR a IN (SELECT ...))
8002
 
        but all table cursors must be closed before the unlock.
8003
 
      */
8004
 
      sl->cleanup_all_joins(full_local);
8005
 
      /* Can't unlock if at least one JOIN is still needed */
8006
 
      can_unlock= can_unlock && full_local;
8007
 
    }
8008
 
 
8009
 
  /*
8010
 
    We are not using tables anymore
8011
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8012
 
  */
8013
 
  if (can_unlock && lock && thd->lock &&
8014
 
      !(select_options & SELECT_NO_UNLOCK) &&
8015
 
      !select_lex->subquery_in_having &&
8016
 
      (select_lex == (thd->lex->unit.fake_select_lex ?
8017
 
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
8018
 
  {
8019
 
    /*
8020
 
      TODO: unlock tables even if the join isn't top level select in the
8021
 
      tree.
8022
 
    */
8023
 
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
8024
 
    lock= 0;
8025
 
  }
8026
 
 
8027
 
  return;
8028
 
}
8029
 
 
8030
 
 
8031
 
/**
8032
 
  Free resources of given join.
8033
 
 
8034
 
  @param fill   true if we should free all resources, call with full==1
8035
 
                should be last, before it this function can be called with
8036
 
                full==0
8037
 
 
8038
 
  @note
8039
 
    With subquery this function definitely will be called several times,
8040
 
    but even for simple query it can be called several times.
8041
 
*/
8042
 
 
8043
 
void JOIN::cleanup(bool full)
8044
 
{
8045
 
  if (table)
8046
 
  {
8047
 
    JOIN_TAB *tab,*end;
8048
 
    /*
8049
 
      Only a sorted table may be cached.  This sorted table is always the
8050
 
      first non const table in join->table
8051
 
    */
8052
 
    if (tables > const_tables) // Test for not-const tables
8053
 
    {
8054
 
      free_io_cache(table[const_tables]);
8055
 
      filesort_free_buffers(table[const_tables],full);
8056
 
    }
8057
 
 
8058
 
    if (full)
8059
 
    {
8060
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8061
 
        tab->cleanup();
8062
 
      table= 0;
8063
 
    }
8064
 
    else
8065
 
    {
8066
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8067
 
      {
8068
 
        if (tab->table)
8069
 
          tab->table->file->ha_index_or_rnd_end();
8070
 
      }
8071
 
    }
8072
 
    cleanup_sj_tmp_tables(this);//
8073
 
  }
8074
 
  /*
8075
 
    We are not using tables anymore
8076
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8077
 
  */
8078
 
  if (full)
8079
 
  {
8080
 
    if (tmp_join)
8081
 
      tmp_table_param.copy_field= 0;
8082
 
    group_fields.delete_elements();
8083
 
    /*
8084
 
      We can't call delete_elements() on copy_funcs as this will cause
8085
 
      problems in free_elements() as some of the elements are then deleted.
8086
 
    */
8087
 
    tmp_table_param.copy_funcs.empty();
8088
 
    /*
8089
 
      If we have tmp_join and 'this' JOIN is not tmp_join and
8090
 
      tmp_table_param.copy_field's  of them are equal then we have to remove
8091
 
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
8092
 
      be removed in tmp_table_param.cleanup().
8093
 
    */
8094
 
    if (tmp_join &&
8095
 
        tmp_join != this &&
8096
 
        tmp_join->tmp_table_param.copy_field ==
8097
 
        tmp_table_param.copy_field)
8098
 
    {
8099
 
      tmp_join->tmp_table_param.copy_field=
8100
 
        tmp_join->tmp_table_param.save_copy_field= 0;
8101
 
    }
8102
 
    tmp_table_param.cleanup();
8103
 
  }
8104
 
  return;
8105
 
}
8106
 
 
8107
 
 
8108
 
/**
8109
 
  Remove the following expressions from ORDER BY and GROUP BY:
 
1480
bool only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
1481
{
 
1482
  for (JoinTable **tab=join->map2table ; tables ; tab++, tables>>=1)
 
1483
  {
 
1484
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
1485
      return 0;
 
1486
  }
 
1487
  return 1;
 
1488
}
 
1489
 
 
1490
/**
 
1491
  Remove the following expressions from order_st BY and GROUP BY:
8110
1492
  Constant expressions @n
8111
1493
  Expression that only uses tables that are of type EQ_REF and the reference
8112
 
  is in the ORDER list or if all refereed tables are of the above type.
 
1494
  is in the order_st list or if all refereed tables are of the above type.
8113
1495
 
8114
1496
  In the following, the X field can be removed:
8115
1497
  @code
8116
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
8117
 
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b ORDER BY t1.a,t3.X
 
1498
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t1.a,t2.X
 
1499
  SELECT * FROM t1,t2,t3 WHERE t1.a=t2.a AND t2.b=t3.b order_st BY t1.a,t3.X
8118
1500
  @endcode
8119
1501
 
8120
1502
  These can't be optimized:
8121
1503
  @code
8122
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
8123
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
8124
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.b,t1.a
 
1504
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.X,t1.a
 
1505
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b order_st BY t1.a,t2.c
 
1506
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
8125
1507
  @endcode
8126
1508
*/
8127
 
 
8128
 
static bool
8129
 
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
 
1509
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
8130
1510
{
8131
1511
  if (tab->cached_eq_ref_table)                 // If cached
8132
1512
    return tab->eq_ref_table;
8133
1513
  tab->cached_eq_ref_table=1;
8134
1514
  /* We can skip const tables only if not an outer table */
8135
 
  if (tab->type == JT_CONST && !tab->first_inner)
8136
 
    return (tab->eq_ref_table=1);               /* purecov: inspected */
8137
 
  if (tab->type != JT_EQ_REF || tab->table->maybe_null)
 
1515
  if (tab->type == AM_CONST && !tab->first_inner)
 
1516
    return (tab->eq_ref_table=1);
 
1517
  if (tab->type != AM_EQ_REF || tab->table->maybe_null)
8138
1518
    return (tab->eq_ref_table=0);               // We must use this
8139
1519
  Item **ref_item=tab->ref.items;
8140
1520
  Item **end=ref_item+tab->ref.key_parts;
8141
 
  uint found=0;
 
1521
  uint32_t found=0;
8142
1522
  table_map map=tab->table->map;
8143
1523
 
8144
1524
  for (; ref_item != end ; ref_item++)
8145
1525
  {
8146
1526
    if (! (*ref_item)->const_item())
8147
1527
    {                                           // Not a const ref
8148
 
      ORDER *order;
 
1528
      order_st *order;
8149
1529
      for (order=start_order ; order ; order=order->next)
8150
1530
      {
8151
 
        if ((*ref_item)->eq(order->item[0],0))
8152
 
          break;
 
1531
        if ((*ref_item)->eq(order->item[0],0))
 
1532
          break;
8153
1533
      }
8154
1534
      if (order)
8155
1535
      {
8156
 
        found++;
8157
 
        assert(!(order->used & map));
8158
 
        order->used|=map;
8159
 
        continue;                               // Used in ORDER BY
 
1536
        found++;
 
1537
        assert(!(order->used & map));
 
1538
        order->used|=map;
 
1539
        continue;                               // Used in order_st BY
8160
1540
      }
8161
1541
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8162
 
        return (tab->eq_ref_table=0);
 
1542
        return (tab->eq_ref_table= 0);
8163
1543
    }
8164
1544
  }
8165
1545
  /* Check that there was no reference to table before sort order */
8171
1551
      continue;
8172
1552
    }
8173
1553
    if (start_order->depend_map & map)
8174
 
      return (tab->eq_ref_table=0);
8175
 
  }
8176
 
  return tab->eq_ref_table=1;
8177
 
}
8178
 
 
8179
 
 
8180
 
static bool
8181
 
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8182
 
{
8183
 
  if (specialflag &  SPECIAL_SAFE_MODE)
8184
 
    return 0;                   // skip this optimize /* purecov: inspected */
8185
 
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8186
 
  {
8187
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8188
 
      return 0;
8189
 
  }
8190
 
  return 1;
8191
 
}
8192
 
 
8193
 
 
8194
 
/** Update the dependency map for the tables. */
8195
 
 
8196
 
static void update_depend_map(JOIN *join)
8197
 
{
8198
 
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8199
 
 
8200
 
  for (; join_tab != end ; join_tab++)
8201
 
  {
8202
 
    TABLE_REF *ref= &join_tab->ref;
8203
 
    table_map depend_map=0;
8204
 
    Item **item=ref->items;
8205
 
    uint i;
8206
 
    for (i=0 ; i < ref->key_parts ; i++,item++)
8207
 
      depend_map|=(*item)->used_tables();
8208
 
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8209
 
    depend_map&= ~OUTER_REF_TABLE_BIT;
8210
 
    for (JOIN_TAB **tab=join->map2table;
8211
 
         depend_map ;
8212
 
         tab++,depend_map>>=1 )
8213
 
    {
8214
 
      if (depend_map & 1)
8215
 
        ref->depend_map|=(*tab)->ref.depend_map;
8216
 
    }
8217
 
  }
8218
 
}
8219
 
 
8220
 
 
8221
 
/** Update the dependency map for the sort order. */
8222
 
 
8223
 
static void update_depend_map(JOIN *join, ORDER *order)
8224
 
{
8225
 
  for (; order ; order=order->next)
8226
 
  {
8227
 
    table_map depend_map;
8228
 
    order->item[0]->update_used_tables();
8229
 
    order->depend_map=depend_map=order->item[0]->used_tables();
8230
 
    // Not item_sum(), RAND() and no reference to table outside of sub select
8231
 
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8232
 
        && !order->item[0]->with_sum_func)
8233
 
    {
8234
 
      for (JOIN_TAB **tab=join->map2table;
8235
 
           depend_map ;
8236
 
           tab++, depend_map>>=1)
8237
 
      {
8238
 
        if (depend_map & 1)
8239
 
          order->depend_map|=(*tab)->ref.depend_map;
8240
 
      }
8241
 
    }
8242
 
  }
8243
 
}
8244
 
 
8245
 
 
8246
 
/**
8247
 
  Remove all constants and check if ORDER only contains simple
8248
 
  expressions.
8249
 
 
8250
 
  simple_order is set to 1 if sort_order only uses fields from head table
8251
 
  and the head table is not a LEFT JOIN table.
8252
 
 
8253
 
  @param join                   Join handler
8254
 
  @param first_order            List of SORT or GROUP order
8255
 
  @param cond                   WHERE statement
8256
 
  @param change_list            Set to 1 if we should remove things from list.
8257
 
                               If this is not set, then only simple_order is
8258
 
                               calculated.
8259
 
  @param simple_order           Set to 1 if we are only using simple expressions
8260
 
 
8261
 
  @return
8262
 
    Returns new sort order
8263
 
*/
8264
 
 
8265
 
static ORDER *
8266
 
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8267
 
             bool change_list, bool *simple_order)
8268
 
{
8269
 
  if (join->tables == join->const_tables)
8270
 
    return change_list ? 0 : first_order;               // No need to sort
8271
 
 
8272
 
  ORDER *order,**prev_ptr;
8273
 
  table_map first_table= join->join_tab[join->const_tables].table->map;
8274
 
  table_map not_const_tables= ~join->const_table_map;
8275
 
  table_map ref;
8276
 
 
8277
 
  prev_ptr= &first_order;
8278
 
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8279
 
 
8280
 
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8281
 
 
8282
 
  update_depend_map(join, first_order);
8283
 
  for (order=first_order; order ; order=order->next)
8284
 
  {
8285
 
    table_map order_tables=order->item[0]->used_tables();
8286
 
    if (order->item[0]->with_sum_func)
8287
 
      *simple_order=0;                          // Must do a temp table to sort
8288
 
    else if (!(order_tables & not_const_tables))
8289
 
    {
8290
 
      if (order->item[0]->with_subselect)
8291
 
        order->item[0]->val_str(&order->item[0]->str_value);
8292
 
      continue;                                 // skip const item
8293
 
    }
8294
 
    else
8295
 
    {
8296
 
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8297
 
        *simple_order=0;
8298
 
      else
8299
 
      {
8300
 
        Item *comp_item=0;
8301
 
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8302
 
        {
8303
 
          continue;
8304
 
        }
8305
 
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8306
 
        {
8307
 
          if (!(order_tables & first_table) &&
8308
 
              only_eq_ref_tables(join,first_order, ref))
8309
 
          {
8310
 
            continue;
8311
 
          }
8312
 
          *simple_order=0;                      // Must do a temp table to sort
8313
 
        }
8314
 
      }
8315
 
    }
8316
 
    if (change_list)
8317
 
      *prev_ptr= order;                         // use this entry
8318
 
    prev_ptr= &order->next;
8319
 
  }
8320
 
  if (change_list)
8321
 
    *prev_ptr=0;
8322
 
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8323
 
    *simple_order=1;
8324
 
  return(first_order);
8325
 
}
8326
 
 
8327
 
 
8328
 
static int
8329
 
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
8330
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
8331
 
                 const char *info, Item *having)
8332
 
{
8333
 
  if (select_options & SELECT_DESCRIBE)
8334
 
  {
8335
 
    select_describe(join, false, false, false, info);
8336
 
    return(0);
8337
 
  }
8338
 
 
8339
 
  join->join_free();
8340
 
 
8341
 
  if (send_row)
8342
 
  {
8343
 
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8344
 
      mark_as_null_row(table->table);           // All fields are NULL
8345
 
    if (having && having->val_int() == 0)
8346
 
      send_row=0;
8347
 
  }
8348
 
  if (!(result->send_fields(fields,
8349
 
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8350
 
  {
8351
 
    if (send_row)
8352
 
    {
8353
 
      List_iterator_fast<Item> it(fields);
8354
 
      Item *item;
8355
 
      while ((item= it++))
8356
 
        item->no_rows_in_result();
8357
 
      result->send_data(fields);
8358
 
    }
8359
 
    result->send_eof();                         // Should be safe
8360
 
  }
8361
 
  /* Update results for FOUND_ROWS */
8362
 
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8363
 
  return(0);
8364
 
}
8365
 
 
8366
 
/*
8367
 
  used only in JOIN::clear
8368
 
*/
8369
 
static void clear_tables(JOIN *join)
8370
 
{
8371
 
  /* 
8372
 
    must clear only the non-const tables, as const tables
8373
 
    are not re-calculated.
8374
 
  */
8375
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
8376
 
    mark_as_null_row(join->table[i]);           // All fields are NULL
8377
 
}
8378
 
 
8379
 
/*****************************************************************************
8380
 
  Make som simple condition optimization:
8381
 
  If there is a test 'field = const' change all refs to 'field' to 'const'
8382
 
  Remove all dummy tests 'item = item', 'const op const'.
8383
 
  Remove all 'item is NULL', when item can never be null!
8384
 
  item->marker should be 0 for all items on entry
8385
 
  Return in cond_value false if condition is impossible (1 = 2)
8386
 
*****************************************************************************/
8387
 
 
8388
 
class COND_CMP :public ilink {
8389
 
public:
8390
 
  static void *operator new(size_t size)
8391
 
  {
8392
 
    return (void*) sql_alloc((uint) size);
8393
 
  }
8394
 
  static void operator delete(void *ptr __attribute__((unused)),
8395
 
                              size_t size __attribute__((unused)))
8396
 
  { TRASH(ptr, size); }
8397
 
 
8398
 
  Item *and_level;
8399
 
  Item_func *cmp_func;
8400
 
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8401
 
};
8402
 
 
8403
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8404
 
template class I_List<COND_CMP>;
8405
 
template class I_List_iterator<COND_CMP>;
8406
 
#endif
8407
 
 
 
1554
      return (tab->eq_ref_table= 0);
 
1555
  }
 
1556
  return tab->eq_ref_table= 1;
 
1557
}
8408
1558
 
8409
1559
/**
8410
1560
  Find the multiple equality predicate containing a field.
8424
1574
    - Item_equal for the found multiple equality predicate if a success;
8425
1575
    - NULL otherwise.
8426
1576
*/
8427
 
 
8428
 
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8429
 
                            bool *inherited_fl)
 
1577
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
8430
1578
{
8431
1579
  Item_equal *item= 0;
8432
1580
  bool in_upper_level= false;
8447
1595
  return item;
8448
1596
}
8449
1597
 
8450
 
  
8451
1598
/**
8452
1599
  Check whether an equality can be used to build multiple equalities.
8453
1600
 
8474
1621
    the check_equality will be called for the following equality
8475
1622
    predicates a=b, b=c, b=2 and f=e.
8476
1623
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
8477
 
      *cond_equal into (0,[Item_equal(a,b)]). 
 
1624
      *cond_equal into (0,[Item_equal(a,b)]).
8478
1625
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
8479
1626
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
8480
1627
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
8487
1634
    the Field::eq_def method) are placed to the same multiple equalities.
8488
1635
    Because of this some equality predicates are not eliminated and
8489
1636
    can be used in the constant propagation procedure.
8490
 
    We could weeken the equlity test as soon as at least one of the 
8491
 
    equal fields is to be equal to a constant. It would require a 
 
1637
    We could weeken the equlity test as soon as at least one of the
 
1638
    equal fields is to be equal to a constant. It would require a
8492
1639
    more complicated implementation: we would have to store, in
8493
1640
    general case, its own constant for each fields from the multiple
8494
1641
    equality. But at the same time it would allow us to get rid
8506
1653
    containing just field1 and field2 is added to the existing
8507
1654
    multiple equalities.
8508
1655
    If the function processes the predicate of the form field1=const,
8509
 
    it looks for a multiple equality containing field1. If found, the 
 
1656
    it looks for a multiple equality containing field1. If found, the
8510
1657
    function checks the constant of the multiple equality. If the value
8511
1658
    is unknown, it is setup to const. Otherwise the value is compared with
8512
1659
    const and the evaluation of the equality predicate is performed.
8529
1676
  @retval
8530
1677
    false   otherwise
8531
1678
*/
8532
 
 
8533
 
static bool check_simple_equality(Item *left_item, Item *right_item,
8534
 
                                  Item *item, COND_EQUAL *cond_equal)
 
1679
static bool check_simple_equality(Item *left_item,
 
1680
                                  Item *right_item,
 
1681
                                  Item *item,
 
1682
                                  COND_EQUAL *cond_equal)
8535
1683
{
8536
 
  if (left_item->type() == Item::REF_ITEM &&
8537
 
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
8538
 
  {
8539
 
    if (((Item_ref*)left_item)->depended_from)
8540
 
      return false;
8541
 
    left_item= left_item->real_item();
8542
 
  }
8543
 
  if (right_item->type() == Item::REF_ITEM &&
8544
 
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
8545
 
  {
8546
 
    if (((Item_ref*)right_item)->depended_from)
8547
 
      return false;
8548
 
    right_item= right_item->real_item();
8549
 
  }
8550
1684
  if (left_item->type() == Item::FIELD_ITEM &&
8551
1685
      right_item->type() == Item::FIELD_ITEM &&
8552
1686
      !((Item_field*)left_item)->depended_from &&
8564
1698
    bool left_copyfl, right_copyfl;
8565
1699
    Item_equal *left_item_equal=
8566
1700
               find_item_equal(cond_equal, left_field, &left_copyfl);
8567
 
    Item_equal *right_item_equal= 
 
1701
    Item_equal *right_item_equal=
8568
1702
               find_item_equal(cond_equal, right_field, &right_copyfl);
8569
1703
 
8570
1704
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
8571
1705
    if (left_field->eq(right_field)) /* f = f */
8572
 
      return (!(left_field->maybe_null() && !left_item_equal)); 
 
1706
      return (!(left_field->maybe_null() && !left_item_equal));
8573
1707
 
8574
1708
    if (left_item_equal && left_item_equal == right_item_equal)
8575
1709
    {
8576
 
      /* 
 
1710
      /*
8577
1711
        The equality predicate is inference of one of the existing
8578
1712
        multiple equalities, i.e the condition is already covered
8579
1713
        by upper level equalities
8580
1714
      */
8581
1715
       return true;
8582
1716
    }
8583
 
    
8584
 
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name && 
 
1717
 
 
1718
    bool copy_item_name= test(item && item->name >= subq_sj_cond_name &&
8585
1719
                              item->name < subq_sj_cond_name + 64);
8586
1720
    /* Copy the found multiple equalities at the current level if needed */
8587
1721
    if (left_copyfl)
8602
1736
    }
8603
1737
 
8604
1738
    if (left_item_equal)
8605
 
    { 
 
1739
    {
8606
1740
      /* left item was found in the current or one of the upper levels */
8607
1741
      if (! right_item_equal)
8608
1742
        left_item_equal->add((Item_field *) right_item);
8617
1751
      }
8618
1752
    }
8619
1753
    else
8620
 
    { 
 
1754
    {
8621
1755
      /* left item was not found neither the current nor in upper levels  */
8622
1756
      if (right_item_equal)
8623
1757
      {
8625
1759
        if (copy_item_name)
8626
1760
          right_item_equal->name = item->name;
8627
1761
      }
8628
 
      else 
 
1762
      else
8629
1763
      {
8630
1764
        /* None of the fields was found in multiple equalities */
8631
1765
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
8664
1798
 
8665
1799
      if (field_item->result_type() == STRING_RESULT)
8666
1800
      {
8667
 
        CHARSET_INFO *cs= ((Field_str*) field_item->field)->charset();
 
1801
        const CHARSET_INFO * const cs= ((Field_str*) field_item->field)->charset();
8668
1802
        if (!item)
8669
1803
        {
8670
1804
          Item_func_eq *eq_item;
8673
1807
          eq_item->set_cmp_func();
8674
1808
          eq_item->quick_fix_field();
8675
1809
          item= eq_item;
8676
 
        }  
 
1810
        }
8677
1811
        if ((cs != ((Item_func *) item)->compare_collation()) ||
8678
1812
            !cs->coll->propagate(cs, 0, 0))
8679
1813
          return false;
8688
1822
      }
8689
1823
      if (item_equal)
8690
1824
      {
8691
 
        /* 
 
1825
        /*
8692
1826
          The flag cond_false will be set to 1 after this, if item_equal
8693
1827
          already contains a constant and its value is  not equal to
8694
1828
          the value of const_item.
8706
1840
  return false;
8707
1841
}
8708
1842
 
8709
 
 
8710
1843
/**
8711
1844
  Convert row equalities into a conjunction of regular equalities.
8712
1845
 
8719
1852
    simple equality nor a row equality the item for this predicate is added
8720
1853
    to eq_list.
8721
1854
 
8722
 
  @param thd        thread handle
 
1855
  @param session        thread handle
8723
1856
  @param left_row   left term of the row equality to be processed
8724
1857
  @param right_row  right term of the row equality to be processed
8725
1858
  @param cond_equal multiple equalities that must hold together with the
8732
1865
  @retval
8733
1866
    false   otherwise
8734
1867
*/
8735
 
 
8736
 
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8737
 
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8738
 
8739
 
  uint n= left_row->cols();
8740
 
  for (uint i= 0 ; i < n; i++)
 
1868
static bool check_row_equality(Session *session,
 
1869
                               Item *left_row, 
 
1870
                               Item_row *right_row,
 
1871
                               COND_EQUAL *cond_equal,
 
1872
                               List<Item>* eq_list)
 
1873
{
 
1874
  uint32_t n= left_row->cols();
 
1875
  for (uint32_t i= 0 ; i < n; i++)
8741
1876
  {
8742
1877
    bool is_converted;
8743
1878
    Item *left_item= left_row->element_index(i);
8745
1880
    if (left_item->type() == Item::ROW_ITEM &&
8746
1881
        right_item->type() == Item::ROW_ITEM)
8747
1882
    {
8748
 
      is_converted= check_row_equality(thd, 
 
1883
      is_converted= check_row_equality(session,
8749
1884
                                       (Item_row *) left_item,
8750
1885
                                       (Item_row *) right_item,
8751
1886
                                       cond_equal, eq_list);
8752
1887
      if (!is_converted)
8753
 
        thd->lex->current_select->cond_count++;      
 
1888
        session->lex->current_select->cond_count++;
8754
1889
    }
8755
1890
    else
8756
 
    { 
 
1891
    {
8757
1892
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
8758
 
      thd->lex->current_select->cond_count++;
8759
 
    }  
8760
 
 
 
1893
      session->lex->current_select->cond_count++;
 
1894
    }
 
1895
 
8761
1896
    if (!is_converted)
8762
1897
    {
8763
1898
      Item_func_eq *eq_item;
8771
1906
  return true;
8772
1907
}
8773
1908
 
8774
 
 
8775
1909
/**
8776
1910
  Eliminate row equalities and form multiple equalities predicates.
8777
1911
 
8786
1920
    equalities which are treated in the same way as original equality
8787
1921
    predicates.
8788
1922
 
8789
 
  @param thd        thread handle
 
1923
  @param session        thread handle
8790
1924
  @param item       predicate to process
8791
1925
  @param cond_equal multiple equalities that must hold together with the
8792
1926
                    predicate
8801
1935
           or, if the equality is neither a simple one nor a row equality,
8802
1936
           or, if the procedure fails by a fatal error.
8803
1937
*/
8804
 
 
8805
 
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
8806
 
                           List<Item> *eq_list)
 
1938
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
8807
1939
{
8808
1940
  if (item->type() == Item::FUNC_ITEM &&
8809
1941
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
8814
1946
    if (left_item->type() == Item::ROW_ITEM &&
8815
1947
        right_item->type() == Item::ROW_ITEM)
8816
1948
    {
8817
 
      thd->lex->current_select->cond_count--;
8818
 
      return check_row_equality(thd,
 
1949
      session->lex->current_select->cond_count--;
 
1950
      return check_row_equality(session,
8819
1951
                                (Item_row *) left_item,
8820
1952
                                (Item_row *) right_item,
8821
1953
                                cond_equal, eq_list);
8822
1954
    }
8823
 
    else 
 
1955
    else
8824
1956
      return check_simple_equality(left_item, right_item, item, cond_equal);
8825
 
  } 
 
1957
  }
8826
1958
  return false;
8827
1959
}
8828
1960
 
8829
 
                          
8830
1961
/**
8831
1962
  Replace all equality predicates in a condition by multiple equality items.
8832
1963
 
8833
1964
    At each 'and' level the function detects items for equality predicates
8834
1965
    and replaced them by a set of multiple equality items of class Item_equal,
8835
 
    taking into account inherited equalities from upper levels. 
 
1966
    taking into account inherited equalities from upper levels.
8836
1967
    If an equality predicate is used not in a conjunction it's just
8837
1968
    replaced by a multiple equality predicate.
8838
1969
    For each 'and' level the function set a pointer to the inherited
8839
1970
    multiple equalities in the cond_equal field of the associated
8840
 
    object of the type Item_cond_and.   
 
1971
    object of the type Item_cond_and.
8841
1972
    The function also traverses the cond tree and and for each field reference
8842
1973
    sets a pointer to the multiple equality item containing the field, if there
8843
1974
    is any. If this multiple equality equates fields to a constant the
8844
 
    function replaces the field reference by the constant in the cases 
 
1975
    function replaces the field reference by the constant in the cases
8845
1976
    when the field is not of a string type or when the field reference is
8846
1977
    just an argument of a comparison predicate.
8847
 
    The function also determines the maximum number of members in 
 
1978
    The function also determines the maximum number of members in
8848
1979
    equality lists of each Item_cond_and object assigning it to
8849
 
    thd->lex->current_select->max_equal_elems.
 
1980
    session->lex->current_select->max_equal_elems.
8850
1981
 
8851
1982
  @note
8852
1983
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
8858
1989
    in a conjuction for a minimal set of multiple equality predicates.
8859
1990
    This set can be considered as a canonical representation of the
8860
1991
    sub-conjunction of the equality predicates.
8861
 
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by 
 
1992
    E.g. (t1.a=t2.b AND t2.b>5 AND t1.a=t3.c) is replaced by
8862
1993
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
8863
1994
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
8864
1995
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
8869
2000
    The function performs the substitution in a recursive descent by
8870
2001
    the condtion tree, passing to the next AND level a chain of multiple
8871
2002
    equality predicates which have been built at the upper levels.
8872
 
    The Item_equal items built at the level are attached to other 
 
2003
    The Item_equal items built at the level are attached to other
8873
2004
    non-equality conjucts as a sublist. The pointer to the inherited
8874
2005
    multiple equalities is saved in the and condition object (Item_cond_and).
8875
 
    This chain allows us for any field reference occurence easyly to find a 
 
2006
    This chain allows us for any field reference occurence easyly to find a
8876
2007
    multiple equality that must be held for this occurence.
8877
2008
    For each AND level we do the following:
8878
2009
    - scan it for all equality predicate (=) items
8879
2010
    - join them into disjoint Item_equal() groups
8880
 
    - process the included OR conditions recursively to do the same for 
8881
 
      lower AND levels. 
 
2011
    - process the included OR conditions recursively to do the same for
 
2012
      lower AND levels.
8882
2013
 
8883
2014
    We need to do things in this order as lower AND levels need to know about
8884
2015
    all possible Item_equal objects in upper levels.
8885
2016
 
8886
 
  @param thd        thread handle
 
2017
  @param session        thread handle
8887
2018
  @param cond       condition(expression) where to make replacement
8888
2019
  @param inherited  path to all inherited multiple equality items
8889
2020
 
8890
2021
  @return
8891
2022
    pointer to the transformed condition
8892
2023
*/
8893
 
 
8894
 
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
8895
 
                                        COND_EQUAL *inherited)
 
2024
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
8896
2025
{
8897
2026
  Item_equal *item_equal;
8898
2027
  COND_EQUAL cond_equal;
8904
2033
    bool and_level= ((Item_cond*) cond)->functype() ==
8905
2034
      Item_func::COND_AND_FUNC;
8906
2035
    List<Item> *args= ((Item_cond*) cond)->argument_list();
8907
 
    
 
2036
 
8908
2037
    List_iterator<Item> li(*args);
8909
2038
    Item *item;
8910
2039
 
8913
2042
      /*
8914
2043
         Retrieve all conjucts of this level detecting the equality
8915
2044
         that are subject to substitution by multiple equality items and
8916
 
         removing each such predicate from the conjunction after having 
 
2045
         removing each such predicate from the conjunction after having
8917
2046
         found/created a multiple equality whose inference the predicate is.
8918
 
     */      
 
2047
     */
8919
2048
      while ((item= li++))
8920
2049
      {
8921
2050
        /*
8923
2052
          structure here because it's restored before each
8924
2053
          re-execution of any prepared statement/stored procedure.
8925
2054
        */
8926
 
        if (check_equality(thd, item, &cond_equal, &eq_list))
 
2055
        if (check_equality(session, item, &cond_equal, &eq_list))
8927
2056
          li.remove();
8928
2057
      }
8929
2058
 
8932
2061
      {
8933
2062
        item_equal->fix_length_and_dec();
8934
2063
        item_equal->update_used_tables();
8935
 
        set_if_bigger(thd->lex->current_select->max_equal_elems,
8936
 
                      item_equal->members());  
 
2064
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
2065
                      item_equal->members());
8937
2066
      }
8938
2067
 
8939
2068
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
8945
2074
    */
8946
2075
    li.rewind();
8947
2076
    while ((item= li++))
8948
 
    { 
 
2077
    {
8949
2078
      Item *new_item;
8950
 
      if ((new_item= build_equal_items_for_cond(thd, item, inherited)) != item)
 
2079
      if ((new_item= build_equal_items_for_cond(session, item, inherited)) != item)
8951
2080
      {
8952
2081
        /* This replacement happens only for standalone equalities */
8953
2082
        /*
8975
2104
      (b=5) and (a=c) are standalone equalities.
8976
2105
      In general we can't leave alone standalone eqalities:
8977
2106
      for WHERE a=b AND c=d AND (b=c OR d=5)
8978
 
      b=c is replaced by =(a,b,c,d).  
 
2107
      b=c is replaced by =(a,b,c,d).
8979
2108
     */
8980
 
    if (check_equality(thd, cond, &cond_equal, &eq_list))
 
2109
    if (check_equality(session, cond, &cond_equal, &eq_list))
8981
2110
    {
8982
2111
      int n= cond_equal.current_level.elements + eq_list.elements;
8983
2112
      if (n == 0)
8988
2117
        {
8989
2118
          item_equal->fix_length_and_dec();
8990
2119
          item_equal->update_used_tables();
8991
 
        }
 
2120
        }
8992
2121
        else
8993
2122
          item_equal= (Item_equal *) eq_list.pop();
8994
 
        set_if_bigger(thd->lex->current_select->max_equal_elems,
8995
 
                      item_equal->members());  
 
2123
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
2124
                      item_equal->members());
8996
2125
        return item_equal;
8997
2126
      }
8998
2127
      else
8999
2128
      {
9000
 
        /* 
 
2129
        /*
9001
2130
          Here a new AND level must be created. It can happen only
9002
2131
          when a row equality is processed as a standalone predicate.
9003
 
        */
 
2132
        */
9004
2133
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
9005
2134
        and_cond->quick_fix_field();
9006
2135
        List<Item> *args= and_cond->argument_list();
9009
2138
        {
9010
2139
          item_equal->fix_length_and_dec();
9011
2140
          item_equal->update_used_tables();
9012
 
          set_if_bigger(thd->lex->current_select->max_equal_elems,
9013
 
                        item_equal->members());  
 
2141
          set_if_bigger(session->lex->current_select->max_equal_elems,
 
2142
                        item_equal->members());
9014
2143
        }
9015
2144
        and_cond->cond_equal= cond_equal;
9016
2145
        args->concat((List<Item> *)&cond_equal.current_level);
9017
 
        
 
2146
 
9018
2147
        return and_cond;
9019
2148
      }
9020
2149
    }
9021
 
    /* 
 
2150
    /*
9022
2151
      For each field reference in cond, not from equal item predicates,
9023
2152
      set a pointer to the multiple equality it belongs to (if there is any)
9024
2153
      as soon the field is not of a string type or the field reference is
9025
 
      an argument of a comparison predicate. 
9026
 
    */ 
9027
 
    uchar *is_subst_valid= (uchar *) 1;
 
2154
      an argument of a comparison predicate.
 
2155
    */
 
2156
    unsigned char *is_subst_valid= (unsigned char *) 1;
9028
2157
    cond= cond->compile(&Item::subst_argument_checker,
9029
 
                        &is_subst_valid, 
 
2158
                        &is_subst_valid,
9030
2159
                        &Item::equal_fields_propagator,
9031
 
                        (uchar *) inherited);
 
2160
                        (unsigned char *) inherited);
9032
2161
    cond->update_used_tables();
9033
2162
  }
9034
2163
  return cond;
9035
2164
}
9036
2165
 
9037
 
 
9038
2166
/**
9039
2167
  Build multiple equalities for a condition and all on expressions that
9040
2168
  inherit these multiple equalities.
9080
2208
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
9081
2209
        WHERE t1.a=t2.a
9082
2210
    @endcode
9083
 
    that is equivalent to:   
 
2211
    that is equivalent to:
9084
2212
    @code
9085
2213
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
9086
2214
        WHERE t1.a=t2.a
9087
2215
    @endcode
9088
2216
    Thus, applying equalities from the where condition we basically
9089
2217
    can get more freedom in performing join operations.
9090
 
    Althogh we don't use this property now, it probably makes sense to use 
9091
 
    it in the future.    
9092
 
  @param thd                  Thread handler
 
2218
    Althogh we don't use this property now, it probably makes sense to use
 
2219
    it in the future.
 
2220
  @param session                      Thread Cursor
9093
2221
  @param cond                condition to build the multiple equalities for
9094
2222
  @param inherited           path to all inherited multiple equality items
9095
2223
  @param join_list           list of join tables to which the condition
9100
2228
  @return
9101
2229
    pointer to the transformed condition containing multiple equalities
9102
2230
*/
9103
 
   
9104
 
static COND *build_equal_items(THD *thd, COND *cond,
 
2231
static COND *build_equal_items(Session *session, COND *cond,
9105
2232
                               COND_EQUAL *inherited,
9106
 
                               List<TABLE_LIST> *join_list,
 
2233
                               List<TableList> *join_list,
9107
2234
                               COND_EQUAL **cond_equal_ref)
9108
2235
{
9109
2236
  COND_EQUAL *cond_equal= 0;
9110
2237
 
9111
 
  if (cond) 
 
2238
  if (cond)
9112
2239
  {
9113
 
    cond= build_equal_items_for_cond(thd, cond, inherited);
 
2240
    cond= build_equal_items_for_cond(session, cond, inherited);
9114
2241
    cond->update_used_tables();
9115
2242
    if (cond->type() == Item::COND_ITEM &&
9116
2243
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
9131
2258
 
9132
2259
  if (join_list)
9133
2260
  {
9134
 
    TABLE_LIST *table;
9135
 
    List_iterator<TABLE_LIST> li(*join_list);
 
2261
    TableList *table;
 
2262
    List_iterator<TableList> li(*join_list);
9136
2263
 
9137
2264
    while ((table= li++))
9138
2265
    {
9139
2266
      if (table->on_expr)
9140
2267
      {
9141
 
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
 
2268
        List<TableList> *nested_join_list= table->nested_join ?
9142
2269
          &table->nested_join->join_list : NULL;
9143
2270
        /*
9144
2271
          We can modify table->on_expr because its old value will
9145
2272
          be restored before re-execution of PS/SP.
9146
2273
        */
9147
 
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
 
2274
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
9148
2275
                                          nested_join_list,
9149
2276
                                          &table->cond_equal);
9150
2277
      }
9152
2279
  }
9153
2280
 
9154
2281
  return cond;
9155
 
}    
9156
 
 
 
2282
}
9157
2283
 
9158
2284
/**
9159
2285
  Compare field items by table order in the execution plan.
9160
2286
 
9161
2287
    field1 considered as better than field2 if the table containing
9162
 
    field1 is accessed earlier than the table containing field2.   
 
2288
    field1 is accessed earlier than the table containing field2.
9163
2289
    The function finds out what of two fields is better according
9164
2290
    this criteria.
9165
2291
 
9174
2300
  @retval
9175
2301
    0  otherwise
9176
2302
*/
9177
 
 
9178
2303
static int compare_fields_by_table_order(Item_field *field1,
9179
 
                                  Item_field *field2,
9180
 
                                  void *table_join_idx)
 
2304
                                         Item_field *field2,
 
2305
                                         void *table_join_idx)
9181
2306
{
9182
2307
  int cmp= 0;
9183
2308
  bool outer_ref= 0;
9184
2309
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9185
 
  {  
 
2310
  {
9186
2311
    outer_ref= 1;
9187
2312
    cmp= -1;
9188
2313
  }
9193
2318
  }
9194
2319
  if (outer_ref)
9195
2320
    return cmp;
9196
 
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
2321
  JoinTable **idx= (JoinTable **) table_join_idx;
9197
2322
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
9198
2323
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
9199
2324
}
9200
2325
 
9201
 
 
9202
2326
/**
9203
2327
  Generate minimal set of simple equalities equivalent to a multiple equality.
9204
2328
 
9238
2362
    a pointer to the simple generated equality, if success.
9239
2363
    - 0, otherwise.
9240
2364
*/
9241
 
 
9242
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9243
 
                                  Item_equal *item_equal)
 
2365
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
9244
2366
{
9245
2367
  List<Item> eq_list;
9246
2368
  Item_func_eq *eq_item= 0;
9247
2369
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9248
 
    return new Item_int((int64_t) 0,1); 
 
2370
    return new Item_int((int64_t) 0,1);
9249
2371
  Item *item_const= item_equal->get_const();
9250
2372
  Item_equal_iterator it(*item_equal);
9251
2373
  Item *head;
9262
2384
    Item_equal *upper= item_field->find_item_equal(upper_levels);
9263
2385
    Item_field *item= item_field;
9264
2386
    if (upper)
9265
 
    { 
 
2387
    {
9266
2388
      if (item_const && upper->get_const())
9267
2389
        item= 0;
9268
2390
      else
9306
2428
 
9307
2429
  cond->quick_fix_field();
9308
2430
  cond->update_used_tables();
9309
 
   
 
2431
 
9310
2432
  return cond;
9311
2433
}
9312
2434
 
9313
 
 
9314
2435
/**
9315
2436
  Substitute every field reference in a condition by the best equal field
9316
2437
  and eliminate all multiple equality predicates.
9319
2440
    multiple equality predicate it sorts the field references in it
9320
2441
    according to the order of tables specified by the table_join_idx
9321
2442
    parameter. Then it eliminates the multiple equality predicate it
9322
 
    replacing it by the conjunction of simple equality predicates 
 
2443
    replacing it by the conjunction of simple equality predicates
9323
2444
    equating every field from the multiple equality to the first
9324
2445
    field in it, or to the constant, if there is any.
9325
2446
    After this the function retrieves all other conjuncted
9338
2459
  @return
9339
2460
    The transformed condition
9340
2461
*/
9341
 
 
9342
 
static COND* substitute_for_best_equal_field(COND *cond,
9343
 
                                             COND_EQUAL *cond_equal,
9344
 
                                             void *table_join_idx)
 
2462
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
9345
2463
{
9346
2464
  Item_equal *item_equal;
9347
2465
 
9356
2474
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
9357
2475
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
9358
2476
 
9359
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
 
2477
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
9360
2478
      while ((item_equal= it++))
9361
2479
      {
9362
2480
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9363
2481
      }
9364
2482
    }
9365
 
    
 
2483
 
9366
2484
    List_iterator<Item> li(*cond_list);
9367
2485
    Item *item;
9368
2486
    while ((item= li++))
9395
2513
      cond= new Item_int((int32_t)cond->val_bool());
9396
2514
 
9397
2515
  }
9398
 
  else if (cond->type() == Item::FUNC_ITEM && 
 
2516
  else if (cond->type() == Item::FUNC_ITEM &&
9399
2517
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9400
2518
  {
9401
2519
    item_equal= (Item_equal *) cond;
9409
2527
  return cond;
9410
2528
}
9411
2529
 
9412
 
 
9413
2530
/**
9414
2531
  Check appearance of new constant items in multiple equalities
9415
2532
  of a condition after reading a constant table.
9422
2539
  @param cond       condition whose multiple equalities are to be checked
9423
2540
  @param table      constant table that has been read
9424
2541
*/
9425
 
 
9426
 
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
 
2542
static void update_const_equal_items(COND *cond, JoinTable *tab)
9427
2543
{
9428
2544
  if (!(cond->used_tables() & tab->table->map))
9429
2545
    return;
9430
2546
 
9431
2547
  if (cond->type() == Item::COND_ITEM)
9432
2548
  {
9433
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
 
2549
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9434
2550
    List_iterator_fast<Item> li(*cond_list);
9435
2551
    Item *item;
9436
2552
    while ((item= li++))
9437
2553
      update_const_equal_items(item, tab);
9438
2554
  }
9439
 
  else if (cond->type() == Item::FUNC_ITEM && 
 
2555
  else if (cond->type() == Item::FUNC_ITEM &&
9440
2556
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9441
2557
  {
9442
2558
    Item_equal *item_equal= (Item_equal *) cond;
9450
2566
      while ((item_field= it++))
9451
2567
      {
9452
2568
        Field *field= item_field->field;
9453
 
        JOIN_TAB *stat= field->table->reginfo.join_tab;
 
2569
        JoinTable *stat= field->table->reginfo.join_tab;
9454
2570
        key_map possible_keys= field->key_start;
9455
 
        possible_keys.intersect(field->table->keys_in_use_for_query);
9456
 
        stat[0].const_keys.merge(possible_keys);
 
2571
        possible_keys&= field->table->keys_in_use_for_query;
 
2572
        stat[0].const_keys|= possible_keys;
9457
2573
 
9458
2574
        /*
9459
 
          For each field in the multiple equality (for which we know that it 
9460
 
          is a constant) we have to find its corresponding key part, and set 
 
2575
          For each field in the multiple equality (for which we know that it
 
2576
          is a constant) we have to find its corresponding key part, and set
9461
2577
          that key part in const_key_parts.
9462
 
        */  
9463
 
        if (!possible_keys.is_clear_all())
 
2578
        */
 
2579
        if (possible_keys.any())
9464
2580
        {
9465
 
          TABLE *tab= field->table;
9466
 
          KEYUSE *use;
9467
 
          for (use= stat->keyuse; use && use->table == tab; use++)
9468
 
            if (possible_keys.is_set(use->key) && 
9469
 
                tab->key_info[use->key].key_part[use->keypart].field ==
 
2581
          Table *field_tab= field->table;
 
2582
          optimizer::KeyUse *use;
 
2583
          for (use= stat->keyuse; use && use->getTable() == field_tab; use++)
 
2584
            if (possible_keys.test(use->getKey()) &&
 
2585
                field_tab->key_info[use->getKey()].key_part[use->getKeypart()].field ==
9470
2586
                field)
9471
 
              tab->const_key_parts[use->key]|= use->keypart_map;
 
2587
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
9472
2588
        }
9473
2589
      }
9474
2590
    }
9475
2591
  }
9476
2592
}
9477
2593
 
9478
 
 
9479
2594
/*
9480
2595
  change field = field to field = const for each found field = const in the
9481
2596
  and_level
9482
2597
*/
9483
 
 
9484
 
static void
9485
 
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
9486
 
                         Item *and_father, Item *cond,
9487
 
                         Item *field, Item *value)
 
2598
static void change_cond_ref_to_const(Session *session,
 
2599
                                     vector<COND_CMP>& save_list,
 
2600
                                     Item *and_father,
 
2601
                                     Item *cond,
 
2602
                                     Item *field,
 
2603
                                     Item *value)
9488
2604
{
9489
2605
  if (cond->type() == Item::COND_ITEM)
9490
2606
  {
9491
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9492
 
      Item_func::COND_AND_FUNC;
 
2607
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
9493
2608
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
9494
2609
    Item *item;
9495
2610
    while ((item=li++))
9496
 
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
9497
 
                               field, value);
 
2611
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
9498
2612
    return;
9499
2613
  }
9500
2614
  if (cond->eq_cmp_result() == Item::COND_OK)
9514
2628
  {
9515
2629
    Item *tmp=value->clone_item();
9516
2630
    tmp->collation.set(right_item->collation);
9517
 
    
 
2631
 
9518
2632
    if (tmp)
9519
2633
    {
9520
 
      thd->change_item_tree(args + 1, tmp);
 
2634
      session->change_item_tree(args + 1, tmp);
9521
2635
      func->update_used_tables();
9522
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9523
 
          && and_father != cond && !left_item->const_item())
 
2636
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
2637
                and_father != cond && 
 
2638
          ! left_item->const_item())
9524
2639
      {
9525
 
        cond->marker=1;
9526
 
        COND_CMP *tmp2;
9527
 
        if ((tmp2=new COND_CMP(and_father,func)))
9528
 
          save_list->push_back(tmp2);
 
2640
        cond->marker=1;
 
2641
        save_list.push_back( COND_CMP(and_father, func) );
9529
2642
      }
9530
2643
      func->set_cmp_func();
9531
2644
    }
9538
2651
  {
9539
2652
    Item *tmp= value->clone_item();
9540
2653
    tmp->collation.set(left_item->collation);
9541
 
    
 
2654
 
9542
2655
    if (tmp)
9543
2656
    {
9544
 
      thd->change_item_tree(args, tmp);
 
2657
      session->change_item_tree(args, tmp);
9545
2658
      value= tmp;
9546
2659
      func->update_used_tables();
9547
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9548
 
          && and_father != cond && !right_item->const_item())
 
2660
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
2661
          and_father != cond && 
 
2662
          ! right_item->const_item())
9549
2663
      {
9550
2664
        args[0]= args[1];                       // For easy check
9551
 
        thd->change_item_tree(args + 1, value);
9552
 
        cond->marker=1;
9553
 
        COND_CMP *tmp2;
9554
 
        if ((tmp2=new COND_CMP(and_father,func)))
9555
 
          save_list->push_back(tmp2);
 
2665
        session->change_item_tree(args + 1, value);
 
2666
        cond->marker=1;
 
2667
        save_list.push_back( COND_CMP(and_father, func) );
9556
2668
      }
9557
2669
      func->set_cmp_func();
9558
2670
    }
9567
2679
  @return
9568
2680
    new conditions
9569
2681
*/
9570
 
 
9571
 
static Item *remove_additional_cond(Item* conds)
 
2682
Item *remove_additional_cond(Item* conds)
9572
2683
{
9573
2684
  if (conds->name == in_additional_cond)
9574
2685
    return 0;
9591
2702
  return conds;
9592
2703
}
9593
2704
 
9594
 
static void
9595
 
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
9596
 
                         COND *and_father, COND *cond)
 
2705
static void propagate_cond_constants(Session *session, 
 
2706
                                     vector<COND_CMP>& save_list, 
 
2707
                                     COND *and_father, 
 
2708
                                     COND *cond)
9597
2709
{
9598
2710
  if (cond->type() == Item::COND_ITEM)
9599
2711
  {
9600
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9601
 
      Item_func::COND_AND_FUNC;
 
2712
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
9602
2713
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
9603
2714
    Item *item;
9604
 
    I_List<COND_CMP> save;
 
2715
    vector<COND_CMP> save;
9605
2716
    while ((item=li++))
9606
2717
    {
9607
 
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
 
2718
      propagate_cond_constants(session, save, and_level ? cond : item, item);
9608
2719
    }
9609
2720
    if (and_level)
9610
 
    {                                           // Handle other found items
9611
 
      I_List_iterator<COND_CMP> cond_itr(save);
9612
 
      COND_CMP *cond_cmp;
9613
 
      while ((cond_cmp=cond_itr++))
 
2721
    {
 
2722
      // Handle other found items
 
2723
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
9614
2724
      {
9615
 
        Item **args= cond_cmp->cmp_func->arguments();
 
2725
        Item **args= iter->cmp_func->arguments();
9616
2726
        if (!args[0]->const_item())
9617
 
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
9618
 
                                   cond_cmp->and_level, args[0], args[1]);
 
2727
        {
 
2728
          change_cond_ref_to_const( session, save, iter->and_level,
 
2729
                                    iter->and_level, args[0], args[1] );
 
2730
        }
9619
2731
      }
9620
2732
    }
9621
2733
  }
9622
2734
  else if (and_father != cond && !cond->marker)         // In a AND group
9623
2735
  {
9624
2736
    if (cond->type() == Item::FUNC_ITEM &&
9625
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9626
 
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
2737
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
2738
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
9627
2739
    {
9628
2740
      Item_func_eq *func=(Item_func_eq*) cond;
9629
2741
      Item **args= func->arguments();
9632
2744
      if (!(left_const && right_const) &&
9633
2745
          args[0]->result_type() == args[1]->result_type())
9634
2746
      {
9635
 
        if (right_const)
9636
 
        {
9637
 
          resolve_const_item(thd, &args[1], args[0]);
9638
 
          func->update_used_tables();
9639
 
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9640
 
                                   args[0], args[1]);
9641
 
        }
9642
 
        else if (left_const)
9643
 
        {
9644
 
          resolve_const_item(thd, &args[0], args[1]);
9645
 
          func->update_used_tables();
9646
 
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9647
 
                                   args[1], args[0]);
9648
 
        }
9649
 
      }
9650
 
    }
9651
 
  }
9652
 
}
9653
 
 
9654
 
 
9655
 
/**
9656
 
  Simplify joins replacing outer joins by inner joins whenever it's
9657
 
  possible.
9658
 
 
9659
 
    The function, during a retrieval of join_list,  eliminates those
9660
 
    outer joins that can be converted into inner join, possibly nested.
9661
 
    It also moves the on expressions for the converted outer joins
9662
 
    and from inner joins to conds.
9663
 
    The function also calculates some attributes for nested joins:
9664
 
    - used_tables    
9665
 
    - not_null_tables
9666
 
    - dep_tables.
9667
 
    - on_expr_dep_tables
9668
 
    The first two attributes are used to test whether an outer join can
9669
 
    be substituted for an inner join. The third attribute represents the
9670
 
    relation 'to be dependent on' for tables. If table t2 is dependent
9671
 
    on table t1, then in any evaluated execution plan table access to
9672
 
    table t2 must precede access to table t2. This relation is used also
9673
 
    to check whether the query contains  invalid cross-references.
9674
 
    The forth attribute is an auxiliary one and is used to calculate
9675
 
    dep_tables.
9676
 
    As the attribute dep_tables qualifies possibles orders of tables in the
9677
 
    execution plan, the dependencies required by the straight join
9678
 
    modifiers are reflected in this attribute as well.
9679
 
    The function also removes all braces that can be removed from the join
9680
 
    expression without changing its meaning.
9681
 
 
9682
 
  @note
9683
 
    An outer join can be replaced by an inner join if the where condition
9684
 
    or the on expression for an embedding nested join contains a conjunctive
9685
 
    predicate rejecting null values for some attribute of the inner tables.
9686
 
 
9687
 
    E.g. in the query:    
9688
 
    @code
9689
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9690
 
    @endcode
9691
 
    the predicate t2.b < 5 rejects nulls.
9692
 
    The query is converted first to:
9693
 
    @code
9694
 
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9695
 
    @endcode
9696
 
    then to the equivalent form:
9697
 
    @code
9698
 
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9699
 
    @endcode
9700
 
 
9701
 
 
9702
 
    Similarly the following query:
9703
 
    @code
9704
 
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9705
 
        WHERE t2.c < 5  
9706
 
    @endcode
9707
 
    is converted to:
9708
 
    @code
9709
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
9710
 
 
9711
 
    @endcode
9712
 
 
9713
 
    One conversion might trigger another:
9714
 
    @code
9715
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9716
 
                       LEFT JOIN t3 ON t3.b=t2.b
9717
 
        WHERE t3 IS NOT NULL =>
9718
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9719
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
9720
 
      SELECT * FROM t1, t2, t3
9721
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9722
 
  @endcode
9723
 
 
9724
 
    The function removes all unnecessary braces from the expression
9725
 
    produced by the conversions.
9726
 
    E.g.
9727
 
    @code
9728
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9729
 
    @endcode
9730
 
    finally is converted to: 
9731
 
    @code
9732
 
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9733
 
 
9734
 
    @endcode
9735
 
 
9736
 
 
9737
 
    It also will remove braces from the following queries:
9738
 
    @code
9739
 
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9740
 
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9741
 
    @endcode
9742
 
 
9743
 
    The benefit of this simplification procedure is that it might return 
9744
 
    a query for which the optimizer can evaluate execution plan with more
9745
 
    join orders. With a left join operation the optimizer does not
9746
 
    consider any plan where one of the inner tables is before some of outer
9747
 
    tables.
9748
 
 
9749
 
  IMPLEMENTATION
9750
 
    The function is implemented by a recursive procedure.  On the recursive
9751
 
    ascent all attributes are calculated, all outer joins that can be
9752
 
    converted are replaced and then all unnecessary braces are removed.
9753
 
    As join list contains join tables in the reverse order sequential
9754
 
    elimination of outer joins does not require extra recursive calls.
9755
 
 
9756
 
  SEMI-JOIN NOTES
9757
 
    Remove all semi-joins that have are within another semi-join (i.e. have
9758
 
    an "ancestor" semi-join nest)
9759
 
 
9760
 
  EXAMPLES
9761
 
    Here is an example of a join query with invalid cross references:
9762
 
    @code
9763
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
9764
 
    @endcode
9765
 
 
9766
 
  @param join        reference to the query info
9767
 
  @param join_list   list representation of the join to be converted
9768
 
  @param conds       conditions to add on expressions for converted joins
9769
 
  @param top         true <=> conds is the where condition
9770
 
 
9771
 
  @return
9772
 
    - The new condition, if success
9773
 
    - 0, otherwise
9774
 
*/
9775
 
 
9776
 
static COND *
9777
 
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9778
 
               bool in_sj)
9779
 
{
9780
 
  TABLE_LIST *table;
9781
 
  NESTED_JOIN *nested_join;
9782
 
  TABLE_LIST *prev_table= 0;
9783
 
  List_iterator<TABLE_LIST> li(*join_list);
9784
 
 
9785
 
  /* 
9786
 
    Try to simplify join operations from join_list.
9787
 
    The most outer join operation is checked for conversion first. 
9788
 
  */
9789
 
  while ((table= li++))
9790
 
  {
9791
 
    table_map used_tables;
9792
 
    table_map not_null_tables= (table_map) 0;
9793
 
 
9794
 
    if ((nested_join= table->nested_join))
9795
 
    {
9796
 
      /* 
9797
 
         If the element of join_list is a nested join apply
9798
 
         the procedure to its nested join list first.
9799
 
      */
9800
 
      if (table->on_expr)
9801
 
      {
9802
 
        Item *expr= table->on_expr;
9803
 
        /* 
9804
 
           If an on expression E is attached to the table, 
9805
 
           check all null rejected predicates in this expression.
9806
 
           If such a predicate over an attribute belonging to
9807
 
           an inner table of an embedded outer join is found,
9808
 
           the outer join is converted to an inner join and
9809
 
           the corresponding on expression is added to E. 
9810
 
        */ 
9811
 
        expr= simplify_joins(join, &nested_join->join_list,
9812
 
                             expr, false, in_sj || table->sj_on_expr);
9813
 
 
9814
 
        if (!table->prep_on_expr || expr != table->on_expr)
9815
 
        {
9816
 
          assert(expr);
9817
 
 
9818
 
          table->on_expr= expr;
9819
 
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
9820
 
        }
9821
 
      }
9822
 
      nested_join->used_tables= (table_map) 0;
9823
 
      nested_join->not_null_tables=(table_map) 0;
9824
 
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
9825
 
                            in_sj || table->sj_on_expr);
9826
 
      used_tables= nested_join->used_tables;
9827
 
      not_null_tables= nested_join->not_null_tables;  
9828
 
    }
9829
 
    else
9830
 
    {
9831
 
      if (!table->prep_on_expr)
9832
 
        table->prep_on_expr= table->on_expr;
9833
 
      used_tables= table->table->map;
9834
 
      if (conds)
9835
 
        not_null_tables= conds->not_null_tables();
9836
 
    }
9837
 
      
9838
 
    if (table->embedding)
9839
 
    {
9840
 
      table->embedding->nested_join->used_tables|= used_tables;
9841
 
      table->embedding->nested_join->not_null_tables|= not_null_tables;
9842
 
    }
9843
 
 
9844
 
    if (!table->outer_join || (used_tables & not_null_tables))
9845
 
    {
9846
 
      /* 
9847
 
        For some of the inner tables there are conjunctive predicates
9848
 
        that reject nulls => the outer join can be replaced by an inner join.
9849
 
      */
9850
 
      table->outer_join= 0;
9851
 
      if (table->on_expr)
9852
 
      {
9853
 
        /* Add ON expression to the WHERE or upper-level ON condition. */
9854
 
        if (conds)
9855
 
        {
9856
 
          conds= and_conds(conds, table->on_expr);
9857
 
          conds->top_level_item();
9858
 
          /* conds is always a new item as both cond and on_expr existed */
9859
 
          assert(!conds->fixed);
9860
 
          conds->fix_fields(join->thd, &conds);
9861
 
        }
9862
 
        else
9863
 
          conds= table->on_expr; 
9864
 
        table->prep_on_expr= table->on_expr= 0;
9865
 
      }
9866
 
    }
9867
 
    
9868
 
    if (!top)
9869
 
      continue;
9870
 
 
9871
 
    /* 
9872
 
      Only inner tables of non-convertible outer joins
9873
 
      remain with on_expr.
9874
 
    */ 
9875
 
    if (table->on_expr)
9876
 
    {
9877
 
      table->dep_tables|= table->on_expr->used_tables(); 
9878
 
      if (table->embedding)
9879
 
      {
9880
 
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
9881
 
        /*
9882
 
           Embedding table depends on tables used
9883
 
           in embedded on expressions. 
9884
 
        */
9885
 
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9886
 
      }
9887
 
      else
9888
 
        table->dep_tables&= ~table->table->map;
9889
 
    }
9890
 
 
9891
 
    if (prev_table)
9892
 
    {
9893
 
      /* The order of tables is reverse: prev_table follows table */
9894
 
      if (prev_table->straight)
9895
 
        prev_table->dep_tables|= used_tables;
9896
 
      if (prev_table->on_expr)
9897
 
      {
9898
 
        prev_table->dep_tables|= table->on_expr_dep_tables;
9899
 
        table_map prev_used_tables= prev_table->nested_join ?
9900
 
                                    prev_table->nested_join->used_tables :
9901
 
                                    prev_table->table->map;
9902
 
        /* 
9903
 
          If on expression contains only references to inner tables
9904
 
          we still make the inner tables dependent on the outer tables.
9905
 
          It would be enough to set dependency only on one outer table
9906
 
          for them. Yet this is really a rare case.
9907
 
        */  
9908
 
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9909
 
          prev_table->dep_tables|= used_tables;
9910
 
      }
9911
 
    }
9912
 
    prev_table= table;
9913
 
  }
9914
 
    
9915
 
  /* 
9916
 
    Flatten nested joins that can be flattened.
9917
 
    no ON expression and not a semi-join => can be flattened.
9918
 
  */
9919
 
  li.rewind();
9920
 
  while ((table= li++))
9921
 
  {
9922
 
    nested_join= table->nested_join;
9923
 
    if (table->sj_on_expr && !in_sj)
9924
 
    {
9925
 
       /*
9926
 
         If this is a semi-join that is not contained within another semi-join, 
9927
 
         leave it intact (otherwise it is flattened)
9928
 
       */
9929
 
      join->select_lex->sj_nests.push_back(table);
9930
 
    }
9931
 
    else if (nested_join && !table->on_expr)
9932
 
    {
9933
 
      TABLE_LIST *tbl;
9934
 
      List_iterator<TABLE_LIST> it(nested_join->join_list);
9935
 
      while ((tbl= it++))
9936
 
      {
9937
 
        tbl->embedding= table->embedding;
9938
 
        tbl->join_list= table->join_list;
9939
 
      }      
9940
 
      li.replace(nested_join->join_list);
9941
 
    }
9942
 
  }
9943
 
  return(conds); 
9944
 
}
9945
 
 
9946
 
 
9947
 
/**
9948
 
  Assign each nested join structure a bit in nested_join_map.
9949
 
 
9950
 
    Assign each nested join structure (except "confluent" ones - those that
9951
 
    embed only one element) a bit in nested_join_map.
9952
 
 
9953
 
  @param join          Join being processed
9954
 
  @param join_list     List of tables
9955
 
  @param first_unused  Number of first unused bit in nested_join_map before the
9956
 
                       call
9957
 
 
9958
 
  @note
9959
 
    This function is called after simplify_joins(), when there are no
9960
 
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9961
 
    we will not run out of bits in nested_join_map.
9962
 
 
9963
 
  @return
9964
 
    First unused bit in nested_join_map after the call.
9965
 
*/
9966
 
 
9967
 
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
9968
 
                                          uint first_unused)
9969
 
{
9970
 
  List_iterator<TABLE_LIST> li(*join_list);
9971
 
  TABLE_LIST *table;
9972
 
  while ((table= li++))
9973
 
  {
9974
 
    NESTED_JOIN *nested_join;
9975
 
    if ((nested_join= table->nested_join))
9976
 
    {
9977
 
      /*
9978
 
        It is guaranteed by simplify_joins() function that a nested join
9979
 
        that has only one child is either
9980
 
         - a single-table view (the child is the underlying table), or 
9981
 
         - a single-table semi-join nest
9982
 
 
9983
 
        We don't assign bits to such sj-nests because 
9984
 
        1. it is redundant (a "sequence" of one table cannot be interleaved 
9985
 
            with anything)
9986
 
        2. we could run out bits in nested_join_map otherwise.
9987
 
      */
9988
 
      if (nested_join->join_list.elements != 1)
9989
 
      {
9990
 
        /* Don't assign bits to sj-nests */
9991
 
        if (table->on_expr)
9992
 
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9993
 
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9994
 
                                                    first_unused);
9995
 
      }
9996
 
    }
9997
 
  }
9998
 
  return(first_unused);
9999
 
}
10000
 
 
10001
 
 
10002
 
/**
10003
 
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
10004
 
 
10005
 
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
10006
 
    the passed join_list.
10007
 
 
10008
 
  @param join_list  List of nested joins to process. It may also contain base
10009
 
                    tables which will be ignored.
10010
 
*/
10011
 
 
10012
 
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10013
 
{
10014
 
  List_iterator<TABLE_LIST> li(*join_list);
10015
 
  TABLE_LIST *table;
10016
 
  while ((table= li++))
10017
 
  {
10018
 
    NESTED_JOIN *nested_join;
10019
 
    if ((nested_join= table->nested_join))
10020
 
    {
10021
 
      nested_join->counter_= 0;
10022
 
      reset_nj_counters(&nested_join->join_list);
10023
 
    }
10024
 
  }
10025
 
  return;
10026
 
}
10027
 
 
 
2747
        if (right_const)
 
2748
        {
 
2749
                resolve_const_item(session, &args[1], args[0]);
 
2750
          func->update_used_tables();
 
2751
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
2752
                                        args[0], args[1]);
 
2753
        }
 
2754
        else if (left_const)
 
2755
        {
 
2756
                resolve_const_item(session, &args[0], args[1]);
 
2757
          func->update_used_tables();
 
2758
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
2759
                                        args[1], args[0]);
 
2760
        }
 
2761
      }
 
2762
    }
 
2763
  }
 
2764
}
10028
2765
 
10029
2766
/**
10030
2767
  Check interleaving with an inner tables of an outer join for
10031
2768
  extension table.
10032
2769
 
10033
 
    Check if table next_tab can be added to current partial join order, and 
 
2770
    Check if table next_tab can be added to current partial join order, and
10034
2771
    if yes, record that it has been added.
10035
2772
 
10036
2773
    The function assumes that both current partial join order and its
10037
2774
    extension with next_tab are valid wrt table dependencies.
10038
2775
 
10039
2776
  @verbatim
10040
 
     IMPLEMENTATION 
10041
 
       LIMITATIONS ON JOIN ORDER
 
2777
     IMPLEMENTATION
 
2778
       LIMITATIONS ON JOIN order_st
10042
2779
         The nested [outer] joins executioner algorithm imposes these limitations
10043
2780
         on join order:
10044
 
         1. "Outer tables first" -  any "outer" table must be before any 
 
2781
         1. "Outer tables first" -  any "outer" table must be before any
10045
2782
             corresponding "inner" table.
10046
2783
         2. "No interleaving" - tables inside a nested join must form a continuous
10047
 
            sequence in join order (i.e. the sequence must not be interrupted by 
 
2784
            sequence in join order (i.e. the sequence must not be interrupted by
10048
2785
            tables that are outside of this nested join).
10049
2786
 
10050
2787
         #1 is checked elsewhere, this function checks #2 provided that #1 has
10051
2788
         been already checked.
10052
2789
 
10053
2790
       WHY NEED NON-INTERLEAVING
10054
 
         Consider an example: 
 
2791
         Consider an example:
10055
2792
 
10056
2793
           select * from t0 join t1 left join (t2 join t3) on cond1
10057
2794
 
10075
2812
         The limitations on join order can be rephrased as follows: for valid
10076
2813
         join order one must be able to:
10077
2814
           1. write down the used tables in the join order on one line.
10078
 
           2. for each nested join, put one '(' and one ')' on the said line        
 
2815
           2. for each nested join, put one '(' and one ')' on the said line
10079
2816
           3. write "LEFT JOIN" and "ON (...)" where appropriate
10080
2817
           4. get a query equivalent to the query we're trying to execute.
10081
2818
 
10082
2819
         Calls to check_interleaving_with_nj() are equivalent to writing the
10083
 
         above described line from left to right. 
10084
 
         A single check_interleaving_with_nj(A,B) call is equivalent to writing 
 
2820
         above described line from left to right.
 
2821
         A single check_interleaving_with_nj(A,B) call is equivalent to writing
10085
2822
         table B and appropriate brackets on condition that table A and
10086
2823
         appropriate brackets is the last what was written. Graphically the
10087
2824
         transition is as follows:
10094
2831
                                                      position.
10095
2832
 
10096
2833
         Notes about the position:
10097
 
           The caller guarantees that there is no more then one X-bracket by 
10098
 
           checking "!(remaining_tables & s->dependent)" before calling this 
 
2834
           The caller guarantees that there is no more then one X-bracket by
 
2835
           checking "!(remaining_tables & s->dependent)" before calling this
10099
2836
           function. X-bracket may have a pair in Y-bracket.
10100
2837
 
10101
2838
         When "writing" we store/update this auxilary info about the current
10102
2839
         position:
10103
2840
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10104
2841
             joins) we've opened but didn't close.
10105
 
          2. {each NESTED_JOIN structure not simplified away}->counter - number
 
2842
          2. {each nested_join_st structure not simplified away}->counter - number
10106
2843
             of this nested join's children that have already been added to to
10107
2844
             the partial join order.
10108
2845
  @endverbatim
10118
2855
  @retval
10119
2856
    true   Requested join order extension not allowed.
10120
2857
*/
10121
 
 
10122
 
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
 
2858
bool check_interleaving_with_nj(JoinTable *last_tab, JoinTable *next_tab)
10123
2859
{
10124
 
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
 
2860
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
10125
2861
  JOIN *join= last_tab->join;
10126
2862
 
10127
 
  if (join->cur_embedding_map & ~next_tab->embedding_map)
 
2863
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
10128
2864
  {
10129
 
    /* 
 
2865
    /*
10130
2866
      next_tab is outside of the "pair of brackets" we're currently in.
10131
2867
      Cannot add it.
10132
2868
    */
10133
2869
    return true;
10134
2870
  }
10135
 
   
 
2871
 
10136
2872
  /*
10137
2873
    Do update counters for "pairs of brackets" that we've left (marked as
10138
2874
    X,Y,Z in the above picture)
10142
2878
    next_emb->nested_join->counter_++;
10143
2879
    if (next_emb->nested_join->counter_ == 1)
10144
2880
    {
10145
 
      /* 
 
2881
      /*
10146
2882
        next_emb is the first table inside a nested join we've "entered". In
10147
2883
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
10148
2884
        X bracket might have Y pair bracket.
10149
2885
      */
10150
2886
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
10151
2887
    }
10152
 
    
 
2888
 
10153
2889
    if (next_emb->nested_join->join_list.elements !=
10154
2890
        next_emb->nested_join->counter_)
10155
2891
      break;
10163
2899
  return false;
10164
2900
}
10165
2901
 
10166
 
 
10167
 
/**
10168
 
  Nested joins perspective: Remove the last table from the join order.
10169
 
 
10170
 
    Remove the last table from the partial join order and update the nested
10171
 
    joins counters and join->cur_embedding_map. It is ok to call this 
10172
 
    function for the first table in join order (for which 
10173
 
    check_interleaving_with_nj has not been called)
10174
 
 
10175
 
  @param last  join table to remove, it is assumed to be the last in current
10176
 
               partial join order.
10177
 
*/
10178
 
 
10179
 
static void restore_prev_nj_state(JOIN_TAB *last)
10180
 
{
10181
 
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10182
 
  JOIN *join= last->join;
10183
 
  while (last_emb)
10184
 
  {
10185
 
    if (last_emb->on_expr)
10186
 
    {
10187
 
      if (!(--last_emb->nested_join->counter_))
10188
 
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10189
 
      else if (last_emb->nested_join->join_list.elements-1 ==
10190
 
               last_emb->nested_join->counter_) 
10191
 
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
10192
 
      else
10193
 
        break;
10194
 
    }
10195
 
    last_emb= last_emb->embedding;
10196
 
  }
10197
 
}
10198
 
 
10199
 
 
10200
 
 
10201
 
static 
10202
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10203
 
{
10204
 
  TABLE_LIST *emb_sj_nest;
10205
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10206
 
  {
10207
 
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10208
 
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
10209
 
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
10210
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10211
 
  }
10212
 
}
10213
 
 
10214
 
 
10215
 
/*
10216
 
  we assume remaining_tables doesnt contain @tab.
10217
 
*/
10218
 
 
10219
 
static void restore_prev_sj_state(const table_map remaining_tables, 
10220
 
                                  const JOIN_TAB *tab)
10221
 
{
10222
 
  TABLE_LIST *emb_sj_nest;
10223
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10224
 
  {
10225
 
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10226
 
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
10227
 
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
10228
 
    {
10229
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10230
 
    }
10231
 
  }
10232
 
}
10233
 
 
10234
 
 
10235
 
static COND *
10236
 
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10237
 
              Item::cond_result *cond_value)
10238
 
{
10239
 
  THD *thd= join->thd;
 
2902
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
 
2903
{
 
2904
  Session *session= join->session;
10240
2905
 
10241
2906
  if (!conds)
10242
2907
    *cond_value= Item::COND_TRUE;
10243
2908
  else
10244
2909
  {
10245
 
    /* 
 
2910
    /*
10246
2911
      Build all multiple equality predicates and eliminate equality
10247
2912
      predicates that can be inferred from these multiple equalities.
10248
2913
      For each reference of a field included into a multiple equality
10249
2914
      that occurs in a function set a pointer to the multiple equality
10250
2915
      predicate. Substitute a constant instead of this field if the
10251
2916
      multiple equality contains a constant.
10252
 
    */ 
10253
 
    conds= build_equal_items(join->thd, conds, NULL, join_list,
 
2917
    */
 
2918
    conds= build_equal_items(join->session, conds, NULL, join_list,
10254
2919
                             &join->cond_equal);
10255
2920
 
10256
2921
    /* change field = field to field = const for each found field = const */
10257
 
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
 
2922
    vector<COND_CMP> temp;
 
2923
    propagate_cond_constants(session, temp, conds, conds);
10258
2924
    /*
10259
2925
      Remove all instances of item == item
10260
2926
      Remove all and-levels where CONST item != CONST item
10261
2927
    */
10262
 
    conds= remove_eq_conds(thd, conds, cond_value) ;
 
2928
    conds= remove_eq_conds(session, conds, cond_value) ;
10263
2929
  }
10264
2930
  return(conds);
10265
2931
}
10266
2932
 
10267
 
 
10268
2933
/**
10269
2934
  Remove const and eq items.
10270
2935
 
10275
2940
    - COND_TRUE   : always true ( 1 = 1 )
10276
2941
    - COND_FALSE  : always false        ( 1 = 2 )
10277
2942
*/
10278
 
 
10279
 
COND *
10280
 
remove_eq_conds(THD *thd, COND *cond, Item::cond_result *cond_value)
 
2943
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
10281
2944
{
10282
2945
  if (cond->type() == Item::COND_ITEM)
10283
2946
  {
10284
 
    bool and_level= ((Item_cond*) cond)->functype()
10285
 
      == Item_func::COND_AND_FUNC;
 
2947
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
 
2948
 
10286
2949
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
10287
2950
    Item::cond_result tmp_cond_value;
10288
 
    bool should_fix_fields=0;
 
2951
    bool should_fix_fields= false;
10289
2952
 
10290
 
    *cond_value=Item::COND_UNDEF;
 
2953
    *cond_value= Item::COND_UNDEF;
10291
2954
    Item *item;
10292
 
    while ((item=li++))
 
2955
    while ((item= li++))
10293
2956
    {
10294
 
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
10295
 
      if (!new_item)
10296
 
        li.remove();
 
2957
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
 
2958
      if (! new_item)
 
2959
              li.remove();
10297
2960
      else if (item != new_item)
10298
2961
      {
10299
 
        VOID(li.replace(new_item));
10300
 
        should_fix_fields=1;
 
2962
        li.replace(new_item);
 
2963
        should_fix_fields= true;
10301
2964
      }
10302
2965
      if (*cond_value == Item::COND_UNDEF)
10303
 
        *cond_value=tmp_cond_value;
10304
 
      switch (tmp_cond_value) {
10305
 
      case Item::COND_OK:                       // Not true or false
10306
 
        if (and_level || *cond_value == Item::COND_FALSE)
10307
 
          *cond_value=tmp_cond_value;
10308
 
        break;
10309
 
      case Item::COND_FALSE:
10310
 
        if (and_level)
10311
 
        {
10312
 
          *cond_value=tmp_cond_value;
10313
 
          return (COND*) 0;                     // Always false
10314
 
        }
10315
 
        break;
10316
 
      case Item::COND_TRUE:
10317
 
        if (!and_level)
10318
 
        {
10319
 
          *cond_value= tmp_cond_value;
10320
 
          return (COND*) 0;                     // Always true
10321
 
        }
10322
 
        break;
10323
 
      case Item::COND_UNDEF:                    // Impossible
10324
 
        break; /* purecov: deadcode */
 
2966
              *cond_value= tmp_cond_value;
 
2967
 
 
2968
      switch (tmp_cond_value) 
 
2969
      {
 
2970
        case Item::COND_OK:                     /* Not true or false */
 
2971
          if (and_level || (*cond_value == Item::COND_FALSE))
 
2972
            *cond_value= tmp_cond_value;
 
2973
          break;
 
2974
        case Item::COND_FALSE:
 
2975
          if (and_level)
 
2976
          {
 
2977
            *cond_value= tmp_cond_value;
 
2978
            return (COND *) NULL;                       /* Always false */
 
2979
          }
 
2980
          break;
 
2981
        case Item::COND_TRUE:
 
2982
          if (! and_level)
 
2983
          {
 
2984
            *cond_value= tmp_cond_value;
 
2985
            return (COND *) NULL;                       /* Always true */
 
2986
          }
 
2987
          break;
 
2988
        case Item::COND_UNDEF:                  /* Impossible */
 
2989
          break;
10325
2990
      }
10326
2991
    }
 
2992
 
10327
2993
    if (should_fix_fields)
10328
2994
      cond->update_used_tables();
10329
2995
 
10330
 
    if (!((Item_cond*) cond)->argument_list()->elements ||
10331
 
        *cond_value != Item::COND_OK)
10332
 
      return (COND*) 0;
 
2996
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
 
2997
      return (COND*) NULL;
 
2998
 
10333
2999
    if (((Item_cond*) cond)->argument_list()->elements == 1)
10334
 
    {                                           // Remove list
 
3000
    {                                           
 
3001
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
10335
3002
      item= ((Item_cond*) cond)->argument_list()->head();
10336
3003
      ((Item_cond*) cond)->argument_list()->empty();
10337
3004
      return item;
10338
3005
    }
10339
3006
  }
10340
 
  else if (cond->type() == Item::FUNC_ITEM &&
10341
 
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
3007
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
10342
3008
  {
10343
3009
    /*
10344
3010
      Handles this special case for some ODBC applications:
10350
3016
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
10351
3017
    */
10352
3018
 
10353
 
    Item_func_isnull *func=(Item_func_isnull*) cond;
 
3019
    Item_func_isnull *func= (Item_func_isnull*) cond;
10354
3020
    Item **args= func->arguments();
10355
3021
    if (args[0]->type() == Item::FIELD_ITEM)
10356
3022
    {
10357
 
      Field *field=((Item_field*) args[0])->field;
10358
 
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
10359
 
          (thd->options & OPTION_AUTO_IS_NULL) &&
10360
 
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
10361
 
           thd->substitute_null_with_insert_id))
 
3023
      Field *field= ((Item_field*) args[0])->field;
 
3024
      if (field->flags & AUTO_INCREMENT_FLAG 
 
3025
          && ! field->table->maybe_null 
 
3026
          && session->options & OPTION_AUTO_IS_NULL
 
3027
          && (
 
3028
            session->first_successful_insert_id_in_prev_stmt > 0 
 
3029
            && session->substitute_null_with_insert_id
 
3030
            )
 
3031
          )
10362
3032
      {
10363
 
        COND *new_cond;
10364
 
        if ((new_cond= new Item_func_eq(args[0],
10365
 
                                        new Item_int("last_insert_id()",
10366
 
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
10367
 
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
10368
 
        {
10369
 
          cond=new_cond;
 
3033
        COND *new_cond;
 
3034
        if ((new_cond= new Item_func_eq(args[0], new Item_int("last_insert_id()",
 
3035
                                                          session->read_first_successful_insert_id_in_prev_stmt(),
 
3036
                                                          MY_INT64_NUM_DECIMAL_DIGITS))))
 
3037
        {
 
3038
          cond= new_cond;
10370
3039
          /*
10371
3040
            Item_func_eq can't be fixed after creation so we do not check
10372
3041
            cond->fixed, also it do not need tables so we use 0 as second
10373
3042
            argument.
10374
3043
          */
10375
 
          cond->fix_fields(thd, &cond);
10376
 
        }
 
3044
          cond->fix_fields(session, &cond);
 
3045
        }
10377
3046
        /*
10378
3047
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
10379
3048
          clear for next row
10380
3049
        */
10381
 
        thd->substitute_null_with_insert_id= false;
 
3050
        session->substitute_null_with_insert_id= false;
10382
3051
      }
 
3052
#ifdef NOTDEFINED
10383
3053
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10384
 
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
10385
 
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
10386
 
                (field->flags & NOT_NULL_FLAG) &&
10387
 
               !field->table->maybe_null)
 
3054
      else if (
 
3055
          ((field->type() == DRIZZLE_TYPE_DATE) || (field->type() == DRIZZLE_TYPE_DATETIME)) 
 
3056
          && (field->flags & NOT_NULL_FLAG) 
 
3057
          && ! field->table->maybe_null)
10388
3058
      {
10389
 
        COND *new_cond;
10390
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
10391
 
        {
10392
 
          cond=new_cond;
 
3059
        COND *new_cond;
 
3060
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
 
3061
        {
 
3062
          cond= new_cond;
10393
3063
          /*
10394
3064
            Item_func_eq can't be fixed after creation so we do not check
10395
3065
            cond->fixed, also it do not need tables so we use 0 as second
10396
3066
            argument.
10397
3067
          */
10398
 
          cond->fix_fields(thd, &cond);
10399
 
        }
 
3068
          cond->fix_fields(session, &cond);
 
3069
        }
10400
3070
      }
 
3071
#endif /* NOTDEFINED */
10401
3072
    }
10402
3073
    if (cond->const_item())
10403
3074
    {
10404
3075
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10405
 
      return (COND*) 0;
 
3076
      return (COND *) NULL;
10406
3077
    }
10407
3078
  }
10408
3079
  else if (cond->const_item() && !cond->is_expensive())
10418
3089
  */
10419
3090
  {
10420
3091
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10421
 
    return (COND*) 0;
 
3092
    return (COND *) NULL;
10422
3093
  }
10423
3094
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
10424
 
  {                                             // boolan compare function
 
3095
  {                                             
 
3096
    /* boolan compare function */
10425
3097
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10426
3098
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10427
3099
    if (left_item->eq(right_item,1))
10428
3100
    {
10429
 
      if (!left_item->maybe_null ||
10430
 
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
10431
 
        return (COND*) 0;                       // Compare of identical items
 
3101
      if (!left_item->maybe_null || ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
 
3102
              return (COND*) NULL;                      /* Comparison of identical items */
10432
3103
    }
10433
3104
  }
10434
 
  *cond_value=Item::COND_OK;
10435
 
  return cond;                                  // Point at next and level
 
3105
  *cond_value= Item::COND_OK;
 
3106
  return cond;                                  /* Point at next and return into recursion */
10436
3107
}
10437
3108
 
10438
 
/* 
 
3109
/*
10439
3110
  Check if equality can be used in removing components of GROUP BY/DISTINCT
10440
 
  
 
3111
 
10441
3112
  SYNOPSIS
10442
3113
    test_if_equality_guarantees_uniqueness()
10443
3114
      l          the left comparison argument (a field if any)
10444
3115
      r          the right comparison argument (a const of any)
10445
 
  
10446
 
  DESCRIPTION    
10447
 
    Checks if an equality predicate can be used to take away 
10448
 
    DISTINCT/GROUP BY because it is known to be true for exactly one 
 
3116
 
 
3117
  DESCRIPTION
 
3118
    Checks if an equality predicate can be used to take away
 
3119
    DISTINCT/GROUP BY because it is known to be true for exactly one
10449
3120
    distinct value (e.g. <expr> == <const>).
10450
 
    Arguments must be of the same type because e.g. 
10451
 
    <string_field> = <int_const> may match more than 1 distinct value from 
10452
 
    the column. 
10453
 
    We must take into consideration and the optimization done for various 
 
3121
    Arguments must be of the same type because e.g.
 
3122
    <string_field> = <int_const> may match more than 1 distinct value from
 
3123
    the column.
 
3124
    We must take into consideration and the optimization done for various
10454
3125
    string constants when compared to dates etc (see Item_int_with_ref) as
10455
3126
    well as the collation of the arguments.
10456
 
  
10457
 
  RETURN VALUE  
 
3127
 
 
3128
  RETURN VALUE
10458
3129
    true    can be used
10459
3130
    false   cannot be used
10460
3131
*/
10461
 
static bool
10462
 
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
3132
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10463
3133
{
10464
3134
  return r->const_item() &&
10465
3135
    /* elements must be compared as dates */
10474
3144
/**
10475
3145
  Return true if the item is a const value in all the WHERE clause.
10476
3146
*/
10477
 
 
10478
 
static bool
10479
 
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
3147
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
10480
3148
{
10481
3149
  if (cond->type() == Item::COND_ITEM)
10482
3150
  {
10489
3157
      bool res=const_expression_in_where(item, comp_item, const_item);
10490
3158
      if (res)                                  // Is a const value
10491
3159
      {
10492
 
        if (and_level)
10493
 
          return 1;
 
3160
        if (and_level)
 
3161
          return 1;
10494
3162
      }
10495
3163
      else if (!and_level)
10496
 
        return 0;
 
3164
        return 0;
10497
3165
    }
10498
3166
    return and_level ? 0 : 1;
10499
3167
  }
10501
3169
  {                                             // boolan compare function
10502
3170
    Item_func* func= (Item_func*) cond;
10503
3171
    if (func->functype() != Item_func::EQUAL_FUNC &&
10504
 
        func->functype() != Item_func::EQ_FUNC)
 
3172
              func->functype() != Item_func::EQ_FUNC)
10505
3173
      return 0;
10506
3174
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10507
3175
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10509
3177
    {
10510
3178
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10511
3179
      {
10512
 
        if (*const_item)
10513
 
          return right_item->eq(*const_item, 1);
10514
 
        *const_item=right_item;
10515
 
        return 1;
 
3180
        if (*const_item)
 
3181
          return right_item->eq(*const_item, 1);
 
3182
        *const_item=right_item;
 
3183
        return 1;
10516
3184
      }
10517
3185
    }
10518
3186
    else if (right_item->eq(comp_item,1))
10519
3187
    {
10520
3188
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10521
3189
      {
10522
 
        if (*const_item)
10523
 
          return left_item->eq(*const_item, 1);
10524
 
        *const_item=left_item;
10525
 
        return 1;
10526
 
      }
10527
 
    }
10528
 
  }
10529
 
  return 0;
10530
 
}
10531
 
 
10532
 
/****************************************************************************
10533
 
  Create internal temporary table
10534
 
****************************************************************************/
10535
 
 
10536
 
/**
10537
 
  Create field for temporary table from given field.
10538
 
 
10539
 
  @param thd           Thread handler
10540
 
  @param org_field    field from which new field will be created
10541
 
  @param name         New field name
10542
 
  @param table         Temporary table
10543
 
  @param item          !=NULL if item->result_field should point to new field.
10544
 
                      This is relevant for how fill_record() is going to work:
10545
 
                      If item != NULL then fill_record() will update
10546
 
                      the record in the original table.
10547
 
                      If item == NULL then fill_record() will update
10548
 
                      the temporary table
10549
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10550
 
                               field instead of blob.
10551
 
 
10552
 
  @retval
10553
 
    NULL                on error
10554
 
  @retval
10555
 
    new_created field
10556
 
*/
10557
 
 
10558
 
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10559
 
                                   const char *name, TABLE *table,
10560
 
                                   Item_field *item, uint convert_blob_length)
10561
 
{
10562
 
  Field *new_field;
10563
 
 
10564
 
  /* 
10565
 
    Make sure that the blob fits into a Field_varstring which has 
10566
 
    2-byte lenght. 
10567
 
  */
10568
 
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10569
 
      (org_field->flags & BLOB_FLAG))
10570
 
    new_field= new Field_varstring(convert_blob_length,
10571
 
                                   org_field->maybe_null(),
10572
 
                                   org_field->field_name, table->s,
10573
 
                                   org_field->charset());
10574
 
  else
10575
 
    new_field= org_field->new_field(thd->mem_root, table,
10576
 
                                    table == org_field->table);
10577
 
  if (new_field)
10578
 
  {
10579
 
    new_field->init(table);
10580
 
    new_field->orig_table= org_field->orig_table;
10581
 
    if (item)
10582
 
      item->result_field= new_field;
10583
 
    else
10584
 
      new_field->field_name= name;
10585
 
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10586
 
    if (org_field->maybe_null() || (item && item->maybe_null))
10587
 
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
10588
 
    if (org_field->type() == DRIZZLE_TYPE_VAR_STRING ||
10589
 
        org_field->type() == DRIZZLE_TYPE_VARCHAR)
10590
 
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10591
 
    else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
10592
 
      ((Field_double *) new_field)->not_fixed= true;
10593
 
  }
10594
 
  return new_field;
10595
 
}
10596
 
 
10597
 
/**
10598
 
  Create field for temporary table using type of given item.
10599
 
 
10600
 
  @param thd                   Thread handler
10601
 
  @param item                  Item to create a field for
10602
 
  @param table                 Temporary table
10603
 
  @param copy_func             If set and item is a function, store copy of
10604
 
                               item in this array
10605
 
  @param modify_item           1 if item->result_field should point to new
10606
 
                               item. This is relevent for how fill_record()
10607
 
                               is going to work:
10608
 
                               If modify_item is 1 then fill_record() will
10609
 
                               update the record in the original table.
10610
 
                               If modify_item is 0 then fill_record() will
10611
 
                               update the temporary table
10612
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10613
 
                               field instead of blob.
10614
 
 
10615
 
  @retval
10616
 
    0  on error
10617
 
  @retval
10618
 
    new_created field
10619
 
*/
10620
 
 
10621
 
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
10622
 
                                         Item *item, TABLE *table,
10623
 
                                         Item ***copy_func, bool modify_item,
10624
 
                                         uint convert_blob_length)
10625
 
{
10626
 
  bool maybe_null= item->maybe_null;
10627
 
  Field *new_field;
10628
 
 
10629
 
  switch (item->result_type()) {
10630
 
  case REAL_RESULT:
10631
 
    new_field= new Field_double(item->max_length, maybe_null,
10632
 
                                item->name, item->decimals, true);
10633
 
    break;
10634
 
  case INT_RESULT:
10635
 
    /* 
10636
 
      Select an integer type with the minimal fit precision.
10637
 
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10638
 
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
10639
 
      Field_long : make them Field_int64_t.  
10640
 
    */
10641
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10642
 
      new_field=new Field_int64_t(item->max_length, maybe_null,
10643
 
                                   item->name, item->unsigned_flag);
10644
 
    else
10645
 
      new_field=new Field_long(item->max_length, maybe_null,
10646
 
                               item->name, item->unsigned_flag);
10647
 
    break;
10648
 
  case STRING_RESULT:
10649
 
    assert(item->collation.collation);
10650
 
  
10651
 
    enum enum_field_types type;
10652
 
    /*
10653
 
      DATE/TIME fields have STRING_RESULT result type. 
10654
 
      To preserve type they needed to be handled separately.
10655
 
    */
10656
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
10657
 
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
10658
 
        type == DRIZZLE_TYPE_TIMESTAMP)
10659
 
      new_field= item->tmp_table_field_from_field_type(table, 1);
10660
 
    /* 
10661
 
      Make sure that the blob fits into a Field_varstring which has 
10662
 
      2-byte lenght. 
10663
 
    */
10664
 
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10665
 
             convert_blob_length <= Field_varstring::MAX_SIZE && 
10666
 
             convert_blob_length)
10667
 
      new_field= new Field_varstring(convert_blob_length, maybe_null,
10668
 
                                     item->name, table->s,
10669
 
                                     item->collation.collation);
10670
 
    else
10671
 
      new_field= item->make_string_field(table);
10672
 
    new_field->set_derivation(item->collation.derivation);
10673
 
    break;
10674
 
  case DECIMAL_RESULT:
10675
 
  {
10676
 
    uint8_t dec= item->decimals;
10677
 
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10678
 
    uint32_t len= item->max_length;
10679
 
 
10680
 
    /*
10681
 
      Trying to put too many digits overall in a DECIMAL(prec,dec)
10682
 
      will always throw a warning. We must limit dec to
10683
 
      DECIMAL_MAX_SCALE however to prevent an assert() later.
10684
 
    */
10685
 
 
10686
 
    if (dec > 0)
10687
 
    {
10688
 
      signed int overflow;
10689
 
 
10690
 
      dec= min(dec, DECIMAL_MAX_SCALE);
10691
 
 
10692
 
      /*
10693
 
        If the value still overflows the field with the corrected dec,
10694
 
        we'll throw out decimals rather than integers. This is still
10695
 
        bad and of course throws a truncation warning.
10696
 
        +1: for decimal point
10697
 
      */
10698
 
 
10699
 
      overflow= my_decimal_precision_to_length(intg + dec, dec,
10700
 
                                               item->unsigned_flag) - len;
10701
 
 
10702
 
      if (overflow > 0)
10703
 
        dec= max(0, dec - overflow);            // too long, discard fract
10704
 
      else
10705
 
        len -= item->decimals - dec;            // corrected value fits
10706
 
    }
10707
 
 
10708
 
    new_field= new Field_new_decimal(len, maybe_null, item->name,
10709
 
                                     dec, item->unsigned_flag);
10710
 
    break;
10711
 
  }
10712
 
  case ROW_RESULT:
10713
 
  default:
10714
 
    // This case should never be choosen
10715
 
    assert(0);
10716
 
    new_field= 0;
10717
 
    break;
10718
 
  }
10719
 
  if (new_field)
10720
 
    new_field->init(table);
10721
 
    
10722
 
  if (copy_func && item->is_result_field())
10723
 
    *((*copy_func)++) = item;                   // Save for copy_funcs
10724
 
  if (modify_item)
10725
 
    item->set_result_field(new_field);
10726
 
  if (item->type() == Item::NULL_ITEM)
10727
 
    new_field->is_created_from_null_item= true;
10728
 
  return new_field;
10729
 
}
10730
 
 
10731
 
 
10732
 
/**
10733
 
  Create field for information schema table.
10734
 
 
10735
 
  @param thd            Thread handler
10736
 
  @param table          Temporary table
10737
 
  @param item           Item to create a field for
10738
 
 
10739
 
  @retval
10740
 
    0                   on error
10741
 
  @retval
10742
 
    new_created field
10743
 
*/
10744
 
 
10745
 
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
10746
 
                                   Item *item, TABLE *table)
10747
 
{
10748
 
  if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
10749
 
  {
10750
 
    Field *field;
10751
 
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10752
 
      field= new Field_blob(item->max_length, item->maybe_null,
10753
 
                            item->name, item->collation.collation);
10754
 
    else
10755
 
      field= new Field_varstring(item->max_length, item->maybe_null,
10756
 
                                 item->name,
10757
 
                                 table->s, item->collation.collation);
10758
 
    if (field)
10759
 
      field->init(table);
10760
 
    return field;
10761
 
  }
10762
 
  return item->tmp_table_field_from_field_type(table, 0);
10763
 
}
10764
 
 
10765
 
 
10766
 
/**
10767
 
  Create field for temporary table.
10768
 
 
10769
 
  @param thd            Thread handler
10770
 
  @param table          Temporary table
10771
 
  @param item           Item to create a field for
10772
 
  @param type           Type of item (normally item->type)
10773
 
  @param copy_func      If set and item is a function, store copy of item
10774
 
                       in this array
10775
 
  @param from_field    if field will be created using other field as example,
10776
 
                       pointer example field will be written here
10777
 
  @param default_field  If field has a default value field, store it here
10778
 
  @param group          1 if we are going to do a relative group by on result
10779
 
  @param modify_item    1 if item->result_field should point to new item.
10780
 
                       This is relevent for how fill_record() is going to
10781
 
                       work:
10782
 
                       If modify_item is 1 then fill_record() will update
10783
 
                       the record in the original table.
10784
 
                       If modify_item is 0 then fill_record() will update
10785
 
                       the temporary table
10786
 
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
10787
 
                             field instead of blob.
10788
 
 
10789
 
  @retval
10790
 
    0                   on error
10791
 
  @retval
10792
 
    new_created field
10793
 
*/
10794
 
 
10795
 
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10796
 
                        Item ***copy_func, Field **from_field,
10797
 
                        Field **default_field,
10798
 
                        bool group, bool modify_item,
10799
 
                        bool table_cant_handle_bit_fields __attribute__((unused)),
10800
 
                        bool make_copy_field,
10801
 
                        uint convert_blob_length)
10802
 
{
10803
 
  Field *result;
10804
 
  Item::Type orig_type= type;
10805
 
  Item *orig_item= 0;
10806
 
 
10807
 
  if (type != Item::FIELD_ITEM &&
10808
 
      item->real_item()->type() == Item::FIELD_ITEM)
10809
 
  {
10810
 
    orig_item= item;
10811
 
    item= item->real_item();
10812
 
    type= Item::FIELD_ITEM;
10813
 
  }
10814
 
 
10815
 
  switch (type) {
10816
 
  case Item::SUM_FUNC_ITEM:
10817
 
  {
10818
 
    Item_sum *item_sum=(Item_sum*) item;
10819
 
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
10820
 
    if (!result)
10821
 
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10822
 
    return result;
10823
 
  }
10824
 
  case Item::FIELD_ITEM:
10825
 
  case Item::DEFAULT_VALUE_ITEM:
10826
 
  {
10827
 
    Item_field *field= (Item_field*) item;
10828
 
    bool orig_modify= modify_item;
10829
 
    if (orig_type == Item::REF_ITEM)
10830
 
      modify_item= 0;
10831
 
    /*
10832
 
      If item have to be able to store NULLs but underlaid field can't do it,
10833
 
      create_tmp_field_from_field() can't be used for tmp field creation.
10834
 
    */
10835
 
    if (field->maybe_null && !field->field->maybe_null())
10836
 
    {
10837
 
      result= create_tmp_field_from_item(thd, item, table, NULL,
10838
 
                                         modify_item, convert_blob_length);
10839
 
      *from_field= field->field;
10840
 
      if (result && modify_item)
10841
 
        field->result_field= result;
10842
 
    } 
10843
 
    else
10844
 
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
10845
 
                                          orig_item ? orig_item->name :
10846
 
                                          item->name,
10847
 
                                          table,
10848
 
                                          modify_item ? field :
10849
 
                                          NULL,
10850
 
                                          convert_blob_length);
10851
 
    if (orig_type == Item::REF_ITEM && orig_modify)
10852
 
      ((Item_ref*)orig_item)->set_result_field(result);
10853
 
    if (field->field->eq_def(result))
10854
 
      *default_field= field->field;
10855
 
    return result;
10856
 
  }
10857
 
  /* Fall through */
10858
 
  case Item::FUNC_ITEM:
10859
 
    /* Fall through */
10860
 
  case Item::COND_ITEM:
10861
 
  case Item::FIELD_AVG_ITEM:
10862
 
  case Item::FIELD_STD_ITEM:
10863
 
  case Item::SUBSELECT_ITEM:
10864
 
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
10865
 
  case Item::PROC_ITEM:
10866
 
  case Item::INT_ITEM:
10867
 
  case Item::REAL_ITEM:
10868
 
  case Item::DECIMAL_ITEM:
10869
 
  case Item::STRING_ITEM:
10870
 
  case Item::REF_ITEM:
10871
 
  case Item::NULL_ITEM:
10872
 
  case Item::VARBIN_ITEM:
10873
 
    if (make_copy_field)
10874
 
    {
10875
 
      assert(((Item_result_field*)item)->result_field);
10876
 
      *from_field= ((Item_result_field*)item)->result_field;
10877
 
    }
10878
 
    return create_tmp_field_from_item(thd, item, table,
10879
 
                                      (make_copy_field ? 0 : copy_func),
10880
 
                                       modify_item, convert_blob_length);
10881
 
  case Item::TYPE_HOLDER:  
10882
 
    result= ((Item_type_holder *)item)->make_field_by_type(table);
10883
 
    result->set_derivation(item->collation.derivation);
10884
 
    return result;
10885
 
  default:                                      // Dosen't have to be stored
10886
 
    return 0;
10887
 
  }
10888
 
}
10889
 
 
10890
 
/*
10891
 
  Set up column usage bitmaps for a temporary table
10892
 
 
10893
 
  IMPLEMENTATION
10894
 
    For temporary tables, we need one bitmap with all columns set and
10895
 
    a tmp_set bitmap to be used by things like filesort.
10896
 
*/
10897
 
 
10898
 
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10899
 
{
10900
 
  uint field_count= table->s->fields;
10901
 
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10902
 
              false);
10903
 
  bitmap_init(&table->tmp_set,
10904
 
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10905
 
              field_count, false);
10906
 
  /* write_set and all_set are copies of read_set */
10907
 
  table->def_write_set= table->def_read_set;
10908
 
  table->s->all_set= table->def_read_set;
10909
 
  bitmap_set_all(&table->s->all_set);
10910
 
  table->default_column_bitmaps();
10911
 
}
10912
 
 
10913
 
 
10914
 
/**
10915
 
  Create a temp table according to a field list.
10916
 
 
10917
 
  Given field pointers are changed to point at tmp_table for
10918
 
  send_fields. The table object is self contained: it's
10919
 
  allocated in its own memory root, as well as Field objects
10920
 
  created for table columns.
10921
 
  This function will replace Item_sum items in 'fields' list with
10922
 
  corresponding Item_field items, pointing at the fields in the
10923
 
  temporary table, unless this was prohibited by true
10924
 
  value of argument save_sum_fields. The Item_field objects
10925
 
  are created in THD memory root.
10926
 
 
10927
 
  @param thd                  thread handle
10928
 
  @param param                a description used as input to create the table
10929
 
  @param fields               list of items that will be used to define
10930
 
                              column types of the table (also see NOTES)
10931
 
  @param group                TODO document
10932
 
  @param distinct             should table rows be distinct
10933
 
  @param save_sum_fields      see NOTES
10934
 
  @param select_options
10935
 
  @param rows_limit
10936
 
  @param table_alias          possible name of the temporary table that can
10937
 
                              be used for name resolving; can be "".
10938
 
*/
10939
 
 
10940
 
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10941
 
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
10942
 
#define RATIO_TO_PACK_ROWS             2
10943
 
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
10944
 
 
10945
 
TABLE *
10946
 
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10947
 
                 ORDER *group, bool distinct, bool save_sum_fields,
10948
 
                 uint64_t select_options, ha_rows rows_limit,
10949
 
                 char *table_alias)
10950
 
{
10951
 
  MEM_ROOT *mem_root_save, own_root;
10952
 
  TABLE *table;
10953
 
  TABLE_SHARE *share;
10954
 
  uint  i,field_count,null_count,null_pack_length;
10955
 
  uint  copy_func_count= param->func_count;
10956
 
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
10957
 
  uint  blob_count,group_null_items, string_count;
10958
 
  uint  temp_pool_slot=MY_BIT_NONE;
10959
 
  uint fieldnr= 0;
10960
 
  ulong reclength, string_total_length;
10961
 
  bool  using_unique_constraint= 0;
10962
 
  bool  use_packed_rows= 0;
10963
 
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10964
 
  char  *tmpname,path[FN_REFLEN];
10965
 
  uchar *pos, *group_buff, *bitmaps;
10966
 
  uchar *null_flags;
10967
 
  Field **reg_field, **from_field, **default_field;
10968
 
  uint *blob_field;
10969
 
  Copy_field *copy=0;
10970
 
  KEY *keyinfo;
10971
 
  KEY_PART_INFO *key_part_info;
10972
 
  Item **copy_func;
10973
 
  MI_COLUMNDEF *recinfo;
10974
 
  uint total_uneven_bit_length= 0;
10975
 
  bool force_copy_fields= param->force_copy_fields;
10976
 
 
10977
 
  status_var_increment(thd->status_var.created_tmp_tables);
10978
 
 
10979
 
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10980
 
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10981
 
 
10982
 
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10983
 
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10984
 
            current_pid, temp_pool_slot);
10985
 
  else
10986
 
  {
10987
 
    /* if we run out of slots or we are not using tempool */
10988
 
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10989
 
            thd->thread_id, thd->tmp_table++);
10990
 
  }
10991
 
 
10992
 
  /*
10993
 
    No need to change table name to lower case as we are only creating
10994
 
    MyISAM or HEAP tables here
10995
 
  */
10996
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
10997
 
 
10998
 
 
10999
 
  if (group)
11000
 
  {
11001
 
    if (!param->quick_group)
11002
 
      group=0;                                  // Can't use group key
11003
 
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
11004
 
    {
11005
 
      /*
11006
 
        marker == 4 means two things:
11007
 
        - store NULLs in the key, and
11008
 
        - convert BIT fields to 64-bit long, needed because MEMORY tables
11009
 
          can't index BIT fields.
11010
 
      */
11011
 
      (*tmp->item)->marker= 4;
11012
 
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11013
 
        using_unique_constraint=1;
11014
 
    }
11015
 
    if (param->group_length >= MAX_BLOB_WIDTH)
11016
 
      using_unique_constraint=1;
11017
 
    if (group)
11018
 
      distinct=0;                               // Can't use distinct
11019
 
  }
11020
 
 
11021
 
  field_count=param->field_count+param->func_count+param->sum_func_count;
11022
 
  hidden_field_count=param->hidden_field_count;
11023
 
 
11024
 
  /*
11025
 
    When loose index scan is employed as access method, it already
11026
 
    computes all groups and the result of all aggregate functions. We
11027
 
    make space for the items of the aggregate function in the list of
11028
 
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11029
 
    these items are stored in the temporary table.
11030
 
  */
11031
 
  if (param->precomputed_group_by)
11032
 
    copy_func_count+= param->sum_func_count;
11033
 
  
11034
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11035
 
 
11036
 
  if (!multi_alloc_root(&own_root,
11037
 
                        &table, sizeof(*table),
11038
 
                        &share, sizeof(*share),
11039
 
                        &reg_field, sizeof(Field*) * (field_count+1),
11040
 
                        &default_field, sizeof(Field*) * (field_count),
11041
 
                        &blob_field, sizeof(uint)*(field_count+1),
11042
 
                        &from_field, sizeof(Field*)*field_count,
11043
 
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
11044
 
                        &param->keyinfo, sizeof(*param->keyinfo),
11045
 
                        &key_part_info,
11046
 
                        sizeof(*key_part_info)*(param->group_parts+1),
11047
 
                        &param->start_recinfo,
11048
 
                        sizeof(*param->recinfo)*(field_count*2+4),
11049
 
                        &tmpname, (uint) strlen(path)+1,
11050
 
                        &group_buff, (group && ! using_unique_constraint ?
11051
 
                                      param->group_length : 0),
11052
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
11053
 
                        NullS))
11054
 
  {
11055
 
    if (temp_pool_slot != MY_BIT_NONE)
11056
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11057
 
    return(NULL);                               /* purecov: inspected */
11058
 
  }
11059
 
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11060
 
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11061
 
  {
11062
 
    if (temp_pool_slot != MY_BIT_NONE)
11063
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11064
 
    free_root(&own_root, MYF(0));               /* purecov: inspected */
11065
 
    return(NULL);                               /* purecov: inspected */
11066
 
  }
11067
 
  param->items_to_copy= copy_func;
11068
 
  strmov(tmpname,path);
11069
 
  /* make table according to fields */
11070
 
 
11071
 
  bzero((char*) table,sizeof(*table));
11072
 
  bzero((char*) reg_field,sizeof(Field*)*(field_count+1));
11073
 
  bzero((char*) default_field, sizeof(Field*) * (field_count));
11074
 
  bzero((char*) from_field,sizeof(Field*)*field_count);
11075
 
 
11076
 
  table->mem_root= own_root;
11077
 
  mem_root_save= thd->mem_root;
11078
 
  thd->mem_root= &table->mem_root;
11079
 
 
11080
 
  table->field=reg_field;
11081
 
  table->alias= table_alias;
11082
 
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
11083
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11084
 
  table->map=1;
11085
 
  table->temp_pool_slot = temp_pool_slot;
11086
 
  table->copy_blobs= 1;
11087
 
  table->in_use= thd;
11088
 
  table->quick_keys.init();
11089
 
  table->covering_keys.init();
11090
 
  table->keys_in_use_for_query.init();
11091
 
 
11092
 
  table->s= share;
11093
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11094
 
  share->blob_field= blob_field;
11095
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11096
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11097
 
  share->table_charset= param->table_charset;
11098
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
11099
 
  share->keys_for_keyread.init();
11100
 
  share->keys_in_use.init();
11101
 
 
11102
 
  /* Calculate which type of fields we will store in the temporary table */
11103
 
 
11104
 
  reclength= string_total_length= 0;
11105
 
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11106
 
  param->using_indirect_summary_function=0;
11107
 
 
11108
 
  List_iterator_fast<Item> li(fields);
11109
 
  Item *item;
11110
 
  Field **tmp_from_field=from_field;
11111
 
  while ((item=li++))
11112
 
  {
11113
 
    Item::Type type=item->type();
11114
 
    if (not_all_columns)
11115
 
    {
11116
 
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11117
 
      {
11118
 
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
11119
 
          item->update_used_tables();
11120
 
        if (type == Item::SUBSELECT_ITEM ||
11121
 
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
11122
 
        {
11123
 
          /*
11124
 
            Mark that the we have ignored an item that refers to a summary
11125
 
            function. We need to know this if someone is going to use
11126
 
            DISTINCT on the result.
11127
 
          */
11128
 
          param->using_indirect_summary_function=1;
11129
 
          continue;
11130
 
        }
11131
 
      }
11132
 
      if (item->const_item() && (int) hidden_field_count <= 0)
11133
 
        continue; // We don't have to store this
11134
 
    }
11135
 
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11136
 
    {                                           /* Can't calc group yet */
11137
 
      ((Item_sum*) item)->result_field=0;
11138
 
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11139
 
      {
11140
 
        Item **argp= ((Item_sum*) item)->args + i;
11141
 
        Item *arg= *argp;
11142
 
        if (!arg->const_item())
11143
 
        {
11144
 
          Field *new_field=
11145
 
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
11146
 
                             tmp_from_field, &default_field[fieldnr],
11147
 
                             group != 0,not_all_columns,
11148
 
                             distinct, 0,
11149
 
                             param->convert_blob_length);
11150
 
          if (!new_field)
11151
 
            goto err;                                   // Should be OOM
11152
 
          tmp_from_field++;
11153
 
          reclength+=new_field->pack_length();
11154
 
          if (new_field->flags & BLOB_FLAG)
11155
 
          {
11156
 
            *blob_field++= fieldnr;
11157
 
            blob_count++;
11158
 
          }
11159
 
          *(reg_field++)= new_field;
11160
 
          if (new_field->real_type() == DRIZZLE_TYPE_STRING ||
11161
 
              new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
11162
 
          {
11163
 
            string_count++;
11164
 
            string_total_length+= new_field->pack_length();
11165
 
          }
11166
 
          thd->mem_root= mem_root_save;
11167
 
          thd->change_item_tree(argp, new Item_field(new_field));
11168
 
          thd->mem_root= &table->mem_root;
11169
 
          if (!(new_field->flags & NOT_NULL_FLAG))
11170
 
          {
11171
 
            null_count++;
11172
 
            /*
11173
 
              new_field->maybe_null() is still false, it will be
11174
 
              changed below. But we have to setup Item_field correctly
11175
 
            */
11176
 
            (*argp)->maybe_null=1;
11177
 
          }
11178
 
          new_field->field_index= fieldnr++;
11179
 
        }
11180
 
      }
11181
 
    }
11182
 
    else
11183
 
    {
11184
 
      /*
11185
 
        The last parameter to create_tmp_field() is a bit tricky:
11186
 
 
11187
 
        We need to set it to 0 in union, to get fill_record() to modify the
11188
 
        temporary table.
11189
 
        We need to set it to 1 on multi-table-update and in select to
11190
 
        write rows to the temporary table.
11191
 
        We here distinguish between UNION and multi-table-updates by the fact
11192
 
        that in the later case group is set to the row pointer.
11193
 
      */
11194
 
      Field *new_field= (param->schema_table) ?
11195
 
        create_tmp_field_for_schema(thd, item, table) :
11196
 
        create_tmp_field(thd, table, item, type, &copy_func,
11197
 
                         tmp_from_field, &default_field[fieldnr],
11198
 
                         group != 0,
11199
 
                         !force_copy_fields &&
11200
 
                           (not_all_columns || group !=0),
11201
 
                         /*
11202
 
                           If item->marker == 4 then we force create_tmp_field
11203
 
                           to create a 64-bit longs for BIT fields because HEAP
11204
 
                           tables can't index BIT fields directly. We do the same
11205
 
                           for distinct, as we want the distinct index to be
11206
 
                           usable in this case too.
11207
 
                         */
11208
 
                         item->marker == 4 || param->bit_fields_as_long,
11209
 
                         force_copy_fields,
11210
 
                         param->convert_blob_length);
11211
 
 
11212
 
      if (!new_field)
11213
 
      {
11214
 
        if (thd->is_fatal_error)
11215
 
          goto err;                             // Got OOM
11216
 
        continue;                               // Some kindf of const item
11217
 
      }
11218
 
      if (type == Item::SUM_FUNC_ITEM)
11219
 
        ((Item_sum *) item)->result_field= new_field;
11220
 
      tmp_from_field++;
11221
 
      reclength+=new_field->pack_length();
11222
 
      if (!(new_field->flags & NOT_NULL_FLAG))
11223
 
        null_count++;
11224
 
      if (new_field->flags & BLOB_FLAG)
11225
 
      {
11226
 
        *blob_field++= fieldnr;
11227
 
        blob_count++;
11228
 
      }
11229
 
      if (item->marker == 4 && item->maybe_null)
11230
 
      {
11231
 
        group_null_items++;
11232
 
        new_field->flags|= GROUP_FLAG;
11233
 
      }
11234
 
      new_field->field_index= fieldnr++;
11235
 
      *(reg_field++)= new_field;
11236
 
    }
11237
 
    if (!--hidden_field_count)
11238
 
    {
11239
 
      /*
11240
 
        This was the last hidden field; Remember how many hidden fields could
11241
 
        have null
11242
 
      */
11243
 
      hidden_null_count=null_count;
11244
 
      /*
11245
 
        We need to update hidden_field_count as we may have stored group
11246
 
        functions with constant arguments
11247
 
      */
11248
 
      param->hidden_field_count= fieldnr;
11249
 
      null_count= 0;
11250
 
    }
11251
 
  }
11252
 
  assert(fieldnr == (uint) (reg_field - table->field));
11253
 
  assert(field_count >= (uint) (reg_field - table->field));
11254
 
  field_count= fieldnr;
11255
 
  *reg_field= 0;
11256
 
  *blob_field= 0;                               // End marker
11257
 
  share->fields= field_count;
11258
 
 
11259
 
  /* If result table is small; use a heap */
11260
 
  /* future: storage engine selection can be made dynamic? */
11261
 
  if (blob_count || using_unique_constraint ||
11262
 
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11263
 
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11264
 
  {
11265
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11266
 
    table->file= get_new_handler(share, &table->mem_root,
11267
 
                                 share->db_type());
11268
 
    if (group &&
11269
 
        (param->group_parts > table->file->max_key_parts() ||
11270
 
         param->group_length > table->file->max_key_length()))
11271
 
      using_unique_constraint=1;
11272
 
  }
11273
 
  else
11274
 
  {
11275
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
11276
 
    table->file= get_new_handler(share, &table->mem_root,
11277
 
                                 share->db_type());
11278
 
  }
11279
 
  if (!table->file)
11280
 
    goto err;
11281
 
 
11282
 
 
11283
 
  if (!using_unique_constraint)
11284
 
    reclength+= group_null_items;       // null flag is stored separately
11285
 
 
11286
 
  share->blob_fields= blob_count;
11287
 
  if (blob_count == 0)
11288
 
  {
11289
 
    /* We need to ensure that first byte is not 0 for the delete link */
11290
 
    if (param->hidden_field_count)
11291
 
      hidden_null_count++;
11292
 
    else
11293
 
      null_count++;
11294
 
  }
11295
 
  hidden_null_pack_length=(hidden_null_count+7)/8;
11296
 
  null_pack_length= (hidden_null_pack_length +
11297
 
                     (null_count + total_uneven_bit_length + 7) / 8);
11298
 
  reclength+=null_pack_length;
11299
 
  if (!reclength)
11300
 
    reclength=1;                                // Dummy select
11301
 
  /* Use packed rows if there is blobs or a lot of space to gain */
11302
 
  if (blob_count || ((string_total_length >= STRING_TOTAL_LENGTH_TO_PACK_ROWS) && (reclength / string_total_length <= RATIO_TO_PACK_ROWS || (string_total_length / string_count) >= AVG_STRING_LENGTH_TO_PACK_ROWS)))
11303
 
    use_packed_rows= 1;
11304
 
 
11305
 
  share->reclength= reclength;
11306
 
  {
11307
 
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11308
 
    share->rec_buff_length= alloc_length;
11309
 
    if (!(table->record[0]= (uchar*)
11310
 
                            alloc_root(&table->mem_root, alloc_length*3)))
11311
 
      goto err;
11312
 
    table->record[1]= table->record[0]+alloc_length;
11313
 
    share->default_values= table->record[1]+alloc_length;
11314
 
  }
11315
 
  copy_func[0]=0;                               // End marker
11316
 
  param->func_count= copy_func - param->items_to_copy; 
11317
 
 
11318
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11319
 
 
11320
 
  recinfo=param->start_recinfo;
11321
 
  null_flags=(uchar*) table->record[0];
11322
 
  pos=table->record[0]+ null_pack_length;
11323
 
  if (null_pack_length)
11324
 
  {
11325
 
    bzero((uchar*) recinfo,sizeof(*recinfo));
11326
 
    recinfo->type=FIELD_NORMAL;
11327
 
    recinfo->length=null_pack_length;
11328
 
    recinfo++;
11329
 
    bfill(null_flags,null_pack_length,255);     // Set null fields
11330
 
 
11331
 
    table->null_flags= (uchar*) table->record[0];
11332
 
    share->null_fields= null_count+ hidden_null_count;
11333
 
    share->null_bytes= null_pack_length;
11334
 
  }
11335
 
  null_count= (blob_count == 0) ? 1 : 0;
11336
 
  hidden_field_count=param->hidden_field_count;
11337
 
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11338
 
  {
11339
 
    Field *field= *reg_field;
11340
 
    uint length;
11341
 
    bzero((uchar*) recinfo,sizeof(*recinfo));
11342
 
 
11343
 
    if (!(field->flags & NOT_NULL_FLAG))
11344
 
    {
11345
 
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
11346
 
      {
11347
 
        /*
11348
 
          We have to reserve one byte here for NULL bits,
11349
 
          as this is updated by 'end_update()'
11350
 
        */
11351
 
        *pos++=0;                               // Null is stored here
11352
 
        recinfo->length=1;
11353
 
        recinfo->type=FIELD_NORMAL;
11354
 
        recinfo++;
11355
 
        bzero((uchar*) recinfo,sizeof(*recinfo));
11356
 
      }
11357
 
      else
11358
 
      {
11359
 
        recinfo->null_bit= 1 << (null_count & 7);
11360
 
        recinfo->null_pos= null_count/8;
11361
 
      }
11362
 
      field->move_field(pos,null_flags+null_count/8,
11363
 
                        1 << (null_count & 7));
11364
 
      null_count++;
11365
 
    }
11366
 
    else
11367
 
      field->move_field(pos,(uchar*) 0,0);
11368
 
    field->reset();
11369
 
 
11370
 
    /*
11371
 
      Test if there is a default field value. The test for ->ptr is to skip
11372
 
      'offset' fields generated by initalize_tables
11373
 
    */
11374
 
    if (default_field[i] && default_field[i]->ptr)
11375
 
    {
11376
 
      /* 
11377
 
         default_field[i] is set only in the cases  when 'field' can
11378
 
         inherit the default value that is defined for the field referred
11379
 
         by the Item_field object from which 'field' has been created.
11380
 
      */
11381
 
      my_ptrdiff_t diff;
11382
 
      Field *orig_field= default_field[i];
11383
 
      /* Get the value from default_values */
11384
 
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11385
 
                            orig_field->table->record[0]);
11386
 
      orig_field->move_field_offset(diff);      // Points now at default_values
11387
 
      if (orig_field->is_real_null())
11388
 
        field->set_null();
11389
 
      else
11390
 
      {
11391
 
        field->set_notnull();
11392
 
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
11393
 
      }
11394
 
      orig_field->move_field_offset(-diff);     // Back to record[0]
11395
 
    } 
11396
 
 
11397
 
    if (from_field[i])
11398
 
    {                                           /* Not a table Item */
11399
 
      copy->set(field,from_field[i],save_sum_fields);
11400
 
      copy++;
11401
 
    }
11402
 
    length=field->pack_length();
11403
 
    pos+= length;
11404
 
 
11405
 
    /* Make entry for create table */
11406
 
    recinfo->length=length;
11407
 
    if (field->flags & BLOB_FLAG)
11408
 
      recinfo->type= (int) FIELD_BLOB;
11409
 
    else if (use_packed_rows &&
11410
 
             field->real_type() == DRIZZLE_TYPE_STRING &&
11411
 
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11412
 
      recinfo->type=FIELD_SKIP_ENDSPACE;
11413
 
    else
11414
 
      recinfo->type=FIELD_NORMAL;
11415
 
    if (!--hidden_field_count)
11416
 
      null_count=(null_count+7) & ~7;           // move to next byte
11417
 
 
11418
 
    // fix table name in field entry
11419
 
    field->table_name= &table->alias;
11420
 
  }
11421
 
 
11422
 
  param->copy_field_end=copy;
11423
 
  param->recinfo=recinfo;
11424
 
  store_record(table,s->default_values);        // Make empty default record
11425
 
 
11426
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
11427
 
    share->max_rows= ~(ha_rows) 0;
11428
 
  else
11429
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11430
 
                                 min(thd->variables.tmp_table_size,
11431
 
                                     thd->variables.max_heap_table_size) :
11432
 
                                 thd->variables.tmp_table_size) /
11433
 
                                 share->reclength);
11434
 
  set_if_bigger(share->max_rows,1);             // For dummy start options
11435
 
  /*
11436
 
    Push the LIMIT clause to the temporary table creation, so that we
11437
 
    materialize only up to 'rows_limit' records instead of all result records.
11438
 
  */
11439
 
  set_if_smaller(share->max_rows, rows_limit);
11440
 
  param->end_write_records= rows_limit;
11441
 
 
11442
 
  keyinfo= param->keyinfo;
11443
 
 
11444
 
  if (group)
11445
 
  {
11446
 
    table->group=group;                         /* Table is grouped by key */
11447
 
    param->group_buff=group_buff;
11448
 
    share->keys=1;
11449
 
    share->uniques= test(using_unique_constraint);
11450
 
    table->key_info=keyinfo;
11451
 
    keyinfo->key_part=key_part_info;
11452
 
    keyinfo->flags=HA_NOSAME;
11453
 
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11454
 
    keyinfo->key_length=0;
11455
 
    keyinfo->rec_per_key=0;
11456
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11457
 
    keyinfo->name= (char*) "group_key";
11458
 
    ORDER *cur_group= group;
11459
 
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11460
 
    {
11461
 
      Field *field=(*cur_group->item)->get_tmp_table_field();
11462
 
      bool maybe_null=(*cur_group->item)->maybe_null;
11463
 
      key_part_info->null_bit=0;
11464
 
      key_part_info->field=  field;
11465
 
      key_part_info->offset= field->offset(table->record[0]);
11466
 
      key_part_info->length= (uint16_t) field->key_length();
11467
 
      key_part_info->type=   (uint8_t) field->key_type();
11468
 
      key_part_info->key_type =
11469
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11470
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11471
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11472
 
        0 : FIELDFLAG_BINARY;
11473
 
      if (!using_unique_constraint)
11474
 
      {
11475
 
        cur_group->buff=(char*) group_buff;
11476
 
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11477
 
                                                     group_buff +
11478
 
                                                     test(maybe_null),
11479
 
                                                     field->null_ptr,
11480
 
                                                     field->null_bit)))
11481
 
          goto err; /* purecov: inspected */
11482
 
        if (maybe_null)
11483
 
        {
11484
 
          /*
11485
 
            To be able to group on NULL, we reserved place in group_buff
11486
 
            for the NULL flag just before the column. (see above).
11487
 
            The field data is after this flag.
11488
 
            The NULL flag is updated in 'end_update()' and 'end_write()'
11489
 
          */
11490
 
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
11491
 
          key_part_info->null_bit=field->null_bit;
11492
 
          key_part_info->null_offset= (uint) (field->null_ptr -
11493
 
                                              (uchar*) table->record[0]);
11494
 
          cur_group->buff++;                        // Pointer to field data
11495
 
          group_buff++;                         // Skipp null flag
11496
 
        }
11497
 
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11498
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11499
 
        group_buff+= cur_group->field->pack_length();
11500
 
      }
11501
 
      keyinfo->key_length+=  key_part_info->length;
11502
 
    }
11503
 
  }
11504
 
 
11505
 
  if (distinct && field_count != param->hidden_field_count)
11506
 
  {
11507
 
    /*
11508
 
      Create an unique key or an unique constraint over all columns
11509
 
      that should be in the result.  In the temporary table, there are
11510
 
      'param->hidden_field_count' extra columns, whose null bits are stored
11511
 
      in the first 'hidden_null_pack_length' bytes of the row.
11512
 
    */
11513
 
    if (blob_count)
11514
 
    {
11515
 
      /*
11516
 
        Special mode for index creation in MyISAM used to support unique
11517
 
        indexes on blobs with arbitrary length. Such indexes cannot be
11518
 
        used for lookups.
11519
 
      */
11520
 
      share->uniques= 1;
11521
 
    }
11522
 
    null_pack_length-=hidden_null_pack_length;
11523
 
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11524
 
                         (share->uniques ? test(null_pack_length) : 0));
11525
 
    table->distinct= 1;
11526
 
    share->keys= 1;
11527
 
    if (!(key_part_info= (KEY_PART_INFO*)
11528
 
          alloc_root(&table->mem_root,
11529
 
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11530
 
      goto err;
11531
 
    bzero((void*) key_part_info, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11532
 
    table->key_info=keyinfo;
11533
 
    keyinfo->key_part=key_part_info;
11534
 
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11535
 
    keyinfo->key_length=(uint16_t) reclength;
11536
 
    keyinfo->name= (char*) "distinct_key";
11537
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11538
 
    keyinfo->rec_per_key=0;
11539
 
 
11540
 
    /*
11541
 
      Create an extra field to hold NULL bits so that unique indexes on
11542
 
      blobs can distinguish NULL from 0. This extra field is not needed
11543
 
      when we do not use UNIQUE indexes for blobs.
11544
 
    */
11545
 
    if (null_pack_length && share->uniques)
11546
 
    {
11547
 
      key_part_info->null_bit=0;
11548
 
      key_part_info->offset=hidden_null_pack_length;
11549
 
      key_part_info->length=null_pack_length;
11550
 
      key_part_info->field= new Field_string(table->record[0],
11551
 
                                             (uint32_t) key_part_info->length,
11552
 
                                             (uchar*) 0,
11553
 
                                             (uint) 0,
11554
 
                                             Field::NONE,
11555
 
                                             NullS, &my_charset_bin);
11556
 
      if (!key_part_info->field)
11557
 
        goto err;
11558
 
      key_part_info->field->init(table);
11559
 
      key_part_info->key_type=FIELDFLAG_BINARY;
11560
 
      key_part_info->type=    HA_KEYTYPE_BINARY;
11561
 
      key_part_info++;
11562
 
    }
11563
 
    /* Create a distinct key over the columns we are going to return */
11564
 
    for (i=param->hidden_field_count, reg_field=table->field + i ;
11565
 
         i < field_count;
11566
 
         i++, reg_field++, key_part_info++)
11567
 
    {
11568
 
      key_part_info->null_bit=0;
11569
 
      key_part_info->field=    *reg_field;
11570
 
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
11571
 
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
11572
 
      /* TODO:
11573
 
        The below method of computing the key format length of the
11574
 
        key part is a copy/paste from opt_range.cc, and table.cc.
11575
 
        This should be factored out, e.g. as a method of Field.
11576
 
        In addition it is not clear if any of the Field::*_length
11577
 
        methods is supposed to compute the same length. If so, it
11578
 
        might be reused.
11579
 
      */
11580
 
      key_part_info->store_length= key_part_info->length;
11581
 
 
11582
 
      if ((*reg_field)->real_maybe_null())
11583
 
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11584
 
      if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB || 
11585
 
          (*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
11586
 
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11587
 
 
11588
 
      key_part_info->type=     (uint8_t) (*reg_field)->key_type();
11589
 
      key_part_info->key_type =
11590
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11591
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11592
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11593
 
        0 : FIELDFLAG_BINARY;
11594
 
    }
11595
 
  }
11596
 
 
11597
 
  if (thd->is_fatal_error)                              // If end of memory
11598
 
    goto err;                                    /* purecov: inspected */
11599
 
  share->db_record_offset= 1;
11600
 
  if (share->db_type() == myisam_hton)
11601
 
  {
11602
 
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11603
 
                                &param->recinfo, select_options))
11604
 
      goto err;
11605
 
  }
11606
 
  if (open_tmp_table(table))
11607
 
    goto err;
11608
 
 
11609
 
  thd->mem_root= mem_root_save;
11610
 
 
11611
 
  return(table);
11612
 
 
11613
 
err:
11614
 
  thd->mem_root= mem_root_save;
11615
 
  free_tmp_table(thd,table);                    /* purecov: inspected */
11616
 
  if (temp_pool_slot != MY_BIT_NONE)
11617
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11618
 
  return(NULL);                         /* purecov: inspected */
11619
 
}
11620
 
 
11621
 
 
11622
 
 
11623
 
 
11624
 
/*
11625
 
  Create a temporary table to weed out duplicate rowid combinations
11626
 
 
11627
 
  SYNOPSIS
11628
 
 
11629
 
    create_duplicate_weedout_tmp_table()
11630
 
      thd
11631
 
      uniq_tuple_length_arg
11632
 
      SJ_TMP_TABLE 
11633
 
 
11634
 
  DESCRIPTION
11635
 
    Create a temporary table to weed out duplicate rowid combinations. The
11636
 
    table has a single column that is a concatenation of all rowids in the
11637
 
    combination. 
11638
 
 
11639
 
    Depending on the needed length, there are two cases:
11640
 
 
11641
 
    1. When the length of the column < max_key_length:
11642
 
 
11643
 
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11644
 
 
11645
 
    2. Otherwise (not a valid SQL syntax but internally supported):
11646
 
 
11647
 
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11648
 
 
11649
 
    The code in this function was produced by extraction of relevant parts
11650
 
    from create_tmp_table().
11651
 
 
11652
 
  RETURN
11653
 
    created table
11654
 
    NULL on error
11655
 
*/
11656
 
 
11657
 
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
11658
 
                                          uint uniq_tuple_length_arg,
11659
 
                                          SJ_TMP_TABLE *sjtbl)
11660
 
{
11661
 
  MEM_ROOT *mem_root_save, own_root;
11662
 
  TABLE *table;
11663
 
  TABLE_SHARE *share;
11664
 
  uint  temp_pool_slot=MY_BIT_NONE;
11665
 
  char  *tmpname,path[FN_REFLEN];
11666
 
  Field **reg_field;
11667
 
  KEY_PART_INFO *key_part_info;
11668
 
  KEY *keyinfo;
11669
 
  uchar *group_buff;
11670
 
  uchar *bitmaps;
11671
 
  uint *blob_field;
11672
 
  MI_COLUMNDEF *recinfo, *start_recinfo;
11673
 
  bool using_unique_constraint=false;
11674
 
  bool use_packed_rows= false;
11675
 
  Field *field, *key_field;
11676
 
  uint blob_count, null_pack_length, null_count;
11677
 
  uchar *null_flags;
11678
 
  uchar *pos;
11679
 
  
11680
 
  /*
11681
 
    STEP 1: Get temporary table name
11682
 
  */
11683
 
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11684
 
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11685
 
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11686
 
 
11687
 
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11688
 
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11689
 
            current_pid, temp_pool_slot);
11690
 
  else
11691
 
  {
11692
 
    /* if we run out of slots or we are not using tempool */
11693
 
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11694
 
            thd->thread_id, thd->tmp_table++);
11695
 
  }
11696
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11697
 
 
11698
 
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
11699
 
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11700
 
    using_unique_constraint= true;
11701
 
 
11702
 
  /* STEP 3: Allocate memory for temptable description */
11703
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11704
 
  if (!multi_alloc_root(&own_root,
11705
 
                        &table, sizeof(*table),
11706
 
                        &share, sizeof(*share),
11707
 
                        &reg_field, sizeof(Field*) * (1+1),
11708
 
                        &blob_field, sizeof(uint)*2,
11709
 
                        &keyinfo, sizeof(*keyinfo),
11710
 
                        &key_part_info, sizeof(*key_part_info) * 2,
11711
 
                        &start_recinfo,
11712
 
                        sizeof(*recinfo)*(1*2+4),
11713
 
                        &tmpname, (uint) strlen(path)+1,
11714
 
                        &group_buff, (!using_unique_constraint ?
11715
 
                                      uniq_tuple_length_arg : 0),
11716
 
                        &bitmaps, bitmap_buffer_size(1)*2,
11717
 
                        NullS))
11718
 
  {
11719
 
    if (temp_pool_slot != MY_BIT_NONE)
11720
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11721
 
    return(NULL);
11722
 
  }
11723
 
  strmov(tmpname,path);
11724
 
  
11725
 
 
11726
 
  /* STEP 4: Create TABLE description */
11727
 
  bzero((char*) table,sizeof(*table));
11728
 
  bzero((char*) reg_field,sizeof(Field*)*2);
11729
 
 
11730
 
  table->mem_root= own_root;
11731
 
  mem_root_save= thd->mem_root;
11732
 
  thd->mem_root= &table->mem_root;
11733
 
 
11734
 
  table->field=reg_field;
11735
 
  table->alias= "weedout-tmp";
11736
 
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
11737
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11738
 
  table->map=1;
11739
 
  table->temp_pool_slot = temp_pool_slot;
11740
 
  table->copy_blobs= 1;
11741
 
  table->in_use= thd;
11742
 
  table->quick_keys.init();
11743
 
  table->covering_keys.init();
11744
 
  table->keys_in_use_for_query.init();
11745
 
 
11746
 
  table->s= share;
11747
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11748
 
  share->blob_field= blob_field;
11749
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11750
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11751
 
  share->table_charset= NULL;
11752
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
11753
 
  share->keys_for_keyread.init();
11754
 
  share->keys_in_use.init();
11755
 
 
11756
 
  blob_count= 0;
11757
 
 
11758
 
  /* Create the field */
11759
 
  {
11760
 
    /*
11761
 
      For the sake of uniformity, always use Field_varstring (altough we could
11762
 
      use Field_string for shorter keys)
11763
 
    */
11764
 
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11765
 
                               &my_charset_bin);
11766
 
    if (!field)
11767
 
      return(0);
11768
 
    field->table= table;
11769
 
    field->key_start.init(0);
11770
 
    field->part_of_key.init(0);
11771
 
    field->part_of_sortkey.init(0);
11772
 
    field->unireg_check= Field::NONE;
11773
 
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11774
 
    field->reset_fields();
11775
 
    field->init(table);
11776
 
    field->orig_table= NULL;
11777
 
     
11778
 
    field->field_index= 0;
11779
 
    
11780
 
    *(reg_field++)= field;
11781
 
    *blob_field= 0;
11782
 
    *reg_field= 0;
11783
 
 
11784
 
    share->fields= 1;
11785
 
    share->blob_fields= 0;
11786
 
  }
11787
 
 
11788
 
  uint reclength= field->pack_length();
11789
 
  if (using_unique_constraint)
11790
 
  { 
11791
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11792
 
    table->file= get_new_handler(share, &table->mem_root,
11793
 
                                 share->db_type());
11794
 
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
11795
 
  }
11796
 
  else
11797
 
  {
11798
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
11799
 
    table->file= get_new_handler(share, &table->mem_root,
11800
 
                                 share->db_type());
11801
 
  }
11802
 
  if (!table->file)
11803
 
    goto err;
11804
 
 
11805
 
  null_count=1;
11806
 
  
11807
 
  null_pack_length= 1;
11808
 
  reclength += null_pack_length;
11809
 
 
11810
 
  share->reclength= reclength;
11811
 
  {
11812
 
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11813
 
    share->rec_buff_length= alloc_length;
11814
 
    if (!(table->record[0]= (uchar*)
11815
 
                            alloc_root(&table->mem_root, alloc_length*3)))
11816
 
      goto err;
11817
 
    table->record[1]= table->record[0]+alloc_length;
11818
 
    share->default_values= table->record[1]+alloc_length;
11819
 
  }
11820
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11821
 
 
11822
 
  recinfo= start_recinfo;
11823
 
  null_flags=(uchar*) table->record[0];
11824
 
  pos=table->record[0]+ null_pack_length;
11825
 
  if (null_pack_length)
11826
 
  {
11827
 
    bzero((uchar*) recinfo,sizeof(*recinfo));
11828
 
    recinfo->type=FIELD_NORMAL;
11829
 
    recinfo->length=null_pack_length;
11830
 
    recinfo++;
11831
 
    bfill(null_flags,null_pack_length,255);     // Set null fields
11832
 
 
11833
 
    table->null_flags= (uchar*) table->record[0];
11834
 
    share->null_fields= null_count;
11835
 
    share->null_bytes= null_pack_length;
11836
 
  }
11837
 
  null_count=1;
11838
 
 
11839
 
  {
11840
 
    //Field *field= *reg_field;
11841
 
    uint length;
11842
 
    bzero((uchar*) recinfo,sizeof(*recinfo));
11843
 
    field->move_field(pos,(uchar*) 0,0);
11844
 
 
11845
 
    field->reset();
11846
 
    /*
11847
 
      Test if there is a default field value. The test for ->ptr is to skip
11848
 
      'offset' fields generated by initalize_tables
11849
 
    */
11850
 
    // Initialize the table field:
11851
 
    bzero(field->ptr, field->pack_length());
11852
 
 
11853
 
    length=field->pack_length();
11854
 
    pos+= length;
11855
 
 
11856
 
    /* Make entry for create table */
11857
 
    recinfo->length=length;
11858
 
    if (field->flags & BLOB_FLAG)
11859
 
      recinfo->type= (int) FIELD_BLOB;
11860
 
    else if (use_packed_rows &&
11861
 
             field->real_type() == DRIZZLE_TYPE_STRING &&
11862
 
             length >= MIN_STRING_LENGTH_TO_PACK_ROWS)
11863
 
      recinfo->type=FIELD_SKIP_ENDSPACE;
11864
 
    else
11865
 
      recinfo->type=FIELD_NORMAL;
11866
 
 
11867
 
    field->table_name= &table->alias;
11868
 
  }
11869
 
 
11870
 
  //param->recinfo=recinfo;
11871
 
  //store_record(table,s->default_values);        // Make empty default record
11872
 
 
11873
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
11874
 
    share->max_rows= ~(ha_rows) 0;
11875
 
  else
11876
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11877
 
                                 min(thd->variables.tmp_table_size,
11878
 
                                     thd->variables.max_heap_table_size) :
11879
 
                                 thd->variables.tmp_table_size) /
11880
 
                                 share->reclength);
11881
 
  set_if_bigger(share->max_rows,1);             // For dummy start options
11882
 
 
11883
 
 
11884
 
  //// keyinfo= param->keyinfo;
11885
 
  if (true)
11886
 
  {
11887
 
    share->keys=1;
11888
 
    share->uniques= test(using_unique_constraint);
11889
 
    table->key_info=keyinfo;
11890
 
    keyinfo->key_part=key_part_info;
11891
 
    keyinfo->flags=HA_NOSAME;
11892
 
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11893
 
    keyinfo->key_length=0;
11894
 
    keyinfo->rec_per_key=0;
11895
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11896
 
    keyinfo->name= (char*) "weedout_key";
11897
 
    {
11898
 
      key_part_info->null_bit=0;
11899
 
      key_part_info->field=  field;
11900
 
      key_part_info->offset= field->offset(table->record[0]);
11901
 
      key_part_info->length= (uint16_t) field->key_length();
11902
 
      key_part_info->type=   (uint8_t) field->key_type();
11903
 
      key_part_info->key_type = FIELDFLAG_BINARY;
11904
 
      if (!using_unique_constraint)
11905
 
      {
11906
 
        if (!(key_field= field->new_key_field(thd->mem_root, table,
11907
 
                                              group_buff,
11908
 
                                              field->null_ptr,
11909
 
                                              field->null_bit)))
11910
 
          goto err;
11911
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11912
 
      }
11913
 
      keyinfo->key_length+=  key_part_info->length;
11914
 
    }
11915
 
  }
11916
 
 
11917
 
  if (thd->is_fatal_error)                              // If end of memory
11918
 
    goto err;
11919
 
  share->db_record_offset= 1;
11920
 
  if (share->db_type() == myisam_hton)
11921
 
  {
11922
 
    recinfo++;
11923
 
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11924
 
      goto err;
11925
 
  }
11926
 
  sjtbl->start_recinfo= start_recinfo;
11927
 
  sjtbl->recinfo=       recinfo;
11928
 
  if (open_tmp_table(table))
11929
 
    goto err;
11930
 
 
11931
 
  thd->mem_root= mem_root_save;
11932
 
  return(table);
11933
 
 
11934
 
err:
11935
 
  thd->mem_root= mem_root_save;
11936
 
  free_tmp_table(thd,table);                    /* purecov: inspected */
11937
 
  if (temp_pool_slot != MY_BIT_NONE)
11938
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11939
 
  return(NULL);                         /* purecov: inspected */
11940
 
}
11941
 
 
11942
 
/****************************************************************************/
11943
 
 
11944
 
/**
11945
 
  Create a reduced TABLE object with properly set up Field list from a
11946
 
  list of field definitions.
11947
 
 
11948
 
    The created table doesn't have a table handler associated with
11949
 
    it, has no keys, no group/distinct, no copy_funcs array.
11950
 
    The sole purpose of this TABLE object is to use the power of Field
11951
 
    class to read/write data to/from table->record[0]. Then one can store
11952
 
    the record in any container (RB tree, hash, etc).
11953
 
    The table is created in THD mem_root, so are the table's fields.
11954
 
    Consequently, if you don't BLOB fields, you don't need to free it.
11955
 
 
11956
 
  @param thd         connection handle
11957
 
  @param field_list  list of column definitions
11958
 
 
11959
 
  @return
11960
 
    0 if out of memory, TABLE object in case of success
11961
 
*/
11962
 
 
11963
 
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11964
 
{
11965
 
  uint field_count= field_list.elements;
11966
 
  uint blob_count= 0;
11967
 
  Field **field;
11968
 
  Create_field *cdef;                           /* column definition */
11969
 
  uint record_length= 0;
11970
 
  uint null_count= 0;                 /* number of columns which may be null */
11971
 
  uint null_pack_length;              /* NULL representation array length */
11972
 
  uint *blob_field;
11973
 
  uchar *bitmaps;
11974
 
  TABLE *table;
11975
 
  TABLE_SHARE *share;
11976
 
 
11977
 
  if (!multi_alloc_root(thd->mem_root,
11978
 
                        &table, sizeof(*table),
11979
 
                        &share, sizeof(*share),
11980
 
                        &field, (field_count + 1) * sizeof(Field*),
11981
 
                        &blob_field, (field_count+1) *sizeof(uint),
11982
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
11983
 
                        NullS))
11984
 
    return 0;
11985
 
 
11986
 
  bzero(table, sizeof(*table));
11987
 
  bzero(share, sizeof(*share));
11988
 
  table->field= field;
11989
 
  table->s= share;
11990
 
  share->blob_field= blob_field;
11991
 
  share->fields= field_count;
11992
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11993
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11994
 
 
11995
 
  /* Create all fields and calculate the total length of record */
11996
 
  List_iterator_fast<Create_field> it(field_list);
11997
 
  while ((cdef= it++))
11998
 
  {
11999
 
    *field= make_field(share, 0, cdef->length,
12000
 
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
12001
 
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
12002
 
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
12003
 
                       cdef->unireg_check,
12004
 
                       cdef->interval, cdef->field_name);
12005
 
    if (!*field)
12006
 
      goto error;
12007
 
    (*field)->init(table);
12008
 
    record_length+= (*field)->pack_length();
12009
 
    if (! ((*field)->flags & NOT_NULL_FLAG))
12010
 
      null_count++;
12011
 
 
12012
 
    if ((*field)->flags & BLOB_FLAG)
12013
 
      share->blob_field[blob_count++]= (uint) (field - table->field);
12014
 
 
12015
 
    field++;
12016
 
  }
12017
 
  *field= NULL;                             /* mark the end of the list */
12018
 
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
12019
 
  share->blob_fields= blob_count;
12020
 
 
12021
 
  null_pack_length= (null_count + 7)/8;
12022
 
  share->reclength= record_length + null_pack_length;
12023
 
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12024
 
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12025
 
  if (!table->record[0])
12026
 
    goto error;
12027
 
 
12028
 
  if (null_pack_length)
12029
 
  {
12030
 
    table->null_flags= (uchar*) table->record[0];
12031
 
    share->null_fields= null_count;
12032
 
    share->null_bytes= null_pack_length;
12033
 
  }
12034
 
 
12035
 
  table->in_use= thd;           /* field->reset() may access table->in_use */
12036
 
  {
12037
 
    /* Set up field pointers */
12038
 
    uchar *null_pos= table->record[0];
12039
 
    uchar *field_pos= null_pos + share->null_bytes;
12040
 
    uint null_bit= 1;
12041
 
 
12042
 
    for (field= table->field; *field; ++field)
12043
 
    {
12044
 
      Field *cur_field= *field;
12045
 
      if ((cur_field->flags & NOT_NULL_FLAG))
12046
 
        cur_field->move_field(field_pos);
12047
 
      else
12048
 
      {
12049
 
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12050
 
        null_bit<<= 1;
12051
 
        if (null_bit == (1 << 8))
12052
 
        {
12053
 
          ++null_pos;
12054
 
          null_bit= 1;
12055
 
        }
12056
 
      }
12057
 
      cur_field->reset();
12058
 
 
12059
 
      field_pos+= cur_field->pack_length();
12060
 
    }
12061
 
  }
12062
 
  return table;
12063
 
error:
12064
 
  for (field= table->field; *field; ++field)
12065
 
    delete *field;                         /* just invokes field destructor */
12066
 
  return 0;
12067
 
}
12068
 
 
12069
 
 
12070
 
static bool open_tmp_table(TABLE *table)
12071
 
{
12072
 
  int error;
12073
 
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12074
 
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12075
 
  {
12076
 
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
12077
 
    table->db_stat=0;
12078
 
    return(1);
12079
 
  }
12080
 
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
12081
 
  return(0);
12082
 
}
12083
 
 
12084
 
 
12085
 
/*
12086
 
  Create MyISAM temporary table
12087
 
 
12088
 
  SYNOPSIS
12089
 
    create_myisam_tmp_table()
12090
 
      table           Table object that descrimes the table to be created
12091
 
      keyinfo         Description of the index (there is always one index)
12092
 
      start_recinfo   MyISAM's column descriptions
12093
 
      recinfo INOUT   End of MyISAM's column descriptions
12094
 
      options         Option bits
12095
 
   
12096
 
  DESCRIPTION
12097
 
    Create a MyISAM temporary table according to passed description. The is
12098
 
    assumed to have one unique index or constraint.
12099
 
 
12100
 
    The passed array or MI_COLUMNDEF structures must have this form:
12101
 
 
12102
 
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12103
 
         when there are many nullable columns)
12104
 
      2. Table columns
12105
 
      3. One free MI_COLUMNDEF element (*recinfo points here)
12106
 
   
12107
 
    This function may use the free element to create hash column for unique
12108
 
    constraint.
12109
 
 
12110
 
   RETURN
12111
 
     false - OK
12112
 
     true  - Error
12113
 
*/
12114
 
 
12115
 
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
12116
 
                                    MI_COLUMNDEF *start_recinfo,
12117
 
                                    MI_COLUMNDEF **recinfo, 
12118
 
                                    uint64_t options)
12119
 
{
12120
 
  int error;
12121
 
  MI_KEYDEF keydef;
12122
 
  MI_UNIQUEDEF uniquedef;
12123
 
  TABLE_SHARE *share= table->s;
12124
 
 
12125
 
  if (share->keys)
12126
 
  {                                             // Get keys for ni_create
12127
 
    bool using_unique_constraint=0;
12128
 
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12129
 
                                            sizeof(*seg) * keyinfo->key_parts);
12130
 
    if (!seg)
12131
 
      goto err;
12132
 
 
12133
 
    bzero(seg, sizeof(*seg) * keyinfo->key_parts);
12134
 
    if (keyinfo->key_length >= table->file->max_key_length() ||
12135
 
        keyinfo->key_parts > table->file->max_key_parts() ||
12136
 
        share->uniques)
12137
 
    {
12138
 
      /* Can't create a key; Make a unique constraint instead of a key */
12139
 
      share->keys=    0;
12140
 
      share->uniques= 1;
12141
 
      using_unique_constraint=1;
12142
 
      bzero((char*) &uniquedef,sizeof(uniquedef));
12143
 
      uniquedef.keysegs=keyinfo->key_parts;
12144
 
      uniquedef.seg=seg;
12145
 
      uniquedef.null_are_equal=1;
12146
 
 
12147
 
      /* Create extra column for hash value */
12148
 
      bzero((uchar*) *recinfo,sizeof(**recinfo));
12149
 
      (*recinfo)->type= FIELD_CHECK;
12150
 
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12151
 
      (*recinfo)++;
12152
 
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
12153
 
    }
12154
 
    else
12155
 
    {
12156
 
      /* Create an unique key */
12157
 
      bzero((char*) &keydef,sizeof(keydef));
12158
 
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12159
 
      keydef.keysegs=  keyinfo->key_parts;
12160
 
      keydef.seg= seg;
12161
 
    }
12162
 
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12163
 
    {
12164
 
      Field *field=keyinfo->key_part[i].field;
12165
 
      seg->flag=     0;
12166
 
      seg->language= field->charset()->number;
12167
 
      seg->length=   keyinfo->key_part[i].length;
12168
 
      seg->start=    keyinfo->key_part[i].offset;
12169
 
      if (field->flags & BLOB_FLAG)
12170
 
      {
12171
 
        seg->type=
12172
 
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12173
 
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12174
 
        seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12175
 
        seg->flag= HA_BLOB_PART;
12176
 
        seg->length=0;                  // Whole blob in unique constraint
12177
 
      }
12178
 
      else
12179
 
      {
12180
 
        seg->type= keyinfo->key_part[i].type;
12181
 
        /* Tell handler if it can do suffic space compression */
12182
 
        if (field->real_type() == DRIZZLE_TYPE_STRING &&
12183
 
            keyinfo->key_part[i].length > 4)
12184
 
          seg->flag|= HA_SPACE_PACK;
12185
 
      }
12186
 
      if (!(field->flags & NOT_NULL_FLAG))
12187
 
      {
12188
 
        seg->null_bit= field->null_bit;
12189
 
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12190
 
        /*
12191
 
          We are using a GROUP BY on something that contains NULL
12192
 
          In this case we have to tell MyISAM that two NULL should
12193
 
          on INSERT be regarded at the same value
12194
 
        */
12195
 
        if (!using_unique_constraint)
12196
 
          keydef.flag|= HA_NULL_ARE_EQUAL;
12197
 
      }
12198
 
    }
12199
 
  }
12200
 
  MI_CREATE_INFO create_info;
12201
 
  bzero((char*) &create_info,sizeof(create_info));
12202
 
 
12203
 
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12204
 
      OPTION_BIG_TABLES)
12205
 
    create_info.data_file_length= ~(uint64_t) 0;
12206
 
 
12207
 
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12208
 
                       (uint) (*recinfo-start_recinfo),
12209
 
                       start_recinfo,
12210
 
                       share->uniques, &uniquedef,
12211
 
                       &create_info,
12212
 
                       HA_CREATE_TMP_TABLE)))
12213
 
  {
12214
 
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
12215
 
    table->db_stat=0;
12216
 
    goto err;
12217
 
  }
12218
 
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12219
 
  share->db_record_offset= 1;
12220
 
  return(0);
12221
 
 err:
12222
 
  return(1);
12223
 
}
12224
 
 
12225
 
 
12226
 
void
12227
 
free_tmp_table(THD *thd, TABLE *entry)
12228
 
{
12229
 
  MEM_ROOT own_root= entry->mem_root;
12230
 
  const char *save_proc_info;
12231
 
 
12232
 
  save_proc_info=thd->proc_info;
12233
 
  thd_proc_info(thd, "removing tmp table");
12234
 
 
12235
 
  if (entry->file)
12236
 
  {
12237
 
    if (entry->db_stat)
12238
 
      entry->file->ha_drop_table(entry->s->table_name.str);
12239
 
    else
12240
 
      entry->file->ha_delete_table(entry->s->table_name.str);
12241
 
    delete entry->file;
12242
 
  }
12243
 
 
12244
 
  /* free blobs */
12245
 
  for (Field **ptr=entry->field ; *ptr ; ptr++)
12246
 
    (*ptr)->free();
12247
 
  free_io_cache(entry);
12248
 
 
12249
 
  if (entry->temp_pool_slot != MY_BIT_NONE)
12250
 
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12251
 
 
12252
 
  plugin_unlock(0, entry->s->db_plugin);
12253
 
 
12254
 
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12255
 
  thd_proc_info(thd, save_proc_info);
12256
 
 
12257
 
  return;
12258
 
}
12259
 
 
12260
 
/**
12261
 
  If a HEAP table gets full, create a MyISAM table and copy all rows
12262
 
  to this.
12263
 
*/
12264
 
 
12265
 
bool create_myisam_from_heap(THD *thd, TABLE *table,
12266
 
                             MI_COLUMNDEF *start_recinfo,
12267
 
                             MI_COLUMNDEF **recinfo, 
12268
 
                             int error, bool ignore_last_dupp_key_error)
12269
 
{
12270
 
  TABLE new_table;
12271
 
  TABLE_SHARE share;
12272
 
  const char *save_proc_info;
12273
 
  int write_err;
12274
 
 
12275
 
  if (table->s->db_type() != heap_hton || 
12276
 
      error != HA_ERR_RECORD_FILE_FULL)
12277
 
  {
12278
 
    table->file->print_error(error,MYF(0));
12279
 
    return(1);
12280
 
  }
12281
 
  new_table= *table;
12282
 
  share= *table->s;
12283
 
  new_table.s= &share;
12284
 
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12285
 
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12286
 
                                        new_table.s->db_type())))
12287
 
    return(1);                          // End of memory
12288
 
 
12289
 
  save_proc_info=thd->proc_info;
12290
 
  thd_proc_info(thd, "converting HEAP to MyISAM");
12291
 
 
12292
 
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12293
 
                              recinfo, thd->lex->select_lex.options | 
12294
 
                                               thd->options))
12295
 
    goto err2;
12296
 
  if (open_tmp_table(&new_table))
12297
 
    goto err1;
12298
 
  if (table->file->indexes_are_disabled())
12299
 
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12300
 
  table->file->ha_index_or_rnd_end();
12301
 
  table->file->ha_rnd_init(1);
12302
 
  if (table->no_rows)
12303
 
  {
12304
 
    new_table.file->extra(HA_EXTRA_NO_ROWS);
12305
 
    new_table.no_rows=1;
12306
 
  }
12307
 
 
12308
 
#ifdef TO_BE_DONE_LATER_IN_4_1
12309
 
  /*
12310
 
    To use start_bulk_insert() (which is new in 4.1) we need to find
12311
 
    all places where a corresponding end_bulk_insert() should be put.
12312
 
  */
12313
 
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12314
 
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
12315
 
#else
12316
 
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12317
 
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12318
 
#endif
12319
 
 
12320
 
  /*
12321
 
    copy all old rows from heap table to MyISAM table
12322
 
    This is the only code that uses record[1] to read/write but this
12323
 
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12324
 
  */
12325
 
  while (!table->file->rnd_next(new_table.record[1]))
12326
 
  {
12327
 
    write_err= new_table.file->ha_write_row(new_table.record[1]);
12328
 
    if (write_err)
12329
 
      goto err;
12330
 
  }
12331
 
  /* copy row that filled HEAP table */
12332
 
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
12333
 
  {
12334
 
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12335
 
        !ignore_last_dupp_key_error)
12336
 
      goto err;
12337
 
  }
12338
 
 
12339
 
  /* remove heap table and change to use myisam table */
12340
 
  (void) table->file->ha_rnd_end();
12341
 
  (void) table->file->close();                  // This deletes the table !
12342
 
  delete table->file;
12343
 
  table->file=0;
12344
 
  plugin_unlock(0, table->s->db_plugin);
12345
 
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12346
 
  new_table.s= table->s;                       // Keep old share
12347
 
  *table= new_table;
12348
 
  *table->s= share;
12349
 
  
12350
 
  table->file->change_table_ptr(table, table->s);
12351
 
  table->use_all_columns();
12352
 
  if (save_proc_info)
12353
 
  {
12354
 
    const char *new_proc_info=
12355
 
      (!strcmp(save_proc_info,"Copying to tmp table") ?
12356
 
      "Copying to tmp table on disk" : save_proc_info);
12357
 
    thd_proc_info(thd, new_proc_info);
12358
 
  }
12359
 
  return(0);
12360
 
 
12361
 
 err:
12362
 
  table->file->print_error(write_err, MYF(0));
12363
 
  (void) table->file->ha_rnd_end();
12364
 
  (void) new_table.file->close();
12365
 
 err1:
12366
 
  new_table.file->ha_delete_table(new_table.s->table_name.str);
12367
 
 err2:
12368
 
  delete new_table.file;
12369
 
  thd_proc_info(thd, save_proc_info);
12370
 
  table->mem_root= new_table.mem_root;
12371
 
  return(1);
12372
 
}
12373
 
 
 
3190
        if (*const_item)
 
3191
          return left_item->eq(*const_item, 1);
 
3192
        *const_item=left_item;
 
3193
        return 1;
 
3194
      }
 
3195
    }
 
3196
  }
 
3197
  return 0;
 
3198
}
12374
3199
 
12375
3200
/**
12376
3201
  @details
12383
3208
  @return
12384
3209
    end_select function to use. This function can't fail.
12385
3210
*/
12386
 
 
12387
3211
Next_select_func setup_end_select_func(JOIN *join)
12388
3212
{
12389
 
  TABLE *table= join->tmp_table;
12390
 
  TMP_TABLE_PARAM *tmp_tbl= &join->tmp_table_param;
 
3213
  Table *table= join->tmp_table;
 
3214
  Tmp_Table_Param *tmp_tbl= &join->tmp_table_param;
12391
3215
  Next_select_func end_select;
12392
3216
 
12393
3217
  /* Set up select_end */
12394
3218
  if (table)
12395
3219
  {
12396
 
    if (table->group && tmp_tbl->sum_func_count && 
 
3220
    if (table->group && tmp_tbl->sum_func_count &&
12397
3221
        !tmp_tbl->precomputed_group_by)
12398
3222
    {
12399
3223
      if (table->s->keys)
12400
3224
      {
12401
 
        end_select=end_update;
 
3225
        end_select= end_update;
12402
3226
      }
12403
3227
      else
12404
3228
      {
12405
 
        end_select=end_unique_update;
 
3229
        end_select= end_unique_update;
12406
3230
      }
12407
3231
    }
12408
3232
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
12417
3241
        /*
12418
3242
          A preceding call to create_tmp_table in the case when loose
12419
3243
          index scan is used guarantees that
12420
 
          TMP_TABLE_PARAM::items_to_copy has enough space for the group
 
3244
          Tmp_Table_Param::items_to_copy has enough space for the group
12421
3245
          by functions. It is OK here to use memcpy since we copy
12422
3246
          Item_sum pointers into an array of Item pointers.
12423
3247
        */
12439
3263
  return end_select;
12440
3264
}
12441
3265
 
12442
 
 
12443
3266
/**
12444
3267
  Make a join of all tables and write it on socket or to table.
12445
3268
 
12450
3273
  @retval
12451
3274
    -1  if error should be sent
12452
3275
*/
12453
 
 
12454
 
static int
12455
 
do_select(JOIN *join,List<Item> *fields,TABLE *table)
 
3276
int do_select(JOIN *join, List<Item> *fields, Table *table)
12456
3277
{
12457
3278
  int rc= 0;
12458
3279
  enum_nested_loop_state error= NESTED_LOOP_OK;
12459
 
  JOIN_TAB *join_tab= NULL;
12460
 
  
 
3280
  JoinTable *join_tab= NULL;
 
3281
 
12461
3282
  join->tmp_table= table;                       /* Save for easy recursion */
12462
3283
  join->fields= fields;
12463
3284
 
12464
3285
  if (table)
12465
3286
  {
12466
 
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
12467
 
    empty_record(table);
 
3287
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
3288
    table->emptyRecord();
12468
3289
    if (table->group && join->tmp_table_param.sum_func_count &&
12469
 
        table->s->keys && !table->file->inited)
12470
 
      table->file->ha_index_init(0, 0);
 
3290
        table->s->keys && !table->cursor->inited)
 
3291
      table->cursor->ha_index_init(0, 0);
12471
3292
  }
12472
3293
  /* Set up select_end */
12473
3294
  Next_select_func end_select= setup_end_select_func(join);
12488
3309
    {
12489
3310
      error= (*end_select)(join, 0, 0);
12490
3311
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
12491
 
        error= (*end_select)(join, 0, 1);
 
3312
              error= (*end_select)(join, 0, 1);
12492
3313
 
12493
3314
      /*
12494
3315
        If we don't go through evaluate_join_record(), do the counting
12496
3317
        so we don't touch it here.
12497
3318
      */
12498
3319
      join->examined_rows++;
12499
 
      join->thd->row_count++;
 
3320
      join->session->row_count++;
12500
3321
      assert(join->examined_rows <= 1);
12501
3322
    }
12502
3323
    else if (join->send_row_on_empty_set())
12526
3347
    if (!table)                                 // If sending data to client
12527
3348
    {
12528
3349
      /*
12529
 
        The following will unlock all cursors if the command wasn't an
12530
 
        update command
 
3350
        The following will unlock all cursors if the command wasn't an
 
3351
        update command
12531
3352
      */
12532
3353
      join->join_free();                        // Unlock all cursors
12533
3354
      if (join->result->send_eof())
12534
 
        rc= 1;                                  // Don't send error
 
3355
        rc= 1;                                  // Don't send error
12535
3356
    }
12536
3357
  }
12537
3358
  else
12539
3360
  if (table)
12540
3361
  {
12541
3362
    int tmp, new_errno= 0;
12542
 
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
 
3363
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
12543
3364
    {
12544
3365
      new_errno= tmp;
12545
3366
    }
12546
 
    if ((tmp=table->file->ha_index_or_rnd_end()))
 
3367
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
12547
3368
    {
12548
3369
      new_errno= tmp;
12549
3370
    }
12550
3371
    if (new_errno)
12551
 
      table->file->print_error(new_errno,MYF(0));
 
3372
      table->print_error(new_errno,MYF(0));
12552
3373
  }
12553
 
  return(join->thd->is_error() ? -1 : rc);
 
3374
  return(join->session->is_error() ? -1 : rc);
12554
3375
}
12555
3376
 
12556
 
 
12557
 
enum_nested_loop_state
12558
 
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
3377
enum_nested_loop_state sub_select_cache(JOIN *join, JoinTable *join_tab, bool end_of_records)
12559
3378
{
12560
3379
  enum_nested_loop_state rc;
12561
3380
 
12566
3385
      rc= sub_select(join,join_tab,end_of_records);
12567
3386
    return rc;
12568
3387
  }
12569
 
  if (join->thd->killed)                // If aborted by user
 
3388
  if (join->session->killed)            // If aborted by user
12570
3389
  {
12571
 
    join->thd->send_kill_message();
12572
 
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
 
3390
    join->session->send_kill_message();
 
3391
    return NESTED_LOOP_KILLED;
12573
3392
  }
12574
3393
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
12575
3394
  {
12576
 
    if (!store_record_in_cache(&join_tab->cache))
 
3395
    if (! store_record_in_cache(&join_tab->cache))
12577
3396
      return NESTED_LOOP_OK;                     // There is more room in cache
12578
3397
    return flush_cached_records(join,join_tab,false);
12579
3398
  }
12586
3405
/**
12587
3406
  Retrieve records ends with a given beginning from the result of a join.
12588
3407
 
12589
 
    For a given partial join record consisting of records from the tables 
 
3408
    For a given partial join record consisting of records from the tables
12590
3409
    preceding the table join_tab in the execution plan, the function
12591
3410
    retrieves all matching full records from the result set and
12592
 
    send them to the result set stream. 
 
3411
    send them to the result set stream.
12593
3412
 
12594
3413
  @note
12595
3414
    The function effectively implements the  final (n-k) nested loops
12629
3448
    first row with t3.a=t1.a has been encountered.
12630
3449
    Thus, the second predicate P2 is supplied with a guarded value that are
12631
3450
    stored in the field 'found' of the first inner table for the outer join
12632
 
    (table t2). When the first row with t3.a=t1.a for the  current row 
 
3451
    (table t2). When the first row with t3.a=t1.a for the  current row
12633
3452
    of table t1  appears, the value becomes true. For now on the predicate
12634
3453
    is evaluated immediately after the row of table t2 has been read.
12635
3454
    When the first row with t3.a=t1.a has been encountered all
12637
3456
    Only when all of them are true the row is sent to the output stream.
12638
3457
    If not, the function returns to the lowest nest level that has a false
12639
3458
    attached condition.
12640
 
    The predicates from on expressions are also pushed down. If in the 
 
3459
    The predicates from on expressions are also pushed down. If in the
12641
3460
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
12642
3461
    then t1.a=t2.a would be pushed down to table t2, and without any
12643
3462
    guard.
12647
3466
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
12648
3467
    are checked for the composed row almost in the same way as it had
12649
3468
    been done for the first row with a match. The only difference is
12650
 
    the predicates from on expressions are not checked. 
 
3469
    the predicates from on expressions are not checked.
12651
3470
 
12652
3471
  @par
12653
3472
  @b IMPLEMENTATION
12663
3482
    and a pointer to a guarding boolean variable.
12664
3483
    When the value of the guard variable is true the value of the object
12665
3484
    is the same as the value of the predicate, otherwise it's just returns
12666
 
    true. 
12667
 
    To carry out a return to a nested loop level of join table t the pointer 
 
3485
    true.
 
3486
    To carry out a return to a nested loop level of join table t the pointer
12668
3487
    to t is remembered in the field 'return_tab' of the join structure.
12669
3488
    Consider the following query:
12670
3489
    @code
12681
3500
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
12682
3501
    becomes 'activated', as well the predicate t4.a=t2.a. But
12683
3502
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
12684
 
    t4.a=t2.a becomes true. 
 
3503
    t4.a=t2.a becomes true.
12685
3504
    In order not to re-evaluate the predicates that were already evaluated
12686
3505
    as attached pushed down predicates, a pointer to the the first
12687
3506
    most inner unmatched table is maintained in join_tab->first_unmatched.
12688
3507
    Thus, when the first row from t5 with t5.a=t3.a is found
12689
 
    this pointer for t5 is changed from t4 to t2.             
 
3508
    this pointer for t5 is changed from t4 to t2.
12690
3509
 
12691
3510
    @par
12692
3511
    @b STRUCTURE @b NOTES
12697
3516
  @param join      pointer to the structure providing all context info for
12698
3517
                   the query
12699
3518
  @param join_tab  the first next table of the execution plan to be retrieved
12700
 
  @param end_records  true when we need to perform final steps of retrival   
 
3519
  @param end_records  true when we need to perform final steps of retrival
12701
3520
 
12702
3521
  @return
12703
3522
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
12704
3523
*/
12705
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
12706
 
 
12707
 
enum_nested_loop_state
12708
 
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
3524
enum_nested_loop_state sub_select(JOIN *join, JoinTable *join_tab, bool end_of_records)
12709
3525
{
12710
3526
  join_tab->table->null_row=0;
12711
3527
  if (end_of_records)
12715
3531
  enum_nested_loop_state rc;
12716
3532
  READ_RECORD *info= &join_tab->read_record;
12717
3533
 
12718
 
  if (join_tab->flush_weedout_table)
12719
 
  {
12720
 
    do_sj_reset(join_tab->flush_weedout_table);
12721
 
  }
12722
 
 
12723
3534
  if (join->resume_nested_loop)
12724
3535
  {
12725
3536
    /* If not the last table, plunge down the nested loop */
12746
3557
      /* Set first_unmatched for the last inner table of this group */
12747
3558
      join_tab->last_inner->first_unmatched= join_tab;
12748
3559
    }
12749
 
    join->thd->row_count= 0;
 
3560
    join->session->row_count= 0;
12750
3561
 
12751
3562
    error= (*join_tab->read_first_record)(join_tab);
12752
3563
    rc= evaluate_join_record(join, join_tab, error);
12753
3564
  }
12754
 
  
12755
 
  /* 
12756
 
    Note: psergey has added the 2nd part of the following condition; the 
 
3565
 
 
3566
  /*
 
3567
    Note: psergey has added the 2nd part of the following condition; the
12757
3568
    change should probably be made in 5.1, too.
12758
3569
  */
12759
3570
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
12771
3582
  return rc;
12772
3583
}
12773
3584
 
12774
 
 
12775
 
 
12776
 
 
12777
 
/*
12778
 
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
12779
 
 
12780
 
  SYNPOSIS
12781
 
    do_sj_dups_weedout()
12782
 
      
12783
 
  RETURN
12784
 
    -1  Error
12785
 
    1   The row combination is a duplicate (discard it)
12786
 
    0   The row combination is not a duplicate (continue)
12787
 
*/
12788
 
 
12789
 
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
12790
 
{
12791
 
  int error;
12792
 
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
12793
 
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
12794
 
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
12795
 
  uchar *nulls_ptr= ptr;
12796
 
  
12797
 
  /* Put the the rowids tuple into table->record[0]: */
12798
 
 
12799
 
  // 1. Store the length 
12800
 
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
12801
 
  {
12802
 
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
12803
 
    ptr++;
12804
 
  }
12805
 
  else
12806
 
  {
12807
 
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
12808
 
    ptr += 2;
12809
 
  }
12810
 
 
12811
 
  // 2. Zero the null bytes 
12812
 
  if (sjtbl->null_bytes)
12813
 
  {
12814
 
    bzero(ptr, sjtbl->null_bytes);
12815
 
    ptr += sjtbl->null_bytes; 
12816
 
  }
12817
 
 
12818
 
  // 3. Put the rowids
12819
 
  for (uint i=0; tab != tab_end; tab++, i++)
12820
 
  {
12821
 
    handler *h= tab->join_tab->table->file;
12822
 
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
12823
 
    {
12824
 
      /* It's a NULL-complemented row */
12825
 
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
12826
 
      bzero(ptr + tab->rowid_offset, h->ref_length);
12827
 
    }
12828
 
    else
12829
 
    {
12830
 
      /* Copy the rowid value */
12831
 
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
12832
 
        h->position(tab->join_tab->table->record[0]);
12833
 
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
12834
 
    }
12835
 
  }
12836
 
 
12837
 
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
12838
 
  if (error)
12839
 
  {
12840
 
    /* create_myisam_from_heap will generate error if needed */
12841
 
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
12842
 
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
12843
 
                                &sjtbl->recinfo, error, 1))
12844
 
      return -1;
12845
 
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
12846
 
    return 1;
12847
 
  }
12848
 
  return 0;
12849
 
}
12850
 
 
12851
 
 
12852
 
/*
12853
 
  SemiJoinDuplicateElimination: Reset the temporary table
12854
 
*/
12855
 
 
12856
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
12857
 
{
12858
 
  if (sj_tbl->tmp_table)
12859
 
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
12860
 
  return 0;
12861
 
}
12862
 
 
12863
 
/*
12864
 
  Process one record of the nested loop join.
12865
 
 
12866
 
    This function will evaluate parts of WHERE/ON clauses that are
12867
 
    applicable to the partial record on hand and in case of success
12868
 
    submit this record to the next level of the nested loop.
12869
 
*/
12870
 
 
12871
 
static enum_nested_loop_state
12872
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
12873
 
                     int error)
12874
 
{
12875
 
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
12876
 
  ha_rows found_records=join->found_records;
12877
 
  COND *select_cond= join_tab->select_cond;
12878
 
 
12879
 
  if (error > 0 || (join->thd->is_error()))     // Fatal error
12880
 
    return NESTED_LOOP_ERROR;
12881
 
  if (error < 0)
12882
 
    return NESTED_LOOP_NO_MORE_ROWS;
12883
 
  if (join->thd->killed)                        // Aborted by user
12884
 
  {
12885
 
    join->thd->send_kill_message();
12886
 
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
12887
 
  }
12888
 
  if (!select_cond || select_cond->val_int())
12889
 
  {
12890
 
    /*
12891
 
      There is no select condition or the attached pushed down
12892
 
      condition is true => a match is found.
12893
 
    */
12894
 
    bool found= 1;
12895
 
    while (join_tab->first_unmatched && found)
12896
 
    {
12897
 
      /*
12898
 
        The while condition is always false if join_tab is not
12899
 
        the last inner join table of an outer join operation.
12900
 
      */
12901
 
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
12902
 
      /*
12903
 
        Mark that a match for current outer table is found.
12904
 
        This activates push down conditional predicates attached
12905
 
        to the all inner tables of the outer join.
12906
 
      */
12907
 
      first_unmatched->found= 1;
12908
 
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
12909
 
      {
12910
 
        if (tab->table->reginfo.not_exists_optimize)
12911
 
          return NESTED_LOOP_NO_MORE_ROWS;
12912
 
        /* Check all predicates that has just been activated. */
12913
 
        /*
12914
 
          Actually all predicates non-guarded by first_unmatched->found
12915
 
          will be re-evaluated again. It could be fixed, but, probably,
12916
 
          it's not worth doing now.
12917
 
        */
12918
 
        if (tab->select_cond && !tab->select_cond->val_int())
12919
 
        {
12920
 
          /* The condition attached to table tab is false */
12921
 
          if (tab == join_tab)
12922
 
            found= 0;
12923
 
          else
12924
 
          {
12925
 
            /*
12926
 
              Set a return point if rejected predicate is attached
12927
 
              not to the last table of the current nest level.
12928
 
            */
12929
 
            join->return_tab= tab;
12930
 
            return NESTED_LOOP_OK;
12931
 
          }
12932
 
        }
12933
 
      }
12934
 
      /*
12935
 
        Check whether join_tab is not the last inner table
12936
 
        for another embedding outer join.
12937
 
      */
12938
 
      if ((first_unmatched= first_unmatched->first_upper) &&
12939
 
          first_unmatched->last_inner != join_tab)
12940
 
        first_unmatched= 0;
12941
 
      join_tab->first_unmatched= first_unmatched;
12942
 
    }
12943
 
 
12944
 
    JOIN_TAB *return_tab= join->return_tab;
12945
 
    join_tab->found_match= true;
12946
 
    if (join_tab->check_weed_out_table)
12947
 
    {
12948
 
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
12949
 
      if (res == -1)
12950
 
        return NESTED_LOOP_ERROR;
12951
 
      if (res == 1)
12952
 
        return NESTED_LOOP_OK;
12953
 
    }
12954
 
    else if (join_tab->do_firstmatch)
12955
 
    {
12956
 
      /* 
12957
 
        We should return to the join_tab->do_firstmatch after we have 
12958
 
        enumerated all the suffixes for current prefix row combination
12959
 
      */
12960
 
      return_tab= join_tab->do_firstmatch;
12961
 
    }
12962
 
 
12963
 
    /*
12964
 
      It was not just a return to lower loop level when one
12965
 
      of the newly activated predicates is evaluated as false
12966
 
      (See above join->return_tab= tab).
12967
 
    */
12968
 
    join->examined_rows++;
12969
 
    join->thd->row_count++;
12970
 
 
12971
 
    if (found)
12972
 
    {
12973
 
      enum enum_nested_loop_state rc;
12974
 
      /* A match from join_tab is found for the current partial join. */
12975
 
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
12976
 
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
12977
 
        return rc;
12978
 
      if (return_tab < join->return_tab)
12979
 
        join->return_tab= return_tab;
12980
 
 
12981
 
      if (join->return_tab < join_tab)
12982
 
        return NESTED_LOOP_OK;
12983
 
      /*
12984
 
        Test if this was a SELECT DISTINCT query on a table that
12985
 
        was not in the field list;  In this case we can abort if
12986
 
        we found a row, as no new rows can be added to the result.
12987
 
      */
12988
 
      if (not_used_in_distinct && found_records != join->found_records)
12989
 
        return NESTED_LOOP_NO_MORE_ROWS;
12990
 
    }
12991
 
    else
12992
 
      join_tab->read_record.file->unlock_row();
12993
 
  }
12994
 
  else
12995
 
  {
12996
 
    /*
12997
 
      The condition pushed down to the table join_tab rejects all rows
12998
 
      with the beginning coinciding with the current partial join.
12999
 
    */
13000
 
    join->examined_rows++;
13001
 
    join->thd->row_count++;
13002
 
    join_tab->read_record.file->unlock_row();
13003
 
  }
13004
 
  return NESTED_LOOP_OK;
13005
 
}
13006
 
 
13007
 
 
13008
 
/**
13009
 
 
13010
 
  @details
13011
 
    Construct a NULL complimented partial join record and feed it to the next
13012
 
    level of the nested loop. This function is used in case we have
13013
 
    an OUTER join and no matching record was found.
13014
 
*/
13015
 
 
13016
 
static enum_nested_loop_state
13017
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
13018
 
{
13019
 
  /*
13020
 
    The table join_tab is the first inner table of a outer join operation
13021
 
    and no matches has been found for the current outer row.
13022
 
  */
13023
 
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
13024
 
  /* Cache variables for faster loop */
13025
 
  COND *select_cond;
13026
 
  for ( ; join_tab <= last_inner_tab ; join_tab++)
13027
 
  {
13028
 
    /* Change the the values of guard predicate variables. */
13029
 
    join_tab->found= 1;
13030
 
    join_tab->not_null_compl= 0;
13031
 
    /* The outer row is complemented by nulls for each inner tables */
13032
 
    restore_record(join_tab->table,s->default_values);  // Make empty record
13033
 
    mark_as_null_row(join_tab->table);       // For group by without error
13034
 
    select_cond= join_tab->select_cond;
13035
 
    /* Check all attached conditions for inner table rows. */
13036
 
    if (select_cond && !select_cond->val_int())
13037
 
      return NESTED_LOOP_OK;
13038
 
  }
13039
 
  join_tab--;
13040
 
  /*
13041
 
    The row complemented by nulls might be the first row
13042
 
    of embedding outer joins.
13043
 
    If so, perform the same actions as in the code
13044
 
    for the first regular outer join row above.
13045
 
  */
13046
 
  for ( ; ; )
13047
 
  {
13048
 
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13049
 
    if ((first_unmatched= first_unmatched->first_upper) &&
13050
 
        first_unmatched->last_inner != join_tab)
13051
 
      first_unmatched= 0;
13052
 
    join_tab->first_unmatched= first_unmatched;
13053
 
    if (!first_unmatched)
13054
 
      break;
13055
 
    first_unmatched->found= 1;
13056
 
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13057
 
    {
13058
 
      if (tab->select_cond && !tab->select_cond->val_int())
13059
 
      {
13060
 
        join->return_tab= tab;
13061
 
        return NESTED_LOOP_OK;
13062
 
      }
13063
 
    }
13064
 
  }
13065
 
  /*
13066
 
    The row complemented by nulls satisfies all conditions
13067
 
    attached to inner tables.
13068
 
    Send the row complemented by nulls to be joined with the
13069
 
    remaining tables.
13070
 
  */
13071
 
  return (*join_tab->next_select)(join, join_tab+1, 0);
13072
 
}
13073
 
 
13074
 
 
13075
 
static enum_nested_loop_state
13076
 
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13077
 
{
13078
 
  enum_nested_loop_state rc= NESTED_LOOP_OK;
13079
 
  int error;
13080
 
  READ_RECORD *info;
13081
 
 
13082
 
  join_tab->table->null_row= 0;
13083
 
  if (!join_tab->cache.records)
13084
 
    return NESTED_LOOP_OK;                      /* Nothing to do */
13085
 
  if (skip_last)
13086
 
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
13087
 
  if (join_tab->use_quick == 2)
13088
 
  {
13089
 
    if (join_tab->select->quick)
13090
 
    {                                   /* Used quick select last. reset it */
13091
 
      delete join_tab->select->quick;
13092
 
      join_tab->select->quick=0;
13093
 
    }
13094
 
  }
13095
 
 /* read through all records */
13096
 
  if ((error=join_init_read_record(join_tab)))
13097
 
  {
13098
 
    reset_cache_write(&join_tab->cache);
13099
 
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13100
 
  }
13101
 
 
13102
 
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13103
 
  {
13104
 
    tmp->status=tmp->table->status;
13105
 
    tmp->table->status=0;
13106
 
  }
13107
 
 
13108
 
  info= &join_tab->read_record;
13109
 
  do
13110
 
  {
13111
 
    if (join->thd->killed)
13112
 
    {
13113
 
      join->thd->send_kill_message();
13114
 
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13115
 
    }
13116
 
    SQL_SELECT *select=join_tab->select;
13117
 
    if (rc == NESTED_LOOP_OK &&
13118
 
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13119
 
    {
13120
 
      uint i;
13121
 
      reset_cache_read(&join_tab->cache);
13122
 
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13123
 
      {
13124
 
        read_cached_record(join_tab);
13125
 
        if (!select || !select->skip_record())
13126
 
        {
13127
 
          int res= 0;
13128
 
          if (!join_tab->check_weed_out_table || 
13129
 
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13130
 
          {
13131
 
            rc= (join_tab->next_select)(join,join_tab+1,0);
13132
 
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13133
 
            {
13134
 
              reset_cache_write(&join_tab->cache);
13135
 
              return rc;
13136
 
            }
13137
 
          }
13138
 
          if (res == -1)
13139
 
            return NESTED_LOOP_ERROR;
13140
 
        }
13141
 
      }
13142
 
    }
13143
 
  } while (!(error=info->read_record(info)));
13144
 
 
13145
 
  if (skip_last)
13146
 
    read_cached_record(join_tab);               // Restore current record
13147
 
  reset_cache_write(&join_tab->cache);
13148
 
  if (error > 0)                                // Fatal error
13149
 
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
13150
 
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13151
 
    tmp2->table->status=tmp2->status;
13152
 
  return NESTED_LOOP_OK;
13153
 
}
13154
 
 
13155
 
 
13156
 
/*****************************************************************************
13157
 
  The different ways to read a record
13158
 
  Returns -1 if row was not found, 0 if row was found and 1 on errors
13159
 
*****************************************************************************/
13160
 
 
13161
 
/** Help function when we get some an error from the table handler. */
13162
 
 
13163
 
int report_error(TABLE *table, int error)
13164
 
{
13165
 
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13166
 
  {
13167
 
    table->status= STATUS_GARBAGE;
13168
 
    return -1;                                  // key not found; ok
13169
 
  }
13170
 
  /*
13171
 
    Locking reads can legally return also these errors, do not
13172
 
    print them to the .err log
13173
 
  */
13174
 
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13175
 
    sql_print_error("Got error %d when reading table '%s'",
13176
 
                    error, table->s->path.str);
13177
 
  table->file->print_error(error,MYF(0));
13178
 
  return 1;
13179
 
}
13180
 
 
13181
 
 
13182
 
int safe_index_read(JOIN_TAB *tab)
13183
 
{
13184
 
  int error;
13185
 
  TABLE *table= tab->table;
13186
 
  if ((error=table->file->index_read_map(table->record[0],
 
3585
int safe_index_read(JoinTable *tab)
 
3586
{
 
3587
  int error;
 
3588
  Table *table= tab->table;
 
3589
  if ((error=table->cursor->index_read_map(table->record[0],
13187
3590
                                         tab->ref.key_buff,
13188
3591
                                         make_prev_keypart_map(tab->ref.key_parts),
13189
3592
                                         HA_READ_KEY_EXACT)))
13190
 
    return report_error(table, error);
 
3593
    return table->report_error(error);
13191
3594
  return 0;
13192
3595
}
13193
3596
 
13194
 
 
13195
 
static int
13196
 
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
3597
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
13197
3598
{
13198
3599
  int error;
13199
 
  TABLE *table=tab->table;
 
3600
  Table *table=tab->table;
13200
3601
  table->const_table=1;
13201
3602
  table->null_row=0;
13202
3603
  table->status=STATUS_NO_RECORD;
13203
 
  
13204
 
  if (tab->type == JT_SYSTEM)
 
3604
 
 
3605
  if (tab->type == AM_SYSTEM)
13205
3606
  {
13206
3607
    if ((error=join_read_system(tab)))
13207
3608
    {                                           // Info for DESCRIBE
13208
3609
      tab->info="const row not found";
13209
3610
      /* Mark for EXPLAIN that the row was not found */
13210
 
      pos->records_read=0.0;
13211
 
      pos->ref_depend_map= 0;
13212
 
      if (!table->maybe_null || error > 0)
13213
 
        return(error);
 
3611
      pos->setFanout(0.0);
 
3612
      pos->clearRefDependMap();
 
3613
      if (! table->maybe_null || error > 0)
 
3614
        return(error);
13214
3615
    }
13215
3616
  }
13216
3617
  else
13217
3618
  {
13218
 
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13219
 
        !table->no_keyread &&
13220
 
        (int) table->reginfo.lock_type <= (int) TL_READ_HIGH_PRIORITY)
 
3619
    if (! table->key_read && 
 
3620
        table->covering_keys.test(tab->ref.key) && 
 
3621
        ! table->no_keyread &&
 
3622
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
13221
3623
    {
13222
3624
      table->key_read=1;
13223
 
      table->file->extra(HA_EXTRA_KEYREAD);
 
3625
      table->cursor->extra(HA_EXTRA_KEYREAD);
13224
3626
      tab->index= tab->ref.key;
13225
3627
    }
13226
3628
    error=join_read_const(tab);
13227
3629
    if (table->key_read)
13228
3630
    {
13229
3631
      table->key_read=0;
13230
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
3632
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13231
3633
    }
13232
3634
    if (error)
13233
3635
    {
13234
3636
      tab->info="unique row not found";
13235
3637
      /* Mark for EXPLAIN that the row was not found */
13236
 
      pos->records_read=0.0;
13237
 
      pos->ref_depend_map= 0;
 
3638
      pos->setFanout(0.0);
 
3639
      pos->clearRefDependMap();
13238
3640
      if (!table->maybe_null || error > 0)
13239
 
        return(error);
 
3641
        return(error);
13240
3642
    }
13241
3643
  }
13242
3644
  if (*tab->on_expr_ref && !table->null_row)
13243
3645
  {
13244
3646
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13245
 
      mark_as_null_row(table);  
 
3647
      table->mark_as_null_row();
13246
3648
  }
13247
3649
  if (!table->null_row)
13248
3650
    table->maybe_null=0;
13251
3653
  JOIN *join= tab->join;
13252
3654
  if (join->conds)
13253
3655
    update_const_equal_items(join->conds, tab);
13254
 
  TABLE_LIST *tbl;
 
3656
  TableList *tbl;
13255
3657
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
13256
3658
  {
13257
 
    TABLE_LIST *embedded;
13258
 
    TABLE_LIST *embedding= tbl;
 
3659
    TableList *embedded;
 
3660
    TableList *embedding= tbl;
13259
3661
    do
13260
3662
    {
13261
3663
      embedded= embedding;
13270
3672
  return(0);
13271
3673
}
13272
3674
 
13273
 
 
13274
 
static int
13275
 
join_read_system(JOIN_TAB *tab)
 
3675
int join_read_system(JoinTable *tab)
13276
3676
{
13277
 
  TABLE *table= tab->table;
 
3677
  Table *table= tab->table;
13278
3678
  int error;
13279
3679
  if (table->status & STATUS_GARBAGE)           // If first read
13280
3680
  {
13281
 
    if ((error=table->file->read_first_row(table->record[0],
 
3681
    if ((error=table->cursor->read_first_row(table->record[0],
13282
3682
                                           table->s->primary_key)))
13283
3683
    {
13284
3684
      if (error != HA_ERR_END_OF_FILE)
13285
 
        return report_error(table, error);
13286
 
      mark_as_null_row(tab->table);
13287
 
      empty_record(table);                      // Make empty record
 
3685
        return table->report_error(error);
 
3686
      tab->table->mark_as_null_row();
 
3687
      table->emptyRecord();                     // Make empty record
13288
3688
      return -1;
13289
3689
    }
13290
 
    store_record(table,record[1]);
 
3690
    table->storeRecord();
13291
3691
  }
13292
3692
  else if (!table->status)                      // Only happens with left join
13293
 
    restore_record(table,record[1]);                    // restore old record
 
3693
    table->restoreRecord();                     // restore old record
13294
3694
  table->null_row=0;
13295
3695
  return table->status ? -1 : 0;
13296
3696
}
13297
3697
 
13298
 
 
13299
3698
/**
13300
3699
  Read a (constant) table when there is at most one matching row.
13301
3700
 
13308
3707
  @retval
13309
3708
    1   Got an error (other than row not found) during read
13310
3709
*/
13311
 
 
13312
 
static int
13313
 
join_read_const(JOIN_TAB *tab)
 
3710
int join_read_const(JoinTable *tab)
13314
3711
{
13315
3712
  int error;
13316
 
  TABLE *table= tab->table;
 
3713
  Table *table= tab->table;
13317
3714
  if (table->status & STATUS_GARBAGE)           // If first read
13318
3715
  {
13319
3716
    table->status= 0;
13320
 
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13321
 
      error=HA_ERR_KEY_NOT_FOUND;
 
3717
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
3718
      error= HA_ERR_KEY_NOT_FOUND;
13322
3719
    else
13323
3720
    {
13324
 
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
13325
 
                                            (uchar*) tab->ref.key_buff,
 
3721
      error=table->cursor->index_read_idx_map(table->record[0],tab->ref.key,
 
3722
                                            (unsigned char*) tab->ref.key_buff,
13326
3723
                                            make_prev_keypart_map(tab->ref.key_parts),
13327
3724
                                            HA_READ_KEY_EXACT);
13328
3725
    }
13329
3726
    if (error)
13330
3727
    {
13331
3728
      table->status= STATUS_NOT_FOUND;
13332
 
      mark_as_null_row(tab->table);
13333
 
      empty_record(table);
 
3729
      tab->table->mark_as_null_row();
 
3730
      table->emptyRecord();
13334
3731
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13335
 
        return report_error(table, error);
 
3732
        return table->report_error(error);
13336
3733
      return -1;
13337
3734
    }
13338
 
    store_record(table,record[1]);
 
3735
    table->storeRecord();
13339
3736
  }
13340
3737
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
13341
3738
  {
13342
3739
    table->status=0;
13343
 
    restore_record(table,record[1]);                    // restore old record
 
3740
    table->restoreRecord();                     // restore old record
13344
3741
  }
13345
3742
  table->null_row=0;
13346
3743
  return table->status ? -1 : 0;
13347
3744
}
13348
3745
 
13349
 
 
13350
3746
/*
13351
3747
  eq_ref access method implementation: "read_first" function
13352
3748
 
13353
3749
  SYNOPSIS
13354
3750
    join_read_key()
13355
 
      tab  JOIN_TAB of the accessed table
 
3751
      tab  JoinTable of the accessed table
13356
3752
 
13357
3753
  DESCRIPTION
13358
3754
    This is "read_fist" function for the "ref" access method. The difference
13360
3756
 
13361
3757
  RETURN
13362
3758
    0  - Ok
13363
 
   -1  - Row not found 
 
3759
   -1  - Row not found
13364
3760
    1  - Error
13365
3761
*/
13366
 
 
13367
 
static int
13368
 
join_read_key(JOIN_TAB *tab)
 
3762
int join_read_key(JoinTable *tab)
13369
3763
{
13370
3764
  int error;
13371
 
  TABLE *table= tab->table;
 
3765
  Table *table= tab->table;
13372
3766
 
13373
 
  if (!table->file->inited)
 
3767
  if (!table->cursor->inited)
13374
3768
  {
13375
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
3769
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
13376
3770
  }
13377
3771
 
13378
3772
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
13384
3778
      table->status=STATUS_NOT_FOUND;
13385
3779
      return -1;
13386
3780
    }
13387
 
    error=table->file->index_read_map(table->record[0],
 
3781
    error=table->cursor->index_read_map(table->record[0],
13388
3782
                                      tab->ref.key_buff,
13389
3783
                                      make_prev_keypart_map(tab->ref.key_parts),
13390
3784
                                      HA_READ_KEY_EXACT);
13391
3785
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13392
 
      return report_error(table, error);
 
3786
      return table->report_error(error);
13393
3787
  }
13394
3788
  table->null_row=0;
13395
3789
  return table->status ? -1 : 0;
13396
3790
}
13397
3791
 
13398
 
 
13399
3792
/*
13400
3793
  ref access method implementation: "read_first" function
13401
3794
 
13402
3795
  SYNOPSIS
13403
3796
    join_read_always_key()
13404
 
      tab  JOIN_TAB of the accessed table
 
3797
      tab  JoinTable of the accessed table
13405
3798
 
13406
3799
  DESCRIPTION
13407
 
    This is "read_fist" function for the "ref" access method.
13408
 
   
 
3800
    This is "read_first" function for the "ref" access method.
 
3801
 
13409
3802
    The functon must leave the index initialized when it returns.
13410
3803
    ref_or_null access implementation depends on that.
13411
3804
 
13412
3805
  RETURN
13413
3806
    0  - Ok
13414
 
   -1  - Row not found 
 
3807
   -1  - Row not found
13415
3808
    1  - Error
13416
3809
*/
13417
 
 
13418
 
static int
13419
 
join_read_always_key(JOIN_TAB *tab)
 
3810
int join_read_always_key(JoinTable *tab)
13420
3811
{
13421
3812
  int error;
13422
 
  TABLE *table= tab->table;
 
3813
  Table *table= tab->table;
13423
3814
 
13424
3815
  /* Initialize the index first */
13425
 
  if (!table->file->inited)
13426
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13427
 
 
 
3816
  if (!table->cursor->inited)
 
3817
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3818
 
13428
3819
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
13429
 
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
 
3820
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
13430
3821
  {
13431
3822
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
13432
3823
        return -1;
13433
3824
  }
13434
3825
 
13435
 
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
3826
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
13436
3827
    return -1;
13437
 
  if ((error=table->file->index_read_map(table->record[0],
 
3828
  if ((error=table->cursor->index_read_map(table->record[0],
13438
3829
                                         tab->ref.key_buff,
13439
3830
                                         make_prev_keypart_map(tab->ref.key_parts),
13440
3831
                                         HA_READ_KEY_EXACT)))
13441
3832
  {
13442
3833
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13443
 
      return report_error(table, error);
13444
 
    return -1; /* purecov: inspected */
 
3834
      return table->report_error(error);
 
3835
    return -1;
13445
3836
  }
 
3837
 
13446
3838
  return 0;
13447
3839
}
13448
3840
 
13449
 
 
13450
3841
/**
13451
 
  This function is used when optimizing away ORDER BY in 
13452
 
  SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC.
 
3842
  This function is used when optimizing away order_st BY in
 
3843
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
13453
3844
*/
13454
 
  
13455
 
static int
13456
 
join_read_last_key(JOIN_TAB *tab)
 
3845
int join_read_last_key(JoinTable *tab)
13457
3846
{
13458
3847
  int error;
13459
 
  TABLE *table= tab->table;
 
3848
  Table *table= tab->table;
13460
3849
 
13461
 
  if (!table->file->inited)
13462
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13463
 
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
3850
  if (!table->cursor->inited)
 
3851
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3852
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
13464
3853
    return -1;
13465
 
  if ((error=table->file->index_read_last_map(table->record[0],
 
3854
  if ((error=table->cursor->index_read_last_map(table->record[0],
13466
3855
                                              tab->ref.key_buff,
13467
3856
                                              make_prev_keypart_map(tab->ref.key_parts))))
13468
3857
  {
13469
3858
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13470
 
      return report_error(table, error);
13471
 
    return -1; /* purecov: inspected */
 
3859
      return table->report_error(error);
 
3860
    return -1;
13472
3861
  }
13473
3862
  return 0;
13474
3863
}
13475
3864
 
13476
 
 
13477
 
        /* ARGSUSED */
13478
 
static int
13479
 
join_no_more_records(READ_RECORD *info __attribute__((unused)))
 
3865
int join_no_more_records(READ_RECORD *)
13480
3866
{
13481
3867
  return -1;
13482
3868
}
13483
3869
 
13484
 
static int
13485
 
join_read_next_same_diff(READ_RECORD *info)
 
3870
int join_read_next_same_diff(READ_RECORD *info)
13486
3871
{
13487
 
  TABLE *table= info->table;
13488
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3872
  Table *table= info->table;
 
3873
  JoinTable *tab=table->reginfo.join_tab;
13489
3874
  if (tab->insideout_match_tab->found_match)
13490
3875
  {
13491
3876
    KEY *key= tab->table->key_info + tab->index;
13492
 
    do 
 
3877
    do
13493
3878
    {
13494
3879
      int error;
13495
3880
      /* Save index tuple from record to the buffer */
13496
3881
      key_copy(tab->insideout_buf, info->record, key, 0);
13497
3882
 
13498
 
      if ((error=table->file->index_next_same(table->record[0],
 
3883
      if ((error=table->cursor->index_next_same(table->record[0],
13499
3884
                                              tab->ref.key_buff,
13500
3885
                                              tab->ref.key_length)))
13501
3886
      {
13502
3887
        if (error != HA_ERR_END_OF_FILE)
13503
 
          return report_error(table, error);
 
3888
          return table->report_error(error);
13504
3889
        table->status= STATUS_GARBAGE;
13505
3890
        return -1;
13506
3891
      }
13507
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
 
3892
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
13508
3893
                      tab->insideout_buf, key->key_length));
13509
3894
    tab->insideout_match_tab->found_match= 0;
13510
3895
    return 0;
13513
3898
    return join_read_next_same(info);
13514
3899
}
13515
3900
 
13516
 
static int
13517
 
join_read_next_same(READ_RECORD *info)
 
3901
int join_read_next_same(READ_RECORD *info)
13518
3902
{
13519
3903
  int error;
13520
 
  TABLE *table= info->table;
13521
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3904
  Table *table= info->table;
 
3905
  JoinTable *tab=table->reginfo.join_tab;
13522
3906
 
13523
 
  if ((error=table->file->index_next_same(table->record[0],
 
3907
  if ((error=table->cursor->index_next_same(table->record[0],
13524
3908
                                          tab->ref.key_buff,
13525
3909
                                          tab->ref.key_length)))
13526
3910
  {
13527
3911
    if (error != HA_ERR_END_OF_FILE)
13528
 
      return report_error(table, error);
 
3912
      return table->report_error(error);
13529
3913
    table->status= STATUS_GARBAGE;
13530
3914
    return -1;
13531
3915
  }
 
3916
 
13532
3917
  return 0;
13533
3918
}
13534
3919
 
13535
 
 
13536
 
static int
13537
 
join_read_prev_same(READ_RECORD *info)
 
3920
int join_read_prev_same(READ_RECORD *info)
13538
3921
{
13539
3922
  int error;
13540
 
  TABLE *table= info->table;
13541
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3923
  Table *table= info->table;
 
3924
  JoinTable *tab=table->reginfo.join_tab;
13542
3925
 
13543
 
  if ((error=table->file->index_prev(table->record[0])))
13544
 
    return report_error(table, error);
 
3926
  if ((error=table->cursor->index_prev(table->record[0])))
 
3927
    return table->report_error(error);
13545
3928
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
13546
3929
                      tab->ref.key_length))
13547
3930
  {
13551
3934
  return error;
13552
3935
}
13553
3936
 
13554
 
 
13555
 
static int
13556
 
join_init_quick_read_record(JOIN_TAB *tab)
 
3937
int join_init_quick_read_record(JoinTable *tab)
13557
3938
{
13558
3939
  if (test_if_quick_select(tab) == -1)
13559
3940
    return -1;                                  /* No possible records */
13560
3941
  return join_init_read_record(tab);
13561
3942
}
13562
3943
 
13563
 
 
13564
3944
int rr_sequential(READ_RECORD *info);
13565
 
int init_read_record_seq(JOIN_TAB *tab)
 
3945
int init_read_record_seq(JoinTable *tab)
13566
3946
{
13567
3947
  tab->read_record.read_record= rr_sequential;
13568
 
  if (tab->read_record.file->ha_rnd_init(1))
 
3948
  if (tab->read_record.cursor->ha_rnd_init(1))
13569
3949
    return 1;
13570
3950
  return (*tab->read_record.read_record)(&tab->read_record);
13571
3951
}
13572
3952
 
13573
 
static int
13574
 
test_if_quick_select(JOIN_TAB *tab)
 
3953
int test_if_quick_select(JoinTable *tab)
13575
3954
{
13576
3955
  delete tab->select->quick;
13577
 
  tab->select->quick=0;
13578
 
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
13579
 
                                        (table_map) 0, HA_POS_ERROR, 0,
13580
 
                                        false);
 
3956
  tab->select->quick= 0;
 
3957
  return tab->select->test_quick_select(tab->join->session, tab->keys,
 
3958
                                        (table_map) 0, HA_POS_ERROR, 0, false);
13581
3959
}
13582
3960
 
13583
 
 
13584
 
static int
13585
 
join_init_read_record(JOIN_TAB *tab)
 
3961
int join_init_read_record(JoinTable *tab)
13586
3962
{
13587
3963
  if (tab->select && tab->select->quick && tab->select->quick->reset())
13588
3964
    return 1;
13589
 
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
3965
  init_read_record(&tab->read_record, tab->join->session, tab->table,
13590
3966
                   tab->select,1,1);
13591
3967
  return (*tab->read_record.read_record)(&tab->read_record);
13592
3968
}
13593
3969
 
13594
 
 
13595
 
static int
13596
 
join_read_first(JOIN_TAB *tab)
 
3970
int join_read_first(JoinTable *tab)
13597
3971
{
13598
3972
  int error;
13599
 
  TABLE *table=tab->table;
13600
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
3973
  Table *table=tab->table;
 
3974
  if (!table->key_read && table->covering_keys.test(tab->index) &&
13601
3975
      !table->no_keyread)
13602
3976
  {
13603
3977
    table->key_read=1;
13604
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
3978
    table->cursor->extra(HA_EXTRA_KEYREAD);
13605
3979
  }
13606
3980
  tab->table->status=0;
13607
3981
  tab->read_record.table=table;
13608
 
  tab->read_record.file=table->file;
 
3982
  tab->read_record.cursor=table->cursor;
13609
3983
  tab->read_record.index=tab->index;
13610
3984
  tab->read_record.record=table->record[0];
13611
3985
  if (tab->insideout_match_tab)
13620
3994
    tab->read_record.do_insideout_scan= 0;
13621
3995
  }
13622
3996
 
13623
 
  if (!table->file->inited)
13624
 
    table->file->ha_index_init(tab->index, tab->sorted);
13625
 
  if ((error=tab->table->file->index_first(tab->table->record[0])))
 
3997
  if (!table->cursor->inited)
 
3998
    table->cursor->ha_index_init(tab->index, tab->sorted);
 
3999
  if ((error=tab->table->cursor->index_first(tab->table->record[0])))
13626
4000
  {
13627
4001
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13628
 
      report_error(table, error);
 
4002
      table->report_error(error);
13629
4003
    return -1;
13630
4004
  }
 
4005
 
13631
4006
  return 0;
13632
4007
}
13633
4008
 
13634
 
 
13635
 
static int
13636
 
join_read_next_different(READ_RECORD *info)
 
4009
int join_read_next_different(READ_RECORD *info)
13637
4010
{
13638
 
  JOIN_TAB *tab= info->do_insideout_scan;
 
4011
  JoinTable *tab= info->do_insideout_scan;
13639
4012
  if (tab->insideout_match_tab->found_match)
13640
4013
  {
13641
4014
    KEY *key= tab->table->key_info + tab->index;
13642
 
    do 
 
4015
    do
13643
4016
    {
13644
4017
      int error;
13645
4018
      /* Save index tuple from record to the buffer */
13646
4019
      key_copy(tab->insideout_buf, info->record, key, 0);
13647
4020
 
13648
 
      if ((error=info->file->index_next(info->record)))
13649
 
        return report_error(info->table, error);
13650
 
      
13651
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
 
4021
      if ((error=info->cursor->index_next(info->record)))
 
4022
        return info->table->report_error(error);
 
4023
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
13652
4024
                      tab->insideout_buf, key->key_length));
13653
4025
    tab->insideout_match_tab->found_match= 0;
13654
4026
    return 0;
13657
4029
    return join_read_next(info);
13658
4030
}
13659
4031
 
13660
 
 
13661
 
static int
13662
 
join_read_next(READ_RECORD *info)
 
4032
int join_read_next(READ_RECORD *info)
13663
4033
{
13664
4034
  int error;
13665
 
  if ((error=info->file->index_next(info->record)))
13666
 
    return report_error(info->table, error);
 
4035
  if ((error=info->cursor->index_next(info->record)))
 
4036
    return info->table->report_error(error);
13667
4037
  return 0;
13668
4038
}
13669
4039
 
13670
 
 
13671
 
static int
13672
 
join_read_last(JOIN_TAB *tab)
 
4040
int join_read_last(JoinTable *tab)
13673
4041
{
13674
 
  TABLE *table=tab->table;
 
4042
  Table *table=tab->table;
13675
4043
  int error;
13676
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
4044
  if (!table->key_read && table->covering_keys.test(tab->index) &&
13677
4045
      !table->no_keyread)
13678
4046
  {
13679
4047
    table->key_read=1;
13680
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
4048
    table->cursor->extra(HA_EXTRA_KEYREAD);
13681
4049
  }
13682
4050
  tab->table->status=0;
13683
4051
  tab->read_record.read_record=join_read_prev;
13684
4052
  tab->read_record.table=table;
13685
 
  tab->read_record.file=table->file;
 
4053
  tab->read_record.cursor=table->cursor;
13686
4054
  tab->read_record.index=tab->index;
13687
4055
  tab->read_record.record=table->record[0];
13688
 
  if (!table->file->inited)
13689
 
    table->file->ha_index_init(tab->index, 1);
13690
 
  if ((error= tab->table->file->index_last(tab->table->record[0])))
13691
 
    return report_error(table, error);
 
4056
  if (!table->cursor->inited)
 
4057
    table->cursor->ha_index_init(tab->index, 1);
 
4058
  if ((error= tab->table->cursor->index_last(tab->table->record[0])))
 
4059
    return table->report_error(error);
 
4060
 
13692
4061
  return 0;
13693
4062
}
13694
4063
 
13695
 
 
13696
 
static int
13697
 
join_read_prev(READ_RECORD *info)
 
4064
int join_read_prev(READ_RECORD *info)
13698
4065
{
13699
4066
  int error;
13700
 
  if ((error= info->file->index_prev(info->record)))
13701
 
    return report_error(info->table, error);
 
4067
  if ((error= info->cursor->index_prev(info->record)))
 
4068
    return info->table->report_error(error);
 
4069
 
13702
4070
  return 0;
13703
4071
}
13704
4072
 
13705
4073
/**
13706
4074
  Reading of key with key reference and one part that may be NULL.
13707
4075
*/
13708
 
 
13709
 
int
13710
 
join_read_always_key_or_null(JOIN_TAB *tab)
 
4076
int join_read_always_key_or_null(JoinTable *tab)
13711
4077
{
13712
4078
  int res;
13713
4079
 
13721
4087
  return safe_index_read(tab);
13722
4088
}
13723
4089
 
13724
 
 
13725
 
int
13726
 
join_read_next_same_or_null(READ_RECORD *info)
 
4090
int join_read_next_same_or_null(READ_RECORD *info)
13727
4091
{
13728
4092
  int error;
13729
4093
  if ((error= join_read_next_same(info)) >= 0)
13730
4094
    return error;
13731
 
  JOIN_TAB *tab= info->table->reginfo.join_tab;
 
4095
  JoinTable *tab= info->table->reginfo.join_tab;
13732
4096
 
13733
4097
  /* Test if we have already done a read after null key */
13734
4098
  if (*tab->ref.null_ref_key)
13737
4101
  return safe_index_read(tab);                  // then read null keys
13738
4102
}
13739
4103
 
13740
 
 
13741
 
/*****************************************************************************
13742
 
  DESCRIPTION
13743
 
    Functions that end one nested loop iteration. Different functions
13744
 
    are used to support GROUP BY clause and to redirect records
13745
 
    to a table (e.g. in case of SELECT into a temporary table) or to the
13746
 
    network client.
13747
 
 
13748
 
  RETURN VALUES
13749
 
    NESTED_LOOP_OK           - the record has been successfully handled
13750
 
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
13751
 
                               was detected
13752
 
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
13753
 
                               the record
13754
 
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
13755
 
                               additionally, the nested loop produced the
13756
 
                               number of rows specified in the LIMIT clause
13757
 
                               for the query
13758
 
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
13759
 
                               additionally, there is a cursor and the nested
13760
 
                               loop algorithm produced the number of rows
13761
 
                               that is specified for current cursor fetch
13762
 
                               operation.
13763
 
   All return values except NESTED_LOOP_OK abort the nested loop.
13764
 
*****************************************************************************/
13765
 
 
13766
 
/* ARGSUSED */
13767
 
static enum_nested_loop_state
13768
 
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13769
 
         bool end_of_records)
13770
 
{
13771
 
  if (!end_of_records)
13772
 
  {
13773
 
    int error;
13774
 
    if (join->having && join->having->val_int() == 0)
13775
 
      return(NESTED_LOOP_OK);               // Didn't match having
13776
 
    error=0;
13777
 
    if (join->do_send_rows)
13778
 
      error=join->result->send_data(*join->fields);
13779
 
    if (error)
13780
 
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
13781
 
    if (++join->send_records >= join->unit->select_limit_cnt &&
13782
 
        join->do_send_rows)
13783
 
    {
13784
 
      if (join->select_options & OPTION_FOUND_ROWS)
13785
 
      {
13786
 
        JOIN_TAB *jt=join->join_tab;
13787
 
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
13788
 
            && !join->send_group_parts && !join->having && !jt->select_cond &&
13789
 
            !(jt->select && jt->select->quick) &&
13790
 
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
13791
 
            (jt->ref.key < 0))
13792
 
        {
13793
 
          /* Join over all rows in table;  Return number of found rows */
13794
 
          TABLE *table=jt->table;
13795
 
 
13796
 
          join->select_options ^= OPTION_FOUND_ROWS;
13797
 
          if (table->sort.record_pointers ||
13798
 
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
13799
 
          {
13800
 
            /* Using filesort */
13801
 
            join->send_records= table->sort.found_records;
13802
 
          }
13803
 
          else
13804
 
          {
13805
 
            table->file->info(HA_STATUS_VARIABLE);
13806
 
            join->send_records= table->file->stats.records;
13807
 
          }
13808
 
        }
13809
 
        else 
13810
 
        {
13811
 
          join->do_send_rows= 0;
13812
 
          if (join->unit->fake_select_lex)
13813
 
            join->unit->fake_select_lex->select_limit= 0;
13814
 
          return(NESTED_LOOP_OK);
13815
 
        }
13816
 
      }
13817
 
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
13818
 
    }
13819
 
    else if (join->send_records >= join->fetch_limit)
13820
 
    {
13821
 
      /*
13822
 
        There is a server side cursor and all rows for
13823
 
        this fetch request are sent.
13824
 
      */
13825
 
      return(NESTED_LOOP_CURSOR_LIMIT);
13826
 
    }
13827
 
  }
13828
 
 
13829
 
  return(NESTED_LOOP_OK);
13830
 
}
13831
 
 
13832
 
 
13833
 
        /* ARGSUSED */
13834
 
enum_nested_loop_state
13835
 
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13836
 
               bool end_of_records)
 
4104
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *, bool end_of_records)
13837
4105
{
13838
4106
  int idx= -1;
13839
4107
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
13841
4109
  if (!join->first_record || end_of_records ||
13842
4110
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
13843
4111
  {
13844
 
    if (join->first_record || 
 
4112
    if (join->first_record ||
13845
4113
        (end_of_records && !join->group && !join->group_optimized_away))
13846
4114
    {
13847
4115
      if (idx < (int) join->send_group_parts)
13848
4116
      {
13849
 
        int error=0;
13850
 
        {
13851
 
          if (!join->first_record)
13852
 
          {
13853
 
            List_iterator_fast<Item> it(*join->fields);
13854
 
            Item *item;
13855
 
            /* No matching rows for group function */
13856
 
            join->clear();
 
4117
        int error=0;
 
4118
        {
 
4119
          if (!join->first_record)
 
4120
          {
 
4121
                  List_iterator_fast<Item> it(*join->fields);
 
4122
                  Item *item;
 
4123
            /* No matching rows for group function */
 
4124
            join->clear();
13857
4125
 
13858
4126
            while ((item= it++))
13859
4127
              item->no_rows_in_result();
13860
 
          }
13861
 
          if (join->having && join->having->val_int() == 0)
13862
 
            error= -1;                          // Didn't satisfy having
13863
 
          else
13864
 
          {
13865
 
            if (join->do_send_rows)
13866
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
13867
 
            join->send_records++;
13868
 
          }
13869
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
13870
 
          {
13871
 
            if (join->rollup_send_data((uint) (idx+1)))
13872
 
              error= 1;
13873
 
          }
13874
 
        }
13875
 
        if (error > 0)
13876
 
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
13877
 
        if (end_of_records)
13878
 
          return(NESTED_LOOP_OK);
13879
 
        if (join->send_records >= join->unit->select_limit_cnt &&
13880
 
            join->do_send_rows)
13881
 
        {
13882
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
13883
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
13884
 
          join->do_send_rows=0;
13885
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
4128
          }
 
4129
          if (join->having && join->having->val_int() == 0)
 
4130
            error= -1;                          // Didn't satisfy having
 
4131
          else
 
4132
          {
 
4133
            if (join->do_send_rows)
 
4134
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
4135
            join->send_records++;
 
4136
          }
 
4137
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
4138
          {
 
4139
            if (join->rollup_send_data((uint32_t) (idx+1)))
 
4140
              error= 1;
 
4141
          }
 
4142
        }
 
4143
        if (error > 0)
 
4144
          return(NESTED_LOOP_ERROR);
 
4145
        if (end_of_records)
 
4146
          return(NESTED_LOOP_OK);
 
4147
        if (join->send_records >= join->unit->select_limit_cnt &&
 
4148
            join->do_send_rows)
 
4149
        {
 
4150
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
4151
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
4152
          join->do_send_rows=0;
 
4153
          join->unit->select_limit_cnt = HA_POS_ERROR;
13886
4154
        }
13887
4155
        else if (join->send_records >= join->fetch_limit)
13888
4156
        {
13901
4169
    else
13902
4170
    {
13903
4171
      if (end_of_records)
13904
 
        return(NESTED_LOOP_OK);
 
4172
        return(NESTED_LOOP_OK);
13905
4173
      join->first_record=1;
13906
 
      VOID(test_if_item_cache_changed(join->group_fields));
 
4174
      test_if_item_cache_changed(join->group_fields);
13907
4175
    }
13908
4176
    if (idx < (int) join->send_group_parts)
13909
4177
    {
13913
4181
      */
13914
4182
      copy_fields(&join->tmp_table_param);
13915
4183
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
13916
 
        return(NESTED_LOOP_ERROR);
 
4184
        return(NESTED_LOOP_ERROR);
13917
4185
      return(ok_code);
13918
4186
    }
13919
4187
  }
13922
4190
  return(NESTED_LOOP_OK);
13923
4191
}
13924
4192
 
13925
 
 
13926
 
        /* ARGSUSED */
13927
 
enum_nested_loop_state
13928
 
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13929
 
          bool end_of_records)
13930
 
{
13931
 
  TABLE *table=join->tmp_table;
13932
 
 
13933
 
  if (join->thd->killed)                        // Aborted by user
13934
 
  {
13935
 
    join->thd->send_kill_message();
13936
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
13937
 
  }
13938
 
  if (!end_of_records)
13939
 
  {
13940
 
    copy_fields(&join->tmp_table_param);
13941
 
    copy_funcs(join->tmp_table_param.items_to_copy);
13942
 
#ifdef TO_BE_DELETED
13943
 
    if (!table->uniques)                        // If not unique handling
13944
 
    {
13945
 
      /* Copy null values from group to row */
13946
 
      ORDER   *group;
13947
 
      for (group=table->group ; group ; group=group->next)
13948
 
      {
13949
 
        Item *item= *group->item;
13950
 
        if (item->maybe_null)
13951
 
        {
13952
 
          Field *field=item->get_tmp_table_field();
13953
 
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
13954
 
        }
13955
 
      }
13956
 
    }
13957
 
#endif
13958
 
    if (!join->having || join->having->val_int())
13959
 
    {
13960
 
      int error;
13961
 
      join->found_records++;
13962
 
      if ((error=table->file->ha_write_row(table->record[0])))
13963
 
      {
13964
 
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
13965
 
          goto end;
13966
 
        if (create_myisam_from_heap(join->thd, table,
13967
 
                                    join->tmp_table_param.start_recinfo,
13968
 
                                    &join->tmp_table_param.recinfo,
13969
 
                                    error, 1))
13970
 
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
13971
 
        table->s->uniques=0;                    // To ensure rows are the same
13972
 
      }
13973
 
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
13974
 
          join->do_send_rows)
13975
 
      {
13976
 
        if (!(join->select_options & OPTION_FOUND_ROWS))
13977
 
          return(NESTED_LOOP_QUERY_LIMIT);
13978
 
        join->do_send_rows=0;
13979
 
        join->unit->select_limit_cnt = HA_POS_ERROR;
13980
 
        return(NESTED_LOOP_OK);
13981
 
      }
13982
 
    }
13983
 
  }
13984
 
end:
13985
 
  return(NESTED_LOOP_OK);
13986
 
}
13987
 
 
13988
 
/* ARGSUSED */
13989
 
/** Group by searching after group record and updating it if possible. */
13990
 
 
13991
 
static enum_nested_loop_state
13992
 
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13993
 
           bool end_of_records)
13994
 
{
13995
 
  TABLE *table=join->tmp_table;
13996
 
  ORDER   *group;
13997
 
  int     error;
13998
 
 
13999
 
  if (end_of_records)
14000
 
    return(NESTED_LOOP_OK);
14001
 
  if (join->thd->killed)                        // Aborted by user
14002
 
  {
14003
 
    join->thd->send_kill_message();
14004
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14005
 
  }
14006
 
 
14007
 
  join->found_records++;
14008
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
14009
 
  /* Make a key of group index */
14010
 
  for (group=table->group ; group ; group=group->next)
14011
 
  {
14012
 
    Item *item= *group->item;
14013
 
    item->save_org_in_field(group->field);
14014
 
    /* Store in the used key if the field was 0 */
14015
 
    if (item->maybe_null)
14016
 
      group->buff[-1]= (char) group->field->is_null();
14017
 
  }
14018
 
  if (!table->file->index_read_map(table->record[1],
14019
 
                                   join->tmp_table_param.group_buff,
14020
 
                                   HA_WHOLE_KEY,
14021
 
                                   HA_READ_KEY_EXACT))
14022
 
  {                                             /* Update old record */
14023
 
    restore_record(table,record[1]);
14024
 
    update_tmptable_sum_func(join->sum_funcs,table);
14025
 
    if ((error=table->file->ha_update_row(table->record[1],
14026
 
                                          table->record[0])))
14027
 
    {
14028
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14029
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14030
 
    }
14031
 
    return(NESTED_LOOP_OK);
14032
 
  }
14033
 
 
14034
 
  /*
14035
 
    Copy null bits from group key to table
14036
 
    We can't copy all data as the key may have different format
14037
 
    as the row data (for example as with VARCHAR keys)
14038
 
  */
14039
 
  KEY_PART_INFO *key_part;
14040
 
  for (group=table->group,key_part=table->key_info[0].key_part;
14041
 
       group ;
14042
 
       group=group->next,key_part++)
14043
 
  {
14044
 
    if (key_part->null_bit)
14045
 
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
14046
 
  }
14047
 
  init_tmptable_sum_functions(join->sum_funcs);
14048
 
  copy_funcs(join->tmp_table_param.items_to_copy);
14049
 
  if ((error=table->file->ha_write_row(table->record[0])))
14050
 
  {
14051
 
    if (create_myisam_from_heap(join->thd, table,
14052
 
                                join->tmp_table_param.start_recinfo,
14053
 
                                &join->tmp_table_param.recinfo,
14054
 
                                error, 0))
14055
 
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
14056
 
    /* Change method to update rows */
14057
 
    table->file->ha_index_init(0, 0);
14058
 
    join->join_tab[join->tables-1].next_select=end_unique_update;
14059
 
  }
14060
 
  join->send_records++;
14061
 
  return(NESTED_LOOP_OK);
14062
 
}
14063
 
 
14064
 
 
14065
 
/** Like end_update, but this is done with unique constraints instead of keys.  */
14066
 
 
14067
 
static enum_nested_loop_state
14068
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14069
 
                  bool end_of_records)
14070
 
{
14071
 
  TABLE *table=join->tmp_table;
14072
 
  int     error;
14073
 
 
14074
 
  if (end_of_records)
14075
 
    return(NESTED_LOOP_OK);
14076
 
  if (join->thd->killed)                        // Aborted by user
14077
 
  {
14078
 
    join->thd->send_kill_message();
14079
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14080
 
  }
14081
 
 
14082
 
  init_tmptable_sum_functions(join->sum_funcs);
14083
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
14084
 
  copy_funcs(join->tmp_table_param.items_to_copy);
14085
 
 
14086
 
  if (!(error=table->file->ha_write_row(table->record[0])))
14087
 
    join->send_records++;                       // New group
14088
 
  else
14089
 
  {
14090
 
    if ((int) table->file->get_dup_key(error) < 0)
14091
 
    {
14092
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14093
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14094
 
    }
14095
 
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
14096
 
    {
14097
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14098
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14099
 
    }
14100
 
    restore_record(table,record[1]);
14101
 
    update_tmptable_sum_func(join->sum_funcs,table);
14102
 
    if ((error=table->file->ha_update_row(table->record[1],
14103
 
                                          table->record[0])))
14104
 
    {
14105
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14106
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14107
 
    }
14108
 
  }
14109
 
  return(NESTED_LOOP_OK);
14110
 
}
14111
 
 
14112
 
 
14113
 
        /* ARGSUSED */
14114
 
enum_nested_loop_state
14115
 
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14116
 
                bool end_of_records)
14117
 
{
14118
 
  TABLE *table=join->tmp_table;
 
4193
enum_nested_loop_state end_write_group(JOIN *join, JoinTable *, bool end_of_records)
 
4194
{
 
4195
  Table *table=join->tmp_table;
14119
4196
  int     idx= -1;
14120
4197
 
14121
 
  if (join->thd->killed)
 
4198
  if (join->session->killed)
14122
4199
  {                                             // Aborted by user
14123
 
    join->thd->send_kill_message();
14124
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
4200
    join->session->send_kill_message();
 
4201
    return NESTED_LOOP_KILLED;
14125
4202
  }
14126
4203
  if (!join->first_record || end_of_records ||
14127
4204
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14131
4208
      int send_group_parts= join->send_group_parts;
14132
4209
      if (idx < send_group_parts)
14133
4210
      {
14134
 
        if (!join->first_record)
14135
 
        {
14136
 
          /* No matching rows for group function */
14137
 
          join->clear();
14138
 
        }
14139
 
        copy_sum_funcs(join->sum_funcs,
14140
 
                       join->sum_funcs_end[send_group_parts]);
14141
 
        if (!join->having || join->having->val_int())
14142
 
        {
14143
 
          int error= table->file->ha_write_row(table->record[0]);
14144
 
          if (error && create_myisam_from_heap(join->thd, table,
14145
 
                                               join->tmp_table_param.start_recinfo,
 
4211
        if (!join->first_record)
 
4212
        {
 
4213
          /* No matching rows for group function */
 
4214
          join->clear();
 
4215
        }
 
4216
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
 
4217
        if (!join->having || join->having->val_int())
 
4218
        {
 
4219
          int error= table->cursor->ha_write_row(table->record[0]);
 
4220
          if (error && create_myisam_from_heap(join->session, table,
 
4221
                                              join->tmp_table_param.start_recinfo,
14146
4222
                                                &join->tmp_table_param.recinfo,
14147
 
                                               error, 0))
14148
 
            return(NESTED_LOOP_ERROR);
 
4223
                                              error, 0))
 
4224
          return NESTED_LOOP_ERROR;
14149
4225
        }
14150
4226
        if (join->rollup.state != ROLLUP::STATE_NONE)
14151
 
        {
14152
 
          if (join->rollup_write_data((uint) (idx+1), table))
14153
 
            return(NESTED_LOOP_ERROR);
14154
 
        }
14155
 
        if (end_of_records)
14156
 
          return(NESTED_LOOP_OK);
 
4227
        {
 
4228
          if (join->rollup_write_data((uint32_t) (idx+1), table))
 
4229
            return NESTED_LOOP_ERROR;
 
4230
        }
 
4231
        if (end_of_records)
 
4232
          return NESTED_LOOP_OK;
14157
4233
      }
14158
4234
    }
14159
4235
    else
14160
4236
    {
14161
4237
      if (end_of_records)
14162
 
        return(NESTED_LOOP_OK);
 
4238
        return NESTED_LOOP_OK;
14163
4239
      join->first_record=1;
14164
 
      VOID(test_if_item_cache_changed(join->group_fields));
 
4240
      test_if_item_cache_changed(join->group_fields);
14165
4241
    }
14166
4242
    if (idx < (int) join->send_group_parts)
14167
4243
    {
14168
4244
      copy_fields(&join->tmp_table_param);
14169
4245
      copy_funcs(join->tmp_table_param.items_to_copy);
14170
4246
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14171
 
        return(NESTED_LOOP_ERROR);
14172
 
      return(NESTED_LOOP_OK);
 
4247
        return NESTED_LOOP_ERROR;
 
4248
      return NESTED_LOOP_OK;
14173
4249
    }
14174
4250
  }
14175
4251
  if (update_sum_func(join->sum_funcs))
14176
 
    return(NESTED_LOOP_ERROR);
14177
 
  return(NESTED_LOOP_OK);
 
4252
    return NESTED_LOOP_ERROR;
 
4253
  return NESTED_LOOP_OK;
14178
4254
}
14179
4255
 
14180
 
 
14181
4256
/*****************************************************************************
14182
4257
  Remove calculation with tables that aren't yet read. Remove also tests
14183
4258
  against fields that are read through key where the table is not a
14190
4265
  @return
14191
4266
    1 if right_item is used removable reference key on left_item
14192
4267
*/
14193
 
 
14194
 
static bool test_if_ref(Item_field *left_item,Item *right_item)
 
4268
bool test_if_ref(Item_field *left_item,Item *right_item)
14195
4269
{
14196
4270
  Field *field=left_item->field;
14197
4271
  // No need to change const test. We also have to keep tests on LEFT JOIN
14202
4276
    {
14203
4277
      right_item= right_item->real_item();
14204
4278
      if (right_item->type() == Item::FIELD_ITEM)
14205
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
4279
        return (field->eq_def(((Item_field *) right_item)->field));
14206
4280
      /* remove equalities injected by IN->EXISTS transformation */
14207
4281
      else if (right_item->type() == Item::CACHE_ITEM)
14208
4282
        return ((Item_cache *)right_item)->eq_def (field);
14209
4283
      if (right_item->const_item() && !(right_item->is_null()))
14210
4284
      {
14211
 
        /*
14212
 
          We can remove binary fields and numerical fields except float,
14213
 
          as float comparison isn't 100 % secure
14214
 
          We have to keep normal strings to be able to check for end spaces
 
4285
        /*
 
4286
          We can remove binary fields and numerical fields except float,
 
4287
          as float comparison isn't 100 % secure
 
4288
          We have to keep normal strings to be able to check for end spaces
14215
4289
 
14216
 
          sergefp: the above seems to be too restrictive. Counterexample:
14217
 
            create table t100 (v varchar(10), key(v)) default charset=latin1;
14218
 
            insert into t100 values ('a'),('a ');
14219
 
            explain select * from t100 where v='a';
14220
 
          The EXPLAIN shows 'using Where'. Running the query returns both
14221
 
          rows, so it seems there are no problems with endspace in the most
14222
 
          frequent case?
14223
 
        */
14224
 
        if (field->binary() &&
14225
 
            field->real_type() != DRIZZLE_TYPE_STRING &&
14226
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
14227
 
            field->decimals() == 0)
14228
 
        {
14229
 
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
14230
 
        }
 
4290
                sergefp: the above seems to be too restrictive. Counterexample:
 
4291
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
 
4292
                  insert into t100 values ('a'),('a ');
 
4293
                  explain select * from t100 where v='a';
 
4294
                The EXPLAIN shows 'using Where'. Running the query returns both
 
4295
                rows, so it seems there are no problems with endspace in the most
 
4296
                frequent case?
 
4297
        */
 
4298
        if (field->binary() &&
 
4299
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
4300
            field->decimals() == 0)
 
4301
        {
 
4302
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
4303
        }
14231
4304
      }
14232
4305
    }
14233
4306
  }
14234
 
  return 0;                                     // keep test
14235
 
}
14236
 
 
14237
 
/**
14238
 
   @brief Replaces an expression destructively inside the expression tree of
14239
 
   the WHERE clase.
14240
 
 
14241
 
   @note Because of current requirements for semijoin flattening, we do not
14242
 
   need to recurse here, hence this function will only examine the top-level
14243
 
   AND conditions. (see JOIN::prepare, comment above the line 
14244
 
   'if (do_materialize)'
14245
 
   
14246
 
   @param join The top-level query.
14247
 
   @param old_cond The expression to be replaced.
14248
 
   @param new_cond The expression to be substituted.
14249
 
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
14250
 
   the new expression.
14251
 
   @return <code>true</code> if there was an error, <code>false</code> if
14252
 
   successful.
14253
 
*/
14254
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
14255
 
                                       Item *new_cond, bool do_fix_fields)
14256
 
{
14257
 
  if (join->conds == old_cond) {
14258
 
    join->conds= new_cond;
14259
 
    if (do_fix_fields)
14260
 
      new_cond->fix_fields(join->thd, &join->conds);
14261
 
    return false;
14262
 
  }
14263
 
  
14264
 
  if (join->conds->type() == Item::COND_ITEM) {
14265
 
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
14266
 
    Item *item;
14267
 
    while ((item= li++))
14268
 
      if (item == old_cond) 
14269
 
      {
14270
 
        li.replace(new_cond);
14271
 
        if (do_fix_fields)
14272
 
          new_cond->fix_fields(join->thd, li.ref());
14273
 
        return false;
14274
 
      }
14275
 
  }
14276
 
 
14277
 
  return true;
 
4307
  return 0;
14278
4308
}
14279
4309
 
14280
4310
/*
14281
4311
  Extract a condition that can be checked after reading given table
14282
 
  
 
4312
 
14283
4313
  SYNOPSIS
14284
4314
    make_cond_for_table()
14285
4315
      cond         Condition to analyze
14286
4316
      tables       Tables for which "current field values" are available
14287
 
      used_table   Table that we're extracting the condition for (may 
 
4317
      used_table   Table that we're extracting the condition for (may
14288
4318
                   also include PSEUDO_TABLE_BITS
14289
4319
 
14290
4320
  DESCRIPTION
14294
4324
 
14295
4325
    The function assumes that
14296
4326
      - Constant parts of the condition has already been checked.
14297
 
      - Condition that could be checked for tables in 'tables' has already 
 
4327
      - Condition that could be checked for tables in 'tables' has already
14298
4328
        been checked.
14299
 
        
 
4329
 
14300
4330
    The function takes into account that some parts of the condition are
14301
4331
    guaranteed to be true by employed 'ref' access methods (the code that
14302
4332
    does this is located at the end, search down for "EQ_FUNC").
14303
4333
 
14304
4334
 
14305
 
  SEE ALSO 
 
4335
  SEE ALSO
14306
4336
    make_cond_for_info_schema uses similar algorithm
14307
4337
 
14308
4338
  RETURN
14309
4339
    Extracted condition
14310
4340
*/
14311
 
 
14312
 
static COND *
14313
 
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
14314
 
                    bool exclude_expensive_cond)
 
4341
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
14315
4342
{
14316
4343
  if (used_table && !(cond->used_tables() & used_table) &&
14317
 
      /*
14318
 
        Exclude constant conditions not checked at optimization time if
14319
 
        the table we are pushing conditions to is the first one.
14320
 
        As a result, such conditions are not considered as already checked
14321
 
        and will be checked at execution time, attached to the first table.
14322
 
      */
14323
 
      !((used_table & 1) && cond->is_expensive()))
 
4344
    /*
 
4345
      Exclude constant conditions not checked at optimization time if
 
4346
      the table we are pushing conditions to is the first one.
 
4347
      As a result, such conditions are not considered as already checked
 
4348
      and will be checked at execution time, attached to the first table.
 
4349
    */
 
4350
    !((used_table & 1) && cond->is_expensive()))
14324
4351
    return (COND*) 0;                           // Already checked
14325
4352
  if (cond->type() == Item::COND_ITEM)
14326
4353
  {
14329
4356
      /* Create new top level AND item */
14330
4357
      Item_cond_and *new_cond=new Item_cond_and;
14331
4358
      if (!new_cond)
14332
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
4359
        return (COND*) 0;
14333
4360
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14334
4361
      Item *item;
14335
4362
      while ((item=li++))
14336
4363
      {
14337
 
        Item *fix=make_cond_for_table(item,tables,used_table,
14338
 
                                      exclude_expensive_cond);
14339
 
        if (fix)
14340
 
          new_cond->argument_list()->push_back(fix);
 
4364
        Item *fix= make_cond_for_table(item,tables,used_table,
 
4365
                                            exclude_expensive_cond);
 
4366
        if (fix)
 
4367
          new_cond->argument_list()->push_back(fix);
14341
4368
      }
14342
 
      switch (new_cond->argument_list()->elements) {
14343
 
      case 0:
14344
 
        return (COND*) 0;                       // Always true
14345
 
      case 1:
14346
 
        return new_cond->argument_list()->head();
14347
 
      default:
14348
 
        /*
14349
 
          Item_cond_and do not need fix_fields for execution, its parameters
14350
 
          are fixed or do not need fix_fields, too
14351
 
        */
14352
 
        new_cond->quick_fix_field();
14353
 
        new_cond->used_tables_cache=
14354
 
          ((Item_cond_and*) cond)->used_tables_cache &
14355
 
          tables;
14356
 
        return new_cond;
 
4369
      switch (new_cond->argument_list()->elements) 
 
4370
      {
 
4371
        case 0:
 
4372
          return (COND*) 0;                     // Always true
 
4373
        case 1:
 
4374
          return new_cond->argument_list()->head();
 
4375
        default:
 
4376
          /*
 
4377
            Item_cond_and do not need fix_fields for execution, its parameters
 
4378
            are fixed or do not need fix_fields, too
 
4379
          */
 
4380
          new_cond->quick_fix_field();
 
4381
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
 
4382
          return new_cond;
14357
4383
      }
14358
4384
    }
14359
4385
    else
14360
4386
    {                                           // Or list
14361
4387
      Item_cond_or *new_cond=new Item_cond_or;
14362
4388
      if (!new_cond)
14363
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
4389
        return (COND*) 0;
14364
4390
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14365
4391
      Item *item;
14366
4392
      while ((item=li++))
14367
4393
      {
14368
 
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
14369
 
        if (!fix)
14370
 
          return (COND*) 0;                     // Always true
14371
 
        new_cond->argument_list()->push_back(fix);
 
4394
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
4395
        if (!fix)
 
4396
          return (COND*) 0;                     // Always true
 
4397
        new_cond->argument_list()->push_back(fix);
14372
4398
      }
14373
4399
      /*
14374
 
        Item_cond_and do not need fix_fields for execution, its parameters
14375
 
        are fixed or do not need fix_fields, too
 
4400
        Item_cond_and do not need fix_fields for execution, its parameters
 
4401
        are fixed or do not need fix_fields, too
14376
4402
      */
14377
4403
      new_cond->quick_fix_field();
14378
4404
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
14397
4423
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
14398
4424
    return cond;                                // Not boolean op
14399
4425
 
14400
 
  /* 
 
4426
  /*
14401
4427
    Remove equalities that are guaranteed to be true by use of 'ref' access
14402
4428
    method
14403
4429
  */
14405
4431
  {
14406
4432
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
14407
4433
    Item *right_item= ((Item_func*) cond)->arguments()[1];
14408
 
    if (left_item->type() == Item::FIELD_ITEM &&
14409
 
        test_if_ref((Item_field*) left_item,right_item))
 
4434
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
14410
4435
    {
14411
4436
      cond->marker=3;                   // Checked when read
14412
4437
      return (COND*) 0;
14413
4438
    }
14414
 
    if (right_item->type() == Item::FIELD_ITEM &&
14415
 
        test_if_ref((Item_field*) right_item,left_item))
 
4439
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
14416
4440
    {
14417
4441
      cond->marker=3;                   // Checked when read
14418
4442
      return (COND*) 0;
14422
4446
  return cond;
14423
4447
}
14424
4448
 
14425
 
 
14426
 
static Item *
14427
 
part_of_refkey(TABLE *table,Field *field)
 
4449
static Item *part_of_refkey(Table *table,Field *field)
14428
4450
{
14429
4451
  if (!table->reginfo.join_tab)
14430
4452
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
14431
4453
 
14432
 
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
 
4454
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
14433
4455
  if (ref_parts)
14434
4456
  {
14435
4457
    KEY_PART_INFO *key_part=
14436
4458
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
14437
 
    uint part;
 
4459
    uint32_t part;
14438
4460
 
14439
4461
    for (part=0 ; part < ref_parts ; part++)
14440
4462
    {
14450
4472
  return (Item*) 0;
14451
4473
}
14452
4474
 
14453
 
 
14454
4475
/**
14455
 
  Test if one can use the key to resolve ORDER BY.
 
4476
  Test if one can use the key to resolve order_st BY.
14456
4477
 
14457
4478
  @param order                 Sort order
14458
4479
  @param table                 Table to sort
14471
4492
  @retval
14472
4493
    -1   Reverse key can be used
14473
4494
*/
14474
 
 
14475
 
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
14476
 
                                uint *used_key_parts)
 
4495
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
14477
4496
{
14478
 
  KEY_PART_INFO *key_part,*key_part_end;
14479
 
  key_part=table->key_info[idx].key_part;
14480
 
  key_part_end=key_part+table->key_info[idx].key_parts;
 
4497
  KEY_PART_INFO *key_part= NULL;
 
4498
  KEY_PART_INFO *key_part_end= NULL;
 
4499
  key_part= table->key_info[idx].key_part;
 
4500
  key_part_end= key_part + table->key_info[idx].key_parts;
14481
4501
  key_part_map const_key_parts=table->const_key_parts[idx];
14482
 
  int reverse=0;
 
4502
  int reverse= 0;
14483
4503
  bool on_primary_key= false;
14484
4504
 
14485
4505
  for (; order ; order=order->next, const_key_parts>>=1)
14489
4509
 
14490
4510
    /*
14491
4511
      Skip key parts that are constants in the WHERE clause.
14492
 
      These are already skipped in the ORDER BY by const_expression_in_where()
 
4512
      These are already skipped in the order_st BY by const_expression_in_where()
14493
4513
    */
14494
4514
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
14495
 
      key_part++; 
 
4515
      key_part++;
14496
4516
 
14497
4517
    if (key_part == key_part_end)
14498
4518
    {
14499
 
      /* 
 
4519
      /*
14500
4520
        We are at the end of the key. Check if the engine has the primary
14501
4521
        key as a suffix to the secondary keys. If it has continue to check
14502
4522
        the primary key as a suffix.
14503
4523
      */
14504
4524
      if (!on_primary_key &&
14505
 
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
4525
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
14506
4526
          table->s->primary_key != MAX_KEY)
14507
4527
      {
14508
4528
        on_primary_key= true;
14511
4531
        const_key_parts=table->const_key_parts[table->s->primary_key];
14512
4532
 
14513
4533
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
14514
 
          key_part++; 
 
4534
          key_part++;
14515
4535
        /*
14516
4536
         The primary and secondary key parts were all const (i.e. there's
14517
4537
         one row).  The sorting doesn't matter.
14535
4555
    key_part++;
14536
4556
  }
14537
4557
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
14538
 
    (uint) (key_part - table->key_info[idx].key_part);
14539
 
  if (reverse == -1 && !(table->file->index_flags(idx, *used_key_parts-1, 1) &
 
4558
    (uint32_t) (key_part - table->key_info[idx].key_part);
 
4559
  if (reverse == -1 && !(table->index_flags(idx) &
14540
4560
                         HA_READ_PREV))
14541
4561
    reverse= 0;                                 // Index can't be used
14542
4562
  return(reverse);
14543
4563
}
14544
4564
 
14545
 
 
14546
 
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
14547
 
{
14548
 
  uint min_length= (uint) ~0;
14549
 
  uint best= MAX_KEY;
14550
 
  if (!usable_keys->is_clear_all())
14551
 
  {
14552
 
    for (uint nr=0; nr < table->s->keys ; nr++)
14553
 
    {
14554
 
      if (usable_keys->is_set(nr))
14555
 
      {
14556
 
        if (table->key_info[nr].key_length < min_length)
14557
 
        {
14558
 
          min_length=table->key_info[nr].key_length;
14559
 
          best=nr;
14560
 
        }
14561
 
      }
14562
 
    }
14563
 
  }
14564
 
  return best;
14565
 
}
14566
 
 
14567
4565
/**
14568
4566
  Test if a second key is the subkey of the first one.
14569
4567
 
14579
4577
  @retval
14580
4578
    0   no sub key
14581
4579
*/
14582
 
 
14583
 
inline bool 
14584
 
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
14585
 
          KEY_PART_INFO *ref_key_part_end)
 
4580
inline bool is_subkey(KEY_PART_INFO *key_part,
 
4581
                      KEY_PART_INFO *ref_key_part,
 
4582
                      KEY_PART_INFO *ref_key_part_end)
14586
4583
{
14587
4584
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
14588
 
    if (!key_part->field->eq(ref_key_part->field))
 
4585
    if (! key_part->field->eq(ref_key_part->field))
14589
4586
      return 0;
14590
4587
  return 1;
14591
4588
}
14601
4598
    - MAX_KEY                   If we can't use other key
14602
4599
    - the number of found key   Otherwise
14603
4600
*/
14604
 
 
14605
 
static uint
14606
 
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
14607
 
               const key_map *usable_keys)
 
4601
static uint32_t test_if_subkey(order_st *order,
 
4602
                               Table *table,
 
4603
                               uint32_t ref,
 
4604
                               uint32_t ref_key_parts,
 
4605
                               const key_map *usable_keys)
14608
4606
{
14609
 
  uint nr;
14610
 
  uint min_length= (uint) ~0;
14611
 
  uint best= MAX_KEY;
14612
 
  uint not_used;
 
4607
  uint32_t nr;
 
4608
  uint32_t min_length= UINT32_MAX;
 
4609
  uint32_t best= MAX_KEY;
 
4610
  uint32_t not_used;
14613
4611
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
14614
4612
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
14615
4613
 
14616
4614
  for (nr= 0 ; nr < table->s->keys ; nr++)
14617
4615
  {
14618
 
    if (usable_keys->is_set(nr) &&
 
4616
    if (usable_keys->test(nr) &&
14619
4617
        table->key_info[nr].key_length < min_length &&
14620
4618
        table->key_info[nr].key_parts >= ref_key_parts &&
14621
4619
        is_subkey(table->key_info[nr].key_part, ref_key_part,
14629
4627
  return best;
14630
4628
}
14631
4629
 
14632
 
 
14633
4630
/**
14634
4631
  Check if GROUP BY/DISTINCT can be optimized away because the set is
14635
4632
  already known to be distinct.
14648
4645
    of the table are referenced by a list : either the select list
14649
4646
    through find_field_in_item_list or GROUP BY list through
14650
4647
    find_field_in_order_list.
14651
 
    If the above holds and the key parts cannot contain NULLs then we 
 
4648
    If the above holds and the key parts cannot contain NULLs then we
14652
4649
    can safely remove the GROUP BY/DISTINCT,
14653
4650
    as no result set can be more distinct than an unique key.
14654
4651
 
14661
4658
  @retval
14662
4659
    0                    not found.
14663
4660
*/
14664
 
 
14665
 
static bool
14666
 
list_contains_unique_index(TABLE *table,
14667
 
                          bool (*find_func) (Field *, void *), void *data)
 
4661
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
14668
4662
{
14669
 
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
 
4663
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
14670
4664
  {
14671
4665
    if (keynr == table->s->primary_key ||
14672
4666
         (table->key_info[keynr].flags & HA_NOSAME))
14673
4667
    {
14674
4668
      KEY *keyinfo= table->key_info + keynr;
14675
 
      KEY_PART_INFO *key_part, *key_part_end;
 
4669
      KEY_PART_INFO *key_part= NULL;
 
4670
      KEY_PART_INFO *key_part_end= NULL;
14676
4671
 
14677
4672
      for (key_part=keyinfo->key_part,
14678
4673
           key_part_end=key_part+ keyinfo->key_parts;
14679
4674
           key_part < key_part_end;
14680
4675
           key_part++)
14681
4676
      {
14682
 
        if (key_part->field->maybe_null() || 
14683
 
            !find_func(key_part->field, data))
 
4677
        if (key_part->field->maybe_null() ||
 
4678
            ! find_func(key_part->field, data))
14684
4679
          break;
14685
4680
      }
14686
4681
      if (key_part == key_part_end)
14690
4685
  return 0;
14691
4686
}
14692
4687
 
14693
 
 
14694
4688
/**
14695
4689
  Helper function for list_contains_unique_index.
14696
 
  Find a field reference in a list of ORDER structures.
 
4690
  Find a field reference in a list of order_st structures.
14697
4691
  Finds a direct reference of the Field in the list.
14698
4692
 
14699
4693
  @param field                The field to search for.
14700
 
  @param data                 ORDER *.The list to search in
 
4694
  @param data                 order_st *.The list to search in
14701
4695
 
14702
4696
  @retval
14703
4697
    1                    found
14704
4698
  @retval
14705
4699
    0                    not found.
14706
4700
*/
14707
 
 
14708
 
static bool
14709
 
find_field_in_order_list (Field *field, void *data)
 
4701
bool find_field_in_order_list (Field *field, void *data)
14710
4702
{
14711
 
  ORDER *group= (ORDER *) data;
 
4703
  order_st *group= (order_st *) data;
14712
4704
  bool part_found= 0;
14713
 
  for (ORDER *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
 
4705
  for (order_st *tmp_group= group; tmp_group; tmp_group=tmp_group->next)
14714
4706
  {
14715
4707
    Item *item= (*tmp_group->item)->real_item();
14716
4708
    if (item->type() == Item::FIELD_ITEM &&
14723
4715
  return part_found;
14724
4716
}
14725
4717
 
14726
 
 
14727
4718
/**
14728
4719
  Helper function for list_contains_unique_index.
14729
4720
  Find a field reference in a dynamic list of Items.
14737
4728
  @retval
14738
4729
    0                    not found.
14739
4730
*/
14740
 
 
14741
 
static bool
14742
 
find_field_in_item_list (Field *field, void *data)
 
4731
bool find_field_in_item_list (Field *field, void *data)
14743
4732
{
14744
4733
  List<Item> *fields= (List<Item> *) data;
14745
4734
  bool part_found= 0;
14758
4747
  return part_found;
14759
4748
}
14760
4749
 
14761
 
 
14762
4750
/**
14763
 
  Test if we can skip the ORDER BY by using an index.
 
4751
  Test if we can skip the order_st BY by using an index.
14764
4752
 
14765
4753
  SYNOPSIS
14766
4754
    test_if_skip_sort_order()
14770
4758
      no_changes
14771
4759
      map
14772
4760
 
14773
 
  If we can use an index, the JOIN_TAB / tab->select struct
 
4761
  If we can use an index, the JoinTable / tab->select struct
14774
4762
  is changed to use the index.
14775
4763
 
14776
4764
  The index must cover all fields in <order>, or it will not be considered.
14777
4765
 
14778
4766
  @todo
14779
 
    - sergeyp: Results of all index merge selects actually are ordered 
 
4767
    - sergeyp: Results of all index merge selects actually are ordered
14780
4768
    by clustered PK values.
14781
4769
 
14782
4770
  @retval
14784
4772
  @retval
14785
4773
    1    We can use an index.
14786
4774
*/
14787
 
 
14788
 
static bool
14789
 
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
14790
 
                        bool no_changes, const key_map *map)
 
4775
bool test_if_skip_sort_order(JoinTable *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
14791
4776
{
14792
 
  int ref_key;
14793
 
  uint ref_key_parts;
 
4777
  int32_t ref_key;
 
4778
  uint32_t ref_key_parts;
14794
4779
  int order_direction;
14795
 
  uint used_key_parts;
14796
 
  TABLE *table=tab->table;
14797
 
  SQL_SELECT *select=tab->select;
 
4780
  uint32_t used_key_parts;
 
4781
  Table *table=tab->table;
 
4782
  optimizer::SqlSelect *select= tab->select;
14798
4783
  key_map usable_keys;
14799
 
  QUICK_SELECT_I *save_quick= 0;
 
4784
  optimizer::QuickSelectInterface *save_quick= NULL;
14800
4785
 
14801
4786
  /*
14802
 
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
 
4787
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
14803
4788
    been taken into account.
14804
4789
  */
14805
4790
  usable_keys= *map;
14806
4791
 
14807
 
  for (ORDER *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
 
4792
  for (order_st *tmp_order=order; tmp_order ; tmp_order=tmp_order->next)
14808
4793
  {
14809
4794
    Item *item= (*tmp_order->item)->real_item();
14810
4795
    if (item->type() != Item::FIELD_ITEM)
14811
4796
    {
14812
 
      usable_keys.clear_all();
 
4797
      usable_keys.reset();
14813
4798
      return(0);
14814
4799
    }
14815
 
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
14816
 
    if (usable_keys.is_clear_all())
 
4800
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
 
4801
    if (usable_keys.none())
14817
4802
      return(0);                                        // No usable keys
14818
4803
  }
14819
4804
 
14823
4808
  {
14824
4809
    ref_key=       tab->ref.key;
14825
4810
    ref_key_parts= tab->ref.key_parts;
14826
 
    if (tab->type == JT_REF_OR_NULL)
 
4811
    if (tab->type == AM_REF_OR_NULL)
14827
4812
      return(0);
14828
4813
  }
14829
 
  else if (select && select->quick)             // Range found by opt_range
 
4814
  else if (select && select->quick)             // Range found by optimizer/range
14830
4815
  {
14831
4816
    int quick_type= select->quick->get_type();
14832
4817
    save_quick= select->quick;
14833
 
    /* 
14834
 
      assume results are not ordered when index merge is used 
14835
 
      TODO: sergeyp: Results of all index merge selects actually are ordered 
 
4818
    /*
 
4819
      assume results are not ordered when index merge is used
 
4820
      TODO: sergeyp: Results of all index merge selects actually are ordered
14836
4821
      by clustered PK values.
14837
4822
    */
14838
 
  
14839
 
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
14840
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
14841
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT)
 
4823
 
 
4824
    if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
 
4825
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
 
4826
        quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT)
14842
4827
      return(0);
14843
4828
    ref_key=       select->quick->index;
14844
4829
    ref_key_parts= select->quick->used_key_parts;
14849
4834
    /*
14850
4835
      We come here when there is a REF key.
14851
4836
    */
14852
 
    if (!usable_keys.is_set(ref_key))
 
4837
    if (! usable_keys.test(ref_key))
14853
4838
    {
14854
4839
      /*
14855
 
        We come here when ref_key is not among usable_keys
 
4840
        We come here when ref_key is not among usable_keys
14856
4841
      */
14857
 
      uint new_ref_key;
 
4842
      uint32_t new_ref_key;
14858
4843
      /*
14859
 
        If using index only read, only consider other possible index only
14860
 
        keys
 
4844
        If using index only read, only consider other possible index only
 
4845
        keys
14861
4846
      */
14862
 
      if (table->covering_keys.is_set(ref_key))
14863
 
        usable_keys.intersect(table->covering_keys);
 
4847
      if (table->covering_keys.test(ref_key))
 
4848
        usable_keys&= table->covering_keys;
14864
4849
      if (tab->pre_idx_push_select_cond)
14865
4850
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
14866
4851
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
14867
4852
                                       &usable_keys)) < MAX_KEY)
14868
4853
      {
14869
 
        /* Found key that can be used to retrieve data in sorted order */
14870
 
        if (tab->ref.key >= 0)
14871
 
        {
 
4854
        /* Found key that can be used to retrieve data in sorted order */
 
4855
        if (tab->ref.key >= 0)
 
4856
        {
14872
4857
          /*
14873
 
            We'll use ref access method on key new_ref_key. In general case 
 
4858
            We'll use ref access method on key new_ref_key. In general case
14874
4859
            the index search tuple for new_ref_key will be different (e.g.
14875
4860
            when one index is defined as (part1, part2, ...) and another as
14876
 
            (part1, part2(N), ...) and the WHERE clause contains 
14877
 
            "part1 = const1 AND part2=const2". 
 
4861
            (part1, part2(N), ...) and the WHERE clause contains
 
4862
            "part1 = const1 AND part2=const2".
14878
4863
            So we build tab->ref from scratch here.
14879
4864
          */
14880
 
          KEYUSE *keyuse= tab->keyuse;
14881
 
          while (keyuse->key != new_ref_key && keyuse->table == tab->table)
 
4865
          optimizer::KeyUse *keyuse= tab->keyuse;
 
4866
          while (keyuse->getKey() != new_ref_key && keyuse->getTable() == tab->table)
14882
4867
            keyuse++;
14883
4868
 
14884
 
          if (create_ref_for_key(tab->join, tab, keyuse, 
 
4869
          if (create_ref_for_key(tab->join, tab, keyuse,
14885
4870
                                 tab->join->const_table_map))
14886
4871
            return(0);
14887
 
        }
14888
 
        else
14889
 
        {
 
4872
        }
 
4873
        else
 
4874
        {
14890
4875
          /*
14891
 
            The range optimizer constructed QUICK_RANGE for ref_key, and
 
4876
            The range optimizer constructed QuickRange for ref_key, and
14892
4877
            we want to use instead new_ref_key as the index. We can't
14893
4878
            just change the index of the quick select, because this may
14894
4879
            result in an incosistent QUICK_SELECT object. Below we
14896
4881
            parameres are set correctly by the range optimizer.
14897
4882
           */
14898
4883
          key_map new_ref_key_map;
14899
 
          new_ref_key_map.clear_all();  // Force the creation of quick select
14900
 
          new_ref_key_map.set_bit(new_ref_key); // only for new_ref_key.
 
4884
          new_ref_key_map.reset();  // Force the creation of quick select
 
4885
          new_ref_key_map.set(new_ref_key); // only for new_ref_key.
14901
4886
 
14902
 
          if (select->test_quick_select(tab->join->thd, new_ref_key_map, 0,
 
4887
          if (select->test_quick_select(tab->join->session, new_ref_key_map, 0,
14903
4888
                                        (tab->join->select_options &
14904
4889
                                         OPTION_FOUND_ROWS) ?
14905
4890
                                        HA_POS_ERROR :
14907
4892
                                        true) <=
14908
4893
              0)
14909
4894
            return(0);
14910
 
        }
 
4895
        }
14911
4896
        ref_key= new_ref_key;
14912
4897
      }
14913
4898
    }
14914
4899
    /* Check if we get the rows in requested sorted order by using the key */
14915
 
    if (usable_keys.is_set(ref_key) &&
 
4900
    if (usable_keys.test(ref_key) &&
14916
4901
        (order_direction= test_if_order_by_key(order,table,ref_key,
14917
4902
                                               &used_key_parts)))
14918
4903
      goto check_reverse_order;
14922
4907
      Check whether there is an index compatible with the given order
14923
4908
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
14924
4909
      or a table scan.
14925
 
      It may be the case if ORDER/GROUP BY is used with LIMIT.
 
4910
      It may be the case if order_st/GROUP BY is used with LIMIT.
14926
4911
    */
14927
 
    uint nr;
 
4912
    uint32_t nr;
14928
4913
    key_map keys;
14929
 
    uint best_key_parts= 0;
 
4914
    uint32_t best_key_parts= 0;
14930
4915
    int best_key_direction= 0;
14931
4916
    ha_rows best_records= 0;
14932
4917
    double read_time;
14934
4919
    bool is_best_covering= false;
14935
4920
    double fanout= 1;
14936
4921
    JOIN *join= tab->join;
14937
 
    uint tablenr= tab - join->join_tab;
14938
 
    ha_rows table_records= table->file->stats.records;
 
4922
    uint32_t tablenr= tab - join->join_tab;
 
4923
    ha_rows table_records= table->cursor->stats.records;
14939
4924
    bool group= join->group && order == join->group_list;
 
4925
    optimizer::Position cur_pos;
14940
4926
 
14941
4927
    /*
14942
4928
      If not used with LIMIT, only use keys if the whole query can be
14945
4931
    */
14946
4932
    if (select_limit >= table_records)
14947
4933
    {
14948
 
      /* 
14949
 
        filesort() and join cache are usually faster than reading in 
 
4934
      /*
 
4935
        filesort() and join cache are usually faster than reading in
14950
4936
        index order and not using join cache
14951
4937
        */
14952
 
      if (tab->type == JT_ALL && tab->join->tables > tab->join->const_tables + 1)
 
4938
      if (tab->type == AM_ALL && tab->join->tables > tab->join->const_tables + 1)
14953
4939
        return(0);
14954
 
      keys= *table->file->keys_to_use_for_scanning();
14955
 
      keys.merge(table->covering_keys);
 
4940
      keys= *table->cursor->keys_to_use_for_scanning();
 
4941
      keys|= table->covering_keys;
14956
4942
 
14957
4943
      /*
14958
 
        We are adding here also the index specified in FORCE INDEX clause, 
14959
 
        if any.
14960
 
        This is to allow users to use index in ORDER BY.
 
4944
        We are adding here also the index specified in FORCE INDEX clause,
 
4945
        if any.
 
4946
        This is to allow users to use index in order_st BY.
14961
4947
      */
14962
 
      if (table->force_index) 
14963
 
        keys.merge(group ? table->keys_in_use_for_group_by :
14964
 
                           table->keys_in_use_for_order_by);
14965
 
      keys.intersect(usable_keys);
 
4948
      if (table->force_index)
 
4949
        keys|= (group ? table->keys_in_use_for_group_by :
 
4950
                                table->keys_in_use_for_order_by);
 
4951
      keys&= usable_keys;
14966
4952
    }
14967
4953
    else
14968
4954
      keys= usable_keys;
14969
4955
 
14970
 
    read_time= join->best_positions[tablenr].read_time;
14971
 
    for (uint i= tablenr+1; i < join->tables; i++)
14972
 
      fanout*= join->best_positions[i].records_read; // fanout is always >= 1
 
4956
    cur_pos= join->getPosFromOptimalPlan(tablenr);
 
4957
    read_time= cur_pos.getCost();
 
4958
    for (uint32_t i= tablenr+1; i < join->tables; i++)
 
4959
    {
 
4960
      cur_pos= join->getPosFromOptimalPlan(i);
 
4961
      fanout*= cur_pos.getFanout(); // fanout is always >= 1
 
4962
    }
14973
4963
 
14974
4964
    for (nr=0; nr < table->s->keys ; nr++)
14975
4965
    {
14976
4966
      int direction;
14977
 
      if (keys.is_set(nr) &&
 
4967
      if (keys.test(nr) &&
14978
4968
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
14979
4969
      {
14980
 
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
14981
 
        
14982
 
        /* 
14983
 
          Don't use an index scan with ORDER BY without limit.
 
4970
        bool is_covering= table->covering_keys.test(nr) || (nr == table->s->primary_key && table->cursor->primary_key_is_clustered());
 
4971
 
 
4972
        /*
 
4973
          Don't use an index scan with order_st BY without limit.
14984
4974
          For GROUP BY without limit always use index scan
14985
 
          if there is a suitable index. 
 
4975
          if there is a suitable index.
14986
4976
          Why we hold to this asymmetry hardly can be explained
14987
4977
          rationally. It's easy to demonstrate that using
14988
4978
          temporary table + filesort could be cheaper for grouping
14989
4979
          queries too.
14990
 
        */ 
 
4980
        */
14991
4981
        if (is_covering ||
14992
 
            select_limit != HA_POS_ERROR || 
 
4982
            select_limit != HA_POS_ERROR ||
14993
4983
            (ref_key < 0 && (group || table->force_index)))
14994
 
        { 
 
4984
        {
14995
4985
          double rec_per_key;
14996
4986
          double index_scan_time;
14997
4987
          KEY *keyinfo= tab->table->key_info+nr;
15000
4990
          if (group)
15001
4991
          {
15002
4992
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
15003
 
            set_if_bigger(rec_per_key, 1);
 
4993
            set_if_bigger(rec_per_key, 1.0);
15004
4994
            /*
15005
4995
              With a grouping query each group containing on average
15006
4996
              rec_per_key records produces only one row that will
15007
4997
              be included into the result set.
15008
 
            */  
 
4998
            */
15009
4999
            if (select_limit > table_records/rec_per_key)
15010
5000
                select_limit= table_records;
15011
5001
            else
15012
5002
              select_limit= (ha_rows) (select_limit*rec_per_key);
15013
5003
          }
15014
 
          /* 
 
5004
          /*
15015
5005
            If tab=tk is not the last joined table tn then to get first
15016
5006
            L records from the result set we can expect to retrieve
15017
5007
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
15020
5010
            So the estimate for L/fanout(tk,tn) will be too optimistic
15021
5011
            and as result we'll choose an index scan when using ref/range
15022
5012
            access + filesort will be cheaper.
15023
 
          */
 
5013
          */
15024
5014
          select_limit= (ha_rows) (select_limit < fanout ?
15025
5015
                                   1 : select_limit/fanout);
15026
5016
          /*
15027
5017
            We assume that each of the tested indexes is not correlated
15028
5018
            with ref_key. Thus, to select first N records we have to scan
15029
 
            N/selectivity(ref_key) index entries. 
 
5019
            N/selectivity(ref_key) index entries.
15030
5020
            selectivity(ref_key) = #scanned_records/#table_records =
15031
5021
            table->quick_condition_rows/table_records.
15032
5022
            In any case we can't select more than #table_records.
15033
 
            N/(table->quick_condition_rows/table_records) > table_records 
 
5023
            N/(table->quick_condition_rows/table_records) > table_records
15034
5024
            <=> N > table->quick_condition_rows.
15035
 
          */ 
 
5025
          */
15036
5026
          if (select_limit > table->quick_condition_rows)
15037
5027
            select_limit= table_records;
15038
5028
          else
15040
5030
                                     (double) table_records /
15041
5031
                                      table->quick_condition_rows);
15042
5032
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
15043
 
          set_if_bigger(rec_per_key, 1);
 
5033
          set_if_bigger(rec_per_key, 1.0);
15044
5034
          /*
15045
5035
            Here we take into account the fact that rows are
15046
5036
            accessed in sequences rec_per_key records in each.
15047
5037
            Rows in such a sequence are supposed to be ordered
15048
5038
            by rowid/primary key. When reading the data
15049
5039
            in a sequence we'll touch not more pages than the
15050
 
            table file contains.
 
5040
            table cursor contains.
15051
5041
            TODO. Use the formula for a disk sweep sequential access
15052
 
            to calculate the cost of accessing data rows for one 
 
5042
            to calculate the cost of accessing data rows for one
15053
5043
            index entry.
15054
 
          */
 
5044
          */
15055
5045
          index_scan_time= select_limit/rec_per_key *
15056
 
                           min(rec_per_key, table->file->scan_time());
 
5046
                           min(rec_per_key, table->cursor->scan_time());
15057
5047
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
15058
5048
              index_scan_time < read_time)
15059
5049
          {
15060
5050
            ha_rows quick_records= table_records;
15061
5051
            if (is_best_covering && !is_covering)
15062
5052
              continue;
15063
 
            if (table->quick_keys.is_set(nr))
 
5053
            if (table->quick_keys.test(nr))
15064
5054
              quick_records= table->quick_rows[nr];
15065
5055
            if (best_key < 0 ||
15066
5056
                (select_limit <= min(quick_records,best_records) ?
15071
5061
              best_key_parts= keyinfo->key_parts;
15072
5062
              best_records= quick_records;
15073
5063
              is_best_covering= is_covering;
15074
 
              best_key_direction= direction; 
 
5064
              best_key_direction= direction;
15075
5065
            }
15076
 
          }   
15077
 
        }      
 
5066
          }
 
5067
        }
15078
5068
      }
15079
5069
    }
15080
5070
    if (best_key >= 0)
15081
5071
    {
15082
5072
      bool quick_created= false;
15083
 
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
 
5073
      if (table->quick_keys.test(best_key) && best_key != ref_key)
15084
5074
      {
15085
 
        key_map map;
15086
 
        map.clear_all();       // Force the creation of quick select
15087
 
        map.set_bit(best_key); // only best_key.
15088
 
        quick_created=         
15089
 
          select->test_quick_select(join->thd, map, 0,
 
5075
        key_map test_map;
 
5076
        test_map.reset();       // Force the creation of quick select
 
5077
        test_map.set(best_key); // only best_key.
 
5078
        quick_created=
 
5079
          select->test_quick_select(join->session, test_map, 0,
15090
5080
                                    join->select_options & OPTION_FOUND_ROWS ?
15091
5081
                                    HA_POS_ERROR :
15092
5082
                                    join->unit->select_limit_cnt,
15095
5085
      if (!no_changes)
15096
5086
      {
15097
5087
        if (!quick_created)
15098
 
        {
 
5088
        {
15099
5089
          tab->index= best_key;
15100
5090
          tab->read_first_record= best_key_direction > 0 ?
15101
5091
                                  join_read_first:join_read_last;
15102
 
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
 
5092
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
15103
5093
          if (select && select->quick)
15104
5094
          {
15105
5095
            delete select->quick;
15106
5096
            select->quick= 0;
15107
5097
          }
15108
 
          if (table->covering_keys.is_set(best_key))
 
5098
          if (table->covering_keys.test(best_key))
15109
5099
          {
15110
5100
            table->key_read=1;
15111
 
            table->file->extra(HA_EXTRA_KEYREAD);
 
5101
            table->cursor->extra(HA_EXTRA_KEYREAD);
15112
5102
          }
15113
 
          table->file->ha_index_or_rnd_end();
 
5103
          table->cursor->ha_index_or_rnd_end();
15114
5104
          if (join->select_options & SELECT_DESCRIBE)
15115
5105
          {
15116
5106
            tab->ref.key= -1;
15117
5107
            tab->ref.key_parts= 0;
15118
 
            if (select_limit < table_records) 
 
5108
            if (select_limit < table_records)
15119
5109
              tab->limit= select_limit;
15120
5110
          }
15121
5111
        }
15122
 
        else if (tab->type != JT_ALL)
 
5112
        else if (tab->type != AM_ALL)
15123
5113
        {
15124
5114
          /*
15125
5115
            We're about to use a quick access to the table.
15127
5117
            method is actually used.
15128
5118
          */
15129
5119
          assert(tab->select->quick);
15130
 
          tab->type=JT_ALL;
 
5120
          tab->type= AM_ALL;
15131
5121
          tab->use_quick=1;
15132
5122
          tab->ref.key= -1;
15133
5123
          tab->ref.key_parts=0;         // Don't use ref key.
15134
5124
          tab->read_first_record= join_init_read_record;
15135
 
          /*
15136
 
            TODO: update the number of records in join->best_positions[tablenr]
15137
 
          */
15138
5125
        }
15139
5126
      }
15140
5127
      used_key_parts= best_key_parts;
15141
5128
      order_direction= best_key_direction;
15142
5129
    }
15143
5130
    else
15144
 
      return(0); 
15145
 
  } 
 
5131
      return(0);
 
5132
  }
15146
5133
 
15147
 
check_reverse_order:                  
15148
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
5134
check_reverse_order:
 
5135
  if (order_direction == -1)            // If order_st BY ... DESC
15149
5136
  {
15150
5137
    if (select && select->quick)
15151
5138
    {
15152
5139
      /*
15153
 
        Don't reverse the sort order, if it's already done.
 
5140
        Don't reverse the sort order, if it's already done.
15154
5141
        (In some cases test_if_order_by_key() can be called multiple times
15155
5142
      */
15156
 
      if (!select->quick->reverse_sorted())
 
5143
      if (! select->quick->reverse_sorted())
15157
5144
      {
15158
 
        QUICK_SELECT_DESC *tmp;
 
5145
        optimizer::QuickSelectDescending *tmp= NULL;
15159
5146
        bool error= false;
15160
5147
        int quick_type= select->quick->get_type();
15161
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
15162
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
15163
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
15164
 
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
15165
 
        {
15166
 
          tab->limit= 0;
15167
 
          select->quick= save_quick;
15168
 
          return(0);                   // Use filesort
15169
 
        }
15170
 
            
15171
 
        /* ORDER BY range_key DESC */
15172
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
15173
 
                                    used_key_parts, &error);
15174
 
        if (!tmp || error)
15175
 
        {
15176
 
          delete tmp;
15177
 
          select->quick= save_quick;
15178
 
          tab->limit= 0;
15179
 
          return(0);            // Reverse sort not supported
15180
 
        }
15181
 
        select->quick=tmp;
 
5148
        if (quick_type == optimizer::QuickSelectInterface::QS_TYPE_INDEX_MERGE ||
 
5149
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_INTERSECT ||
 
5150
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_ROR_UNION ||
 
5151
            quick_type == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX)
 
5152
        {
 
5153
          tab->limit= 0;
 
5154
          select->quick= save_quick;
 
5155
          return 0; // Use filesort
 
5156
        }
 
5157
 
 
5158
        /* order_st BY range_key DESC */
 
5159
        tmp= new optimizer::QuickSelectDescending((optimizer::QuickRangeSelect*)(select->quick),
 
5160
                                                  used_key_parts, 
 
5161
                                                  &error);
 
5162
        if (! tmp || error)
 
5163
        {
 
5164
          delete tmp;
 
5165
          select->quick= save_quick;
 
5166
          tab->limit= 0;
 
5167
          return 0; // Reverse sort not supported
 
5168
        }
 
5169
        select->quick=tmp;
15182
5170
      }
15183
5171
    }
15184
 
    else if (tab->type != JT_NEXT && 
 
5172
    else if (tab->type != AM_NEXT &&
15185
5173
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
15186
5174
    {
15187
5175
      /*
15188
 
        SELECT * FROM t1 WHERE a=1 ORDER BY a DESC,b DESC
 
5176
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
15189
5177
 
15190
 
        Use a traversal function that starts by reading the last row
15191
 
        with key part (A) and then traverse the index backwards.
 
5178
        Use a traversal function that starts by reading the last row
 
5179
        with key part (A) and then traverse the index backwards.
15192
5180
      */
15193
5181
      tab->read_first_record= join_read_last_key;
15194
5182
      tab->read_record.read_record= join_read_prev_same;
15196
5184
  }
15197
5185
  else if (select && select->quick)
15198
5186
    select->quick->sorted= 1;
15199
 
  return(1);
 
5187
  return 1;
15200
5188
}
15201
5189
 
15202
 
 
15203
5190
/*
15204
5191
  If not selecting by given key, create an index how records should be read
15205
5192
 
15206
5193
  SYNOPSIS
15207
5194
   create_sort_index()
15208
 
     thd                Thread handler
 
5195
     session            Thread Cursor
15209
5196
     tab                Table to sort (in join structure)
15210
5197
     order              How table should be sorted
15211
5198
     filesort_limit     Max number of rows that needs to be sorted
15212
5199
     select_limit       Max number of rows in final output
15213
5200
                        Used to decide if we should use index or not
15214
 
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
15215
 
                        Used to decide if we should use index or not     
 
5201
     is_order_by        true if we are sorting on order_st BY, false if GROUP BY
 
5202
                        Used to decide if we should use index or not
15216
5203
 
15217
5204
 
15218
5205
  IMPLEMENTATION
15219
5206
   - If there is an index that can be used, 'tab' is modified to use
15220
5207
     this index.
15221
 
   - If no index, create with filesort() an index file that can be used to
 
5208
   - If no index, create with filesort() an index cursor that can be used to
15222
5209
     retrieve rows in order (should be done with 'read_record').
15223
5210
     The sorted data is stored in tab->table and will be freed when calling
15224
 
     free_io_cache(tab->table).
 
5211
     tab->table->free_io_cache().
15225
5212
 
15226
5213
  RETURN VALUES
15227
5214
    0           ok
15228
5215
    -1          Some fatal error
15229
5216
    1           No records
15230
5217
*/
15231
 
 
15232
 
static int
15233
 
create_sort_index(THD *thd, JOIN *join, ORDER *order,
15234
 
                  ha_rows filesort_limit, ha_rows select_limit,
15235
 
                  bool is_order_by)
 
5218
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
15236
5219
{
15237
 
  uint length= 0;
 
5220
  uint32_t length= 0;
15238
5221
  ha_rows examined_rows;
15239
 
  TABLE *table;
15240
 
  SQL_SELECT *select;
15241
 
  JOIN_TAB *tab;
 
5222
  Table *table;
 
5223
  optimizer::SqlSelect *select= NULL;
 
5224
  JoinTable *tab;
15242
5225
 
15243
5226
  if (join->tables == join->const_tables)
15244
5227
    return(0);                          // One row, no need to sort
15252
5235
    is going to be used as it is applied now only for one table queries
15253
5236
    with covering indexes.
15254
5237
  */
15255
 
  if ((order != join->group_list || 
 
5238
  if ((order != join->group_list ||
15256
5239
       !(join->select_options & SELECT_BIG_RESULT) ||
15257
 
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
15258
 
      test_if_skip_sort_order(tab,order,select_limit,0, 
 
5240
       (select && select->quick && (select->quick->get_type() == optimizer::QuickSelectInterface::QS_TYPE_GROUP_MIN_MAX))) &&
 
5241
      test_if_skip_sort_order(tab,order,select_limit,0,
15259
5242
                              is_order_by ?  &table->keys_in_use_for_order_by :
15260
5243
                              &table->keys_in_use_for_group_by))
15261
5244
    return(0);
15262
 
  for (ORDER *ord= join->order; ord; ord= ord->next)
 
5245
  for (order_st *ord= join->order; ord; ord= ord->next)
15263
5246
    length++;
15264
 
  if (!(join->sortorder= 
 
5247
  if (!(join->sortorder=
15265
5248
        make_unireg_sortorder(order, &length, join->sortorder)))
15266
 
    goto err;                           /* purecov: inspected */
 
5249
    goto err;
15267
5250
 
15268
 
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
15269
 
                                             MYF(MY_WME | MY_ZEROFILL));
 
5251
  table->sort.io_cache= new IO_CACHE;
 
5252
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
15270
5253
  table->status=0;                              // May be wrong if quick_select
15271
5254
 
15272
5255
  // If table has a range, move it to select
15276
5259
    {
15277
5260
      select->quick=tab->quick;
15278
5261
      tab->quick=0;
15279
 
      /* 
 
5262
      /*
15280
5263
        We can only use 'Only index' if quick key is same as ref_key
15281
5264
        and in index_merge 'Only index' cannot be used
15282
5265
      */
15283
 
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
 
5266
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
15284
5267
      {
15285
 
        table->key_read=0;
15286
 
        table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5268
        table->key_read=0;
 
5269
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
15287
5270
      }
15288
5271
    }
15289
5272
    else
15290
5273
    {
15291
5274
      /*
15292
 
        We have a ref on a const;  Change this to a range that filesort
15293
 
        can use.
15294
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
15295
 
        field, quick will contain an empty record set.
 
5275
        We have a ref on a const;  Change this to a range that filesort
 
5276
        can use.
 
5277
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
5278
        field, quick will contain an empty record set.
15296
5279
      */
15297
 
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
15298
 
                                                     tab->found_records))))
15299
 
        goto err;
 
5280
      if (! (select->quick= (optimizer::get_quick_select_for_ref(session, 
 
5281
                                                                 table, 
 
5282
                                                                 &tab->ref,
 
5283
                                                                 tab->found_records))))
 
5284
      {
 
5285
        goto err;
 
5286
      }
15300
5287
    }
15301
5288
  }
15302
5289
 
15303
 
  /* Fill schema tables with data before filesort if it's necessary */
15304
 
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
15305
 
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
15306
 
    goto err;
15307
 
 
15308
5290
  if (table->s->tmp_table)
15309
 
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
15310
 
  table->sort.found_records=filesort(thd, table,join->sortorder, length,
 
5291
    table->cursor->info(HA_STATUS_VARIABLE);    // Get record count
 
5292
  table->sort.found_records=filesort(session, table,join->sortorder, length,
15311
5293
                                     select, filesort_limit, 0,
15312
5294
                                     &examined_rows);
15313
5295
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
15319
5301
  tab->select_cond=0;
15320
5302
  tab->last_inner= 0;
15321
5303
  tab->first_unmatched= 0;
15322
 
  tab->type=JT_ALL;                             // Read with normal read_record
 
5304
  tab->type= AM_ALL;                            // Read with normal read_record
15323
5305
  tab->read_first_record= join_init_read_record;
15324
5306
  tab->join->examined_rows+=examined_rows;
15325
5307
  if (table->key_read)                          // Restore if we used indexes
15326
5308
  {
15327
5309
    table->key_read=0;
15328
 
    table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5310
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
15329
5311
  }
15330
5312
  return(table->sort.found_records == HA_POS_ERROR);
15331
5313
err:
15332
5314
  return(-1);
15333
5315
}
15334
5316
 
15335
 
/*****************************************************************************
15336
 
  Remove duplicates from tmp table
15337
 
  This should be recoded to add a unique index to the table and remove
15338
 
  duplicates
15339
 
  Table is a locked single thread table
15340
 
  fields is the number of fields to check (from the end)
15341
 
*****************************************************************************/
15342
 
 
15343
 
static bool compare_record(TABLE *table, Field **ptr)
15344
 
{
15345
 
  for (; *ptr ; ptr++)
15346
 
  {
15347
 
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
15348
 
      return 1;
15349
 
  }
15350
 
  return 0;
15351
 
}
15352
 
 
15353
 
static bool copy_blobs(Field **ptr)
15354
 
{
15355
 
  for (; *ptr ; ptr++)
15356
 
  {
15357
 
    if ((*ptr)->flags & BLOB_FLAG)
15358
 
      if (((Field_blob *) (*ptr))->copy())
15359
 
        return 1;                               // Error
15360
 
  }
15361
 
  return 0;
15362
 
}
15363
 
 
15364
 
static void free_blobs(Field **ptr)
15365
 
{
15366
 
  for (; *ptr ; ptr++)
15367
 
  {
15368
 
    if ((*ptr)->flags & BLOB_FLAG)
15369
 
      ((Field_blob *) (*ptr))->free();
15370
 
  }
15371
 
}
15372
 
 
15373
 
 
15374
 
static int
15375
 
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
15376
 
{
15377
 
  int error;
15378
 
  ulong reclength,offset;
15379
 
  uint field_count;
15380
 
  THD *thd= join->thd;
15381
 
 
15382
 
  entry->reginfo.lock_type=TL_WRITE;
15383
 
 
15384
 
  /* Calculate how many saved fields there is in list */
15385
 
  field_count=0;
15386
 
  List_iterator<Item> it(fields);
15387
 
  Item *item;
15388
 
  while ((item=it++))
15389
 
  {
15390
 
    if (item->get_tmp_table_field() && ! item->const_item())
15391
 
      field_count++;
15392
 
  }
15393
 
 
15394
 
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
15395
 
  {                    // only const items with no OPTION_FOUND_ROWS
15396
 
    join->unit->select_limit_cnt= 1;            // Only send first row
15397
 
    return(0);
15398
 
  }
15399
 
  Field **first_field=entry->field+entry->s->fields - field_count;
15400
 
  offset= (field_count ? 
15401
 
           entry->field[entry->s->fields - field_count]->
15402
 
           offset(entry->record[0]) : 0);
15403
 
  reclength=entry->s->reclength-offset;
15404
 
 
15405
 
  free_io_cache(entry);                         // Safety
15406
 
  entry->file->info(HA_STATUS_VARIABLE);
15407
 
  if (entry->s->db_type() == heap_hton ||
15408
 
      (!entry->s->blob_fields &&
15409
 
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
15410
 
        thd->variables.sortbuff_size)))
15411
 
    error=remove_dup_with_hash_index(join->thd, entry,
15412
 
                                     field_count, first_field,
15413
 
                                     reclength, having);
15414
 
  else
15415
 
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
15416
 
                                  having);
15417
 
 
15418
 
  free_blobs(first_field);
15419
 
  return(error);
15420
 
}
15421
 
 
15422
 
 
15423
 
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
15424
 
                                   ulong offset, Item *having)
15425
 
{
15426
 
  handler *file=table->file;
 
5317
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
 
5318
{
 
5319
  Cursor *cursor=table->cursor;
15427
5320
  char *org_record,*new_record;
15428
 
  uchar *record;
 
5321
  unsigned char *record;
15429
5322
  int error;
15430
 
  ulong reclength= table->s->reclength-offset;
 
5323
  uint32_t reclength= table->s->reclength-offset;
15431
5324
 
15432
5325
  org_record=(char*) (record=table->record[0])+offset;
15433
5326
  new_record=(char*) table->record[1]+offset;
15434
5327
 
15435
 
  file->ha_rnd_init(1);
15436
 
  error=file->rnd_next(record);
 
5328
  cursor->ha_rnd_init(1);
 
5329
  error=cursor->rnd_next(record);
15437
5330
  for (;;)
15438
5331
  {
15439
 
    if (thd->killed)
 
5332
    if (session->killed)
15440
5333
    {
15441
 
      thd->send_kill_message();
 
5334
      session->send_kill_message();
15442
5335
      error=0;
15443
5336
      goto err;
15444
5337
    }
15445
5338
    if (error)
15446
5339
    {
15447
5340
      if (error == HA_ERR_RECORD_DELETED)
15448
 
        continue;
 
5341
        continue;
15449
5342
      if (error == HA_ERR_END_OF_FILE)
15450
 
        break;
 
5343
        break;
15451
5344
      goto err;
15452
5345
    }
15453
5346
    if (having && !having->val_int())
15454
5347
    {
15455
 
      if ((error=file->ha_delete_row(record)))
15456
 
        goto err;
15457
 
      error=file->rnd_next(record);
 
5348
      if ((error=cursor->ha_delete_row(record)))
 
5349
        goto err;
 
5350
      error=cursor->rnd_next(record);
15458
5351
      continue;
15459
5352
    }
15460
5353
    if (copy_blobs(first_field))
15465
5358
    }
15466
5359
    memcpy(new_record,org_record,reclength);
15467
5360
 
15468
 
    /* Read through rest of file and mark duplicated rows deleted */
 
5361
    /* Read through rest of cursor and mark duplicated rows deleted */
15469
5362
    bool found=0;
15470
5363
    for (;;)
15471
5364
    {
15472
 
      if ((error=file->rnd_next(record)))
 
5365
      if ((error=cursor->rnd_next(record)))
15473
5366
      {
15474
 
        if (error == HA_ERR_RECORD_DELETED)
15475
 
          continue;
15476
 
        if (error == HA_ERR_END_OF_FILE)
15477
 
          break;
15478
 
        goto err;
 
5367
        if (error == HA_ERR_RECORD_DELETED)
 
5368
          continue;
 
5369
        if (error == HA_ERR_END_OF_FILE)
 
5370
          break;
 
5371
        goto err;
15479
5372
      }
15480
 
      if (compare_record(table, first_field) == 0)
 
5373
      if (table->compare_record(first_field) == 0)
15481
5374
      {
15482
 
        if ((error=file->ha_delete_row(record)))
15483
 
          goto err;
 
5375
        if ((error=cursor->ha_delete_row(record)))
 
5376
          goto err;
15484
5377
      }
15485
5378
      else if (!found)
15486
5379
      {
15487
 
        found=1;
15488
 
        file->position(record); // Remember position
 
5380
        found= 1;
 
5381
        cursor->position(record);       // Remember position
15489
5382
      }
15490
5383
    }
15491
5384
    if (!found)
15492
 
      break;                                    // End of file
 
5385
      break;                                    // End of cursor
15493
5386
    /* Restart search on next row */
15494
 
    error=file->restart_rnd_next(record,file->ref);
 
5387
    error=cursor->restart_rnd_next(record,cursor->ref);
15495
5388
  }
15496
5389
 
15497
 
  file->extra(HA_EXTRA_NO_CACHE);
 
5390
  cursor->extra(HA_EXTRA_NO_CACHE);
15498
5391
  return(0);
15499
5392
err:
15500
 
  file->extra(HA_EXTRA_NO_CACHE);
 
5393
  cursor->extra(HA_EXTRA_NO_CACHE);
15501
5394
  if (error)
15502
 
    file->print_error(error,MYF(0));
 
5395
    table->print_error(error,MYF(0));
15503
5396
  return(1);
15504
5397
}
15505
5398
 
15506
 
 
15507
5399
/**
15508
5400
  Generate a hash index for each row to quickly find duplicate rows.
15509
5401
 
15510
5402
  @note
15511
5403
    Note that this will not work on tables with blobs!
15512
5404
*/
15513
 
 
15514
 
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
15515
 
                                      uint field_count,
15516
 
                                      Field **first_field,
15517
 
                                      ulong key_length,
15518
 
                                      Item *having)
 
5405
int remove_dup_with_hash_index(Session *session, 
 
5406
                               Table *table,
 
5407
                               uint32_t field_count,
 
5408
                               Field **first_field,
 
5409
                               uint32_t key_length,
 
5410
                               Item *having)
15519
5411
{
15520
 
  uchar *key_buffer, *key_pos, *record=table->record[0];
 
5412
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
15521
5413
  int error;
15522
 
  handler *file= table->file;
15523
 
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
15524
 
  uint *field_lengths,*field_length;
 
5414
  Cursor *cursor= table->cursor;
 
5415
  uint32_t extra_length= ALIGN_SIZE(key_length)-key_length;
 
5416
  uint32_t *field_lengths,*field_length;
15525
5417
  HASH hash;
15526
5418
 
15527
 
  if (!my_multi_malloc(MYF(MY_WME),
 
5419
  if (! memory::multi_malloc(false,
15528
5420
                       &key_buffer,
15529
 
                       (uint) ((key_length + extra_length) *
15530
 
                               (long) file->stats.records),
 
5421
                       (uint32_t) ((key_length + extra_length) *
 
5422
                               (long) cursor->stats.records),
15531
5423
                       &field_lengths,
15532
 
                       (uint) (field_count*sizeof(*field_lengths)),
15533
 
                       NullS))
 
5424
                       (uint32_t) (field_count*sizeof(*field_lengths)),
 
5425
                       NULL))
15534
5426
    return(1);
15535
5427
 
15536
5428
  {
15537
5429
    Field **ptr;
15538
 
    ulong total_length= 0;
 
5430
    uint32_t total_length= 0;
15539
5431
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
15540
5432
    {
15541
 
      uint length= (*ptr)->sort_length();
 
5433
      uint32_t length= (*ptr)->sort_length();
15542
5434
      (*field_length++)= length;
15543
5435
      total_length+= length;
15544
5436
    }
15547
5439
    extra_length= ALIGN_SIZE(key_length)-key_length;
15548
5440
  }
15549
5441
 
15550
 
  if (hash_init(&hash, &my_charset_bin, (uint) file->stats.records, 0, 
 
5442
  if (hash_init(&hash, &my_charset_bin, (uint32_t) cursor->stats.records, 0,
15551
5443
                key_length, (hash_get_key) 0, 0, 0))
15552
5444
  {
15553
 
    my_free((char*) key_buffer,MYF(0));
 
5445
    free((char*) key_buffer);
15554
5446
    return(1);
15555
5447
  }
15556
5448
 
15557
 
  file->ha_rnd_init(1);
 
5449
  cursor->ha_rnd_init(1);
15558
5450
  key_pos=key_buffer;
15559
5451
  for (;;)
15560
5452
  {
15561
 
    uchar *org_key_pos;
15562
 
    if (thd->killed)
 
5453
    unsigned char *org_key_pos;
 
5454
    if (session->killed)
15563
5455
    {
15564
 
      thd->send_kill_message();
 
5456
      session->send_kill_message();
15565
5457
      error=0;
15566
5458
      goto err;
15567
5459
    }
15568
 
    if ((error=file->rnd_next(record)))
 
5460
    if ((error=cursor->rnd_next(record)))
15569
5461
    {
15570
5462
      if (error == HA_ERR_RECORD_DELETED)
15571
 
        continue;
 
5463
        continue;
15572
5464
      if (error == HA_ERR_END_OF_FILE)
15573
 
        break;
 
5465
        break;
15574
5466
      goto err;
15575
5467
    }
15576
5468
    if (having && !having->val_int())
15577
5469
    {
15578
 
      if ((error=file->ha_delete_row(record)))
15579
 
        goto err;
 
5470
      if ((error=cursor->ha_delete_row(record)))
 
5471
        goto err;
15580
5472
      continue;
15581
5473
    }
15582
5474
 
15592
5484
    if (hash_search(&hash, org_key_pos, key_length))
15593
5485
    {
15594
5486
      /* Duplicated found ; Remove the row */
15595
 
      if ((error=file->ha_delete_row(record)))
15596
 
        goto err;
 
5487
      if ((error=cursor->ha_delete_row(record)))
 
5488
        goto err;
15597
5489
    }
15598
5490
    else
15599
5491
      (void) my_hash_insert(&hash, org_key_pos);
15600
5492
    key_pos+=extra_length;
15601
5493
  }
15602
 
  my_free((char*) key_buffer,MYF(0));
 
5494
  free((char*) key_buffer);
15603
5495
  hash_free(&hash);
15604
 
  file->extra(HA_EXTRA_NO_CACHE);
15605
 
  (void) file->ha_rnd_end();
 
5496
  cursor->extra(HA_EXTRA_NO_CACHE);
 
5497
  (void) cursor->ha_rnd_end();
15606
5498
  return(0);
15607
5499
 
15608
5500
err:
15609
 
  my_free((char*) key_buffer,MYF(0));
 
5501
  free((char*) key_buffer);
15610
5502
  hash_free(&hash);
15611
 
  file->extra(HA_EXTRA_NO_CACHE);
15612
 
  (void) file->ha_rnd_end();
 
5503
  cursor->extra(HA_EXTRA_NO_CACHE);
 
5504
  (void) cursor->ha_rnd_end();
15613
5505
  if (error)
15614
 
    file->print_error(error,MYF(0));
 
5506
    table->print_error(error,MYF(0));
15615
5507
  return(1);
15616
5508
}
15617
5509
 
15618
 
 
15619
 
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
15620
 
                                  SORT_FIELD *sortorder)
 
5510
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
15621
5511
{
15622
 
  uint count;
 
5512
  uint32_t count;
15623
5513
  SORT_FIELD *sort,*pos;
15624
5514
 
15625
5515
  count=0;
15626
 
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
 
5516
  for (order_st *tmp = order; tmp; tmp=tmp->next)
15627
5517
    count++;
15628
5518
  if (!sortorder)
15629
 
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
5519
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
15630
5520
                                       (max(count, *length) + 1));
15631
5521
  pos= sort= sortorder;
15632
5522
 
15653
5543
  return(sort);
15654
5544
}
15655
5545
 
15656
 
 
15657
 
/*****************************************************************************
15658
 
  Fill join cache with packed records
15659
 
  Records are stored in tab->cache.buffer and last record in
15660
 
  last record is stored with pointers to blobs to support very big
15661
 
  records
15662
 
******************************************************************************/
15663
 
 
15664
 
static int
15665
 
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
15666
 
{
15667
 
  register unsigned int i;
15668
 
  unsigned int length, blobs;
15669
 
  size_t size;
15670
 
  CACHE_FIELD *copy,**blob_ptr;
15671
 
  JOIN_CACHE  *cache;
15672
 
  JOIN_TAB *join_tab;
15673
 
 
15674
 
  cache= &tables[table_count].cache;
15675
 
  cache->fields=blobs=0;
15676
 
 
15677
 
  join_tab=tables;
15678
 
  for (i=0 ; i < table_count ; i++,join_tab++)
15679
 
  {
15680
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
15681
 
      calc_used_field_length(thd, join_tab);
15682
 
    cache->fields+=join_tab->used_fields;
15683
 
    blobs+=join_tab->used_blobs;
15684
 
 
15685
 
    /* SemiJoinDuplicateElimination: reserve space for rowid */
15686
 
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15687
 
    {
15688
 
      cache->fields++;
15689
 
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
15690
 
    }
15691
 
  }
15692
 
  if (!(cache->field=(CACHE_FIELD*)
15693
 
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
15694
 
 
15695
 
                  sizeof(CACHE_FIELD*))))
15696
 
  {
15697
 
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
15698
 
    cache->buff=0;                              /* purecov: inspected */
15699
 
    return(1);                          /* purecov: inspected */
15700
 
  }
15701
 
  copy=cache->field;
15702
 
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
15703
 
    (cache->field+cache->fields+table_count*2);
15704
 
 
15705
 
  length=0;
15706
 
  for (i=0 ; i < table_count ; i++)
15707
 
  {
15708
 
    uint null_fields=0,used_fields;
15709
 
    Field **f_ptr,*field;
15710
 
    MY_BITMAP *read_set= tables[i].table->read_set;
15711
 
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
15712
 
         used_fields ;
15713
 
         f_ptr++)
15714
 
    {
15715
 
      field= *f_ptr;
15716
 
      if (bitmap_is_set(read_set, field->field_index))
15717
 
      {
15718
 
        used_fields--;
15719
 
        length+=field->fill_cache_field(copy);
15720
 
        if (copy->blob_field)
15721
 
          (*blob_ptr++)=copy;
15722
 
        if (field->maybe_null())
15723
 
          null_fields++;
15724
 
        copy->get_rowid= NULL;
15725
 
        copy++;
15726
 
      }
15727
 
    }
15728
 
    /* Copy null bits from table */
15729
 
    if (null_fields && tables[i].table->s->null_fields)
15730
 
    {                                           /* must copy null bits */
15731
 
      copy->str= tables[i].table->null_flags;
15732
 
      copy->length= tables[i].table->s->null_bytes;
15733
 
      copy->strip=0;
15734
 
      copy->blob_field=0;
15735
 
      copy->get_rowid= NULL;
15736
 
      length+=copy->length;
15737
 
      copy++;
15738
 
      cache->fields++;
15739
 
    }
15740
 
    /* If outer join table, copy null_row flag */
15741
 
    if (tables[i].table->maybe_null)
15742
 
    {
15743
 
      copy->str= (uchar*) &tables[i].table->null_row;
15744
 
      copy->length=sizeof(tables[i].table->null_row);
15745
 
      copy->strip=0;
15746
 
      copy->blob_field=0;
15747
 
      copy->get_rowid= NULL;
15748
 
      length+=copy->length;
15749
 
      copy++;
15750
 
      cache->fields++;
15751
 
    }
15752
 
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
15753
 
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15754
 
    {
15755
 
      copy->str= tables[i].table->file->ref;
15756
 
      copy->length= tables[i].table->file->ref_length;
15757
 
      copy->strip=0;
15758
 
      copy->blob_field=0;
15759
 
      copy->get_rowid= NULL;
15760
 
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
15761
 
      {
15762
 
        /* We will need to call h->position(): */
15763
 
        copy->get_rowid= tables[i].table;
15764
 
        /* And those after us won't have to: */
15765
 
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
15766
 
      }
15767
 
      copy++;
15768
 
    }
15769
 
  }
15770
 
 
15771
 
  cache->length=length+blobs*sizeof(char*);
15772
 
  cache->blobs=blobs;
15773
 
  *blob_ptr=0;                                  /* End sequentel */
15774
 
  size=max(thd->variables.join_buff_size, cache->length);
15775
 
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
15776
 
    return(1);                          /* Don't use cache */ /* purecov: inspected */
15777
 
  cache->end=cache->buff+size;
15778
 
  reset_cache_write(cache);
15779
 
  return(0);
15780
 
}
15781
 
 
15782
 
 
15783
 
static ulong
15784
 
used_blob_length(CACHE_FIELD **ptr)
15785
 
{
15786
 
  uint length,blob_length;
15787
 
  for (length=0 ; *ptr ; ptr++)
15788
 
  {
15789
 
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
15790
 
    length+=blob_length;
15791
 
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
15792
 
  }
15793
 
  return length;
15794
 
}
15795
 
 
15796
 
 
15797
 
static bool
15798
 
store_record_in_cache(JOIN_CACHE *cache)
15799
 
{
15800
 
  uint length;
15801
 
  uchar *pos;
15802
 
  CACHE_FIELD *copy,*end_field;
15803
 
  bool last_record;
15804
 
 
15805
 
  pos=cache->pos;
15806
 
  end_field=cache->field+cache->fields;
15807
 
 
15808
 
  length=cache->length;
15809
 
  if (cache->blobs)
15810
 
    length+=used_blob_length(cache->blob_ptr);
15811
 
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
15812
 
    cache->ptr_record=cache->records;
15813
 
  /*
15814
 
    There is room in cache. Put record there
15815
 
  */
15816
 
  cache->records++;
15817
 
  for (copy=cache->field ; copy < end_field; copy++)
15818
 
  {
15819
 
    if (copy->blob_field)
15820
 
    {
15821
 
      if (last_record)
15822
 
      {
15823
 
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
15824
 
                                    copy->blob_field->charset());
15825
 
        pos+=copy->length+sizeof(char*);
15826
 
      }
15827
 
      else
15828
 
      {
15829
 
        copy->blob_field->get_image(pos, copy->length, // blob length
15830
 
                                    copy->blob_field->charset());
15831
 
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
15832
 
        pos+=copy->length+copy->blob_length;
15833
 
      }
15834
 
    }
15835
 
    else
15836
 
    {
15837
 
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
15838
 
      if (copy->get_rowid)
15839
 
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
15840
 
 
15841
 
      if (copy->strip)
15842
 
      {
15843
 
        uchar *str,*end;
15844
 
        for (str=copy->str,end= str+copy->length;
15845
 
             end > str && end[-1] == ' ' ;
15846
 
             end--) ;
15847
 
        length=(uint) (end-str);
15848
 
        memcpy(pos+2, str, length);
15849
 
        int2store(pos, length);
15850
 
        pos+= length+2;
15851
 
      }
15852
 
      else
15853
 
      {
15854
 
        memcpy(pos,copy->str,copy->length);
15855
 
        pos+=copy->length;
15856
 
      }
15857
 
    }
15858
 
  }
15859
 
  cache->pos=pos;
15860
 
  return last_record || (size_t) (cache->end - pos) < cache->length;
15861
 
}
15862
 
 
15863
 
 
15864
 
static void
15865
 
reset_cache_read(JOIN_CACHE *cache)
15866
 
{
15867
 
  cache->record_nr=0;
15868
 
  cache->pos=cache->buff;
15869
 
}
15870
 
 
15871
 
 
15872
 
static void reset_cache_write(JOIN_CACHE *cache)
15873
 
{
15874
 
  reset_cache_read(cache);
15875
 
  cache->records= 0;
15876
 
  cache->ptr_record= (uint) ~0;
15877
 
}
15878
 
 
15879
 
 
15880
 
static void
15881
 
read_cached_record(JOIN_TAB *tab)
15882
 
{
15883
 
  uchar *pos;
15884
 
  uint length;
15885
 
  bool last_record;
15886
 
  CACHE_FIELD *copy,*end_field;
15887
 
 
15888
 
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
15889
 
  pos=tab->cache.pos;
15890
 
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
15891
 
       copy < end_field;
15892
 
       copy++)
15893
 
  {
15894
 
    if (copy->blob_field)
15895
 
    {
15896
 
      if (last_record)
15897
 
      {
15898
 
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
15899
 
                                    copy->blob_field->charset());
15900
 
        pos+=copy->length+sizeof(char*);
15901
 
      }
15902
 
      else
15903
 
      {
15904
 
        copy->blob_field->set_ptr(pos, pos+copy->length);
15905
 
        pos+=copy->length+copy->blob_field->get_length();
15906
 
      }
15907
 
    }
15908
 
    else
15909
 
    {
15910
 
      if (copy->strip)
15911
 
      {
15912
 
        length= uint2korr(pos);
15913
 
        memcpy(copy->str, pos+2, length);
15914
 
        memset(copy->str+length, ' ', copy->length-length);
15915
 
        pos+= 2 + length;
15916
 
      }
15917
 
      else
15918
 
      {
15919
 
        memcpy(copy->str,pos,copy->length);
15920
 
        pos+=copy->length;
15921
 
      }
15922
 
    }
15923
 
  }
15924
 
  tab->cache.pos=pos;
15925
 
  return;
15926
 
}
15927
 
 
15928
 
 
15929
5546
/*
15930
5547
  eq_ref: Create the lookup key and check if it is the same as saved key
15931
5548
 
15932
5549
  SYNOPSIS
15933
5550
    cmp_buffer_with_ref()
15934
5551
      tab  Join tab of the accessed table
15935
 
 
15936
 
  DESCRIPTION 
15937
 
    Used by eq_ref access method: create the index lookup key and check if 
 
5552
 
 
5553
  DESCRIPTION
 
5554
    Used by eq_ref access method: create the index lookup key and check if
15938
5555
    we've used this key at previous lookup (If yes, we don't need to repeat
15939
5556
    the lookup - the record has been already fetched)
15940
5557
 
15941
 
  RETURN 
 
5558
  RETURN
15942
5559
    true   No cached record for the key, or failed to create the key (due to
15943
5560
           out-of-domain error)
15944
 
    false  The created key is the same as the previous one (and the record 
 
5561
    false  The created key is the same as the previous one (and the record
15945
5562
           is already in table->record)
15946
5563
*/
15947
 
 
15948
 
static bool
15949
 
cmp_buffer_with_ref(JOIN_TAB *tab)
 
5564
static bool cmp_buffer_with_ref(JoinTable *tab)
15950
5565
{
15951
5566
  bool no_prev_key;
15952
5567
  if (!tab->ref.disable_cache)
15957
5572
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
15958
5573
    }
15959
5574
  }
15960
 
  else 
 
5575
  else
15961
5576
    no_prev_key= true;
15962
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
15963
 
                                            &tab->ref)) ||
 
5577
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
15964
5578
      no_prev_key)
15965
5579
    return 1;
15966
5580
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
15967
5581
    != 0;
15968
5582
}
15969
5583
 
15970
 
 
15971
 
bool
15972
 
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
 
5584
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
15973
5585
{
15974
 
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
15975
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
15976
 
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table, table->write_set);
 
5586
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
 
5587
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
15977
5588
  bool result= 0;
15978
5589
 
15979
 
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
 
5590
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
15980
5591
  {
15981
5592
    if ((*copy)->copy() & 1)
15982
5593
    {
15984
5595
      break;
15985
5596
    }
15986
5597
  }
15987
 
  thd->count_cuted_fields= save_count_cuted_fields;
15988
 
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
5598
  session->count_cuted_fields= save_count_cuted_fields;
15989
5599
  return result;
15990
5600
}
15991
5601
 
15992
 
 
15993
5602
/*****************************************************************************
15994
5603
  Group and order functions
15995
5604
*****************************************************************************/
15996
5605
 
15997
5606
/**
15998
 
  Resolve an ORDER BY or GROUP BY column reference.
 
5607
  Resolve an order_st BY or GROUP BY column reference.
15999
5608
 
16000
 
  Given a column reference (represented by 'order') from a GROUP BY or ORDER
 
5609
  Given a column reference (represented by 'order') from a GROUP BY or order_st
16001
5610
  BY clause, find the actual column it represents. If the column being
16002
5611
  resolved is from the GROUP BY clause, the procedure searches the SELECT
16003
5612
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
16004
 
  the ORDER BY clause, only the SELECT list is being searched.
 
5613
  the order_st BY clause, only the SELECT list is being searched.
16005
5614
 
16006
5615
  If 'order' is resolved to an Item, then order->item is set to the found
16007
5616
  Item. If there is no item for the found column (that is, it was resolved
16010
5619
 
16011
5620
  ref_pointer_array and all_fields are updated.
16012
5621
 
16013
 
  @param[in] thd                     Pointer to current thread structure
 
5622
  @param[in] session                 Pointer to current thread structure
16014
5623
  @param[in,out] ref_pointer_array  All select, group and order by fields
16015
5624
  @param[in] tables                 List of tables to search in (usually
16016
5625
    FROM clause)
16019
5628
    SELECT list)
16020
5629
  @param[in,out] all_fields         All select, group and order by fields
16021
5630
  @param[in] is_group_field         True if order is a GROUP field, false if
16022
 
    ORDER by field
 
5631
    order_st by field
16023
5632
 
16024
5633
  @retval
16025
5634
    false if OK
16026
5635
  @retval
16027
5636
    true  if error occurred
16028
5637
*/
16029
 
 
16030
 
static bool
16031
 
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16032
 
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
16033
 
                   bool is_group_field)
 
5638
static bool find_order_in_list(Session *session, 
 
5639
                               Item **ref_pointer_array, 
 
5640
                               TableList *tables,
 
5641
                               order_st *order,
 
5642
                               List<Item> &fields,
 
5643
                               List<Item> &all_fields,
 
5644
                               bool is_group_field)
16034
5645
{
16035
 
  Item *order_item= *order->item; /* The item from the GROUP/ORDER caluse. */
 
5646
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
16036
5647
  Item::Type order_item_type;
16037
5648
  Item **select_item; /* The corresponding item from the SELECT clause. */
16038
5649
  Field *from_field;  /* The corresponding field from the FROM clause. */
16039
 
  uint counter;
 
5650
  uint32_t counter;
16040
5651
  enum_resolution_type resolution;
16041
5652
 
16042
5653
  /*
16045
5656
  */
16046
5657
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
16047
5658
  {                                             /* Order by position */
16048
 
    uint count= (uint) order_item->val_int();
 
5659
    uint32_t count= (uint32_t) order_item->val_int();
16049
5660
    if (!count || count > fields.elements)
16050
5661
    {
16051
5662
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
16052
 
               order_item->full_name(), thd->where);
 
5663
               order_item->full_name(), session->where);
16053
5664
      return true;
16054
5665
    }
16055
5666
    order->item= ref_pointer_array + count - 1;
16058
5669
    order->counter_used= 1;
16059
5670
    return false;
16060
5671
  }
16061
 
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
 
5672
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
16062
5673
  select_item= find_item_in_list(order_item, fields, &counter,
16063
5674
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
16064
5675
  if (!select_item)
16075
5686
      for this name (in case if we would perform lookup in all tables).
16076
5687
    */
16077
5688
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
16078
 
        order_item->fix_fields(thd, order->item))
 
5689
        order_item->fix_fields(session, order->item))
16079
5690
      return true;
16080
5691
 
16081
5692
    /* Lookup the current GROUP field in the FROM clause. */
16084
5695
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
16085
5696
        order_item_type == Item::REF_ITEM)
16086
5697
    {
16087
 
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
16088
 
                                       NULL, &view_ref, IGNORE_ERRORS, true,
16089
 
                                       false);
 
5698
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
 
5699
                                       NULL, &view_ref, IGNORE_ERRORS, false);
16090
5700
      if (!from_field)
16091
5701
        from_field= (Field*) not_found_field;
16092
5702
    }
16124
5734
        warning so the user knows that the field from the FROM clause
16125
5735
        overshadows the column reference from the SELECT list.
16126
5736
      */
16127
 
      push_warning_printf(thd, MYSQL_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
5737
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
16128
5738
                          ER(ER_NON_UNIQ_ERROR),
16129
5739
                          ((Item_ident*) order_item)->field_name,
16130
 
                          current_thd->where);
 
5740
                          current_session->where);
16131
5741
    }
16132
5742
  }
16133
5743
 
16144
5754
    arguments for which fix_fields already was called.
16145
5755
  */
16146
5756
  if (!order_item->fixed &&
16147
 
      (order_item->fix_fields(thd, order->item) ||
 
5757
      (order_item->fix_fields(session, order->item) ||
16148
5758
       (order_item= *order->item)->check_cols(1) ||
16149
 
       thd->is_fatal_error))
 
5759
       session->is_fatal_error))
16150
5760
    return true; /* Wrong field. */
16151
5761
 
16152
 
  uint el= all_fields.elements;
 
5762
  uint32_t el= all_fields.elements;
16153
5763
  all_fields.push_front(order_item); /* Add new field to field list. */
16154
5764
  ref_pointer_array[el]= order_item;
16155
5765
  order->item= ref_pointer_array + el;
16156
5766
  return false;
16157
5767
}
16158
5768
 
16159
 
 
16160
5769
/**
16161
5770
  Change order to point at item in select list.
16162
5771
 
16163
5772
  If item isn't a number and doesn't exits in the select list, add it the
16164
5773
  the field list.
16165
5774
*/
16166
 
 
16167
 
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16168
 
                List<Item> &fields, List<Item> &all_fields, ORDER *order)
 
5775
int setup_order(Session *session,
 
5776
                Item **ref_pointer_array,
 
5777
                TableList *tables,
 
5778
                            List<Item> &fields,
 
5779
                List<Item> &all_fields,
 
5780
                order_st *order)
16169
5781
{
16170
 
  thd->where="order clause";
 
5782
  session->where="order clause";
16171
5783
  for (; order; order=order->next)
16172
5784
  {
16173
 
    if (find_order_in_list(thd, ref_pointer_array, tables, order, fields,
 
5785
    if (find_order_in_list(session, ref_pointer_array, tables, order, fields,
16174
5786
                           all_fields, false))
16175
5787
      return 1;
16176
5788
  }
16177
5789
  return 0;
16178
5790
}
16179
5791
 
16180
 
 
16181
5792
/**
16182
5793
  Intitialize the GROUP BY list.
16183
5794
 
16184
 
  @param thd                    Thread handler
 
5795
  @param session                        Thread Cursor
16185
5796
  @param ref_pointer_array      We store references to all fields that was
16186
5797
                               not in 'fields' here.
16187
5798
  @param fields         All fields in the select part. Any item in
16203
5814
  @retval
16204
5815
    1  error (probably out of memory)
16205
5816
*/
16206
 
 
16207
 
int
16208
 
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16209
 
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
16210
 
            bool *hidden_group_fields)
 
5817
int setup_group(Session *session,
 
5818
                Item **ref_pointer_array,
 
5819
                TableList *tables,
 
5820
                      List<Item> &fields,
 
5821
                List<Item> &all_fields,
 
5822
                order_st *order,
 
5823
                      bool *hidden_group_fields)
16211
5824
{
16212
5825
  *hidden_group_fields=0;
16213
 
  ORDER *ord;
 
5826
  order_st *ord;
16214
5827
 
16215
5828
  if (!order)
16216
5829
    return 0;                           /* Everything is ok */
16217
5830
 
16218
 
  uint org_fields=all_fields.elements;
 
5831
  uint32_t org_fields=all_fields.elements;
16219
5832
 
16220
 
  thd->where="group statement";
 
5833
  session->where="group statement";
16221
5834
  for (ord= order; ord; ord= ord->next)
16222
5835
  {
16223
 
    if (find_order_in_list(thd, ref_pointer_array, tables, ord, fields,
 
5836
    if (find_order_in_list(session, ref_pointer_array, tables, ord, fields,
16224
5837
                           all_fields, true))
16225
5838
      return 1;
16226
5839
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
16251
5864
    Item_field *field;
16252
5865
    int cur_pos_in_select_list= 0;
16253
5866
    List_iterator<Item> li(fields);
16254
 
    List_iterator<Item_field> naf_it(thd->lex->current_select->non_agg_fields);
 
5867
    List_iterator<Item_field> naf_it(session->lex->current_select->non_agg_fields);
16255
5868
 
16256
5869
    field= naf_it++;
16257
5870
    while (field && (item=li++))
16300
5913
  Try to use the fields in the order given by 'order' to allow one to
16301
5914
  optimize away 'order by'.
16302
5915
*/
16303
 
 
16304
 
static ORDER *
16305
 
create_distinct_group(THD *thd, Item **ref_pointer_array,
16306
 
                      ORDER *order_list, List<Item> &fields,
16307
 
                      List<Item> &all_fields __attribute__((unused)),
16308
 
                      bool *all_order_by_fields_used)
 
5916
order_st *create_distinct_group(Session *session,
 
5917
                                Item **ref_pointer_array,
 
5918
                                order_st *order_list,
 
5919
                                List<Item> &fields,
 
5920
                                List<Item> &,
 
5921
                                bool *all_order_by_fields_used)
16309
5922
{
16310
5923
  List_iterator<Item> li(fields);
16311
5924
  Item *item;
16312
 
  ORDER *order,*group,**prev;
 
5925
  order_st *order,*group,**prev;
16313
5926
 
16314
5927
  *all_order_by_fields_used= 1;
16315
5928
  while ((item=li++))
16320
5933
  {
16321
5934
    if (order->in_field_list)
16322
5935
    {
16323
 
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
 
5936
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
16324
5937
      if (!ord)
16325
 
        return 0;
 
5938
        return 0;
16326
5939
      *prev=ord;
16327
5940
      prev= &ord->next;
16328
5941
      (*ord->item)->marker=1;
16336
5949
  {
16337
5950
    if (!item->const_item() && !item->with_sum_func && !item->marker)
16338
5951
    {
16339
 
      /* 
16340
 
        Don't put duplicate columns from the SELECT list into the 
 
5952
      /*
 
5953
        Don't put duplicate columns from the SELECT list into the
16341
5954
        GROUP BY list.
16342
5955
      */
16343
 
      ORDER *ord_iter;
 
5956
      order_st *ord_iter;
16344
5957
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
16345
5958
        if ((*ord_iter->item)->eq(item, 1))
16346
5959
          goto next_item;
16347
 
      
16348
 
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
 
5960
 
 
5961
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
16349
5962
      if (!ord)
16350
 
        return 0;
 
5963
        return 0;
16351
5964
 
16352
5965
      /*
16353
5966
        We have here only field_list (not all_field_list), so we can use
16366
5979
  return group;
16367
5980
}
16368
5981
 
16369
 
 
16370
5982
/**
16371
5983
  Update join with count of the different type of fields.
16372
5984
*/
16373
 
 
16374
 
void
16375
 
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
16376
 
                  List<Item> &fields, bool reset_with_sum_func)
 
5985
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
16377
5986
{
16378
5987
  List_iterator<Item> li(fields);
16379
5988
  Item *field;
16390
5999
    {
16391
6000
      if (! field->const_item())
16392
6001
      {
16393
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
6002
        Item_sum *sum_item=(Item_sum*) field->real_item();
16394
6003
        if (!sum_item->depended_from() ||
16395
6004
            sum_item->depended_from() == select_lex)
16396
6005
        {
16398
6007
            param->quick_group=0;                       // UDF SUM function
16399
6008
          param->sum_func_count++;
16400
6009
 
16401
 
          for (uint i=0 ; i < sum_item->arg_count ; i++)
 
6010
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
16402
6011
          {
16403
6012
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
16404
6013
              param->field_count++;
16413
6022
    {
16414
6023
      param->func_count++;
16415
6024
      if (reset_with_sum_func)
16416
 
        field->with_sum_func=0;
16417
 
    }
16418
 
  }
16419
 
}
16420
 
 
16421
 
 
16422
 
/**
16423
 
  Return 1 if second is a subpart of first argument.
16424
 
 
16425
 
  If first parts has different direction, change it to second part
16426
 
  (group is sorted like order)
16427
 
*/
16428
 
 
16429
 
static bool
16430
 
test_if_subpart(ORDER *a,ORDER *b)
16431
 
{
16432
 
  for (; a && b; a=a->next,b=b->next)
16433
 
  {
16434
 
    if ((*a->item)->eq(*b->item,1))
16435
 
      a->asc=b->asc;
16436
 
    else
16437
 
      return 0;
16438
 
  }
16439
 
  return test(!b);
16440
 
}
16441
 
 
16442
 
/**
16443
 
  Return table number if there is only one table in sort order
16444
 
  and group and order is compatible, else return 0.
16445
 
*/
16446
 
 
16447
 
static TABLE *
16448
 
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
16449
 
{
16450
 
  table_map map= (table_map) 0;
16451
 
 
16452
 
  if (!a)
16453
 
    a=b;                                        // Only one need to be given
16454
 
  else if (!b)
16455
 
    b=a;
16456
 
 
16457
 
  for (; a && b; a=a->next,b=b->next)
16458
 
  {
16459
 
    if (!(*a->item)->eq(*b->item,1))
16460
 
      return(0);
16461
 
    map|=a->item[0]->used_tables();
16462
 
  }
16463
 
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
16464
 
    return(0);
16465
 
 
16466
 
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
16467
 
  if (map != tables->table->map)
16468
 
    return(0);                          // More than one table
16469
 
  return(tables->table);
16470
 
}
16471
 
 
16472
 
 
16473
 
/**
16474
 
  calc how big buffer we need for comparing group entries.
16475
 
*/
16476
 
 
16477
 
static void
16478
 
calc_group_buffer(JOIN *join,ORDER *group)
16479
 
{
16480
 
  uint key_length=0, parts=0, null_parts=0;
16481
 
 
16482
 
  if (group)
16483
 
    join->group= 1;
16484
 
  for (; group ; group=group->next)
16485
 
  {
16486
 
    Item *group_item= *group->item;
16487
 
    Field *field= group_item->get_tmp_table_field();
16488
 
    if (field)
16489
 
    {
16490
 
      enum_field_types type;
16491
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
16492
 
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
16493
 
      else if (type == DRIZZLE_TYPE_VARCHAR || type == DRIZZLE_TYPE_VAR_STRING)
16494
 
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
16495
 
      else
16496
 
        key_length+= field->pack_length();
16497
 
    }
16498
 
    else
16499
 
    { 
16500
 
      switch (group_item->result_type()) {
16501
 
      case REAL_RESULT:
16502
 
        key_length+= sizeof(double);
16503
 
        break;
16504
 
      case INT_RESULT:
16505
 
        key_length+= sizeof(int64_t);
16506
 
        break;
16507
 
      case DECIMAL_RESULT:
16508
 
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
16509
 
                                                (group_item->decimals ? 1 : 0),
16510
 
                                                group_item->decimals);
16511
 
        break;
16512
 
      case STRING_RESULT:
16513
 
      {
16514
 
        enum enum_field_types type= group_item->field_type();
16515
 
        /*
16516
 
          As items represented as DATE/TIME fields in the group buffer
16517
 
          have STRING_RESULT result type, we increase the length 
16518
 
          by 8 as maximum pack length of such fields.
16519
 
        */
16520
 
        if (type == DRIZZLE_TYPE_TIME ||
16521
 
            type == DRIZZLE_TYPE_NEWDATE ||
16522
 
            type == DRIZZLE_TYPE_DATETIME ||
16523
 
            type == DRIZZLE_TYPE_TIMESTAMP)
16524
 
        {
16525
 
          key_length+= 8;
16526
 
        }
16527
 
        else
16528
 
        {
16529
 
          /*
16530
 
            Group strings are taken as varstrings and require an length field.
16531
 
            A field is not yet created by create_tmp_field()
16532
 
            and the sizes should match up.
16533
 
          */
16534
 
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
16535
 
        }
16536
 
        break;
16537
 
      }
16538
 
      default:
16539
 
        /* This case should never be choosen */
16540
 
        assert(0);
16541
 
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
16542
 
      }
16543
 
    }
16544
 
    parts++;
16545
 
    if (group_item->maybe_null)
16546
 
      null_parts++;
16547
 
  }
16548
 
  join->tmp_table_param.group_length=key_length+null_parts;
16549
 
  join->tmp_table_param.group_parts=parts;
16550
 
  join->tmp_table_param.group_null_parts=null_parts;
16551
 
}
16552
 
 
16553
 
 
16554
 
/**
16555
 
  allocate group fields or take prepared (cached).
16556
 
 
16557
 
  @param main_join   join of current select
16558
 
  @param curr_join   current join (join of current select or temporary copy
16559
 
                     of it)
16560
 
 
16561
 
  @retval
16562
 
    0   ok
16563
 
  @retval
16564
 
    1   failed
16565
 
*/
16566
 
 
16567
 
static bool
16568
 
make_group_fields(JOIN *main_join, JOIN *curr_join)
16569
 
{
16570
 
  if (main_join->group_fields_cache.elements)
16571
 
  {
16572
 
    curr_join->group_fields= main_join->group_fields_cache;
16573
 
    curr_join->sort_and_group= 1;
16574
 
  }
16575
 
  else
16576
 
  {
16577
 
    if (alloc_group_fields(curr_join, curr_join->group_list))
16578
 
      return (1);
16579
 
    main_join->group_fields_cache= curr_join->group_fields;
16580
 
  }
16581
 
  return (0);
16582
 
}
16583
 
 
16584
 
 
16585
 
/**
16586
 
  Get a list of buffers for saveing last group.
16587
 
 
16588
 
  Groups are saved in reverse order for easyer check loop.
16589
 
*/
16590
 
 
16591
 
static bool
16592
 
alloc_group_fields(JOIN *join,ORDER *group)
16593
 
{
16594
 
  if (group)
16595
 
  {
16596
 
    for (; group ; group=group->next)
16597
 
    {
16598
 
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
16599
 
      if (!tmp || join->group_fields.push_front(tmp))
16600
 
        return true;
16601
 
    }
16602
 
  }
16603
 
  join->sort_and_group=1;                       /* Mark for do_select */
16604
 
  return false;
16605
 
}
16606
 
 
 
6025
        field->with_sum_func=0;
 
6026
    }
 
6027
  }
 
6028
}
16607
6029
 
16608
6030
/*
16609
6031
  Test if a single-row cache of items changed, and update the cache.
16611
6033
  @details Test if a list of items that typically represents a result
16612
6034
  row has changed. If the value of some item changed, update the cached
16613
6035
  value for this item.
16614
 
  
 
6036
 
16615
6037
  @param list list of <item, cached_value> pairs stored as Cached_item.
16616
6038
 
16617
6039
  @return -1 if no item changed
16618
6040
  @return index of the first item that changed
16619
6041
*/
16620
 
 
16621
6042
int test_if_item_cache_changed(List<Cached_item> &list)
16622
6043
{
16623
6044
  List_iterator<Cached_item> li(list);
16632
6053
  return(idx);
16633
6054
}
16634
6055
 
16635
 
 
16636
6056
/**
16637
6057
  Setup copy_fields to save fields at start of new group.
16638
6058
 
16642
6062
  Change old item_field to use a new field with points at saved fieldvalue
16643
6063
  This function is only called before use of send_fields.
16644
6064
 
16645
 
  @param thd                   THD pointer
 
6065
  @param session                   Session pointer
16646
6066
  @param param                 temporary table parameters
16647
6067
  @param ref_pointer_array     array of pointers to top elements of filed list
16648
6068
  @param res_selected_fields   new list of items of select item list
16661
6081
  @retval
16662
6082
    !=0   error
16663
6083
*/
16664
 
 
16665
 
bool
16666
 
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
16667
 
                  Item **ref_pointer_array,
16668
 
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
16669
 
                  uint elements, List<Item> &all_fields)
 
6084
bool setup_copy_fields(Session *session,
 
6085
                       Tmp_Table_Param *param,
 
6086
                       Item **ref_pointer_array,
 
6087
                       List<Item> &res_selected_fields,
 
6088
                       List<Item> &res_all_fields,
 
6089
                       uint32_t elements,
 
6090
                       List<Item> &all_fields)
16670
6091
{
16671
6092
  Item *pos;
16672
6093
  List_iterator_fast<Item> li(all_fields);
16673
 
  Copy_field *copy= NULL;
 
6094
  CopyField *copy= NULL;
16674
6095
  res_selected_fields.empty();
16675
6096
  res_all_fields.empty();
16676
6097
  List_iterator_fast<Item> itr(res_all_fields);
16677
6098
  List<Item> extra_funcs;
16678
 
  uint i, border= all_fields.elements - elements;
 
6099
  uint32_t i, border= all_fields.elements - elements;
16679
6100
 
16680
 
  if (param->field_count && 
16681
 
      !(copy=param->copy_field= new Copy_field[param->field_count]))
 
6101
  if (param->field_count &&
 
6102
      !(copy=param->copy_field= new CopyField[param->field_count]))
16682
6103
    goto err2;
16683
6104
 
16684
6105
  param->copy_funcs.empty();
16685
6106
  for (i= 0; (pos= li++); i++)
16686
6107
  {
16687
6108
    Field *field;
16688
 
    uchar *tmp;
 
6109
    unsigned char *tmp;
16689
6110
    Item *real_pos= pos->real_item();
16690
6111
    if (real_pos->type() == Item::FIELD_ITEM)
16691
6112
    {
16692
6113
      Item_field *item;
16693
 
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
16694
 
        goto err;
 
6114
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
 
6115
        goto err;
16695
6116
      if (pos->type() == Item::REF_ITEM)
16696
6117
      {
16697
6118
        /* preserve the names of the ref when dereferncing */
16703
6124
      pos= item;
16704
6125
      if (item->field->flags & BLOB_FLAG)
16705
6126
      {
16706
 
        if (!(pos= new Item_copy_string(pos)))
16707
 
          goto err;
16708
 
       /*
16709
 
         Item_copy_string::copy for function can call 
16710
 
         Item_copy_string::val_int for blob via Item_ref.
16711
 
         But if Item_copy_string::copy for blob isn't called before,
16712
 
         it's value will be wrong
16713
 
         so let's insert Item_copy_string for blobs in the beginning of 
16714
 
         copy_funcs
16715
 
         (to see full test case look at having.test, BUG #4358) 
16716
 
       */
16717
 
        if (param->copy_funcs.push_front(pos))
16718
 
          goto err;
 
6127
        if (!(pos= new Item_copy_string(pos)))
 
6128
          goto err;
 
6129
            /*
 
6130
              Item_copy_string::copy for function can call
 
6131
              Item_copy_string::val_int for blob via Item_ref.
 
6132
              But if Item_copy_string::copy for blob isn't called before,
 
6133
              it's value will be wrong
 
6134
              so let's insert Item_copy_string for blobs in the beginning of
 
6135
              copy_funcs
 
6136
              (to see full test case look at having.test, BUG #4358)
 
6137
            */
 
6138
        if (param->copy_funcs.push_front(pos))
 
6139
          goto err;
16719
6140
      }
16720
6141
      else
16721
6142
      {
16722
 
        /* 
16723
 
           set up save buffer and change result_field to point at 
16724
 
           saved value
16725
 
        */
16726
 
        field= item->field;
16727
 
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
16728
6143
        /*
16729
 
          We need to allocate one extra byte for null handling and
16730
 
          another extra byte to not get warnings from purify in
16731
 
          Field_string::val_int
 
6144
          set up save buffer and change result_field to point at
 
6145
          saved value
16732
6146
        */
16733
 
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
16734
 
          goto err;
 
6147
        field= item->field;
 
6148
        item->result_field=field->new_field(session->mem_root,field->table, 1);
 
6149
              /*
 
6150
                We need to allocate one extra byte for null handling and
 
6151
                another extra byte to not get warnings from purify in
 
6152
                Field_varstring::val_int
 
6153
              */
 
6154
        if (!(tmp= (unsigned char*) memory::sql_alloc(field->pack_length()+2)))
 
6155
          goto err;
16735
6156
        if (copy)
16736
6157
        {
16737
6158
          copy->set(tmp, item->result_field);
16751
6172
    {                                           // Save for send fields
16752
6173
      pos= real_pos;
16753
6174
      /* TODO:
16754
 
         In most cases this result will be sent to the user.
16755
 
         This should be changed to use copy_int or copy_real depending
16756
 
         on how the value is to be used: In some cases this may be an
16757
 
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
6175
        In most cases this result will be sent to the user.
 
6176
        This should be changed to use copy_int or copy_real depending
 
6177
        on how the value is to be used: In some cases this may be an
 
6178
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
16758
6179
      */
16759
6180
      if (!(pos=new Item_copy_string(pos)))
16760
 
        goto err;
16761
 
      if (i < border)                           // HAVING, ORDER and GROUP BY
 
6181
        goto err;
 
6182
      if (i < border)                           // HAVING, order_st and GROUP BY
16762
6183
      {
16763
6184
        if (extra_funcs.push_back(pos))
16764
6185
          goto err;
16765
6186
      }
16766
6187
      else if (param->copy_funcs.push_back(pos))
16767
 
        goto err;
 
6188
        goto err;
16768
6189
    }
16769
6190
    res_all_fields.push_back(pos);
16770
6191
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
16776
6197
    itr++;
16777
6198
  itr.sublist(res_selected_fields, elements);
16778
6199
  /*
16779
 
    Put elements from HAVING, ORDER BY and GROUP BY last to ensure that any
 
6200
    Put elements from HAVING, order_st BY and GROUP BY last to ensure that any
16780
6201
    reference used in these will resolve to a item that is already calculated
16781
6202
  */
16782
6203
  param->copy_funcs.concat(&extra_funcs);
16783
6204
 
16784
6205
  return(0);
16785
6206
 
16786
 
 err:
 
6207
err:
16787
6208
  if (copy)
16788
6209
    delete [] param->copy_field;                        // This is never 0
16789
6210
  param->copy_field=0;
16791
6212
  return(true);
16792
6213
}
16793
6214
 
16794
 
 
16795
6215
/**
16796
6216
  Make a copy of all simple SELECT'ed items.
16797
6217
 
16798
6218
  This is done at the start of a new group so that we can retrieve
16799
6219
  these later when the group changes.
16800
6220
*/
16801
 
 
16802
 
void
16803
 
copy_fields(TMP_TABLE_PARAM *param)
 
6221
void copy_fields(Tmp_Table_Param *param)
16804
6222
{
16805
 
  Copy_field *ptr=param->copy_field;
16806
 
  Copy_field *end=param->copy_field_end;
 
6223
  CopyField *ptr= param->copy_field;
 
6224
  CopyField *end= param->copy_field_end;
16807
6225
 
16808
6226
  for (; ptr != end; ptr++)
16809
6227
    (*ptr->do_copy)(ptr);
16814
6232
    item->copy();
16815
6233
}
16816
6234
 
16817
 
 
16818
 
/**
16819
 
  Make an array of pointers to sum_functions to speed up
16820
 
  sum_func calculation.
16821
 
 
16822
 
  @retval
16823
 
    0   ok
16824
 
  @retval
16825
 
    1   Error
16826
 
*/
16827
 
 
16828
 
bool JOIN::alloc_func_list()
16829
 
{
16830
 
  uint func_count, group_parts;
16831
 
 
16832
 
  func_count= tmp_table_param.sum_func_count;
16833
 
  /*
16834
 
    If we are using rollup, we need a copy of the summary functions for
16835
 
    each level
16836
 
  */
16837
 
  if (rollup.state != ROLLUP::STATE_NONE)
16838
 
    func_count*= (send_group_parts+1);
16839
 
 
16840
 
  group_parts= send_group_parts;
16841
 
  /*
16842
 
    If distinct, reserve memory for possible
16843
 
    disctinct->group_by optimization
16844
 
  */
16845
 
  if (select_distinct)
16846
 
  {
16847
 
    group_parts+= fields_list.elements;
16848
 
    /*
16849
 
      If the ORDER clause is specified then it's possible that
16850
 
      it also will be optimized, so reserve space for it too
16851
 
    */
16852
 
    if (order)
16853
 
    {
16854
 
      ORDER *ord;
16855
 
      for (ord= order; ord; ord= ord->next)
16856
 
        group_parts++;
16857
 
    }
16858
 
  }
16859
 
 
16860
 
  /* This must use calloc() as rollup_make_fields depends on this */
16861
 
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
16862
 
                                      sizeof(Item_sum***) * (group_parts+1));
16863
 
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
16864
 
  return(sum_funcs == 0);
16865
 
}
16866
 
 
16867
 
 
16868
 
/**
16869
 
  Initialize 'sum_funcs' array with all Item_sum objects.
16870
 
 
16871
 
  @param field_list        All items
16872
 
  @param send_fields       Items in select list
16873
 
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
16874
 
  @param recompute         Set to true if sum_funcs must be recomputed
16875
 
 
16876
 
  @retval
16877
 
    0  ok
16878
 
  @retval
16879
 
    1  error
16880
 
*/
16881
 
 
16882
 
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
16883
 
                              bool before_group_by, bool recompute)
16884
 
{
16885
 
  List_iterator_fast<Item> it(field_list);
16886
 
  Item_sum **func;
16887
 
  Item *item;
16888
 
 
16889
 
  if (*sum_funcs && !recompute)
16890
 
    return(false); /* We have already initialized sum_funcs. */
16891
 
 
16892
 
  func= sum_funcs;
16893
 
  while ((item=it++))
16894
 
  {
16895
 
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
16896
 
        (!((Item_sum*) item)->depended_from() ||
16897
 
         ((Item_sum *)item)->depended_from() == select_lex))
16898
 
      *func++= (Item_sum*) item;
16899
 
  }
16900
 
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
16901
 
  {
16902
 
    rollup.state= ROLLUP::STATE_READY;
16903
 
    if (rollup_make_fields(field_list, send_fields, &func))
16904
 
      return(true);                     // Should never happen
16905
 
  }
16906
 
  else if (rollup.state == ROLLUP::STATE_NONE)
16907
 
  {
16908
 
    for (uint i=0 ; i <= send_group_parts ;i++)
16909
 
      sum_funcs_end[i]= func;
16910
 
  }
16911
 
  else if (rollup.state == ROLLUP::STATE_READY)
16912
 
    return(false);                         // Don't put end marker
16913
 
  *func=0;                                      // End marker
16914
 
  return(false);
16915
 
}
16916
 
 
16917
 
 
16918
6235
/**
16919
6236
  Change all funcs and sum_funcs to fields in tmp table, and create
16920
6237
  new list of all items.
16921
6238
 
16922
 
  @param thd                   THD pointer
 
6239
  @param session                   Session pointer
16923
6240
  @param ref_pointer_array     array of pointers to top elements of filed list
16924
6241
  @param res_selected_fields   new list of items of select item list
16925
6242
  @param res_all_fields        new list of all items
16931
6248
  @retval
16932
6249
    !=0   error
16933
6250
*/
16934
 
 
16935
 
static bool
16936
 
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
16937
 
                         List<Item> &res_selected_fields,
16938
 
                         List<Item> &res_all_fields,
16939
 
                         uint elements, List<Item> &all_fields)
 
6251
bool change_to_use_tmp_fields(Session *session,
 
6252
                              Item **ref_pointer_array,
 
6253
                                                List<Item> &res_selected_fields,
 
6254
                                                List<Item> &res_all_fields,
 
6255
                                                uint32_t elements,
 
6256
                              List<Item> &all_fields)
16940
6257
{
16941
6258
  List_iterator_fast<Item> it(all_fields);
16942
6259
  Item *item_field,*item;
16944
6261
  res_selected_fields.empty();
16945
6262
  res_all_fields.empty();
16946
6263
 
16947
 
  uint i, border= all_fields.elements - elements;
 
6264
  uint32_t i, border= all_fields.elements - elements;
16948
6265
  for (i= 0; (item= it++); i++)
16949
6266
  {
16950
6267
    Field *field;
16957
6274
    {
16958
6275
      if (item->type() == Item::FIELD_ITEM)
16959
6276
      {
16960
 
        item_field= item->get_tmp_table_item(thd);
 
6277
        item_field= item->get_tmp_table_item(session);
16961
6278
      }
16962
6279
      else if ((field= item->get_tmp_table_field()))
16963
6280
      {
16964
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
16965
 
          item_field= ((Item_sum*) item)->result_item(field);
16966
 
        else
16967
 
          item_field= (Item*) new Item_field(field);
16968
 
        if (!item_field)
16969
 
          return(true);                    // Fatal error
 
6281
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
6282
          item_field= ((Item_sum*) item)->result_item(field);
 
6283
        else
 
6284
          item_field= (Item*) new Item_field(field);
 
6285
        if (!item_field)
 
6286
          return(true);                    // Fatal error
16970
6287
 
16971
6288
        if (item->real_item()->type() != Item::FIELD_ITEM)
16972
6289
          field->orig_table= 0;
16973
 
        item_field->name= item->name;
 
6290
        item_field->name= item->name;
16974
6291
        if (item->type() == Item::REF_ITEM)
16975
6292
        {
16976
6293
          Item_field *ifield= (Item_field *) item_field;
16980
6297
        }
16981
6298
      }
16982
6299
      else
16983
 
        item_field= item;
 
6300
        item_field= item;
16984
6301
    }
16985
6302
    res_all_fields.push_back(item_field);
16986
6303
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
16994
6311
  return(false);
16995
6312
}
16996
6313
 
16997
 
 
16998
6314
/**
16999
6315
  Change all sum_func refs to fields to point at fields in tmp table.
17000
6316
  Change all funcs to be fields in tmp table.
17001
6317
 
17002
 
  @param thd                   THD pointer
 
6318
  @param session                   Session pointer
17003
6319
  @param ref_pointer_array     array of pointers to top elements of filed list
17004
6320
  @param res_selected_fields   new list of items of select item list
17005
6321
  @param res_all_fields        new list of all items
17011
6327
  @retval
17012
6328
    1   error
17013
6329
*/
17014
 
 
17015
 
static bool
17016
 
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
17017
 
                          List<Item> &res_selected_fields,
17018
 
                          List<Item> &res_all_fields, uint elements,
17019
 
                          List<Item> &all_fields)
 
6330
bool change_refs_to_tmp_fields(Session *session,
 
6331
                               Item **ref_pointer_array,
 
6332
                               List<Item> &res_selected_fields,
 
6333
                               List<Item> &res_all_fields,
 
6334
                               uint32_t elements,
 
6335
                                                 List<Item> &all_fields)
17020
6336
{
17021
6337
  List_iterator_fast<Item> it(all_fields);
17022
6338
  Item *item, *new_item;
17023
6339
  res_selected_fields.empty();
17024
6340
  res_all_fields.empty();
17025
6341
 
17026
 
  uint i, border= all_fields.elements - elements;
 
6342
  uint32_t i, border= all_fields.elements - elements;
17027
6343
  for (i= 0; (item= it++); i++)
17028
6344
  {
17029
 
    res_all_fields.push_back(new_item= item->get_tmp_table_item(thd));
 
6345
    res_all_fields.push_back(new_item= item->get_tmp_table_item(session));
17030
6346
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17031
6347
      new_item;
17032
6348
  }
17036
6352
    itr++;
17037
6353
  itr.sublist(res_selected_fields, elements);
17038
6354
 
17039
 
  return thd->is_fatal_error;
 
6355
  return session->is_fatal_error;
17040
6356
}
17041
6357
 
17042
 
 
17043
 
 
17044
6358
/******************************************************************************
17045
6359
  Code for calculating functions
17046
6360
******************************************************************************/
17047
6361
 
17048
 
 
17049
6362
/**
17050
6363
  Call ::setup for all sum functions.
17051
6364
 
17052
 
  @param thd           thread handler
 
6365
  @param session           thread Cursor
17053
6366
  @param func_ptr      sum function list
17054
6367
 
17055
6368
  @retval
17057
6370
  @retval
17058
6371
    true   error
17059
6372
*/
17060
 
 
17061
 
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
 
6373
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
17062
6374
{
17063
6375
  Item_sum *func;
17064
6376
  while ((func= *(func_ptr++)))
17065
6377
  {
17066
 
    if (func->setup(thd))
 
6378
    if (func->setup(session))
17067
6379
      return(true);
17068
6380
  }
17069
6381
  return(false);
17070
6382
}
17071
6383
 
17072
 
 
17073
 
static void
17074
 
init_tmptable_sum_functions(Item_sum **func_ptr)
 
6384
void init_tmptable_sum_functions(Item_sum **func_ptr)
17075
6385
{
17076
6386
  Item_sum *func;
17077
6387
  while ((func= *(func_ptr++)))
17078
6388
    func->reset_field();
17079
6389
}
17080
6390
 
17081
 
 
17082
6391
/** Update record 0 in tmp_table from record 1. */
17083
 
 
17084
 
static void
17085
 
update_tmptable_sum_func(Item_sum **func_ptr,
17086
 
                         TABLE *tmp_table __attribute__((unused)))
 
6392
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
17087
6393
{
17088
6394
  Item_sum *func;
17089
6395
  while ((func= *(func_ptr++)))
17090
6396
    func->update_field();
17091
6397
}
17092
6398
 
17093
 
 
17094
6399
/** Copy result of sum functions to record in tmp_table. */
17095
 
 
17096
 
static void
17097
 
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
6400
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
17098
6401
{
17099
6402
  for (; func_ptr != end_ptr ; func_ptr++)
17100
6403
    (void) (*func_ptr)->save_in_result_field(1);
17101
6404
  return;
17102
6405
}
17103
6406
 
17104
 
 
17105
 
static bool
17106
 
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
6407
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
17107
6408
{
17108
6409
  for (; func_ptr != end_ptr ;func_ptr++)
17109
6410
  {
17119
6420
  return 0;
17120
6421
}
17121
6422
 
17122
 
 
17123
 
static bool
17124
 
update_sum_func(Item_sum **func_ptr)
 
6423
bool update_sum_func(Item_sum **func_ptr)
17125
6424
{
17126
6425
  Item_sum *func;
17127
6426
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
17131
6430
}
17132
6431
 
17133
6432
/** Copy result of functions to record in tmp_table. */
17134
 
 
17135
 
void
17136
 
copy_funcs(Item **func_ptr)
 
6433
void copy_funcs(Item **func_ptr)
17137
6434
{
17138
6435
  Item *func;
17139
6436
  for (; (func = *func_ptr) ; func_ptr++)
17140
6437
    func->save_in_result_field(1);
17141
6438
}
17142
6439
 
17143
 
 
17144
 
/**
17145
 
  Create a condition for a const reference and add this to the
17146
 
  currenct select for the table.
17147
 
*/
17148
 
 
17149
 
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
17150
 
{
17151
 
  if (!join_tab->ref.key_parts)
17152
 
    return(false);
17153
 
 
17154
 
  Item_cond_and *cond=new Item_cond_and();
17155
 
  TABLE *table=join_tab->table;
17156
 
  int error;
17157
 
  if (!cond)
17158
 
    return(true);
17159
 
 
17160
 
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
17161
 
  {
17162
 
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
17163
 
                              fieldnr-1];
17164
 
    Item *value=join_tab->ref.items[i];
17165
 
    cond->add(new Item_func_equal(new Item_field(field), value));
17166
 
  }
17167
 
  if (thd->is_fatal_error)
17168
 
    return(true);
17169
 
 
17170
 
  if (!cond->fixed)
17171
 
    cond->fix_fields(thd, (Item**)&cond);
17172
 
  if (join_tab->select)
17173
 
  {
17174
 
    error=(int) cond->add(join_tab->select->cond);
17175
 
    join_tab->select_cond=join_tab->select->cond=cond;
17176
 
  }
17177
 
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
17178
 
                                          &error)))
17179
 
    join_tab->select_cond=cond;
17180
 
 
17181
 
  return(error ? true : false);
17182
 
}
17183
 
 
17184
 
 
17185
6440
/**
17186
6441
  Free joins of subselect of this select.
17187
6442
 
17188
 
  @param thd      THD pointer
17189
 
  @param select   pointer to st_select_lex which subselects joins we will free
 
6443
  @param session      Session pointer
 
6444
  @param select   pointer to Select_Lex which subselects joins we will free
17190
6445
*/
17191
 
 
17192
 
void free_underlaid_joins(THD *thd __attribute__((unused)),
17193
 
                          SELECT_LEX *select)
 
6446
void free_underlaid_joins(Session *, Select_Lex *select)
17194
6447
{
17195
 
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
 
6448
  for (Select_Lex_Unit *unit= select->first_inner_unit();
17196
6449
       unit;
17197
6450
       unit= unit->next_unit())
17198
6451
    unit->cleanup();
17214
6467
  @b EXAMPLES
17215
6468
    @code
17216
6469
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
17217
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
 
6470
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
17218
6471
  @endcode
17219
6472
 
17220
6473
  @b IMPLEMENTATION
17227
6480
    This substitution is needed GROUP BY queries with ROLLUP if
17228
6481
    SELECT list contains expressions over group by attributes.
17229
6482
 
17230
 
  @param thd                  reference to the context
 
6483
  @param session                  reference to the context
17231
6484
  @param expr                 expression to make replacement
17232
6485
  @param group_list           list of references to group by items
17233
6486
  @param changed        out:  returns 1 if item contains a replaced field item
17234
6487
 
17235
6488
  @todo
17236
6489
    - TODO: Some functions are not null-preserving. For those functions
17237
 
    updating of the maybe_null attribute is an overkill. 
 
6490
    updating of the maybe_null attribute is an overkill.
17238
6491
 
17239
6492
  @retval
17240
6493
    0   if ok
17241
6494
  @retval
17242
6495
    1   on error
17243
6496
*/
17244
 
 
17245
 
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
17246
 
                             bool *changed)
 
6497
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
17247
6498
{
17248
6499
  if (expr->arg_count)
17249
6500
  {
17250
 
    Name_resolution_context *context= &thd->lex->current_select->context;
 
6501
    Name_resolution_context *context= &session->lex->current_select->context;
17251
6502
    Item **arg,**arg_end;
17252
6503
    bool arg_changed= false;
17253
6504
    for (arg= expr->arguments(),
17257
6508
      Item *item= *arg;
17258
6509
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
17259
6510
      {
17260
 
        ORDER *group_tmp;
 
6511
        order_st *group_tmp;
17261
6512
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17262
6513
        {
17263
6514
          if (item->eq(*group_tmp->item,0))
17266
6517
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
17267
6518
                                        item->name)))
17268
6519
              return 1;                                 // fatal_error is set
17269
 
            thd->change_item_tree(arg, new_item);
 
6520
            session->change_item_tree(arg, new_item);
17270
6521
            arg_changed= true;
17271
6522
          }
17272
6523
        }
17273
6524
      }
17274
6525
      else if (item->type() == Item::FUNC_ITEM)
17275
6526
      {
17276
 
        if (change_group_ref(thd, (Item_func *) item, group_list, &arg_changed))
 
6527
        if (change_group_ref(session, (Item_func *) item, group_list, &arg_changed))
17277
6528
          return 1;
17278
6529
      }
17279
6530
    }
17287
6538
}
17288
6539
 
17289
6540
 
17290
 
/** Allocate memory needed for other rollup functions. */
17291
 
 
17292
 
bool JOIN::rollup_init()
17293
 
{
17294
 
  uint i,j;
17295
 
  Item **ref_array;
17296
 
 
17297
 
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
17298
 
  rollup.state= ROLLUP::STATE_INITED;
17299
 
 
17300
 
  /*
17301
 
    Create pointers to the different sum function groups
17302
 
    These are updated by rollup_make_fields()
17303
 
  */
17304
 
  tmp_table_param.group_parts= send_group_parts;
17305
 
 
17306
 
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17307
 
                                                sizeof(Item**) +
17308
 
                                                sizeof(List<Item>) +
17309
 
                                                ref_pointer_array_size)
17310
 
                                                * send_group_parts )))
17311
 
    return 1;
17312
 
  
17313
 
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17314
 
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17315
 
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17316
 
 
17317
 
  /*
17318
 
    Prepare space for field list for the different levels
17319
 
    These will be filled up in rollup_make_fields()
17320
 
  */
17321
 
  for (i= 0 ; i < send_group_parts ; i++)
17322
 
  {
17323
 
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17324
 
    List<Item> *rollup_fields= &rollup.fields[i];
17325
 
    rollup_fields->empty();
17326
 
    rollup.ref_pointer_arrays[i]= ref_array;
17327
 
    ref_array+= all_fields.elements;
17328
 
  }
17329
 
  for (i= 0 ; i < send_group_parts; i++)
17330
 
  {
17331
 
    for (j=0 ; j < fields_list.elements ; j++)
17332
 
      rollup.fields[i].push_back(rollup.null_items[i]);
17333
 
  }
17334
 
  List_iterator<Item> it(all_fields);
17335
 
  Item *item;
17336
 
  while ((item= it++))
17337
 
  {
17338
 
    ORDER *group_tmp;
17339
 
    bool found_in_group= 0;
17340
 
 
17341
 
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17342
 
    {
17343
 
      if (*group_tmp->item == item)
17344
 
      {
17345
 
        item->maybe_null= 1;
17346
 
        found_in_group= 1;
17347
 
        if (item->const_item())
17348
 
        {
17349
 
          /*
17350
 
            For ROLLUP queries each constant item referenced in GROUP BY list
17351
 
            is wrapped up into an Item_func object yielding the same value
17352
 
            as the constant item. The objects of the wrapper class are never
17353
 
            considered as constant items and besides they inherit all
17354
 
            properties of the Item_result_field class.
17355
 
            This wrapping allows us to ensure writing constant items
17356
 
            into temporary tables whenever the result of the ROLLUP
17357
 
            operation has to be written into a temporary table, e.g. when
17358
 
            ROLLUP is used together with DISTINCT in the SELECT list.
17359
 
            Usually when creating temporary tables for a intermidiate
17360
 
            result we do not include fields for constant expressions.
17361
 
          */           
17362
 
          Item* new_item= new Item_func_rollup_const(item);
17363
 
          if (!new_item)
17364
 
            return 1;
17365
 
          new_item->fix_fields(thd, (Item **) 0);
17366
 
          thd->change_item_tree(it.ref(), new_item);
17367
 
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17368
 
          { 
17369
 
            if (*tmp->item == item)
17370
 
              thd->change_item_tree(tmp->item, new_item);
17371
 
          }
17372
 
        }
17373
 
      }
17374
 
    }
17375
 
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
17376
 
    {
17377
 
      bool changed= false;
17378
 
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17379
 
        return 1;
17380
 
      /*
17381
 
        We have to prevent creation of a field in a temporary table for
17382
 
        an expression that contains GROUP BY attributes.
17383
 
        Marking the expression item as 'with_sum_func' will ensure this.
17384
 
      */ 
17385
 
      if (changed)
17386
 
        item->with_sum_func= 1;
17387
 
    }
17388
 
  }
17389
 
  return 0;
17390
 
}
17391
 
  
17392
 
 
17393
 
/**
17394
 
  Fill up rollup structures with pointers to fields to use.
17395
 
 
17396
 
  Creates copies of item_sum items for each sum level.
17397
 
 
17398
 
  @param fields_arg             List of all fields (hidden and real ones)
17399
 
  @param sel_fields             Pointer to selected fields
17400
 
  @param func                   Store here a pointer to all fields
17401
 
 
17402
 
  @retval
17403
 
    0   if ok;
17404
 
    In this case func is pointing to next not used element.
17405
 
  @retval
17406
 
    1    on error
17407
 
*/
17408
 
 
17409
 
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17410
 
                              Item_sum ***func)
17411
 
{
17412
 
  List_iterator_fast<Item> it(fields_arg);
17413
 
  Item *first_field= sel_fields.head();
17414
 
  uint level;
17415
 
 
17416
 
  /*
17417
 
    Create field lists for the different levels
17418
 
 
17419
 
    The idea here is to have a separate field list for each rollup level to
17420
 
    avoid all runtime checks of which columns should be NULL.
17421
 
 
17422
 
    The list is stored in reverse order to get sum function in such an order
17423
 
    in func that it makes it easy to reset them with init_sum_functions()
17424
 
 
17425
 
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17426
 
 
17427
 
    rollup.fields[0] will contain list where a,b,c is NULL
17428
 
    rollup.fields[1] will contain list where b,c is NULL
17429
 
    ...
17430
 
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17431
 
    ...
17432
 
    sum_funcs_end[0] points to all sum functions
17433
 
    sum_funcs_end[1] points to all sum functions, except grand totals
17434
 
    ...
17435
 
  */
17436
 
 
17437
 
  for (level=0 ; level < send_group_parts ; level++)
17438
 
  {
17439
 
    uint i;
17440
 
    uint pos= send_group_parts - level -1;
17441
 
    bool real_fields= 0;
17442
 
    Item *item;
17443
 
    List_iterator<Item> new_it(rollup.fields[pos]);
17444
 
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17445
 
    ORDER *start_group;
17446
 
 
17447
 
    /* Point to first hidden field */
17448
 
    Item **ref_array= ref_array_start + fields_arg.elements-1;
17449
 
 
17450
 
    /* Remember where the sum functions ends for the previous level */
17451
 
    sum_funcs_end[pos+1]= *func;
17452
 
 
17453
 
    /* Find the start of the group for this level */
17454
 
    for (i= 0, start_group= group_list ;
17455
 
         i++ < pos ;
17456
 
         start_group= start_group->next)
17457
 
      ;
17458
 
 
17459
 
    it.rewind();
17460
 
    while ((item= it++))
17461
 
    {
17462
 
      if (item == first_field)
17463
 
      {
17464
 
        real_fields= 1;                         // End of hidden fields
17465
 
        ref_array= ref_array_start;
17466
 
      }
17467
 
 
17468
 
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17469
 
          (!((Item_sum*) item)->depended_from() ||
17470
 
           ((Item_sum *)item)->depended_from() == select_lex))
17471
 
          
17472
 
      {
17473
 
        /*
17474
 
          This is a top level summary function that must be replaced with
17475
 
          a sum function that is reset for this level.
17476
 
 
17477
 
          NOTE: This code creates an object which is not that nice in a
17478
 
          sub select.  Fortunately it's not common to have rollup in
17479
 
          sub selects.
17480
 
        */
17481
 
        item= item->copy_or_same(thd);
17482
 
        ((Item_sum*) item)->make_unique();
17483
 
        *(*func)= (Item_sum*) item;
17484
 
        (*func)++;
17485
 
      }
17486
 
      else 
17487
 
      {
17488
 
        /* Check if this is something that is part of this group by */
17489
 
        ORDER *group_tmp;
17490
 
        for (group_tmp= start_group, i= pos ;
17491
 
             group_tmp ; group_tmp= group_tmp->next, i++)
17492
 
        {
17493
 
          if (*group_tmp->item == item)
17494
 
          {
17495
 
            /*
17496
 
              This is an element that is used by the GROUP BY and should be
17497
 
              set to NULL in this level
17498
 
            */
17499
 
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17500
 
            if (!null_item)
17501
 
              return 1;
17502
 
            item->maybe_null= 1;                // Value will be null sometimes
17503
 
            null_item->result_field= item->get_tmp_table_field();
17504
 
            item= null_item;
17505
 
            break;
17506
 
          }
17507
 
        }
17508
 
      }
17509
 
      *ref_array= item;
17510
 
      if (real_fields)
17511
 
      {
17512
 
        (void) new_it++;                        // Point to next item
17513
 
        new_it.replace(item);                   // Replace previous
17514
 
        ref_array++;
17515
 
      }
17516
 
      else
17517
 
        ref_array--;
17518
 
    }
17519
 
  }
17520
 
  sum_funcs_end[0]= *func;                      // Point to last function
17521
 
  return 0;
17522
 
}
17523
 
 
17524
 
/**
17525
 
  Send all rollup levels higher than the current one to the client.
17526
 
 
17527
 
  @b SAMPLE
17528
 
    @code
17529
 
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17530
 
  @endcode
17531
 
 
17532
 
  @param idx            Level we are on:
17533
 
                        - 0 = Total sum level
17534
 
                        - 1 = First group changed  (a)
17535
 
                        - 2 = Second group changed (a,b)
17536
 
 
17537
 
  @retval
17538
 
    0   ok
17539
 
  @retval
17540
 
    1   If send_data_failed()
17541
 
*/
17542
 
 
17543
 
int JOIN::rollup_send_data(uint idx)
17544
 
{
17545
 
  uint i;
17546
 
  for (i= send_group_parts ; i-- > idx ; )
17547
 
  {
17548
 
    /* Get reference pointers to sum functions in place */
17549
 
    memcpy((char*) ref_pointer_array,
17550
 
           (char*) rollup.ref_pointer_arrays[i],
17551
 
           ref_pointer_array_size);
17552
 
    if ((!having || having->val_int()))
17553
 
    {
17554
 
      if (send_records < unit->select_limit_cnt && do_send_rows &&
17555
 
          result->send_data(rollup.fields[i]))
17556
 
        return 1;
17557
 
      send_records++;
17558
 
    }
17559
 
  }
17560
 
  /* Restore ref_pointer_array */
17561
 
  set_items_ref_array(current_ref_pointer_array);
17562
 
  return 0;
17563
 
}
17564
 
 
17565
 
/**
17566
 
  Write all rollup levels higher than the current one to a temp table.
17567
 
 
17568
 
  @b SAMPLE
17569
 
    @code
17570
 
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17571
 
  @endcode
17572
 
 
17573
 
  @param idx                 Level we are on:
17574
 
                               - 0 = Total sum level
17575
 
                               - 1 = First group changed  (a)
17576
 
                               - 2 = Second group changed (a,b)
17577
 
  @param table               reference to temp table
17578
 
 
17579
 
  @retval
17580
 
    0   ok
17581
 
  @retval
17582
 
    1   if write_data_failed()
17583
 
*/
17584
 
 
17585
 
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17586
 
{
17587
 
  uint i;
17588
 
  for (i= send_group_parts ; i-- > idx ; )
17589
 
  {
17590
 
    /* Get reference pointers to sum functions in place */
17591
 
    memcpy((char*) ref_pointer_array,
17592
 
           (char*) rollup.ref_pointer_arrays[i],
17593
 
           ref_pointer_array_size);
17594
 
    if ((!having || having->val_int()))
17595
 
    {
17596
 
      int write_error;
17597
 
      Item *item;
17598
 
      List_iterator_fast<Item> it(rollup.fields[i]);
17599
 
      while ((item= it++))
17600
 
      {
17601
 
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
17602
 
          item->save_in_result_field(1);
17603
 
      }
17604
 
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17605
 
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17606
 
      {
17607
 
        if (create_myisam_from_heap(thd, table_arg, 
17608
 
                                    tmp_table_param.start_recinfo,
17609
 
                                    &tmp_table_param.recinfo,
17610
 
                                    write_error, 0))
17611
 
          return 1;                  
17612
 
      }
17613
 
    }
17614
 
  }
17615
 
  /* Restore ref_pointer_array */
17616
 
  set_items_ref_array(current_ref_pointer_array);
17617
 
  return 0;
17618
 
}
17619
 
 
17620
 
/**
17621
 
  clear results if there are not rows found for group
17622
 
  (end_send_group/end_write_group)
17623
 
*/
17624
 
 
17625
 
void JOIN::clear()
17626
 
{
17627
 
  clear_tables(this);
17628
 
  copy_fields(&tmp_table_param);
17629
 
 
17630
 
  if (sum_funcs)
17631
 
  {
17632
 
    Item_sum *func, **func_ptr= sum_funcs;
17633
 
    while ((func= *(func_ptr++)))
17634
 
      func->clear();
17635
 
  }
17636
 
}
17637
 
 
17638
 
/**
17639
 
  EXPLAIN handling.
17640
 
 
17641
 
  Send a description about what how the select will be done to stdout.
17642
 
*/
17643
 
 
17644
 
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17645
 
                     bool distinct,const char *message)
17646
 
{
17647
 
  List<Item> field_list;
17648
 
  List<Item> item_list;
17649
 
  THD *thd=join->thd;
17650
 
  select_result *result=join->result;
17651
 
  Item *item_null= new Item_null();
17652
 
  CHARSET_INFO *cs= system_charset_info;
17653
 
  int quick_type;
17654
 
  /* Don't log this into the slow query log */
17655
 
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17656
 
  join->unit->offset_limit_cnt= 0;
17657
 
 
17658
 
  /* 
17659
 
    NOTE: the number/types of items pushed into item_list must be in sync with
17660
 
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17661
 
  */
17662
 
  if (message)
17663
 
  {
17664
 
    item_list.push_back(new Item_int((int32_t)
17665
 
                                     join->select_lex->select_number));
17666
 
    item_list.push_back(new Item_string(join->select_lex->type,
17667
 
                                        strlen(join->select_lex->type), cs));
17668
 
    for (uint i=0 ; i < 7; i++)
17669
 
      item_list.push_back(item_null);
17670
 
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17671
 
      item_list.push_back(item_null);
17672
 
  
17673
 
    item_list.push_back(new Item_string(message,strlen(message),cs));
17674
 
    if (result->send_data(item_list))
17675
 
      join->error= 1;
17676
 
  }
17677
 
  else if (join->select_lex == join->unit->fake_select_lex)
17678
 
  {
17679
 
    /* 
17680
 
      here we assume that the query will return at least two rows, so we
17681
 
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17682
 
      and no filesort will be actually done, but executing all selects in
17683
 
      the UNION to provide precise EXPLAIN information will hardly be
17684
 
      appreciated :)
17685
 
    */
17686
 
    char table_name_buffer[NAME_LEN];
17687
 
    item_list.empty();
17688
 
    /* id */
17689
 
    item_list.push_back(new Item_null);
17690
 
    /* select_type */
17691
 
    item_list.push_back(new Item_string(join->select_lex->type,
17692
 
                                        strlen(join->select_lex->type),
17693
 
                                        cs));
17694
 
    /* table */
17695
 
    {
17696
 
      SELECT_LEX *sl= join->unit->first_select();
17697
 
      uint len= 6, lastop= 0;
17698
 
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
17699
 
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
17700
 
      {
17701
 
        len+= lastop;
17702
 
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
17703
 
                         "%u,", sl->select_number);
17704
 
      }
17705
 
      if (sl || len + lastop >= NAME_LEN)
17706
 
      {
17707
 
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
17708
 
        len+= 4;
17709
 
      }
17710
 
      else
17711
 
      {
17712
 
        len+= lastop;
17713
 
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
17714
 
      }
17715
 
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
17716
 
    }
17717
 
    /* type */
17718
 
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
17719
 
                                          strlen(join_type_str[JT_ALL]),
17720
 
                                          cs));
17721
 
    /* possible_keys */
17722
 
    item_list.push_back(item_null);
17723
 
    /* key*/
17724
 
    item_list.push_back(item_null);
17725
 
    /* key_len */
17726
 
    item_list.push_back(item_null);
17727
 
    /* ref */
17728
 
    item_list.push_back(item_null);
17729
 
    /* in_rows */
17730
 
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17731
 
      item_list.push_back(item_null);
17732
 
    /* rows */
17733
 
    item_list.push_back(item_null);
17734
 
    /* extra */
17735
 
    if (join->unit->global_parameters->order_list.first)
17736
 
      item_list.push_back(new Item_string("Using filesort",
17737
 
                                          14, cs));
17738
 
    else
17739
 
      item_list.push_back(new Item_string("", 0, cs));
17740
 
 
17741
 
    if (result->send_data(item_list))
17742
 
      join->error= 1;
17743
 
  }
17744
 
  else
17745
 
  {
17746
 
    table_map used_tables=0;
17747
 
    for (uint i=0 ; i < join->tables ; i++)
17748
 
    {
17749
 
      JOIN_TAB *tab=join->join_tab+i;
17750
 
      TABLE *table=tab->table;
17751
 
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
17752
 
      char buff[512]; 
17753
 
      char buff1[512], buff2[512], buff3[512];
17754
 
      char keylen_str_buf[64];
17755
 
      String extra(buff, sizeof(buff),cs);
17756
 
      char table_name_buffer[NAME_LEN];
17757
 
      String tmp1(buff1,sizeof(buff1),cs);
17758
 
      String tmp2(buff2,sizeof(buff2),cs);
17759
 
      String tmp3(buff3,sizeof(buff3),cs);
17760
 
      extra.length(0);
17761
 
      tmp1.length(0);
17762
 
      tmp2.length(0);
17763
 
      tmp3.length(0);
17764
 
 
17765
 
      quick_type= -1;
17766
 
      item_list.empty();
17767
 
      /* id */
17768
 
      item_list.push_back(new Item_uint((uint32_t)
17769
 
                                       join->select_lex->select_number));
17770
 
      /* select_type */
17771
 
      item_list.push_back(new Item_string(join->select_lex->type,
17772
 
                                          strlen(join->select_lex->type),
17773
 
                                          cs));
17774
 
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
17775
 
      {
17776
 
        quick_type= tab->select->quick->get_type();
17777
 
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
17778
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
17779
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
17780
 
          tab->type = JT_INDEX_MERGE;
17781
 
        else
17782
 
          tab->type = JT_RANGE;
17783
 
      }
17784
 
      /* table */
17785
 
      if (table->derived_select_number)
17786
 
      {
17787
 
        /* Derived table name generation */
17788
 
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
17789
 
                          "<derived%u>",
17790
 
                          table->derived_select_number);
17791
 
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
17792
 
      }
17793
 
      else
17794
 
      {
17795
 
        TABLE_LIST *real_table= table->pos_in_table_list; 
17796
 
        item_list.push_back(new Item_string(real_table->alias,
17797
 
                                            strlen(real_table->alias),
17798
 
                                            cs));
17799
 
      }
17800
 
      /* "type" column */
17801
 
      item_list.push_back(new Item_string(join_type_str[tab->type],
17802
 
                                          strlen(join_type_str[tab->type]),
17803
 
                                          cs));
17804
 
      /* Build "possible_keys" value and add it to item_list */
17805
 
      if (!tab->keys.is_clear_all())
17806
 
      {
17807
 
        uint j;
17808
 
        for (j=0 ; j < table->s->keys ; j++)
17809
 
        {
17810
 
          if (tab->keys.is_set(j))
17811
 
          {
17812
 
            if (tmp1.length())
17813
 
              tmp1.append(',');
17814
 
            tmp1.append(table->key_info[j].name, 
17815
 
                        strlen(table->key_info[j].name),
17816
 
                        system_charset_info);
17817
 
          }
17818
 
        }
17819
 
      }
17820
 
      if (tmp1.length())
17821
 
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
17822
 
      else
17823
 
        item_list.push_back(item_null);
17824
 
 
17825
 
      /* Build "key", "key_len", and "ref" values and add them to item_list */
17826
 
      if (tab->ref.key_parts)
17827
 
      {
17828
 
        KEY *key_info=table->key_info+ tab->ref.key;
17829
 
        register uint length;
17830
 
        item_list.push_back(new Item_string(key_info->name,
17831
 
                                            strlen(key_info->name),
17832
 
                                            system_charset_info));
17833
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
17834
 
                keylen_str_buf;
17835
 
        item_list.push_back(new Item_string(keylen_str_buf, length,
17836
 
                                            system_charset_info));
17837
 
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
17838
 
        {
17839
 
          if (tmp2.length())
17840
 
            tmp2.append(',');
17841
 
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
17842
 
                      system_charset_info);
17843
 
        }
17844
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17845
 
      }
17846
 
      else if (tab->type == JT_NEXT)
17847
 
      {
17848
 
        KEY *key_info=table->key_info+ tab->index;
17849
 
        register uint length;
17850
 
        item_list.push_back(new Item_string(key_info->name,
17851
 
                                            strlen(key_info->name),cs));
17852
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
17853
 
                keylen_str_buf;
17854
 
        item_list.push_back(new Item_string(keylen_str_buf, 
17855
 
                                            length,
17856
 
                                            system_charset_info));
17857
 
        item_list.push_back(item_null);
17858
 
      }
17859
 
      else if (tab->select && tab->select->quick)
17860
 
      {
17861
 
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
17862
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17863
 
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
17864
 
        item_list.push_back(item_null);
17865
 
      }
17866
 
      else
17867
 
      {
17868
 
        if (table_list->schema_table &&
17869
 
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
17870
 
        {
17871
 
          const char *tmp_buff;
17872
 
          int f_idx;
17873
 
          if (table_list->has_db_lookup_value)
17874
 
          {
17875
 
            f_idx= table_list->schema_table->idx_field1;
17876
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17877
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17878
 
          }          
17879
 
          if (table_list->has_table_lookup_value)
17880
 
          {
17881
 
            if (table_list->has_db_lookup_value)
17882
 
              tmp2.append(',');
17883
 
            f_idx= table_list->schema_table->idx_field2;
17884
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17885
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17886
 
          }
17887
 
          if (tmp2.length())
17888
 
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17889
 
          else
17890
 
            item_list.push_back(item_null);
17891
 
        }
17892
 
        else
17893
 
          item_list.push_back(item_null);
17894
 
        item_list.push_back(item_null);
17895
 
        item_list.push_back(item_null);
17896
 
      }
17897
 
      
17898
 
      /* Add "rows" field to item_list. */
17899
 
      if (table_list->schema_table)
17900
 
      {
17901
 
        /* in_rows */
17902
 
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17903
 
          item_list.push_back(item_null);
17904
 
        /* rows */
17905
 
        item_list.push_back(item_null);
17906
 
      }
17907
 
      else
17908
 
      {
17909
 
        double examined_rows;
17910
 
        if (tab->select && tab->select->quick)
17911
 
          examined_rows= rows2double(tab->select->quick->records);
17912
 
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
17913
 
          examined_rows= rows2double(tab->limit ? tab->limit : 
17914
 
                                     tab->table->file->records());
17915
 
        else
17916
 
          examined_rows= join->best_positions[i].records_read; 
17917
 
 
17918
 
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
17919
 
                                         MY_INT64_NUM_DECIMAL_DIGITS));
17920
 
 
17921
 
        /* Add "filtered" field to item_list. */
17922
 
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17923
 
        {
17924
 
          float f= 0.0; 
17925
 
          if (examined_rows)
17926
 
            f= (float) (100.0 * join->best_positions[i].records_read /
17927
 
                        examined_rows);
17928
 
          item_list.push_back(new Item_float(f, 2));
17929
 
        }
17930
 
      }
17931
 
 
17932
 
      /* Build "Extra" field and add it to item_list. */
17933
 
      bool key_read=table->key_read;
17934
 
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
17935
 
          table->covering_keys.is_set(tab->index))
17936
 
        key_read=1;
17937
 
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
17938
 
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
17939
 
        key_read=1;
17940
 
        
17941
 
      if (tab->info)
17942
 
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
17943
 
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
17944
 
      {
17945
 
        if (tab->packed_info & TAB_INFO_USING_INDEX)
17946
 
          extra.append(STRING_WITH_LEN("; Using index"));
17947
 
        if (tab->packed_info & TAB_INFO_USING_WHERE)
17948
 
          extra.append(STRING_WITH_LEN("; Using where"));
17949
 
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
17950
 
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
17951
 
        /* Skip initial "; "*/
17952
 
        const char *str= extra.ptr();
17953
 
        uint32_t len= extra.length();
17954
 
        if (len)
17955
 
        {
17956
 
          str += 2;
17957
 
          len -= 2;
17958
 
        }
17959
 
        item_list.push_back(new Item_string(str, len, cs));
17960
 
      }
17961
 
      else
17962
 
      {
17963
 
        uint keyno= MAX_KEY;
17964
 
        if (tab->ref.key_parts)
17965
 
          keyno= tab->ref.key;
17966
 
        else if (tab->select && tab->select->quick)
17967
 
          keyno = tab->select->quick->index;
17968
 
 
17969
 
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
17970
 
            table->file->pushed_idx_cond)
17971
 
          extra.append(STRING_WITH_LEN("; Using index condition"));
17972
 
 
17973
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
17974
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
17975
 
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
17976
 
        {
17977
 
          extra.append(STRING_WITH_LEN("; Using "));
17978
 
          tab->select->quick->add_info_string(&extra);
17979
 
        }
17980
 
          if (tab->select)
17981
 
        {
17982
 
          if (tab->use_quick == 2)
17983
 
          {
17984
 
            /* 4 bits per 1 hex digit + terminating '\0' */
17985
 
            char buf[MAX_KEY / 4 + 1];
17986
 
            extra.append(STRING_WITH_LEN("; Range checked for each "
17987
 
                                         "record (index map: 0x"));
17988
 
            extra.append(tab->keys.print(buf));
17989
 
            extra.append(')');
17990
 
          }
17991
 
          else if (tab->select->cond)
17992
 
          {
17993
 
            const COND *pushed_cond= tab->table->file->pushed_cond;
17994
 
 
17995
 
            if (thd->variables.engine_condition_pushdown && pushed_cond)
17996
 
            {
17997
 
              extra.append(STRING_WITH_LEN("; Using where with pushed "
17998
 
                                           "condition"));
17999
 
              if (thd->lex->describe & DESCRIBE_EXTENDED)
18000
 
              {
18001
 
                extra.append(STRING_WITH_LEN(": "));
18002
 
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
18003
 
              }
18004
 
            }
18005
 
            else
18006
 
              extra.append(STRING_WITH_LEN("; Using where"));
18007
 
          }
18008
 
        }
18009
 
        if (key_read)
18010
 
        {
18011
 
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
18012
 
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
18013
 
          else
18014
 
            extra.append(STRING_WITH_LEN("; Using index"));
18015
 
        }
18016
 
        if (table->reginfo.not_exists_optimize)
18017
 
          extra.append(STRING_WITH_LEN("; Not exists"));
18018
 
          
18019
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
18020
 
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
18021
 
             HA_MRR_USE_DEFAULT_IMPL))
18022
 
        {
18023
 
          extra.append(STRING_WITH_LEN("; Using MRR"));
18024
 
        }
18025
 
 
18026
 
        if (table_list->schema_table &&
18027
 
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18028
 
        {
18029
 
          if (!table_list->table_open_method)
18030
 
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
18031
 
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
18032
 
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
18033
 
          else
18034
 
            extra.append(STRING_WITH_LEN("; Open_full_table"));
18035
 
          if (table_list->has_db_lookup_value &&
18036
 
              table_list->has_table_lookup_value)
18037
 
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18038
 
          else if (table_list->has_db_lookup_value ||
18039
 
                   table_list->has_table_lookup_value)
18040
 
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18041
 
          else
18042
 
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
18043
 
        }
18044
 
        if (need_tmp_table)
18045
 
        {
18046
 
          need_tmp_table=0;
18047
 
          extra.append(STRING_WITH_LEN("; Using temporary"));
18048
 
        }
18049
 
        if (need_order)
18050
 
        {
18051
 
          need_order=0;
18052
 
          extra.append(STRING_WITH_LEN("; Using filesort"));
18053
 
        }
18054
 
        if (distinct & test_all_bits(used_tables,thd->used_tables))
18055
 
          extra.append(STRING_WITH_LEN("; Distinct"));
18056
 
 
18057
 
        if (tab->insideout_match_tab)
18058
 
        {
18059
 
          extra.append(STRING_WITH_LEN("; LooseScan"));
18060
 
        }
18061
 
 
18062
 
        if (tab->flush_weedout_table)
18063
 
          extra.append(STRING_WITH_LEN("; Start temporary"));
18064
 
        else if (tab->check_weed_out_table)
18065
 
          extra.append(STRING_WITH_LEN("; End temporary"));
18066
 
        else if (tab->do_firstmatch)
18067
 
        {
18068
 
          extra.append(STRING_WITH_LEN("; FirstMatch("));
18069
 
          TABLE *prev_table=tab->do_firstmatch->table;
18070
 
          if (prev_table->derived_select_number)
18071
 
          {
18072
 
            char namebuf[NAME_LEN];
18073
 
            /* Derived table name generation */
18074
 
            int len= snprintf(namebuf, sizeof(namebuf)-1,
18075
 
                              "<derived%u>",
18076
 
                              prev_table->derived_select_number);
18077
 
            extra.append(namebuf, len);
18078
 
          }
18079
 
          else
18080
 
            extra.append(prev_table->pos_in_table_list->alias);
18081
 
          extra.append(STRING_WITH_LEN(")"));
18082
 
        }
18083
 
 
18084
 
        for (uint part= 0; part < tab->ref.key_parts; part++)
18085
 
        {
18086
 
          if (tab->ref.cond_guards[part])
18087
 
          {
18088
 
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18089
 
            break;
18090
 
          }
18091
 
        }
18092
 
 
18093
 
        if (i > 0 && tab[-1].next_select == sub_select_cache)
18094
 
          extra.append(STRING_WITH_LEN("; Using join buffer"));
18095
 
 
18096
 
        /* Skip initial "; "*/
18097
 
        const char *str= extra.ptr();
18098
 
        uint32_t len= extra.length();
18099
 
        if (len)
18100
 
        {
18101
 
          str += 2;
18102
 
          len -= 2;
18103
 
        }
18104
 
        item_list.push_back(new Item_string(str, len, cs));
18105
 
      }
18106
 
      // For next iteration
18107
 
      used_tables|=table->map;
18108
 
      if (result->send_data(item_list))
18109
 
        join->error= 1;
18110
 
    }
18111
 
  }
18112
 
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18113
 
       unit;
18114
 
       unit= unit->next_unit())
18115
 
  {
18116
 
    if (mysql_explain_union(thd, unit, result))
18117
 
      return;
18118
 
  }
18119
 
  return;
18120
 
}
18121
 
 
18122
 
 
18123
 
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18124
 
{
18125
 
  bool res= 0;
18126
 
  SELECT_LEX *first= unit->first_select();
18127
 
 
18128
 
  for (SELECT_LEX *sl= first;
18129
 
       sl;
18130
 
       sl= sl->next_select())
18131
 
  {
18132
 
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18133
 
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18134
 
    sl->type= (((&thd->lex->select_lex)==sl)?
18135
 
               (sl->first_inner_unit() || sl->next_select() ? 
18136
 
                "PRIMARY" : "SIMPLE"):
18137
 
               ((sl == first)?
18138
 
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
18139
 
                 "DERIVED":
18140
 
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
18141
 
                  "DEPENDENT SUBQUERY":
18142
 
                  (uncacheable?"UNCACHEABLE SUBQUERY":
18143
 
                   "SUBQUERY"))):
18144
 
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
18145
 
                 "DEPENDENT UNION":
18146
 
                 uncacheable?"UNCACHEABLE UNION":
18147
 
                 "UNION")));
18148
 
    sl->options|= SELECT_DESCRIBE;
18149
 
  }
18150
 
  if (unit->is_union())
18151
 
  {
18152
 
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18153
 
    unit->fake_select_lex->type= "UNION RESULT";
18154
 
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
18155
 
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18156
 
      res= unit->exec();
18157
 
    res|= unit->cleanup();
18158
 
  }
18159
 
  else
18160
 
  {
18161
 
    thd->lex->current_select= first;
18162
 
    unit->set_limit(unit->global_parameters);
18163
 
    res= mysql_select(thd, &first->ref_pointer_array,
18164
 
                        (TABLE_LIST*) first->table_list.first,
18165
 
                        first->with_wild, first->item_list,
18166
 
                        first->where,
18167
 
                        first->order_list.elements +
18168
 
                        first->group_list.elements,
18169
 
                        (ORDER*) first->order_list.first,
18170
 
                        (ORDER*) first->group_list.first,
18171
 
                        first->having,
18172
 
                        (ORDER*) thd->lex->proc_list.first,
18173
 
                        first->options | thd->options | SELECT_DESCRIBE,
18174
 
                        result, unit, first);
18175
 
  }
18176
 
  return(res || thd->is_error());
18177
 
}
18178
 
 
18179
 
 
18180
 
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
18181
 
                              TABLE_LIST **end)
18182
 
{
18183
 
  (*table)->print(thd, str, QT_ORDINARY);
18184
 
 
18185
 
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18186
 
  {
18187
 
    TABLE_LIST *curr= *tbl;
 
6541
static void print_table_array(Session *session, String *str, TableList **table,
 
6542
                              TableList **end)
 
6543
{
 
6544
  (*table)->print(session, str, QT_ORDINARY);
 
6545
 
 
6546
  for (TableList **tbl= table + 1; tbl < end; tbl++)
 
6547
  {
 
6548
    TableList *curr= *tbl;
18188
6549
    if (curr->outer_join)
18189
6550
    {
18190
6551
      /* MySQL converts right to left joins */
18192
6553
    }
18193
6554
    else if (curr->straight)
18194
6555
      str->append(STRING_WITH_LEN(" straight_join "));
18195
 
    else if (curr->sj_inner_tables)
18196
 
      str->append(STRING_WITH_LEN(" semi join "));
18197
6556
    else
18198
6557
      str->append(STRING_WITH_LEN(" join "));
18199
 
    curr->print(thd, str, QT_ORDINARY);
 
6558
    curr->print(session, str, QT_ORDINARY);
18200
6559
    if (curr->on_expr)
18201
6560
    {
18202
6561
      str->append(STRING_WITH_LEN(" on("));
18206
6565
  }
18207
6566
}
18208
6567
 
18209
 
 
18210
6568
/**
18211
6569
  Print joins from the FROM clause.
18212
 
  @param thd     thread handler
 
6570
  @param session     thread Cursor
18213
6571
  @param str     string where table should be printed
18214
6572
  @param tables  list of tables in join
18215
6573
  @query_type    type of the query is being generated
18216
6574
*/
18217
 
 
18218
 
static void print_join(THD *thd,
18219
 
                       String *str,
18220
 
                       List<TABLE_LIST> *tables,
18221
 
                       enum_query_type query_type __attribute__((unused)))
 
6575
void print_join(Session *session, String *str,
 
6576
                List<TableList> *tables, enum_query_type)
18222
6577
{
18223
6578
  /* List is reversed => we should reverse it before using */
18224
 
  List_iterator_fast<TABLE_LIST> ti(*tables);
18225
 
  TABLE_LIST **table= (TABLE_LIST **)thd->alloc(sizeof(TABLE_LIST*) *
 
6579
  List_iterator_fast<TableList> ti(*tables);
 
6580
  TableList **table= (TableList **)session->alloc(sizeof(TableList*) *
18226
6581
                                                tables->elements);
18227
6582
  if (table == 0)
18228
6583
    return;  // out of memory
18229
6584
 
18230
 
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
 
6585
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
18231
6586
    *t= ti++;
18232
 
  
18233
 
  /* 
18234
 
    If the first table is a semi-join nest, swap it with something that is
18235
 
    not a semi-join nest.
18236
 
  */
18237
 
  if ((*table)->sj_inner_tables)
18238
 
  {
18239
 
    TABLE_LIST **end= table + tables->elements;
18240
 
    for (TABLE_LIST **t2= table; t2!=end; t2++)
18241
 
    {
18242
 
      if (!(*t2)->sj_inner_tables)
18243
 
      {
18244
 
        TABLE_LIST *tmp= *t2;
18245
 
        *t2= *table;
18246
 
        *table= tmp;
18247
 
        break;
18248
 
      }
18249
 
    }
18250
 
  }
18251
6587
  assert(tables->elements >= 1);
18252
 
  print_table_array(thd, str, table, table + tables->elements);
18253
 
}
18254
 
 
18255
 
 
18256
 
/**
18257
 
  @brief Print an index hint
18258
 
 
18259
 
  @details Prints out the USE|FORCE|IGNORE index hint.
18260
 
 
18261
 
  @param      thd         the current thread
18262
 
  @param[out] str         appends the index hint here
18263
 
  @param      hint        what the hint is (as string : "USE INDEX"|
18264
 
                          "FORCE INDEX"|"IGNORE INDEX")
18265
 
  @param      hint_length the length of the string in 'hint'
18266
 
  @param      indexes     a list of index names for the hint
18267
 
*/
18268
 
 
18269
 
void 
18270
 
Index_hint::print(THD *thd, String *str)
18271
 
{
18272
 
  switch (type)
18273
 
  {
18274
 
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
18275
 
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
18276
 
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
18277
 
  }
18278
 
  str->append (STRING_WITH_LEN(" ("));
18279
 
  if (key_name.length)
18280
 
  {
18281
 
    if (thd && !my_strnncoll(system_charset_info,
18282
 
                             (const uchar *)key_name.str, key_name.length, 
18283
 
                             (const uchar *)primary_key_name, 
18284
 
                             strlen(primary_key_name)))
18285
 
      str->append(primary_key_name);
18286
 
    else
18287
 
      append_identifier(thd, str, key_name.str, key_name.length);
18288
 
  }
18289
 
  str->append(')');
18290
 
}
18291
 
 
18292
 
 
18293
 
/**
18294
 
  Print table as it should be in join list.
18295
 
 
18296
 
  @param str   string where table should be printed
18297
 
*/
18298
 
 
18299
 
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
18300
 
{
18301
 
  if (nested_join)
18302
 
  {
18303
 
    str->append('(');
18304
 
    print_join(thd, str, &nested_join->join_list, query_type);
18305
 
    str->append(')');
18306
 
  }
18307
 
  else
18308
 
  {
18309
 
    const char *cmp_name;                         // Name to compare with alias
18310
 
    if (derived)
18311
 
    {
18312
 
      // A derived table
18313
 
      str->append('(');
18314
 
      derived->print(str, query_type);
18315
 
      str->append(')');
18316
 
      cmp_name= "";                               // Force printing of alias
18317
 
    }
18318
 
    else
18319
 
    {
18320
 
      // A normal table
18321
 
      {
18322
 
        append_identifier(thd, str, db, db_length);
18323
 
        str->append('.');
18324
 
      }
18325
 
      if (schema_table)
18326
 
      {
18327
 
        append_identifier(thd, str, schema_table_name,
18328
 
                          strlen(schema_table_name));
18329
 
        cmp_name= schema_table_name;
18330
 
      }
18331
 
      else
18332
 
      {
18333
 
        append_identifier(thd, str, table_name, table_name_length);
18334
 
        cmp_name= table_name;
18335
 
      }
18336
 
    }
18337
 
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
18338
 
    {
18339
 
      char t_alias_buff[MAX_ALIAS_NAME];
18340
 
      const char *t_alias= alias;
18341
 
 
18342
 
      str->append(' ');
18343
 
      if (lower_case_table_names== 1)
18344
 
      {
18345
 
        if (alias && alias[0])
18346
 
        {
18347
 
          strmov(t_alias_buff, alias);
18348
 
          my_casedn_str(files_charset_info, t_alias_buff);
18349
 
          t_alias= t_alias_buff;
18350
 
        }
18351
 
      }
18352
 
 
18353
 
      append_identifier(thd, str, t_alias, strlen(t_alias));
18354
 
    }
18355
 
 
18356
 
    if (index_hints)
18357
 
    {
18358
 
      List_iterator<Index_hint> it(*index_hints);
18359
 
      Index_hint *hint;
18360
 
 
18361
 
      while ((hint= it++))
18362
 
      {
18363
 
        str->append (STRING_WITH_LEN(" "));
18364
 
        hint->print (thd, str);
18365
 
      }
18366
 
    }
18367
 
  }
18368
 
}
18369
 
 
18370
 
 
18371
 
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
18372
 
{
18373
 
  /* QQ: thd may not be set for sub queries, but this should be fixed */
18374
 
  if (!thd)
18375
 
    thd= current_thd;
 
6588
  print_table_array(session, str, table, table + tables->elements);
 
6589
}
 
6590
 
 
6591
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
 
6592
{
 
6593
  /* QQ: session may not be set for sub queries, but this should be fixed */
 
6594
  if (!session)
 
6595
    session= current_session;
18376
6596
 
18377
6597
  str->append(STRING_WITH_LEN("select "));
18378
6598
 
18379
6599
  /* First add options */
18380
6600
  if (options & SELECT_STRAIGHT_JOIN)
18381
6601
    str->append(STRING_WITH_LEN("straight_join "));
18382
 
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
18383
 
      (this == &thd->lex->select_lex))
18384
 
    str->append(STRING_WITH_LEN("high_priority "));
18385
6602
  if (options & SELECT_DISTINCT)
18386
6603
    str->append(STRING_WITH_LEN("distinct "));
18387
6604
  if (options & SELECT_SMALL_RESULT)
18414
6631
  {
18415
6632
    str->append(STRING_WITH_LEN(" from "));
18416
6633
    /* go through join tree */
18417
 
    print_join(thd, str, &top_join_list, query_type);
 
6634
    print_join(session, str, &top_join_list, query_type);
18418
6635
  }
18419
6636
  else if (where)
18420
6637
  {
18421
6638
    /*
18422
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
 
6639
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
18423
6640
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
18424
6641
    */
18425
6642
    str->append(STRING_WITH_LEN(" from DUAL "));
18442
6659
  if (group_list.elements)
18443
6660
  {
18444
6661
    str->append(STRING_WITH_LEN(" group by "));
18445
 
    print_order(str, (ORDER *) group_list.first, query_type);
 
6662
    print_order(str, (order_st *) group_list.first, query_type);
18446
6663
    switch (olap)
18447
6664
    {
18448
6665
      case CUBE_TYPE:
18473
6690
  if (order_list.elements)
18474
6691
  {
18475
6692
    str->append(STRING_WITH_LEN(" order by "));
18476
 
    print_order(str, (ORDER *) order_list.first, query_type);
 
6693
    print_order(str, (order_st *) order_list.first, query_type);
18477
6694
  }
18478
6695
 
18479
6696
  // limit
18480
 
  print_limit(thd, str, query_type);
 
6697
  print_limit(session, str, query_type);
18481
6698
 
18482
6699
  // PROCEDURE unsupported here
18483
6700
}
18484
6701
 
18485
 
 
18486
 
/**
18487
 
  change select_result object of JOIN.
18488
 
 
18489
 
  @param res            new select_result object
18490
 
 
18491
 
  @retval
18492
 
    false   OK
18493
 
  @retval
18494
 
    true    error
18495
 
*/
18496
 
 
18497
 
bool JOIN::change_result(select_result *res)
18498
 
{
18499
 
  result= res;
18500
 
  if (result->prepare(fields_list, select_lex->master_unit()) ||
18501
 
                     result->prepare2())
18502
 
  {
18503
 
    return(true);
18504
 
  }
18505
 
  return(false);
18506
 
}
18507
 
 
18508
6702
/**
18509
6703
  @} (end of group Query_Optimizer)
18510
6704
*/