~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2009-12-29 01:38:38 UTC
  • mfrom: (1251.1.1 drizzle)
  • Revision ID: brian@gaz-20091229013838-03kb2z5xbqw03ddt
Merge of Diego fix.

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
 
#include <drizzled/server_includes.h>
27
 
#include <drizzled/sql_select.h>
28
 
 
29
 
#include <mysys/my_bit.h>
30
 
#include <drizzled/drizzled_error_messages.h>
31
 
 
32
 
const char *join_type_str[]={ "UNKNOWN","system","const","eq_ref","ref",
33
 
                              "MAYBE_REF","ALL","range","index",
34
 
                              "ref_or_null","unique_subquery","index_subquery",
35
 
                              "index_merge"
36
 
};
37
 
 
38
 
struct st_sargable_param;
39
 
 
40
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
41
 
static bool make_join_statistics(JOIN *join, TABLE_LIST *leaves, COND *conds,
42
 
                                 DYNAMIC_ARRAY *keyuse);
43
 
static bool update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,
44
 
                                JOIN_TAB *join_tab,
45
 
                                uint tables, COND *conds,
46
 
                                COND_EQUAL *cond_equal,
47
 
                                table_map table_map, SELECT_LEX *select_lex,
48
 
                                st_sargable_param **sargables);
49
 
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
50
 
static void set_position(JOIN *join,uint index,JOIN_TAB *table,KEYUSE *key);
51
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
52
 
                               table_map used_tables);
53
 
static bool choose_plan(JOIN *join,table_map join_tables);
54
 
 
55
 
static void best_access_path(JOIN *join, JOIN_TAB *s, THD *thd,
56
 
                             table_map remaining_tables, uint idx,
57
 
                             double record_count, double read_time);
58
 
static void optimize_straight_join(JOIN *join, table_map join_tables);
59
 
static bool greedy_search(JOIN *join, table_map remaining_tables,
60
 
                             uint depth, uint prune_level);
61
 
static bool best_extension_by_limited_search(JOIN *join,
62
 
                                             table_map remaining_tables,
63
 
                                             uint idx, double record_count,
64
 
                                             double read_time, uint depth,
65
 
                                             uint prune_level);
66
 
static uint determine_search_depth(JOIN* join);
67
 
static int join_tab_cmp(const void* ptr1, const void* ptr2);
68
 
static int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
69
 
/*
70
 
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
71
 
  tested and approved.
72
 
*/
73
 
static bool find_best(JOIN *join,table_map rest_tables,uint index,
74
 
                      double record_count,double read_time);
75
 
static uint cache_record_length(JOIN *join,uint index);
76
 
static double prev_record_reads(JOIN *join, uint idx, table_map found_ref);
77
 
static bool get_best_combination(JOIN *join);
78
 
static store_key *get_store_key(THD *thd,
79
 
                                KEYUSE *keyuse, table_map used_tables,
80
 
                                KEY_PART_INFO *key_part, uchar *key_buff,
81
 
                                uint maybe_null);
82
 
static bool make_simple_join(JOIN *join,TABLE *tmp_table);
83
 
static void make_outerjoin_info(JOIN *join);
84
 
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
85
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after);
86
 
static bool only_eq_ref_tables(JOIN *join, ORDER *order, table_map tables);
87
 
static void update_depend_map(JOIN *join);
88
 
static void update_depend_map(JOIN *join, ORDER *order);
89
 
static ORDER *remove_const(JOIN *join,ORDER *first_order,COND *cond,
90
 
                           bool change_list, bool *simple_order);
91
 
static int return_zero_rows(JOIN *join, select_result *res,TABLE_LIST *tables,
92
 
                            List<Item> &fields, bool send_row,
93
 
                            uint64_t select_options, const char *info,
94
 
                            Item *having);
95
 
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,
96
72
                               COND_EQUAL *inherited,
97
 
                               List<TABLE_LIST> *join_list,
 
73
                               List<TableList> *join_list,
98
74
                               COND_EQUAL **cond_equal_ref);
99
 
static COND* substitute_for_best_equal_field(COND *cond,
100
 
                                             COND_EQUAL *cond_equal,
101
 
                                             void *table_join_idx);
102
 
static COND *simplify_joins(JOIN *join, List<TABLE_LIST> *join_list,
103
 
                            COND *conds, bool top, bool in_sj);
104
 
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
105
 
static void restore_prev_nj_state(JOIN_TAB *last);
106
 
static void reset_nj_counters(List<TABLE_LIST> *join_list);
107
 
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list,
108
 
                                          uint first_unused);
109
 
 
110
 
static 
111
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
112
 
static void restore_prev_sj_state(const table_map remaining_tables, 
113
 
                                  const JOIN_TAB *tab);
114
 
 
115
 
static COND *optimize_cond(JOIN *join, COND *conds,
116
 
                           List<TABLE_LIST> *join_list,
117
 
                           Item::cond_result *cond_value);
118
 
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
119
 
static bool open_tmp_table(TABLE *table);
120
 
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
121
 
                                    MI_COLUMNDEF *start_recinfo,
122
 
                                    MI_COLUMNDEF **recinfo,
123
 
                                    uint64_t options);
124
 
static int do_select(JOIN *join,List<Item> *fields,TABLE *tmp_table);
125
 
 
126
 
static enum_nested_loop_state
127
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
128
 
                     int error);
129
 
static enum_nested_loop_state
130
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
131
 
static enum_nested_loop_state
132
 
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
133
 
static enum_nested_loop_state
134
 
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
135
 
static enum_nested_loop_state
136
 
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
137
 
static enum_nested_loop_state
138
 
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
139
 
static enum_nested_loop_state
140
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
141
 
 
142
 
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
143
 
static int join_read_system(JOIN_TAB *tab);
144
 
static int join_read_const(JOIN_TAB *tab);
145
 
static int join_read_key(JOIN_TAB *tab);
146
 
static int join_read_always_key(JOIN_TAB *tab);
147
 
static int join_read_last_key(JOIN_TAB *tab);
148
 
static int join_no_more_records(READ_RECORD *info);
149
 
static int join_read_next(READ_RECORD *info);
150
 
static int join_read_next_different(READ_RECORD *info);
151
 
static int join_init_quick_read_record(JOIN_TAB *tab);
152
 
static int test_if_quick_select(JOIN_TAB *tab);
153
 
static int join_init_read_record(JOIN_TAB *tab);
154
 
static int join_read_first(JOIN_TAB *tab);
155
 
static int join_read_next_same(READ_RECORD *info);
156
 
static int join_read_next_same_diff(READ_RECORD *info);
157
 
static int join_read_last(JOIN_TAB *tab);
158
 
static int join_read_prev_same(READ_RECORD *info);
159
 
static int join_read_prev(READ_RECORD *info);
160
 
int join_read_always_key_or_null(JOIN_TAB *tab);
161
 
int join_read_next_same_or_null(READ_RECORD *info);
162
 
static COND *make_cond_for_table(COND *cond,table_map table,
163
 
                                 table_map used_table,
164
 
                                 bool exclude_expensive_cond);
165
 
static Item* part_of_refkey(TABLE *form,Field *field);
166
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,
167
 
                                    ha_rows select_limit, bool no_changes,
168
 
                                    const key_map *map);
169
 
static bool list_contains_unique_index(TABLE *table,
170
 
                          bool (*find_func) (Field *, void *), void *data);
171
 
static bool find_field_in_item_list (Field *field, void *data);
172
 
static bool find_field_in_order_list (Field *field, void *data);
173
 
static int create_sort_index(THD *thd, JOIN *join, ORDER *order,
174
 
                             ha_rows filesort_limit, ha_rows select_limit,
175
 
                             bool is_order_by);
176
 
static int remove_duplicates(JOIN *join,TABLE *entry,List<Item> &fields,
177
 
                             Item *having);
178
 
static int remove_dup_with_compare(THD *thd, TABLE *entry, Field **field,
179
 
                                   ulong offset,Item *having);
180
 
static int remove_dup_with_hash_index(THD *thd,TABLE *table,
181
 
                                      uint field_count, Field **first_field,
182
 
 
183
 
                                      ulong key_length,Item *having);
184
 
static int join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count);
185
 
static ulong used_blob_length(CACHE_FIELD **ptr);
186
 
static bool store_record_in_cache(JOIN_CACHE *cache);
187
 
static void reset_cache_read(JOIN_CACHE *cache);
188
 
static void reset_cache_write(JOIN_CACHE *cache);
189
 
static void read_cached_record(JOIN_TAB *tab);
190
 
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
191
 
static ORDER *create_distinct_group(THD *thd, Item **ref_pointer_array,
192
 
                                    ORDER *order, List<Item> &fields,
193
 
                                    List<Item> &all_fields,
194
 
                                    bool *all_order_by_fields_used);
195
 
static bool test_if_subpart(ORDER *a,ORDER *b);
196
 
static TABLE *get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables);
197
 
static void calc_group_buffer(JOIN *join,ORDER *group);
198
 
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
199
 
static bool alloc_group_fields(JOIN *join,ORDER *group);
200
 
// Create list for using with tempory table
201
 
static bool change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
202
 
                                     List<Item> &new_list1,
203
 
                                     List<Item> &new_list2,
204
 
                                     uint elements, List<Item> &items);
205
 
// Create list for using with tempory table
206
 
static bool change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
207
 
                                      List<Item> &new_list1,
208
 
                                      List<Item> &new_list2,
209
 
                                      uint elements, List<Item> &items);
210
 
static void init_tmptable_sum_functions(Item_sum **func);
211
 
static void update_tmptable_sum_func(Item_sum **func,TABLE *tmp_table);
212
 
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
213
 
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab);
214
 
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr);
215
 
static bool init_sum_functions(Item_sum **func, Item_sum **end);
216
 
static bool update_sum_func(Item_sum **func);
217
 
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
218
 
                            bool distinct, const char *message=NullS);
219
 
static Item *remove_additional_cond(Item* conds);
220
 
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
221
 
static bool test_if_ref(Item_field *left_item,Item *right_item);
222
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
223
 
                                       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
}
224
90
 
225
91
/*
226
92
  This is used to mark equalities that were made from i-th IN-equality.
230
96
const char *subq_sj_cond_name=
231
97
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
232
98
 
233
 
static bool bitmap_covers(const table_map x, const table_map y)
 
99
static bool copy_blobs(Field **ptr)
234
100
{
235
 
  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;
236
108
}
237
109
 
238
110
/**
239
111
  This handles SELECT with and without UNION.
240
112
*/
241
 
 
242
 
bool handle_select(THD *thd, LEX *lex, select_result *result,
243
 
                   ulong setup_tables_done_option)
 
113
bool handle_select(Session *session, LEX *lex, select_result *result,
 
114
                   uint64_t setup_tables_done_option)
244
115
{
245
116
  bool res;
246
 
  register SELECT_LEX *select_lex = &lex->select_lex;
247
 
  DRIZZLE_SELECT_START();
 
117
  register Select_Lex *select_lex= &lex->select_lex;
 
118
  DRIZZLE_SELECT_START(session->query);
248
119
 
249
 
  if (select_lex->master_unit()->is_union() || 
 
120
  if (select_lex->master_unit()->is_union() ||
250
121
      select_lex->master_unit()->fake_select_lex)
251
 
    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);
252
124
  else
253
125
  {
254
 
    SELECT_LEX_UNIT *unit= &lex->unit;
 
126
    Select_Lex_Unit *unit= &lex->unit;
255
127
    unit->set_limit(unit->global_parameters);
256
 
    thd->thd_marker= 0;
 
128
    session->session_marker= 0;
257
129
    /*
258
130
      'options' of mysql_select will be set in JOIN, as far as JOIN for
259
 
      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
260
132
      setup_tables_done_option changed for next rexecution
261
133
    */
262
 
    res= mysql_select(thd, &select_lex->ref_pointer_array,
263
 
                      (TABLE_LIST*) select_lex->table_list.first,
 
134
    res= mysql_select(session, &select_lex->ref_pointer_array,
 
135
                      (TableList*) select_lex->table_list.first,
264
136
                      select_lex->with_wild, select_lex->item_list,
265
137
                      select_lex->where,
266
138
                      select_lex->order_list.elements +
267
139
                      select_lex->group_list.elements,
268
 
                      (ORDER*) select_lex->order_list.first,
269
 
                      (ORDER*) select_lex->group_list.first,
 
140
                      (order_st*) select_lex->order_list.first,
 
141
                      (order_st*) select_lex->group_list.first,
270
142
                      select_lex->having,
271
 
                      (ORDER*) lex->proc_list.first,
272
 
                      select_lex->options | thd->options |
 
143
                      select_lex->options | session->options |
273
144
                      setup_tables_done_option,
274
145
                      result, unit, select_lex);
275
146
  }
276
 
  res|= thd->is_error();
 
147
  res|= session->is_error();
277
148
  if (unlikely(res))
278
149
    result->abort();
279
150
 
280
 
  DRIZZLE_SELECT_END();
281
 
  return(res);
 
151
  DRIZZLE_SELECT_DONE(res, session->limit_found_rows);
 
152
  return res;
282
153
}
283
154
 
284
 
 
285
155
/*
286
156
  Fix fields referenced from inner selects.
287
157
 
288
158
  SYNOPSIS
289
159
    fix_inner_refs()
290
 
    thd               Thread handle
 
160
    session               Thread handle
291
161
    all_fields        List of all fields used in select
292
162
    select            Current select
293
163
    ref_pointer_array Array of references to Items used in current select
322
192
    true  an error occured
323
193
    false ok
324
194
*/
325
 
 
326
 
bool
327
 
fix_inner_refs(THD *thd, List<Item> &all_fields, SELECT_LEX *select,
328
 
                 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)
329
199
{
330
200
  Item_outer_ref *ref;
331
201
  bool res= false;
385
255
    ref->outer_ref= new_ref;
386
256
    ref->ref= &ref->outer_ref;
387
257
 
388
 
    if (!ref->fixed && ref->fix_fields(thd, 0))
 
258
    if (!ref->fixed && ref->fix_fields(session, 0))
389
259
      return true;
390
 
    thd->used_tables|= item->used_tables();
 
260
    session->used_tables|= item->used_tables();
391
261
  }
392
262
  return res;
393
263
}
394
264
 
395
 
#define MAGIC_IN_WHERE_TOP_LEVEL 10
396
 
/**
397
 
  Function to setup clauses without sum functions.
398
 
*/
399
 
inline int setup_without_group(THD *thd, Item **ref_pointer_array,
400
 
                               TABLE_LIST *tables,
401
 
                               TABLE_LIST *leaves,
402
 
                               List<Item> &fields,
403
 
                               List<Item> &all_fields,
404
 
                               COND **conds,
405
 
                               ORDER *order,
406
 
                               ORDER *group, bool *hidden_group_fields)
407
 
{
408
 
  int res;
409
 
  nesting_map save_allow_sum_func=thd->lex->allow_sum_func ;
410
 
 
411
 
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
412
 
  res= setup_conds(thd, tables, leaves, conds);
413
 
 
414
 
  thd->lex->allow_sum_func|= 1 << thd->lex->current_select->nest_level;
415
 
  res= res || setup_order(thd, ref_pointer_array, tables, fields, all_fields,
416
 
                          order);
417
 
  thd->lex->allow_sum_func&= ~(1 << thd->lex->current_select->nest_level);
418
 
  res= res || setup_group(thd, ref_pointer_array, tables, fields, all_fields,
419
 
                          group, hidden_group_fields);
420
 
  thd->lex->allow_sum_func= save_allow_sum_func;
421
 
  return(res);
422
 
}
423
 
 
424
265
/*****************************************************************************
425
266
  Check fields, find best join, do the select and output fields.
426
267
  mysql_select assumes that all tables are already opened
427
268
*****************************************************************************/
428
269
 
429
 
/**
430
 
  Prepare of whole select (including sub queries in future).
431
 
 
432
 
  @todo
433
 
    Add check of calculation of GROUP functions and fields:
434
 
    SELECT COUNT(*)+table.col1 from table1;
435
 
 
436
 
  @retval
437
 
    -1   on error
438
 
  @retval
439
 
    0   on success
440
 
*/
441
 
int
442
 
JOIN::prepare(Item ***rref_pointer_array,
443
 
              TABLE_LIST *tables_init,
444
 
              uint wild_num, COND *conds_init, uint og_num,
445
 
              ORDER *order_init, ORDER *group_init,
446
 
              Item *having_init,
447
 
              ORDER *proc_param_init, SELECT_LEX *select_lex_arg,
448
 
              SELECT_LEX_UNIT *unit_arg)
449
 
{
450
 
  // to prevent double initialization on EXPLAIN
451
 
  if (optimized)
452
 
    return(0);
453
 
 
454
 
  conds= conds_init;
455
 
  order= order_init;
456
 
  group_list= group_init;
457
 
  having= having_init;
458
 
  proc_param= proc_param_init;
459
 
  tables_list= tables_init;
460
 
  select_lex= select_lex_arg;
461
 
  select_lex->join= this;
462
 
  join_list= &select_lex->top_join_list;
463
 
  union_part= unit_arg->is_union();
464
 
 
465
 
  thd->lex->current_select->is_item_list_lookup= 1;
466
 
  /*
467
 
    If we have already executed SELECT, then it have not sense to prevent
468
 
    its table from update (see unique_table())
469
 
  */
470
 
  if (thd->derived_tables_processing)
471
 
    select_lex->exclude_from_table_unique_test= true;
472
 
 
473
 
  /* Check that all tables, fields, conds and order are ok */
474
 
 
475
 
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
476
 
      setup_tables_and_check_access(thd, &select_lex->context, join_list,
477
 
                                    tables_list, &select_lex->leaf_tables,
478
 
                                    false))
479
 
      return(-1);
480
 
 
481
 
  TABLE_LIST *table_ptr;
482
 
  for (table_ptr= select_lex->leaf_tables;
483
 
       table_ptr;
484
 
       table_ptr= table_ptr->next_leaf)
485
 
    tables++;
486
 
 
487
 
  if (setup_wild(thd, tables_list, fields_list, &all_fields, wild_num) ||
488
 
      select_lex->setup_ref_array(thd, og_num) ||
489
 
      setup_fields(thd, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
490
 
                   &all_fields, 1) ||
491
 
      setup_without_group(thd, (*rref_pointer_array), tables_list,
492
 
                          select_lex->leaf_tables, fields_list,
493
 
                          all_fields, &conds, order, group_list,
494
 
                          &hidden_group_fields))
495
 
    return(-1);                         /* purecov: inspected */
496
 
 
497
 
  ref_pointer_array= *rref_pointer_array;
498
 
  
499
 
  if (having)
500
 
  {
501
 
    nesting_map save_allow_sum_func= thd->lex->allow_sum_func;
502
 
    thd->where="having clause";
503
 
    thd->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
504
 
    select_lex->having_fix_field= 1;
505
 
    bool having_fix_rc= (!having->fixed &&
506
 
                         (having->fix_fields(thd, &having) ||
507
 
                          having->check_cols(1)));
508
 
    select_lex->having_fix_field= 0;
509
 
    if (having_fix_rc || thd->is_error())
510
 
      return(-1);                               /* purecov: inspected */
511
 
    thd->lex->allow_sum_func= save_allow_sum_func;
512
 
  }
513
 
 
514
 
  {
515
 
    Item_subselect *subselect;
516
 
    Item_in_subselect *in_subs= NULL;
517
 
    /*
518
 
      Are we in a subquery predicate?
519
 
      TODO: the block below will be executed for every PS execution without need.
520
 
    */
521
 
    if ((subselect= select_lex->master_unit()->item))
522
 
    {
523
 
      bool do_semijoin= !test(thd->variables.optimizer_switch &
524
 
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
525
 
      if (subselect->substype() == Item_subselect::IN_SUBS)
526
 
        in_subs= (Item_in_subselect*)subselect;
527
 
 
528
 
      /*
529
 
        Check if we're in subquery that is a candidate for flattening into a
530
 
        semi-join (which is done done in flatten_subqueries()). The
531
 
        requirements are:
532
 
          1. Subquery predicate is an IN/=ANY subq predicate
533
 
          2. Subquery is a single SELECT (not a UNION)
534
 
          3. Subquery does not have GROUP BY or ORDER BY
535
 
          4. Subquery does not use aggregate functions or HAVING
536
 
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
537
 
          6. No execution method was already chosen (by a prepared statement).
538
 
 
539
 
          (*). We are not in a subquery of a single table UPDATE/DELETE that 
540
 
               doesn't have a JOIN (TODO: We should handle this at some
541
 
               point by switching to multi-table UPDATE/DELETE)
542
 
 
543
 
          (**). We're not in a confluent table-less subquery, like
544
 
                "SELECT 1". 
545
 
      */
546
 
      if (in_subs &&                                                    // 1
547
 
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
548
 
          !select_lex->group_list.elements && !order &&                 // 3
549
 
          !having && !select_lex->with_sum_func &&                      // 4
550
 
          thd->thd_marker &&                                            // 5
551
 
          select_lex->outer_select()->join &&                           // (*)
552
 
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**) 
553
 
          do_semijoin &&
554
 
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
555
 
      {
556
 
        {
557
 
          if (!in_subs->left_expr->fixed &&
558
 
               in_subs->left_expr->fix_fields(thd, &in_subs->left_expr))
559
 
          {
560
 
            return(-1);
561
 
          }
562
 
          /*
563
 
            Check that the right part of the subselect contains no more than one
564
 
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
565
 
          */
566
 
          if (subselect->substype() == Item_subselect::IN_SUBS &&
567
 
             (select_lex->item_list.elements != 
568
 
              ((Item_in_subselect*)subselect)->left_expr->cols()))
569
 
          {
570
 
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
571
 
            return(-1);
572
 
          }
573
 
        }
574
 
 
575
 
        /* Register the subquery for further processing */
576
 
        select_lex->outer_select()->join->sj_subselects.append(thd->mem_root, in_subs);
577
 
        in_subs->expr_join_nest= (TABLE_LIST*)thd->thd_marker;
578
 
      }
579
 
      else
580
 
      {
581
 
        bool do_materialize= !test(thd->variables.optimizer_switch &
582
 
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
583
 
        /*
584
 
          Check if the subquery predicate can be executed via materialization.
585
 
          The required conditions are:
586
 
          1. Subquery predicate is an IN/=ANY subq predicate
587
 
          2. Subquery is a single SELECT (not a UNION)
588
 
          3. Subquery is not a table-less query. In this case there is no
589
 
             point in materializing.
590
 
          4. Subquery predicate is a top-level predicate
591
 
             (this implies it is not negated)
592
 
             TODO: this is a limitation that should be lifeted once we
593
 
             implement correct NULL semantics (WL#3830)
594
 
          5. Subquery is non-correlated
595
 
             TODO:
596
 
             This is an overly restrictive condition. It can be extended to:
597
 
             (Subquery is non-correlated ||
598
 
              Subquery is correlated to any query outer to IN predicate ||
599
 
              (Subquery is correlated to the immediate outer query &&
600
 
               Subquery !contains {GROUP BY, ORDER BY [LIMIT],
601
 
               aggregate functions) && subquery predicate is not under "NOT IN"))
602
 
          6. No execution method was already chosen (by a prepared statement).
603
 
 
604
 
          (*) The subquery must be part of a SELECT statement. The current
605
 
               condition also excludes multi-table update statements.
606
 
 
607
 
          We have to determine whether we will perform subquery materialization
608
 
          before calling the IN=>EXISTS transformation, so that we know whether to
609
 
          perform the whole transformation or only that part of it which wraps
610
 
          Item_in_subselect in an Item_in_optimizer.
611
 
        */
612
 
        if (do_materialize && 
613
 
            in_subs  &&                                                   // 1
614
 
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
615
 
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
616
 
            thd->lex->sql_command == SQLCOM_SELECT)                       // *
617
 
        {
618
 
          if (in_subs->is_top_level_item() &&                             // 4
619
 
              !in_subs->is_correlated &&                                  // 5
620
 
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
621
 
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
622
 
        }
623
 
 
624
 
        Item_subselect::trans_res trans_res;
625
 
        if ((trans_res= subselect->select_transformer(this)) !=
626
 
            Item_subselect::RES_OK)
627
 
        {
628
 
          return((trans_res == Item_subselect::RES_ERROR));
629
 
        }
630
 
      }
631
 
    }
632
 
  }
633
 
 
634
 
  if (order)
635
 
  {
636
 
    ORDER *ord;
637
 
    for (ord= order; ord; ord= ord->next)
638
 
    {
639
 
      Item *item= *ord->item;
640
 
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
641
 
        item->split_sum_func(thd, ref_pointer_array, all_fields);
642
 
    }
643
 
  }
644
 
 
645
 
  if (having && having->with_sum_func)
646
 
    having->split_sum_func2(thd, ref_pointer_array, all_fields,
647
 
                            &having, true);
648
 
  if (select_lex->inner_sum_func_list)
649
 
  {
650
 
    Item_sum *end=select_lex->inner_sum_func_list;
651
 
    Item_sum *item_sum= end;  
652
 
    do
653
 
    { 
654
 
      item_sum= item_sum->next;
655
 
      item_sum->split_sum_func2(thd, ref_pointer_array,
656
 
                                all_fields, item_sum->ref_by, false);
657
 
    } while (item_sum != end);
658
 
  }
659
 
 
660
 
  if (select_lex->inner_refs_list.elements &&
661
 
      fix_inner_refs(thd, all_fields, select_lex, ref_pointer_array))
662
 
    return(-1);
663
 
 
664
 
  /*
665
 
    Check if there are references to un-aggregated columns when computing 
666
 
    aggregate functions with implicit grouping (there is no GROUP BY).
667
 
 
668
 
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
669
 
  */
670
 
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
671
 
  {
672
 
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
673
 
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
674
 
    return(-1);
675
 
  }
676
 
  {
677
 
    /* Caclulate the number of groups */
678
 
    send_group_parts= 0;
679
 
    for (ORDER *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
680
 
      send_group_parts++;
681
 
  }
682
 
  
683
 
  if (error)
684
 
    goto err;                                   /* purecov: inspected */
685
 
 
686
 
  if (result && result->prepare(fields_list, unit_arg))
687
 
    goto err;                                   /* purecov: inspected */
688
 
 
689
 
  /* Init join struct */
690
 
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
691
 
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
692
 
  this->group= group_list != 0;
693
 
  unit= unit_arg;
694
 
 
695
 
#ifdef RESTRICTED_GROUP
696
 
  if (sum_func_count && !group_list && (func_count || field_count))
697
 
  {
698
 
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
699
 
    goto err;
700
 
  }
701
 
#endif
702
 
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
703
 
    goto err;
704
 
  if (alloc_func_list())
705
 
    goto err;
706
 
 
707
 
  return(0); // All OK
708
 
 
709
 
err:
710
 
  return(-1);                           /* purecov: inspected */
711
 
}
712
 
 
713
 
 
714
 
/*
715
 
  Remove the predicates pushed down into the subquery
716
 
 
717
 
  SYNOPSIS
718
 
    JOIN::remove_subq_pushed_predicates()
719
 
      where   IN  Must be NULL
720
 
              OUT The remaining WHERE condition, or NULL
721
 
 
722
 
  DESCRIPTION
723
 
    Given that this join will be executed using (unique|index)_subquery,
724
 
    without "checking NULL", remove the predicates that were pushed down
725
 
    into the subquery.
726
 
 
727
 
    If the subquery compares scalar values, we can remove the condition that
728
 
    was wrapped into trig_cond (it will be checked when needed by the subquery
729
 
    engine)
730
 
 
731
 
    If the subquery compares row values, we need to keep the wrapped
732
 
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
733
 
    and non-NULL values, we'll do a full table scan and will rely on the
734
 
    equalities corresponding to non-NULL parts of left tuple to filter out
735
 
    non-matching records.
736
 
 
737
 
    TODO: We can remove the equalities that will be guaranteed to be true by the
738
 
    fact that subquery engine will be using index lookup. This must be done only
739
 
    for cases where there are no conversion errors of significance, e.g. 257
740
 
    that is searched in a byte. But this requires homogenization of the return 
741
 
    codes of all Field*::store() methods.
742
 
*/
743
 
 
744
 
void JOIN::remove_subq_pushed_predicates(Item **where)
745
 
{
746
 
  if (conds->type() == Item::FUNC_ITEM &&
747
 
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
748
 
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
749
 
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
750
 
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
751
 
                   ((Item_func *)conds)->arguments()[0]))
752
 
  {
753
 
    *where= 0;
754
 
    return;
755
 
  }
756
 
}
757
 
 
758
 
 
759
270
/*
760
271
  Index lookup-based subquery: save some flags for EXPLAIN output
761
272
 
768
279
  DESCRIPTION
769
280
    For index lookup-based subquery (i.e. one executed with
770
281
    subselect_uniquesubquery_engine or subselect_indexsubquery_engine),
771
 
    check its EXPLAIN output row should contain 
772
 
      "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)
773
284
      "Using Where" (TAB_INFO_USING_WHERE)
774
285
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
775
286
    and set appropriate flags in join_tab->packed_info.
776
287
*/
777
 
 
778
 
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)
779
289
{
780
290
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
781
 
  if (join_tab->table->covering_keys.is_set(join_tab->ref.key))
 
291
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
782
292
    join_tab->packed_info |= TAB_INFO_USING_INDEX;
783
293
  if (where)
784
294
    join_tab->packed_info |= TAB_INFO_USING_WHERE;
785
 
  for (uint i = 0; i < join_tab->ref.key_parts; i++)
 
295
  for (uint32_t i = 0; i < join_tab->ref.key_parts; i++)
786
296
  {
787
297
    if (join_tab->ref.cond_guards[i])
788
298
    {
792
302
  }
793
303
}
794
304
 
795
 
 
796
 
 
797
 
 
798
 
/*
799
 
  Check if the table's rowid is included in the temptable
800
 
 
801
 
  SYNOPSIS
802
 
    sj_table_is_included()
803
 
      join      The join
804
 
      join_tab  The table to be checked
805
 
 
806
 
  DESCRIPTION
807
 
    SemiJoinDuplicateElimination: check the table's rowid should be included
808
 
    in the temptable. This is so if
809
 
 
810
 
    1. The table is not embedded within some semi-join nest
811
 
    2. The has been pulled out of a semi-join nest, or
812
 
 
813
 
    3. The table is functionally dependent on some previous table
814
 
 
815
 
    [4. This is also true for constant tables that can't be
816
 
        NULL-complemented but this function is not called for such tables]
817
 
 
818
 
  RETURN
819
 
    true  - Include table's rowid
820
 
    false - Don't
821
 
*/
822
 
 
823
 
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
824
 
{
825
 
  if (join_tab->emb_sj_nest)
826
 
    return false;
827
 
  
828
 
  /* Check if this table is functionally dependent on the tables that
829
 
     are within the same outer join nest
830
 
  */
831
 
  TABLE_LIST *embedding= join_tab->table->pos_in_table_list->embedding;
832
 
  if (join_tab->type == JT_EQ_REF)
833
 
  {
834
 
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
835
 
    uint idx;
836
 
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
837
 
    {
838
 
      JOIN_TAB *ref_tab= join->join_tab + idx;
839
 
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
840
 
        return true;
841
 
    }
842
 
    /* Ok, functionally dependent */
843
 
    return false;
844
 
  }
845
 
  /* Not functionally dependent => need to include*/
846
 
  return true;
847
 
}
848
 
 
849
 
 
850
 
TABLE *create_duplicate_weedout_tmp_table(THD *thd, uint uniq_tuple_length_arg,
851
 
                                          SJ_TMP_TABLE *sjtbl);
852
 
 
853
 
 
854
 
/*
855
 
  Setup the strategies to eliminate semi-join duplicates.
856
 
  
857
 
  SYNOPSIS
858
 
    setup_semijoin_dups_elimination()
859
 
      join           Join to process
860
 
      options        Join options (needed to see if join buffering will be 
861
 
                     used or not)
862
 
      no_jbuf_after  Another bit of information re where join buffering will
863
 
                     be used.
864
 
 
865
 
  DESCRIPTION
866
 
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
867
 
    strategies:
868
 
 
869
 
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
870
 
                         of row combinations)
871
 
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
872
 
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
873
 
                  together and picking the 1st one)
874
 
    
875
 
    The join order has "duplicate-generating ranges", and every range is
876
 
    served by one strategy or a combination of FirstMatch with with some
877
 
    other strategy.
878
 
    
879
 
    "Duplicate-generating range" is defined as a range within the join order
880
 
    that contains all of the inner tables of a semi-join. All ranges must be
881
 
    disjoint, if tables of several semi-joins are interleaved, then the ranges
882
 
    are joined together, which is equivalent to converting
883
 
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
884
 
    to
885
 
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
886
 
    .
887
 
 
888
 
    Applicability conditions are as follows:
889
 
 
890
 
    DuplicateWeedout strategy
891
 
    ~~~~~~~~~~~~~~~~~~~~~~~~~
892
 
 
893
 
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
894
 
      +------+  +=========================+  +---+
895
 
        (1)                 (2)               (3)
896
 
 
897
 
       (1) - Prefix of OuterTables (those that participate in 
898
 
             IN-equality and/or are correlated with subquery) and outer 
899
 
             Noncorrelated Tables.
900
 
       (2) - The handled range. The range starts with the first sj-inner
901
 
             table, and covers all sj-inner and outer tables 
902
 
             Within the range,  Inner, Outer, outer Noncorrelated tables
903
 
             may follow in any order.
904
 
       (3) - The suffix of outer Noncorrelated tables.
905
 
    
906
 
    FirstMatch strategy
907
 
    ~~~~~~~~~~~~~~~~~~~
908
 
 
909
 
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
910
 
      +------+  +==================+  +---+
911
 
        (1)             (2)          (3)
912
 
 
913
 
      (1) - Prefix of outer and non-correlated tables
914
 
      (2) - The handled range, which may contain only inner and
915
 
            non-correlated tables.
916
 
      (3) - The suffix of outer Noncorrelated tables.
917
 
 
918
 
    InsideOut strategy 
919
 
    ~~~~~~~~~~~~~~~~~~
920
 
 
921
 
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
922
 
     +--------+   +===========+ +=============+   +------+
923
 
        (1)           (2)          (3)              (4)
924
 
     
925
 
      (1) - Prefix that may contain any outer tables. The prefix must contain
926
 
            all the non-trivially correlated outer tables. (non-trivially means
927
 
            that the correlation is not just through the IN-equality).
928
 
      
929
 
      (2) - Inner table for which the InsideOut scan is performed.
930
 
 
931
 
      (3) - The remainder of the duplicate-generating range. It is served by 
932
 
            application of FirstMatch strategy, with the exception that
933
 
            outer IN-correlated tables are considered to be non-correlated.
934
 
 
935
 
      (4) - THe suffix of outer and outer non-correlated tables.
936
 
 
937
 
    If several strategies are applicable, their relative priorities are:
938
 
      1. InsideOut
939
 
      2. FirstMatch 
940
 
      3. DuplicateWeedout
941
 
 
942
 
    This function walks over the join order and sets up the strategies by
943
 
    setting appropriate members in join_tab structures.
944
 
 
945
 
  RETURN
946
 
    false  OK 
947
 
    true   Out of memory error
948
 
*/
949
 
 
950
 
static
951
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint no_jbuf_after)
952
 
{
953
 
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
954
 
  struct {
955
 
    /* 
956
 
      0 - invalid (EOF marker), 
957
 
      1 - InsideOut, 
958
 
      2 - Temptable (maybe confluent),
959
 
      3 - Temptable with join buffering
960
 
    */
961
 
    uint strategy;
962
 
    uint start_idx; /* Left range bound */
963
 
    uint end_idx;   /* Right range bound */
964
 
    /* 
965
 
      For Temptable strategy: Bitmap of all outer and correlated tables from 
966
 
      all involved join nests.
967
 
    */
968
 
    table_map outer_tables;
969
 
  } dups_ranges [MAX_TABLES];
970
 
 
971
 
  TABLE_LIST *emb_insideout_nest= NULL;
972
 
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
973
 
                               tables) whose ranges we're in */
974
 
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
975
 
  table_map range_start_map= 0; /* table_map at current range start */
976
 
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
977
 
  int cur_range= 0;
978
 
  uint i;
979
 
 
980
 
  /*
981
 
    First pass: locate the duplicate-generating ranges and pick the strategies.
982
 
  */
983
 
  for (i=join->const_tables ; i < join->tables ; i++)
984
 
  {
985
 
    JOIN_TAB *tab=join->join_tab+i;
986
 
    TABLE *table=tab->table;
987
 
    cur_map |= table->map;
988
 
 
989
 
    if (tab->emb_sj_nest) // Encountered an sj-inner table
990
 
    {
991
 
      if (!emb_sj_map)
992
 
      {
993
 
        dups_ranges[cur_range].start_idx= i;
994
 
        range_start_map= cur_map & ~table->map;
995
 
        /*
996
 
          Remember if this is a possible start of range that is covered by
997
 
          the InsideOut strategy (the reason that it is not covered could
998
 
          be that it overlaps with anther semi-join's range. we don't
999
 
          support InsideOut for joined ranges)
1000
 
        */
1001
 
        if (join->best_positions[i].use_insideout_scan)
1002
 
          emb_insideout_nest= tab->emb_sj_nest;
1003
 
      }
1004
 
 
1005
 
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1006
 
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1007
 
 
1008
 
      if (tab->emb_sj_nest != emb_insideout_nest)
1009
 
      {
1010
 
        /*
1011
 
          Two different semi-joins interleave. This cannot be handled by
1012
 
          InsideOut strategy.
1013
 
        */
1014
 
        emb_insideout_nest= NULL;
1015
 
      }
1016
 
    }
1017
 
 
1018
 
    if (emb_sj_map) /* We're in duplicate-generating range */
1019
 
    {
1020
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1021
 
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1022
 
          i <= no_jbuf_after && !dealing_with_jbuf)
1023
 
      {
1024
 
        /*
1025
 
          This table uses join buffering, which makes use of FirstMatch or 
1026
 
          InsideOut strategies impossible for the current and (we assume) 
1027
 
          preceding duplicate-producing ranges.
1028
 
          That is, for the join order:
1029
 
 
1030
 
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
1031
 
                  |     |     |     |          | \
1032
 
                  +-----+     +-----+          |  join buffering use
1033
 
                     r1          r2         we're here
1034
 
 
1035
 
          we'll have to remove r1 and r2 and use duplicate-elimination
1036
 
          strategy that spans all the tables, starting from the very 1st
1037
 
          one.
1038
 
        */
1039
 
        dealing_with_jbuf= true;
1040
 
        emb_insideout_nest= false;
1041
 
 
1042
 
        /* 
1043
 
          Absorb all preceding duplicate-eliminating ranges. Their strategies
1044
 
          do not matter: 
1045
 
        */
1046
 
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
1047
 
        {
1048
 
          dups_ranges[cur_range].outer_tables |= 
1049
 
            dups_ranges[prev_range].outer_tables;
1050
 
        }
1051
 
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1052
 
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1053
 
        cur_range=  0;
1054
 
      }
1055
 
 
1056
 
      /*
1057
 
        Check if we are at the end of duplicate-producing range. We are if
1058
 
 
1059
 
        1. It's an InsideOut range (which presumes all correlated tables are
1060
 
           in the prefix), and all inner tables are in the join order prefix,
1061
 
           or
1062
 
        2. It's a DuplicateElimination range (possibly covering several
1063
 
           SJ-nests), and all inner, outer, and correlated tables of all 
1064
 
           sj-nests are in the join order prefix.
1065
 
      */
1066
 
      bool end_of_range= false;
1067
 
      if (emb_insideout_nest && 
1068
 
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1069
 
      {
1070
 
        /* Save that this range is handled with InsideOut: */
1071
 
        dups_ranges[cur_range].strategy= 1;
1072
 
        end_of_range= true;
1073
 
      }
1074
 
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1075
 
      {
1076
 
        /*
1077
 
          This is a complete range to be handled with either DuplicateWeedout 
1078
 
          or FirstMatch
1079
 
        */
1080
 
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1081
 
        /* 
1082
 
          This will hold tables from within the range that need to be put 
1083
 
          into the join buffer before we can use the FirstMatch on its tail.
1084
 
        */
1085
 
        dups_ranges[cur_range].outer_tables= emb_outer_tables & 
1086
 
                                             ~range_start_map;
1087
 
        end_of_range= true;
1088
 
      }
1089
 
 
1090
 
      if (end_of_range)
1091
 
      {
1092
 
        dups_ranges[cur_range].end_idx= i+1;
1093
 
        emb_sj_map= emb_outer_tables= 0;
1094
 
        emb_insideout_nest= NULL;
1095
 
        dealing_with_jbuf= false;
1096
 
        dups_ranges[++cur_range].strategy= 0;
1097
 
      }
1098
 
    }
1099
 
  }
1100
 
 
1101
 
  THD *thd= join->thd;
1102
 
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1103
 
  /*
1104
 
    Second pass: setup the chosen strategies    
1105
 
  */
1106
 
  for (int j= 0; j < cur_range; j++)
1107
 
  {
1108
 
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1109
 
    JOIN_TAB *jump_to;
1110
 
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
1111
 
    {
1112
 
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1113
 
      jump_to= tab++;
1114
 
    }
1115
 
    else // DuplicateWeedout strategy
1116
 
    {
1117
 
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1118
 
      table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1119
 
      uint jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1120
 
      uint jt_null_bits= 0;    // # null bits in tuple bytes
1121
 
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1122
 
      uint rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1123
 
      JOIN_TAB *last_outer_tab= tab - 1;
1124
 
      /*
1125
 
        Walk through the range and remember
1126
 
         - tables that need their rowids to be put into temptable
1127
 
         - the last outer table
1128
 
      */
1129
 
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1130
 
      {
1131
 
        if (sj_table_is_included(join, tab))
1132
 
        {
1133
 
          last_tab->join_tab= tab;
1134
 
          last_tab->rowid_offset= jt_rowid_offset;
1135
 
          jt_rowid_offset += tab->table->file->ref_length;
1136
 
          if (tab->table->maybe_null)
1137
 
          {
1138
 
            last_tab->null_byte= jt_null_bits / 8;
1139
 
            last_tab->null_bit= jt_null_bits++;
1140
 
          }
1141
 
          last_tab++;
1142
 
          tab->table->prepare_for_position();
1143
 
          tab->rowid_keep_flags= rowid_keep_flags;
1144
 
        }
1145
 
        cur_map |= tab->table->map;
1146
 
        if (!tab->emb_sj_nest && bitmap_covers(cur_map, 
1147
 
                                               dups_ranges[j].outer_tables))
1148
 
          last_outer_tab= tab;
1149
 
      }
1150
 
 
1151
 
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1152
 
      {
1153
 
        SJ_TMP_TABLE *sjtbl;
1154
 
        uint tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1155
 
        if (!(sjtbl= (SJ_TMP_TABLE*)thd->alloc(sizeof(SJ_TMP_TABLE))) ||
1156
 
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) thd->alloc(tabs_size)))
1157
 
          return(true);
1158
 
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1159
 
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1160
 
        sjtbl->rowid_len= jt_rowid_offset;
1161
 
        sjtbl->null_bits= jt_null_bits;
1162
 
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
1163
 
 
1164
 
        *next_sjtbl_ptr= sjtbl;
1165
 
        next_sjtbl_ptr= &(sjtbl->next);
1166
 
        sjtbl->next= NULL;
1167
 
 
1168
 
        sjtbl->tmp_table= 
1169
 
          create_duplicate_weedout_tmp_table(thd, 
1170
 
                                             sjtbl->rowid_len + 
1171
 
                                             sjtbl->null_bytes,
1172
 
                                             sjtbl);
1173
 
 
1174
 
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1175
 
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1176
 
      }
1177
 
      tab= last_outer_tab + 1;
1178
 
      jump_to= last_outer_tab;
1179
 
    }
1180
 
 
1181
 
    /* Create the FirstMatch tail */
1182
 
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1183
 
    {
1184
 
      if (tab->emb_sj_nest)
1185
 
        tab->do_firstmatch= jump_to; 
1186
 
      else
1187
 
        jump_to= tab;
1188
 
    }
1189
 
  }
1190
 
  return(false);
1191
 
}
1192
 
 
1193
 
 
1194
 
static void cleanup_sj_tmp_tables(JOIN *join)
1195
 
{
1196
 
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl; 
1197
 
       sj_tbl= sj_tbl->next)
1198
 
  {
1199
 
    if (sj_tbl->tmp_table)
1200
 
    {
1201
 
      free_tmp_table(join->thd, sj_tbl->tmp_table);
1202
 
    }
1203
 
  }
1204
 
  join->sj_tmp_tables= NULL;
1205
 
}
1206
 
 
1207
 
uint make_join_orderinfo(JOIN *join);
1208
 
 
1209
 
/**
1210
 
  global select optimisation.
1211
 
 
1212
 
  @note
1213
 
    error code saved in field 'error'
1214
 
 
1215
 
  @retval
1216
 
    0   success
1217
 
  @retval
1218
 
    1   error
1219
 
*/
1220
 
 
1221
 
int
1222
 
JOIN::optimize()
1223
 
{
1224
 
  // to prevent double initialization on EXPLAIN
1225
 
  if (optimized)
1226
 
    return(0);
1227
 
  optimized= 1;
1228
 
 
1229
 
  thd_proc_info(thd, "optimizing");
1230
 
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1231
 
              unit->select_limit_cnt);
1232
 
  /* select_limit is used to decide if we are likely to scan the whole table */
1233
 
  select_limit= unit->select_limit_cnt;
1234
 
  if (having || (select_options & OPTION_FOUND_ROWS))
1235
 
    select_limit= HA_POS_ERROR;
1236
 
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1237
 
  // Ignore errors of execution if option IGNORE present
1238
 
  if (thd->lex->ignore)
1239
 
    thd->lex->current_select->no_error= 1;
1240
 
 
1241
 
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
1242
 
  /* Add HAVING to WHERE if possible */
1243
 
  if (having && !group_list && !sum_func_count)
1244
 
  {
1245
 
    if (!conds)
1246
 
    {
1247
 
      conds= having;
1248
 
      having= 0;
1249
 
    }
1250
 
    else if ((conds=new Item_cond_and(conds,having)))
1251
 
    {
1252
 
      /*
1253
 
        Item_cond_and can't be fixed after creation, so we do not check
1254
 
        conds->fixed
1255
 
      */
1256
 
      conds->fix_fields(thd, &conds);
1257
 
      conds->change_ref_to_fields(thd, tables_list);
1258
 
      conds->top_level_item();
1259
 
      having= 0;
1260
 
    }
1261
 
  }
1262
 
#endif
1263
 
  SELECT_LEX *sel= thd->lex->current_select;
1264
 
  if (sel->first_cond_optimization)
1265
 
  {
1266
 
    /*
1267
 
      The following code will allocate the new items in a permanent
1268
 
      MEMROOT for prepared statements and stored procedures.
1269
 
    */
1270
 
    sel->first_cond_optimization= 0;
1271
 
 
1272
 
    /* Convert all outer joins to inner joins if possible */
1273
 
    conds= simplify_joins(this, join_list, conds, true, false);
1274
 
    build_bitmap_for_nested_joins(join_list, 0);
1275
 
  }
1276
 
 
1277
 
  conds= optimize_cond(this, conds, join_list, &cond_value);   
1278
 
  if (thd->is_error())
1279
 
  {
1280
 
    error= 1;
1281
 
    return(1);
1282
 
  }
1283
 
 
1284
 
  {
1285
 
    having= optimize_cond(this, having, join_list, &having_value);
1286
 
    if (thd->is_error())
1287
 
    {
1288
 
      error= 1;
1289
 
      return(1);
1290
 
    }
1291
 
    if (select_lex->where)
1292
 
      select_lex->cond_value= cond_value;
1293
 
    if (select_lex->having)
1294
 
      select_lex->having_value= having_value;
1295
 
 
1296
 
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE || 
1297
 
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1298
 
    {                                           /* Impossible cond */
1299
 
      zero_result_cause=  having_value == Item::COND_FALSE ?
1300
 
                           "Impossible HAVING" : "Impossible WHERE";
1301
 
      error= 0;
1302
 
      return(0);
1303
 
    }
1304
 
  }
1305
 
 
1306
 
  /* Optimize count(*), min() and max() */
1307
 
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1308
 
  {
1309
 
    int res;
1310
 
    /*
1311
 
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1312
 
      to the WHERE conditions,
1313
 
      or 1 if all items were resolved,
1314
 
      or 0, or an error number HA_ERR_...
1315
 
    */
1316
 
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1317
 
    {
1318
 
      if (res == HA_ERR_KEY_NOT_FOUND)
1319
 
      {
1320
 
        zero_result_cause= "No matching min/max row";
1321
 
        error=0;
1322
 
        return(0);
1323
 
      }
1324
 
      if (res > 1)
1325
 
      {
1326
 
        error= res;
1327
 
        return(1);
1328
 
      }
1329
 
      if (res < 0)
1330
 
      {
1331
 
        zero_result_cause= "No matching min/max row";
1332
 
        error=0;
1333
 
        return(0);
1334
 
      }
1335
 
      zero_result_cause= "Select tables optimized away";
1336
 
      tables_list= 0;                           // All tables resolved
1337
 
      /*
1338
 
        Extract all table-independent conditions and replace the WHERE
1339
 
        clause with them. All other conditions were computed by opt_sum_query
1340
 
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
1341
 
        so there is no need to compute the whole WHERE clause again.
1342
 
        Notice that make_cond_for_table() will always succeed to remove all
1343
 
        computed conditions, because opt_sum_query() is applicable only to
1344
 
        conjunctions.
1345
 
        Preserve conditions for EXPLAIN.
1346
 
      */
1347
 
      if (conds && !(thd->lex->describe & DESCRIBE_EXTENDED))
1348
 
      {
1349
 
        COND *table_independent_conds=
1350
 
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1351
 
        conds= table_independent_conds;
1352
 
      }
1353
 
    }
1354
 
  }
1355
 
  if (!tables_list)
1356
 
  {
1357
 
    error= 0;
1358
 
    return(0);
1359
 
  }
1360
 
  error= -1;                                    // Error is sent to client
1361
 
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1362
 
 
1363
 
  /* Calculate how to do the join */
1364
 
  thd_proc_info(thd, "statistics");
1365
 
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1366
 
      thd->is_fatal_error)
1367
 
  {
1368
 
    return(1);
1369
 
  }
1370
 
 
1371
 
  /* Remove distinct if only const tables */
1372
 
  select_distinct= select_distinct && (const_tables != tables);
1373
 
  thd_proc_info(thd, "preparing");
1374
 
  if (result->initialize_tables(this))
1375
 
  {
1376
 
    return(1);                          // error == -1
1377
 
  }
1378
 
  if (const_table_map != found_const_table_map &&
1379
 
      !(select_options & SELECT_DESCRIBE) &&
1380
 
      (!conds ||
1381
 
       !(conds->used_tables() & RAND_TABLE_BIT) ||
1382
 
       select_lex->master_unit() == &thd->lex->unit)) // upper level SELECT
1383
 
  {
1384
 
    zero_result_cause= "no matching row in const table";
1385
 
    error= 0;
1386
 
    return(0);
1387
 
  }
1388
 
  if (!(thd->options & OPTION_BIG_SELECTS) &&
1389
 
      best_read > (double) thd->variables.max_join_size &&
1390
 
      !(select_options & SELECT_DESCRIBE))
1391
 
  {                                             /* purecov: inspected */
1392
 
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1393
 
    error= -1;
1394
 
    return(1);
1395
 
  }
1396
 
  if (const_tables && !thd->locked_tables &&
1397
 
      !(select_options & SELECT_NO_UNLOCK))
1398
 
    mysql_unlock_some_tables(thd, table, const_tables);
1399
 
  if (!conds && outer_join)
1400
 
  {
1401
 
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1402
 
    conds=new Item_int((int64_t) 1,1);  // Always true
1403
 
  }
1404
 
  select= make_select(*table, const_table_map,
1405
 
                      const_table_map, conds, 1, &error);
1406
 
  if (error)
1407
 
  {                                             /* purecov: inspected */
1408
 
    error= -1;                                  /* purecov: inspected */
1409
 
    return(1);
1410
 
  }
1411
 
  
1412
 
  reset_nj_counters(join_list);
1413
 
  make_outerjoin_info(this);
1414
 
 
1415
 
  /*
1416
 
    Among the equal fields belonging to the same multiple equality
1417
 
    choose the one that is to be retrieved first and substitute
1418
 
    all references to these in where condition for a reference for
1419
 
    the selected field.
1420
 
  */
1421
 
  if (conds)
1422
 
  {
1423
 
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1424
 
    conds->update_used_tables();
1425
 
  }
1426
 
 
1427
 
  /*
1428
 
    Permorm the the optimization on fields evaluation mentioned above
1429
 
    for all on expressions.
1430
 
  */ 
1431
 
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1432
 
  {
1433
 
    if (*tab->on_expr_ref)
1434
 
    {
1435
 
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1436
 
                                                         tab->cond_equal,
1437
 
                                                         map2table);
1438
 
      (*tab->on_expr_ref)->update_used_tables();
1439
 
    }
1440
 
  }
1441
 
 
1442
 
  if (conds &&!outer_join && const_table_map != found_const_table_map && 
1443
 
      (select_options & SELECT_DESCRIBE) &&
1444
 
      select_lex->master_unit() == &thd->lex->unit) // upper level SELECT
1445
 
  {
1446
 
    conds=new Item_int((int64_t) 0,1);  // Always false
1447
 
  }
1448
 
  if (make_join_select(this, select, conds))
1449
 
  {
1450
 
    zero_result_cause=
1451
 
      "Impossible WHERE noticed after reading const tables";
1452
 
    return(0);                          // error == 0
1453
 
  }
1454
 
 
1455
 
  error= -1;                                    /* if goto err */
1456
 
 
1457
 
  /* Optimize distinct away if possible */
1458
 
  {
1459
 
    ORDER *org_order= order;
1460
 
    order=remove_const(this, order,conds,1, &simple_order);
1461
 
    if (thd->is_error())
1462
 
    {
1463
 
      error= 1;
1464
 
      return(1);
1465
 
    }
1466
 
 
1467
 
    /*
1468
 
      If we are using ORDER BY NULL or ORDER BY const_expression,
1469
 
      return result in any order (even if we are using a GROUP BY)
1470
 
    */
1471
 
    if (!order && org_order)
1472
 
      skip_sort_order= 1;
1473
 
  }
1474
 
  /*
1475
 
     Check if we can optimize away GROUP BY/DISTINCT.
1476
 
     We can do that if there are no aggregate functions, the
1477
 
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
1478
 
     (if present) contain direct references to all key parts of
1479
 
     an unique index (in whatever order) and if the key parts of the
1480
 
     unique index cannot contain NULLs.
1481
 
     Note that the unique keys for DISTINCT and GROUP BY should not
1482
 
     be the same (as long as they are unique).
1483
 
 
1484
 
     The FROM clause must contain a single non-constant table.
1485
 
  */
1486
 
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
1487
 
      !tmp_table_param.sum_func_count &&
1488
 
      (!join_tab[const_tables].select ||
1489
 
       !join_tab[const_tables].select->quick ||
1490
 
       join_tab[const_tables].select->quick->get_type() != 
1491
 
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1492
 
  {
1493
 
    if (group_list &&
1494
 
       list_contains_unique_index(join_tab[const_tables].table,
1495
 
                                 find_field_in_order_list,
1496
 
                                 (void *) group_list))
1497
 
    {
1498
 
      /*
1499
 
        We have found that grouping can be removed since groups correspond to
1500
 
        only one row anyway, but we still have to guarantee correct result
1501
 
        order. The line below effectively rewrites the query from GROUP BY
1502
 
        <fields> to ORDER BY <fields>. There are two exceptions:
1503
 
        - if skip_sort_order is set (see above), then we can simply skip
1504
 
          GROUP BY;
1505
 
        - we can only rewrite ORDER BY if the ORDER BY fields are 'compatible'
1506
 
          with the GROUP BY ones, i.e. either one is a prefix of another.
1507
 
          We only check if the ORDER BY is a prefix of GROUP BY. In this case
1508
 
          test_if_subpart() copies the ASC/DESC attributes from the original
1509
 
          ORDER BY fields.
1510
 
          If GROUP BY is a prefix of ORDER BY, then it is safe to leave
1511
 
          'order' as is.
1512
 
       */
1513
 
      if (!order || test_if_subpart(group_list, order))
1514
 
          order= skip_sort_order ? 0 : group_list;
1515
 
      /*
1516
 
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be 
1517
 
        rewritten to IGNORE INDEX FOR ORDER BY(fields).
1518
 
      */
1519
 
      join_tab->table->keys_in_use_for_order_by=
1520
 
        join_tab->table->keys_in_use_for_group_by;
1521
 
      group_list= 0;
1522
 
      group= 0;
1523
 
    }
1524
 
    if (select_distinct &&
1525
 
       list_contains_unique_index(join_tab[const_tables].table,
1526
 
                                 find_field_in_item_list,
1527
 
                                 (void *) &fields_list))
1528
 
    {
1529
 
      select_distinct= 0;
1530
 
    }
1531
 
  }
1532
 
  if (group_list || tmp_table_param.sum_func_count)
1533
 
  {
1534
 
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1535
 
      select_distinct=0;
1536
 
  }
1537
 
  else if (select_distinct && tables - const_tables == 1)
1538
 
  {
1539
 
    /*
1540
 
      We are only using one table. In this case we change DISTINCT to a
1541
 
      GROUP BY query if:
1542
 
      - The GROUP BY can be done through indexes (no sort) and the ORDER
1543
 
        BY only uses selected fields.
1544
 
        (In this case we can later optimize away GROUP BY and ORDER BY)
1545
 
      - We are scanning the whole table without LIMIT
1546
 
        This can happen if:
1547
 
        - We are using CALC_FOUND_ROWS
1548
 
        - We are using an ORDER BY that can't be optimized away.
1549
 
 
1550
 
      We don't want to use this optimization when we are using LIMIT
1551
 
      because in this case we can just create a temporary table that
1552
 
      holds LIMIT rows and stop when this table is full.
1553
 
    */
1554
 
    JOIN_TAB *tab= &join_tab[const_tables];
1555
 
    bool all_order_fields_used;
1556
 
    if (order)
1557
 
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1, 
1558
 
        &tab->table->keys_in_use_for_order_by);
1559
 
    if ((group_list=create_distinct_group(thd, select_lex->ref_pointer_array,
1560
 
                                          order, fields_list, all_fields,
1561
 
                                          &all_order_fields_used)))
1562
 
    {
1563
 
      bool skip_group= (skip_sort_order &&
1564
 
        test_if_skip_sort_order(tab, group_list, select_limit, 1, 
1565
 
                                &tab->table->keys_in_use_for_group_by) != 0);
1566
 
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1567
 
      if ((skip_group && all_order_fields_used) ||
1568
 
          select_limit == HA_POS_ERROR ||
1569
 
          (order && !skip_sort_order))
1570
 
      {
1571
 
        /*  Change DISTINCT to GROUP BY */
1572
 
        select_distinct= 0;
1573
 
        no_order= !order;
1574
 
        if (all_order_fields_used)
1575
 
        {
1576
 
          if (order && skip_sort_order)
1577
 
          {
1578
 
            /*
1579
 
              Force MySQL to read the table in sorted order to get result in
1580
 
              ORDER BY order.
1581
 
            */
1582
 
            tmp_table_param.quick_group=0;
1583
 
          }
1584
 
          order=0;
1585
 
        }
1586
 
        group=1;                                // For end_write_group
1587
 
      }
1588
 
      else
1589
 
        group_list= 0;
1590
 
    }
1591
 
    else if (thd->is_fatal_error)                       // End of memory
1592
 
      return(1);
1593
 
  }
1594
 
  simple_group= 0;
1595
 
  {
1596
 
    ORDER *old_group_list;
1597
 
    group_list= remove_const(this, (old_group_list= group_list), conds,
1598
 
                             rollup.state == ROLLUP::STATE_NONE,
1599
 
                             &simple_group);
1600
 
    if (thd->is_error())
1601
 
    {
1602
 
      error= 1;
1603
 
      return(1);
1604
 
    }
1605
 
    if (old_group_list && !group_list)
1606
 
      select_distinct= 0;
1607
 
  }
1608
 
  if (!group_list && group)
1609
 
  {
1610
 
    order=0;                                    // The output has only one row
1611
 
    simple_order=1;
1612
 
    select_distinct= 0;                       // No need in distinct for 1 row
1613
 
    group_optimized_away= 1;
1614
 
  }
1615
 
 
1616
 
  calc_group_buffer(this, group_list);
1617
 
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1618
 
 
1619
 
  if (test_if_subpart(group_list, order) ||
1620
 
      (!group_list && tmp_table_param.sum_func_count))
1621
 
    order=0;
1622
 
 
1623
 
  // Can't use sort on head table if using row cache
1624
 
  if (full_join)
1625
 
  {
1626
 
    if (group_list)
1627
 
      simple_group=0;
1628
 
    if (order)
1629
 
      simple_order=0;
1630
 
  }
1631
 
 
1632
 
  /*
1633
 
    Check if we need to create a temporary table.
1634
 
    This has to be done if all tables are not already read (const tables)
1635
 
    and one of the following conditions holds:
1636
 
    - We are using DISTINCT (simple distinct's are already optimized away)
1637
 
    - We are using an ORDER BY or GROUP BY on fields not in the first table
1638
 
    - We are using different ORDER BY and GROUP BY orders
1639
 
    - The user wants us to buffer the result.
1640
 
  */
1641
 
  need_tmp= (const_tables != tables &&
1642
 
             ((select_distinct || !simple_order || !simple_group) ||
1643
 
              (group_list && order) ||
1644
 
              test(select_options & OPTION_BUFFER_RESULT)));
1645
 
 
1646
 
  uint no_jbuf_after= make_join_orderinfo(this);
1647
 
  uint64_t select_opts_for_readinfo= 
1648
 
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1649
 
 
1650
 
  sj_tmp_tables= NULL;
1651
 
  if (!select_lex->sj_nests.is_empty())
1652
 
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1653
 
                                    no_jbuf_after);
1654
 
 
1655
 
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1656
 
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1657
 
    return(1);
1658
 
 
1659
 
  /* Create all structures needed for materialized subquery execution. */
1660
 
  if (setup_subquery_materialization())
1661
 
    return(1);
1662
 
 
1663
 
  /*
1664
 
    is this simple IN subquery?
1665
 
  */
1666
 
  if (!group_list && !order &&
1667
 
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1668
 
      tables == 1 && conds &&
1669
 
      !unit->is_union())
1670
 
  {
1671
 
    if (!having)
1672
 
    {
1673
 
      Item *where= conds;
1674
 
      if (join_tab[0].type == JT_EQ_REF &&
1675
 
          join_tab[0].ref.items[0]->name == in_left_expr_name)
1676
 
      {
1677
 
        remove_subq_pushed_predicates(&where);
1678
 
        save_index_subquery_explain_info(join_tab, where);
1679
 
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1680
 
        error= 0;
1681
 
        return(unit->item->
1682
 
                    change_engine(new
1683
 
                                  subselect_uniquesubquery_engine(thd,
1684
 
                                                                  join_tab,
1685
 
                                                                  unit->item,
1686
 
                                                                  where)));
1687
 
      }
1688
 
      else if (join_tab[0].type == JT_REF &&
1689
 
               join_tab[0].ref.items[0]->name == in_left_expr_name)
1690
 
      {
1691
 
        remove_subq_pushed_predicates(&where);
1692
 
        save_index_subquery_explain_info(join_tab, where);
1693
 
        join_tab[0].type= JT_INDEX_SUBQUERY;
1694
 
        error= 0;
1695
 
        return(unit->item->
1696
 
                    change_engine(new
1697
 
                                  subselect_indexsubquery_engine(thd,
1698
 
                                                                 join_tab,
1699
 
                                                                 unit->item,
1700
 
                                                                 where,
1701
 
                                                                 NULL,
1702
 
                                                                 0)));
1703
 
      }
1704
 
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
1705
 
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
1706
 
               having->name == in_having_cond)
1707
 
    {
1708
 
      join_tab[0].type= JT_INDEX_SUBQUERY;
1709
 
      error= 0;
1710
 
      conds= remove_additional_cond(conds);
1711
 
      save_index_subquery_explain_info(join_tab, conds);
1712
 
      return(unit->item->
1713
 
                  change_engine(new subselect_indexsubquery_engine(thd,
1714
 
                                                                   join_tab,
1715
 
                                                                   unit->item,
1716
 
                                                                   conds,
1717
 
                                                                   having,
1718
 
                                                                   1)));
1719
 
    }
1720
 
 
1721
 
  }
1722
 
  /*
1723
 
    Need to tell handlers that to play it safe, it should fetch all
1724
 
    columns of the primary key of the tables: this is because MySQL may
1725
 
    build row pointers for the rows, and for all columns of the primary key
1726
 
    the read set has not necessarily been set by the server code.
1727
 
  */
1728
 
  if (need_tmp || select_distinct || group_list || order)
1729
 
  {
1730
 
    for (uint i = const_tables; i < tables; i++)
1731
 
      join_tab[i].table->prepare_for_position();
1732
 
  }
1733
 
 
1734
 
  if (const_tables != tables)
1735
 
  {
1736
 
    /*
1737
 
      Because filesort always does a full table scan or a quick range scan
1738
 
      we must add the removed reference to the select for the table.
1739
 
      We only need to do this when we have a simple_order or simple_group
1740
 
      as in other cases the join is done before the sort.
1741
 
    */
1742
 
    if ((order || group_list) &&
1743
 
        (join_tab[const_tables].type != JT_ALL) &&
1744
 
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
1745
 
        ((order && simple_order) || (group_list && simple_group)))
1746
 
    {
1747
 
      if (add_ref_to_table_cond(thd,&join_tab[const_tables])) {
1748
 
        return(1);
1749
 
      }
1750
 
    }
1751
 
    
1752
 
    if (!(select_options & SELECT_BIG_RESULT) &&
1753
 
        ((group_list &&
1754
 
          (!simple_group ||
1755
 
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
1756
 
                                    unit->select_limit_cnt, 0, 
1757
 
                                    &join_tab[const_tables].table->
1758
 
                                    keys_in_use_for_group_by))) ||
1759
 
         select_distinct) &&
1760
 
        tmp_table_param.quick_group)
1761
 
    {
1762
 
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
1763
 
    }
1764
 
    if (order)
1765
 
    {
1766
 
      /*
1767
 
        Force using of tmp table if sorting by a SP or UDF function due to
1768
 
        their expensive and probably non-deterministic nature.
1769
 
      */
1770
 
      for (ORDER *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1771
 
      {
1772
 
        Item *item= *tmp_order->item;
1773
 
        if (item->is_expensive())
1774
 
        {
1775
 
          /* Force tmp table without sort */
1776
 
          need_tmp=1; simple_order=simple_group=0;
1777
 
          break;
1778
 
        }
1779
 
      }
1780
 
    }
1781
 
  }
1782
 
 
1783
 
  tmp_having= having;
1784
 
  if (select_options & SELECT_DESCRIBE)
1785
 
  {
1786
 
    error= 0;
1787
 
    return(0);
1788
 
  }
1789
 
  having= 0;
1790
 
 
1791
 
  /*
1792
 
    The loose index scan access method guarantees that all grouping or
1793
 
    duplicate row elimination (for distinct) is already performed
1794
 
    during data retrieval, and that all MIN/MAX functions are already
1795
 
    computed for each group. Thus all MIN/MAX functions should be
1796
 
    treated as regular functions, and there is no need to perform
1797
 
    grouping in the main execution loop.
1798
 
    Notice that currently loose index scan is applicable only for
1799
 
    single table queries, thus it is sufficient to test only the first
1800
 
    join_tab element of the plan for its access method.
1801
 
  */
1802
 
  if (join_tab->is_using_loose_index_scan())
1803
 
    tmp_table_param.precomputed_group_by= true;
1804
 
 
1805
 
  /* Create a tmp table if distinct or if the sort is too complicated */
1806
 
  if (need_tmp)
1807
 
  {
1808
 
    thd_proc_info(thd, "Creating tmp table");
1809
 
 
1810
 
    init_items_ref_array();
1811
 
 
1812
 
    tmp_table_param.hidden_field_count= (all_fields.elements -
1813
 
                                         fields_list.elements);
1814
 
    ORDER *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1815
 
                                                             (ORDER*) 0);
1816
 
    /*
1817
 
      Pushing LIMIT to the temporary table creation is not applicable
1818
 
      when there is ORDER BY or GROUP BY or there is no GROUP BY, but
1819
 
      there are aggregate functions, because in all these cases we need
1820
 
      all result rows.
1821
 
    */
1822
 
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1823
 
                             !tmp_group &&
1824
 
                             !thd->lex->current_select->with_sum_func) ?
1825
 
                            select_limit : HA_POS_ERROR;
1826
 
 
1827
 
    if (!(exec_tmp_table1=
1828
 
          create_tmp_table(thd, &tmp_table_param, all_fields,
1829
 
                           tmp_group,
1830
 
                           group_list ? 0 : select_distinct,
1831
 
                           group_list && simple_group,
1832
 
                           select_options,
1833
 
                           tmp_rows_limit,
1834
 
                           (char *) "")))
1835
 
                {
1836
 
      return(1);
1837
 
    }
1838
 
 
1839
 
    /*
1840
 
      We don't have to store rows in temp table that doesn't match HAVING if:
1841
 
      - we are sorting the table and writing complete group rows to the
1842
 
        temp table.
1843
 
      - We are using DISTINCT without resolving the distinct as a GROUP BY
1844
 
        on all columns.
1845
 
      
1846
 
      If having is not handled here, it will be checked before the row
1847
 
      is sent to the client.
1848
 
    */    
1849
 
    if (tmp_having && 
1850
 
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1851
 
      having= tmp_having;
1852
 
 
1853
 
    /* if group or order on first table, sort first */
1854
 
    if (group_list && simple_group)
1855
 
    {
1856
 
      thd_proc_info(thd, "Sorting for group");
1857
 
      if (create_sort_index(thd, this, group_list,
1858
 
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1859
 
          alloc_group_fields(this, group_list) ||
1860
 
          make_sum_func_list(all_fields, fields_list, 1) ||
1861
 
          setup_sum_funcs(thd, sum_funcs))
1862
 
      {
1863
 
        return(1);
1864
 
      }
1865
 
      group_list=0;
1866
 
    }
1867
 
    else
1868
 
    {
1869
 
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1870
 
          setup_sum_funcs(thd, sum_funcs))
1871
 
      {
1872
 
        return(1);
1873
 
      }
1874
 
 
1875
 
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1876
 
      {
1877
 
        thd_proc_info(thd, "Sorting for order");
1878
 
        if (create_sort_index(thd, this, order,
1879
 
                              HA_POS_ERROR, HA_POS_ERROR, true))
1880
 
        {
1881
 
          return(1);
1882
 
        }
1883
 
        order=0;
1884
 
      }
1885
 
    }
1886
 
    
1887
 
    /*
1888
 
      Optimize distinct when used on some of the tables
1889
 
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1890
 
      In this case we can stop scanning t2 when we have found one t1.a
1891
 
    */
1892
 
 
1893
 
    if (exec_tmp_table1->distinct)
1894
 
    {
1895
 
      table_map used_tables= thd->used_tables;
1896
 
      JOIN_TAB *last_join_tab= join_tab+tables-1;
1897
 
      do
1898
 
      {
1899
 
        if (used_tables & last_join_tab->table->map)
1900
 
          break;
1901
 
        last_join_tab->not_used_in_distinct=1;
1902
 
      } while (last_join_tab-- != join_tab);
1903
 
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1904
 
      if (order && skip_sort_order)
1905
 
      {
1906
 
        /* Should always succeed */
1907
 
        if (test_if_skip_sort_order(&join_tab[const_tables],
1908
 
                                    order, unit->select_limit_cnt, 0, 
1909
 
                                    &join_tab[const_tables].table->
1910
 
                                      keys_in_use_for_order_by))
1911
 
          order=0;
1912
 
      }
1913
 
    }
1914
 
 
1915
 
    /* 
1916
 
      If this join belongs to an uncacheable subquery save 
1917
 
      the original join 
1918
 
    */
1919
 
    if (select_lex->uncacheable && !is_top_level_join() &&
1920
 
        init_save_join_tab())
1921
 
      return(-1);                         /* purecov: inspected */
1922
 
  }
1923
 
 
1924
 
  error= 0;
1925
 
  return(0);
1926
 
}
1927
 
 
1928
 
 
1929
 
/**
1930
 
  Restore values in temporary join.
1931
 
*/
1932
 
void JOIN::restore_tmp()
1933
 
{
1934
 
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1935
 
}
1936
 
 
1937
 
 
1938
 
int
1939
 
JOIN::reinit()
1940
 
{
1941
 
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1942
 
                                    select_lex->offset_limit->val_uint() :
1943
 
                                    0ULL);
1944
 
 
1945
 
  first_record= 0;
1946
 
 
1947
 
  if (exec_tmp_table1)
1948
 
  {
1949
 
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1950
 
    exec_tmp_table1->file->ha_delete_all_rows();
1951
 
    free_io_cache(exec_tmp_table1);
1952
 
    filesort_free_buffers(exec_tmp_table1,0);
1953
 
  }
1954
 
  if (exec_tmp_table2)
1955
 
  {
1956
 
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1957
 
    exec_tmp_table2->file->ha_delete_all_rows();
1958
 
    free_io_cache(exec_tmp_table2);
1959
 
    filesort_free_buffers(exec_tmp_table2,0);
1960
 
  }
1961
 
  if (items0)
1962
 
    set_items_ref_array(items0);
1963
 
 
1964
 
  if (join_tab_save)
1965
 
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1966
 
 
1967
 
  if (tmp_join)
1968
 
    restore_tmp();
1969
 
 
1970
 
  /* Reset of sum functions */
1971
 
  if (sum_funcs)
1972
 
  {
1973
 
    Item_sum *func, **func_ptr= sum_funcs;
1974
 
    while ((func= *(func_ptr++)))
1975
 
      func->clear();
1976
 
  }
1977
 
 
1978
 
  return(0);
1979
 
}
1980
 
 
1981
 
/**
1982
 
   @brief Save the original join layout
1983
 
      
1984
 
   @details Saves the original join layout so it can be reused in 
1985
 
   re-execution and for EXPLAIN.
1986
 
             
1987
 
   @return Operation status
1988
 
   @retval 0      success.
1989
 
   @retval 1      error occurred.
1990
 
*/
1991
 
 
1992
 
bool
1993
 
JOIN::init_save_join_tab()
1994
 
{
1995
 
  if (!(tmp_join= (JOIN*)thd->alloc(sizeof(JOIN))))
1996
 
    return 1;                                  /* purecov: inspected */
1997
 
  error= 0;                                    // Ensure that tmp_join.error= 0
1998
 
  restore_tmp();
1999
 
  return 0;
2000
 
}
2001
 
 
2002
 
 
2003
 
bool
2004
 
JOIN::save_join_tab()
2005
 
{
2006
 
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
2007
 
  {
2008
 
    if (!(join_tab_save= (JOIN_TAB*)thd->memdup((uchar*) join_tab,
2009
 
                                                sizeof(JOIN_TAB) * tables)))
2010
 
      return 1;
2011
 
  }
2012
 
  return 0;
2013
 
}
2014
 
 
2015
 
 
2016
 
/**
2017
 
  Exec select.
2018
 
 
2019
 
  @todo
2020
 
    Note, that create_sort_index calls test_if_skip_sort_order and may
2021
 
    finally replace sorting with index scan if there is a LIMIT clause in
2022
 
    the query.  It's never shown in EXPLAIN!
2023
 
 
2024
 
  @todo
2025
 
    When can we have here thd->net.report_error not zero?
2026
 
*/
2027
 
void
2028
 
JOIN::exec()
2029
 
{
2030
 
  List<Item> *columns_list= &fields_list;
2031
 
  int      tmp_error;
2032
 
 
2033
 
  thd_proc_info(thd, "executing");
2034
 
  error= 0;
2035
 
  (void) result->prepare2(); // Currently, this cannot fail.
2036
 
 
2037
 
  if (!tables_list && (tables || !select_lex->with_sum_func))
2038
 
  {                                           // Only test of functions
2039
 
    if (select_options & SELECT_DESCRIBE)
2040
 
      select_describe(this, false, false, false,
2041
 
                      (zero_result_cause?zero_result_cause:"No tables used"));
2042
 
    else
2043
 
    {
2044
 
      result->send_fields(*columns_list,
2045
 
                          Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2046
 
      /*
2047
 
        We have to test for 'conds' here as the WHERE may not be constant
2048
 
        even if we don't have any tables for prepared statements or if
2049
 
        conds uses something like 'rand()'.
2050
 
      */
2051
 
      if (cond_value != Item::COND_FALSE &&
2052
 
          (!conds || conds->val_int()) &&
2053
 
          (!having || having->val_int()))
2054
 
      {
2055
 
        if (do_send_rows && result->send_data(fields_list))
2056
 
          error= 1;
2057
 
        else
2058
 
        {
2059
 
          error= (int) result->send_eof();
2060
 
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 :
2061
 
                         thd->sent_row_count);
2062
 
        }
2063
 
      }
2064
 
      else
2065
 
      {
2066
 
        error=(int) result->send_eof();
2067
 
        send_records= 0;
2068
 
      }
2069
 
    }
2070
 
    /* Single select (without union) always returns 0 or 1 row */
2071
 
    thd->limit_found_rows= send_records;
2072
 
    thd->examined_row_count= 0;
2073
 
    return;
2074
 
  }
2075
 
  /*
2076
 
    Don't reset the found rows count if there're no tables as
2077
 
    FOUND_ROWS() may be called. Never reset the examined row count here.
2078
 
    It must be accumulated from all join iterations of all join parts.
2079
 
  */
2080
 
  if (tables)
2081
 
    thd->limit_found_rows= 0;
2082
 
 
2083
 
  if (zero_result_cause)
2084
 
  {
2085
 
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
2086
 
                            *columns_list,
2087
 
                            send_row_on_empty_set(),
2088
 
                            select_options,
2089
 
                            zero_result_cause,
2090
 
                            having);
2091
 
    return;
2092
 
  }
2093
 
 
2094
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) &&
2095
 
      get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2096
 
    return;
2097
 
 
2098
 
  if (select_options & SELECT_DESCRIBE)
2099
 
  {
2100
 
    /*
2101
 
      Check if we managed to optimize ORDER BY away and don't use temporary
2102
 
      table to resolve ORDER BY: in that case, we only may need to do
2103
 
      filesort for GROUP BY.
2104
 
    */
2105
 
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2106
 
    {
2107
 
      /*
2108
 
        Reset 'order' to 'group_list' and reinit variables describing
2109
 
        'order'
2110
 
      */
2111
 
      order= group_list;
2112
 
      simple_order= simple_group;
2113
 
      skip_sort_order= 0;
2114
 
    }
2115
 
    if (order && 
2116
 
        (order != group_list || !(select_options & SELECT_BIG_RESULT)) &&
2117
 
        (const_tables == tables ||
2118
 
         ((simple_order || skip_sort_order) &&
2119
 
          test_if_skip_sort_order(&join_tab[const_tables], order,
2120
 
                                  select_limit, 0, 
2121
 
                                  &join_tab[const_tables].table->
2122
 
                                    keys_in_use_for_query))))
2123
 
      order=0;
2124
 
    having= tmp_having;
2125
 
    select_describe(this, need_tmp,
2126
 
                    order != 0 && !skip_sort_order,
2127
 
                    select_distinct,
2128
 
                    !tables ? "No tables used" : NullS);
2129
 
    return;
2130
 
  }
2131
 
 
2132
 
  JOIN *curr_join= this;
2133
 
  List<Item> *curr_all_fields= &all_fields;
2134
 
  List<Item> *curr_fields_list= &fields_list;
2135
 
  TABLE *curr_tmp_table= 0;
2136
 
  /*
2137
 
    Initialize examined rows here because the values from all join parts
2138
 
    must be accumulated in examined_row_count. Hence every join
2139
 
    iteration must count from zero.
2140
 
  */
2141
 
  curr_join->examined_rows= 0;
2142
 
 
2143
 
  /* Create a tmp table if distinct or if the sort is too complicated */
2144
 
  if (need_tmp)
2145
 
  {
2146
 
    if (tmp_join)
2147
 
    {
2148
 
      /*
2149
 
        We are in a non cacheable sub query. Get the saved join structure
2150
 
        after optimization.
2151
 
        (curr_join may have been modified during last exection and we need
2152
 
        to reset it)
2153
 
      */
2154
 
      curr_join= tmp_join;
2155
 
    }
2156
 
    curr_tmp_table= exec_tmp_table1;
2157
 
 
2158
 
    /* Copy data to the temporary table */
2159
 
    thd_proc_info(thd, "Copying to tmp table");
2160
 
    if (!curr_join->sort_and_group &&
2161
 
        curr_join->const_tables != curr_join->tables)
2162
 
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2163
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2164
 
    {
2165
 
      error= tmp_error;
2166
 
      return;
2167
 
    }
2168
 
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2169
 
    
2170
 
    if (curr_join->having)
2171
 
      curr_join->having= curr_join->tmp_having= 0; // Allready done
2172
 
    
2173
 
    /* Change sum_fields reference to calculated fields in tmp_table */
2174
 
    curr_join->all_fields= *curr_all_fields;
2175
 
    if (!items1)
2176
 
    {
2177
 
      items1= items0 + all_fields.elements;
2178
 
      if (sort_and_group || curr_tmp_table->group)
2179
 
      {
2180
 
        if (change_to_use_tmp_fields(thd, items1,
2181
 
                                     tmp_fields_list1, tmp_all_fields1,
2182
 
                                     fields_list.elements, all_fields))
2183
 
          return;
2184
 
      }
2185
 
      else
2186
 
      {
2187
 
        if (change_refs_to_tmp_fields(thd, items1,
2188
 
                                      tmp_fields_list1, tmp_all_fields1,
2189
 
                                      fields_list.elements, all_fields))
2190
 
          return;
2191
 
      }
2192
 
      curr_join->tmp_all_fields1= tmp_all_fields1;
2193
 
      curr_join->tmp_fields_list1= tmp_fields_list1;
2194
 
      curr_join->items1= items1;
2195
 
    }
2196
 
    curr_all_fields= &tmp_all_fields1;
2197
 
    curr_fields_list= &tmp_fields_list1;
2198
 
    curr_join->set_items_ref_array(items1);
2199
 
    
2200
 
    if (sort_and_group || curr_tmp_table->group)
2201
 
    {
2202
 
      curr_join->tmp_table_param.field_count+= 
2203
 
        curr_join->tmp_table_param.sum_func_count+
2204
 
        curr_join->tmp_table_param.func_count;
2205
 
      curr_join->tmp_table_param.sum_func_count= 
2206
 
        curr_join->tmp_table_param.func_count= 0;
2207
 
    }
2208
 
    else
2209
 
    {
2210
 
      curr_join->tmp_table_param.field_count+= 
2211
 
        curr_join->tmp_table_param.func_count;
2212
 
      curr_join->tmp_table_param.func_count= 0;
2213
 
    }
2214
 
    
2215
 
    if (curr_tmp_table->group)
2216
 
    {                                           // Already grouped
2217
 
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2218
 
        curr_join->order= curr_join->group_list;  /* order by group */
2219
 
      curr_join->group_list= 0;
2220
 
    }
2221
 
    
2222
 
    /*
2223
 
      If we have different sort & group then we must sort the data by group
2224
 
      and copy it to another tmp table
2225
 
      This code is also used if we are using distinct something
2226
 
      we haven't been able to store in the temporary table yet
2227
 
      like SEC_TO_TIME(SUM(...)).
2228
 
    */
2229
 
 
2230
 
    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))
2231
 
    {                                   /* Must copy to another table */
2232
 
      /* Free first data from old join */
2233
 
      curr_join->join_free();
2234
 
      if (make_simple_join(curr_join, curr_tmp_table))
2235
 
        return;
2236
 
      calc_group_buffer(curr_join, group_list);
2237
 
      count_field_types(select_lex, &curr_join->tmp_table_param,
2238
 
                        curr_join->tmp_all_fields1,
2239
 
                        curr_join->select_distinct && !curr_join->group_list);
2240
 
      curr_join->tmp_table_param.hidden_field_count= 
2241
 
        (curr_join->tmp_all_fields1.elements-
2242
 
         curr_join->tmp_fields_list1.elements);
2243
 
      
2244
 
      
2245
 
      if (exec_tmp_table2)
2246
 
        curr_tmp_table= exec_tmp_table2;
2247
 
      else
2248
 
      {
2249
 
        /* group data to new table */
2250
 
 
2251
 
        /*
2252
 
          If the access method is loose index scan then all MIN/MAX
2253
 
          functions are precomputed, and should be treated as regular
2254
 
          functions. See extended comment in JOIN::exec.
2255
 
        */
2256
 
        if (curr_join->join_tab->is_using_loose_index_scan())
2257
 
          curr_join->tmp_table_param.precomputed_group_by= true;
2258
 
 
2259
 
        if (!(curr_tmp_table=
2260
 
              exec_tmp_table2= create_tmp_table(thd,
2261
 
                                                &curr_join->tmp_table_param,
2262
 
                                                *curr_all_fields,
2263
 
                                                (ORDER*) 0,
2264
 
                                                curr_join->select_distinct && 
2265
 
                                                !curr_join->group_list,
2266
 
                                                1, curr_join->select_options,
2267
 
                                                HA_POS_ERROR,
2268
 
                                                (char *) "")))
2269
 
          return;
2270
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
2271
 
      }
2272
 
      if (curr_join->group_list)
2273
 
      {
2274
 
        thd_proc_info(thd, "Creating sort index");
2275
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2276
 
        {
2277
 
          return;
2278
 
        }
2279
 
        if (create_sort_index(thd, curr_join, curr_join->group_list,
2280
 
                              HA_POS_ERROR, HA_POS_ERROR, false) ||
2281
 
            make_group_fields(this, curr_join))
2282
 
        {
2283
 
          return;
2284
 
        }
2285
 
        sortorder= curr_join->sortorder;
2286
 
      }
2287
 
      
2288
 
      thd_proc_info(thd, "Copying to group table");
2289
 
      tmp_error= -1;
2290
 
      if (curr_join != this)
2291
 
      {
2292
 
        if (sum_funcs2)
2293
 
        {
2294
 
          curr_join->sum_funcs= sum_funcs2;
2295
 
          curr_join->sum_funcs_end= sum_funcs_end2; 
2296
 
        }
2297
 
        else
2298
 
        {
2299
 
          curr_join->alloc_func_list();
2300
 
          sum_funcs2= curr_join->sum_funcs;
2301
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2302
 
        }
2303
 
      }
2304
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2305
 
                                        1, true))
2306
 
        return;
2307
 
      curr_join->group_list= 0;
2308
 
      if (!curr_join->sort_and_group &&
2309
 
          curr_join->const_tables != curr_join->tables)
2310
 
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2311
 
      if (setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2312
 
          (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2313
 
      {
2314
 
        error= tmp_error;
2315
 
        return;
2316
 
      }
2317
 
      end_read_record(&curr_join->join_tab->read_record);
2318
 
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2319
 
      curr_join->join_tab[0].table= 0;           // Table is freed
2320
 
      
2321
 
      // No sum funcs anymore
2322
 
      if (!items2)
2323
 
      {
2324
 
        items2= items1 + all_fields.elements;
2325
 
        if (change_to_use_tmp_fields(thd, items2,
2326
 
                                     tmp_fields_list2, tmp_all_fields2, 
2327
 
                                     fields_list.elements, tmp_all_fields1))
2328
 
          return;
2329
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2330
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
2331
 
      }
2332
 
      curr_fields_list= &curr_join->tmp_fields_list2;
2333
 
      curr_all_fields= &curr_join->tmp_all_fields2;
2334
 
      curr_join->set_items_ref_array(items2);
2335
 
      curr_join->tmp_table_param.field_count+= 
2336
 
        curr_join->tmp_table_param.sum_func_count;
2337
 
      curr_join->tmp_table_param.sum_func_count= 0;
2338
 
    }
2339
 
    if (curr_tmp_table->distinct)
2340
 
      curr_join->select_distinct=0;             /* Each row is unique */
2341
 
    
2342
 
    curr_join->join_free();                     /* Free quick selects */
2343
 
    if (curr_join->select_distinct && ! curr_join->group_list)
2344
 
    {
2345
 
      thd_proc_info(thd, "Removing duplicates");
2346
 
      if (curr_join->tmp_having)
2347
 
        curr_join->tmp_having->update_used_tables();
2348
 
      if (remove_duplicates(curr_join, curr_tmp_table,
2349
 
                            *curr_fields_list, curr_join->tmp_having))
2350
 
        return;
2351
 
      curr_join->tmp_having=0;
2352
 
      curr_join->select_distinct=0;
2353
 
    }
2354
 
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2355
 
    if (make_simple_join(curr_join, curr_tmp_table))
2356
 
      return;
2357
 
    calc_group_buffer(curr_join, curr_join->group_list);
2358
 
    count_field_types(select_lex, &curr_join->tmp_table_param, 
2359
 
                      *curr_all_fields, 0);
2360
 
    
2361
 
  }
2362
 
  
2363
 
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2364
 
  {
2365
 
    if (make_group_fields(this, curr_join))
2366
 
    {
2367
 
      return;
2368
 
    }
2369
 
    if (!items3)
2370
 
    {
2371
 
      if (!items0)
2372
 
        init_items_ref_array();
2373
 
      items3= ref_pointer_array + (all_fields.elements*4);
2374
 
      setup_copy_fields(thd, &curr_join->tmp_table_param,
2375
 
                        items3, tmp_fields_list3, tmp_all_fields3,
2376
 
                        curr_fields_list->elements, *curr_all_fields);
2377
 
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2378
 
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2379
 
      tmp_table_param.save_copy_field_end=
2380
 
        curr_join->tmp_table_param.copy_field_end;
2381
 
      curr_join->tmp_all_fields3= tmp_all_fields3;
2382
 
      curr_join->tmp_fields_list3= tmp_fields_list3;
2383
 
    }
2384
 
    else
2385
 
    {
2386
 
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2387
 
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2388
 
      curr_join->tmp_table_param.copy_field_end=
2389
 
        tmp_table_param.save_copy_field_end;
2390
 
    }
2391
 
    curr_fields_list= &tmp_fields_list3;
2392
 
    curr_all_fields= &tmp_all_fields3;
2393
 
    curr_join->set_items_ref_array(items3);
2394
 
 
2395
 
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2396
 
                                      1, true) || 
2397
 
        setup_sum_funcs(curr_join->thd, curr_join->sum_funcs) ||
2398
 
        thd->is_fatal_error)
2399
 
      return;
2400
 
  }
2401
 
  if (curr_join->group_list || curr_join->order)
2402
 
  {
2403
 
    thd_proc_info(thd, "Sorting result");
2404
 
    /* If we have already done the group, add HAVING to sorted table */
2405
 
    if (curr_join->tmp_having && ! curr_join->group_list && 
2406
 
        ! curr_join->sort_and_group)
2407
 
    {
2408
 
      // Some tables may have been const
2409
 
      curr_join->tmp_having->update_used_tables();
2410
 
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2411
 
      table_map used_tables= (curr_join->const_table_map |
2412
 
                              curr_table->table->map);
2413
 
 
2414
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having,
2415
 
                                                 used_tables,
2416
 
                                                 used_tables, 0);
2417
 
      if (sort_table_cond)
2418
 
      {
2419
 
        if (!curr_table->select)
2420
 
          if (!(curr_table->select= new SQL_SELECT))
2421
 
            return;
2422
 
        if (!curr_table->select->cond)
2423
 
          curr_table->select->cond= sort_table_cond;
2424
 
        else                                    // This should never happen
2425
 
        {
2426
 
          if (!(curr_table->select->cond=
2427
 
                new Item_cond_and(curr_table->select->cond,
2428
 
                                  sort_table_cond)))
2429
 
            return;
2430
 
          /*
2431
 
            Item_cond_and do not need fix_fields for execution, its parameters
2432
 
            are fixed or do not need fix_fields, too
2433
 
          */
2434
 
          curr_table->select->cond->quick_fix_field();
2435
 
        }
2436
 
        curr_table->select_cond= curr_table->select->cond;
2437
 
        curr_table->select_cond->top_level_item();
2438
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2439
 
                                                   ~ (table_map) 0,
2440
 
                                                   ~used_tables, 0);
2441
 
      }
2442
 
    }
2443
 
    {
2444
 
      if (group)
2445
 
        curr_join->select_limit= HA_POS_ERROR;
2446
 
      else
2447
 
      {
2448
 
        /*
2449
 
          We can abort sorting after thd->select_limit rows if we there is no
2450
 
          WHERE clause for any tables after the sorted one.
2451
 
        */
2452
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2453
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2454
 
        for (; curr_table < end_table ; curr_table++)
2455
 
        {
2456
 
          /*
2457
 
            table->keyuse is set in the case there was an original WHERE clause
2458
 
            on the table that was optimized away.
2459
 
          */
2460
 
          if (curr_table->select_cond ||
2461
 
              (curr_table->keyuse && !curr_table->first_inner))
2462
 
          {
2463
 
            /* We have to sort all rows */
2464
 
            curr_join->select_limit= HA_POS_ERROR;
2465
 
            break;
2466
 
          }
2467
 
        }
2468
 
      }
2469
 
      if (curr_join->join_tab == join_tab && save_join_tab())
2470
 
      {
2471
 
        return;
2472
 
      }
2473
 
      /*
2474
 
        Here we sort rows for ORDER BY/GROUP BY clause, if the optimiser
2475
 
        chose FILESORT to be faster than INDEX SCAN or there is no 
2476
 
        suitable index present.
2477
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2478
 
        finally replace sorting with index scan if there is a LIMIT clause in
2479
 
        the query. XXX: it's never shown in EXPLAIN!
2480
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2481
 
      */
2482
 
      if (create_sort_index(thd, curr_join,
2483
 
                            curr_join->group_list ? 
2484
 
                            curr_join->group_list : curr_join->order,
2485
 
                            curr_join->select_limit,
2486
 
                            (select_options & OPTION_FOUND_ROWS ?
2487
 
                             HA_POS_ERROR : unit->select_limit_cnt),
2488
 
                            curr_join->group_list ? true : false))
2489
 
        return;
2490
 
      sortorder= curr_join->sortorder;
2491
 
      if (curr_join->const_tables != curr_join->tables &&
2492
 
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2493
 
      {
2494
 
        /*
2495
 
          If no IO cache exists for the first table then we are using an
2496
 
          INDEX SCAN and no filesort. Thus we should not remove the sorted
2497
 
          attribute on the INDEX SCAN.
2498
 
        */
2499
 
        skip_sort_order= 1;
2500
 
      }
2501
 
    }
2502
 
  }
2503
 
  /* XXX: When can we have here thd->is_error() not zero? */
2504
 
  if (thd->is_error())
2505
 
  {
2506
 
    error= thd->is_error();
2507
 
    return;
2508
 
  }
2509
 
  curr_join->having= curr_join->tmp_having;
2510
 
  curr_join->fields= curr_fields_list;
2511
 
 
2512
 
  {
2513
 
    thd_proc_info(thd, "Sending data");
2514
 
    result->send_fields(*curr_fields_list,
2515
 
                        Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2516
 
    error= do_select(curr_join, curr_fields_list, NULL);
2517
 
    thd->limit_found_rows= curr_join->send_records;
2518
 
  }
2519
 
 
2520
 
  /* Accumulate the counts from all join iterations of all join parts. */
2521
 
  thd->examined_row_count+= curr_join->examined_rows;
2522
 
 
2523
 
  /* 
2524
 
    With EXPLAIN EXTENDED we have to restore original ref_array
2525
 
    for a derived table which is always materialized.
2526
 
    Otherwise we would not be able to print the query  correctly.
2527
 
  */ 
2528
 
  if (items0 &&
2529
 
      (thd->lex->describe & DESCRIBE_EXTENDED) &&
2530
 
      select_lex->linkage == DERIVED_TABLE_TYPE)      
2531
 
    set_items_ref_array(items0);
2532
 
 
2533
 
  return;
2534
 
}
2535
 
 
2536
 
 
2537
 
/**
2538
 
  Clean up join.
2539
 
 
2540
 
  @return
2541
 
    Return error that hold JOIN.
2542
 
*/
2543
 
 
2544
 
int
2545
 
JOIN::destroy()
2546
 
{
2547
 
  select_lex->join= 0;
2548
 
 
2549
 
  if (tmp_join)
2550
 
  {
2551
 
    if (join_tab != tmp_join->join_tab)
2552
 
    {
2553
 
      JOIN_TAB *tab, *end;
2554
 
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2555
 
        tab->cleanup();
2556
 
    }
2557
 
    tmp_join->tmp_join= 0;
2558
 
    tmp_table_param.copy_field=0;
2559
 
    return(tmp_join->destroy());
2560
 
  }
2561
 
  cond_equal= 0;
2562
 
 
2563
 
  cleanup(1);
2564
 
  if (exec_tmp_table1)
2565
 
    free_tmp_table(thd, exec_tmp_table1);
2566
 
  if (exec_tmp_table2)
2567
 
    free_tmp_table(thd, exec_tmp_table2);
2568
 
  delete select;
2569
 
  delete_dynamic(&keyuse);
2570
 
  return(error);
2571
 
}
2572
 
 
2573
 
 
2574
 
 
2575
305
/**
2576
306
  An entry point to single-unit select (a select without UNION).
2577
307
 
2578
 
  @param thd                  thread handler
 
308
  @param session                  thread Cursor
2579
309
  @param rref_pointer_array   a reference to ref_pointer_array of
2580
310
                              the top-level select_lex for this query
2581
311
  @param tables               list of all tables used in this query.
2582
312
                              The tables have been pre-opened.
2583
 
  @param wild_num             number of wildcards used in the top level 
 
313
  @param wild_num             number of wildcards used in the top level
2584
314
                              select of this query.
2585
315
                              For example statement
2586
316
                              SELECT *, t1.*, catalog.t2.* FROM t0, t1, t2;
2591
321
                              for a, b and c in this list.
2592
322
  @param conds                top level item of an expression representing
2593
323
                              WHERE clause of the top level select
2594
 
  @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
2595
325
                              arguments
2596
 
  @param order                linked list of ORDER BY agruments
 
326
  @param order                linked list of order_st BY agruments
2597
327
  @param group                linked list of GROUP BY arguments
2598
328
  @param having               top level item of HAVING expression
2599
 
  @param proc_param           list of PROCEDUREs
2600
329
  @param select_options       select options (BIG_RESULT, etc)
2601
330
  @param result               an instance of result set handling class.
2602
331
                              This object is responsible for send result
2603
332
                              set rows to the client or inserting them
2604
333
                              into a table.
2605
 
  @param select_lex           the only SELECT_LEX of this query
 
334
  @param select_lex           the only Select_Lex of this query
2606
335
  @param unit                 top-level UNIT of this query
2607
336
                              UNIT is an artificial object created by the
2608
337
                              parser for every SELECT clause.
2615
344
  @retval
2616
345
    true   an error
2617
346
*/
2618
 
 
2619
 
bool
2620
 
mysql_select(THD *thd, Item ***rref_pointer_array,
2621
 
             TABLE_LIST *tables, uint wild_num, List<Item> &fields,
2622
 
             COND *conds, uint og_num,  ORDER *order, ORDER *group,
2623
 
             Item *having, ORDER *proc_param, uint64_t select_options,
2624
 
             select_result *result, SELECT_LEX_UNIT *unit,
2625
 
             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)
2626
361
{
2627
362
  bool err;
2628
363
  bool free_join= 1;
2637
372
      creation
2638
373
    */
2639
374
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2640
 
        (select_options & SELECT_DESCRIBE))
 
375
        (select_options & SELECT_DESCRIBE))
2641
376
    {
2642
377
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2643
378
      {
2644
 
        //here is EXPLAIN of subselect or derived table
2645
 
        if (join->change_result(result))
2646
 
        {
2647
 
          return(true);
2648
 
        }
 
379
        //here is EXPLAIN of subselect or derived table
 
380
        if (join->change_result(result))
 
381
        {
 
382
          return(true);
 
383
        }
2649
384
      }
2650
385
      else
2651
386
      {
2652
387
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2653
 
                               conds, og_num, order, group, having, proc_param,
2654
 
                               select_lex, unit)))
2655
 
        {
2656
 
          goto err;
2657
 
        }
 
388
                               conds, og_num, order, group, having, select_lex, unit)))
 
389
        {
 
390
          goto err;
 
391
        }
2658
392
      }
2659
393
    }
2660
394
    free_join= 0;
2662
396
  }
2663
397
  else
2664
398
  {
2665
 
    if (!(join= new JOIN(thd, fields, select_options, result)))
2666
 
        return(true);
2667
 
    thd_proc_info(thd, "init");
2668
 
    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
2669
403
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2670
 
                           conds, og_num, order, group, having, proc_param,
 
404
                           conds, og_num, order, group, having,
2671
405
                           select_lex, unit)) == true)
2672
406
    {
2673
407
      goto err;
2674
408
    }
2675
409
  }
2676
410
 
2677
 
  /* dump_TABLE_LIST_graph(select_lex, select_lex->leaf_tables); */
2678
 
  if (join->flatten_subqueries())
2679
 
  {
2680
 
    err= 1;
2681
 
    goto err;
2682
 
  }
2683
 
  /* dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables); */
2684
 
 
2685
 
  if ((err= join->optimize()))
2686
 
  {
2687
 
    goto err;                                   // 1
2688
 
  }
2689
 
 
2690
 
  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)
2691
418
  {
2692
419
    join->conds_history= join->conds;
2693
420
    join->having_history= (join->having?join->having:join->tmp_having);
2694
421
  }
2695
422
 
2696
 
  if (thd->is_error())
 
423
  if (session->is_error())
2697
424
    goto err;
2698
425
 
2699
426
  join->exec();
2700
427
 
2701
 
  if (thd->lex->describe & DESCRIBE_EXTENDED)
 
428
  if (session->lex->describe & DESCRIBE_EXTENDED)
2702
429
  {
2703
430
    select_lex->where= join->conds_history;
2704
431
    select_lex->having= join->having_history;
2707
434
err:
2708
435
  if (free_join)
2709
436
  {
2710
 
    thd_proc_info(thd, "end");
 
437
    session->set_proc_info("end");
2711
438
    err|= select_lex->cleanup();
2712
 
    return(err || thd->is_error());
 
439
    return(err || session->is_error());
2713
440
  }
2714
441
  return(join->error);
2715
442
}
2716
443
 
2717
 
 
2718
 
int subq_sj_candidate_cmp(Item_in_subselect* const *el1, 
2719
 
                          Item_in_subselect* const *el2)
2720
 
{
2721
 
  return ((*el1)->sj_convert_priority < (*el2)->sj_convert_priority) ? 1 : 
2722
 
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2723
 
}
2724
 
 
2725
 
 
2726
 
inline Item * and_items(Item* cond, Item *item)
 
444
inline Item *and_items(Item* cond, Item *item)
2727
445
{
2728
446
  return (cond? (new Item_cond_and(cond, item)) : item);
2729
447
}
2730
448
 
2731
 
 
2732
 
static TABLE_LIST *alloc_join_nest(THD *thd)
2733
 
{
2734
 
  TABLE_LIST *tbl;
2735
 
  if (!(tbl= (TABLE_LIST*) thd->calloc(ALIGN_SIZE(sizeof(TABLE_LIST))+
2736
 
                                       sizeof(NESTED_JOIN))))
2737
 
    return NULL;
2738
 
  tbl->nested_join= (NESTED_JOIN*) ((uchar*)tbl + 
2739
 
                                    ALIGN_SIZE(sizeof(TABLE_LIST)));
2740
 
  return tbl;
2741
 
}
2742
 
 
2743
 
 
2744
 
void fix_list_after_tbl_changes(SELECT_LEX *new_parent, List<TABLE_LIST> *tlist)
2745
 
{
2746
 
  List_iterator<TABLE_LIST> it(*tlist);
2747
 
  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;
2748
453
  while ((table= it++))
2749
454
  {
2750
455
    if (table->on_expr)
2754
459
  }
2755
460
}
2756
461
 
2757
 
 
2758
 
/*
2759
 
  Convert a subquery predicate into a TABLE_LIST semi-join nest
2760
 
 
2761
 
  SYNOPSIS
2762
 
    convert_subq_to_sj()
2763
 
       parent_join  Parent join, the one that has subq_pred in its WHERE/ON 
2764
 
                    clause
2765
 
       subq_pred    Subquery predicate to be converted
2766
 
  
2767
 
  DESCRIPTION
2768
 
    Convert a subquery predicate into a TABLE_LIST semi-join nest. All the 
2769
 
    prerequisites are already checked, so the conversion is always successfull.
2770
 
 
2771
 
    Prepared Statements: the transformation is permanent:
2772
 
     - Changes in TABLE_LIST structures are naturally permanent
2773
 
     - Item tree changes are performed on statement MEM_ROOT:
2774
 
        = we activate statement MEM_ROOT 
2775
 
        = this function is called before the first fix_prepare_information
2776
 
          call.
2777
 
 
2778
 
    This is intended because the criteria for subquery-to-sj conversion remain
2779
 
    constant for the lifetime of the Prepared Statement.
2780
 
 
2781
 
  RETURN
2782
 
    false  OK
2783
 
    true   Out of memory error
2784
 
*/
2785
 
 
2786
 
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2787
 
{
2788
 
  SELECT_LEX *parent_lex= parent_join->select_lex;
2789
 
  TABLE_LIST *emb_tbl_nest= NULL;
2790
 
  List<TABLE_LIST> *emb_join_list= &parent_lex->top_join_list;
2791
 
  THD *thd= parent_join->thd;
2792
 
 
2793
 
  /*
2794
 
    1. Find out where to put the predicate into.
2795
 
     Note: for "t1 LEFT JOIN t2" this will be t2, a leaf.
2796
 
  */
2797
 
  if ((void*)subq_pred->expr_join_nest != (void*)1)
2798
 
  {
2799
 
    if (subq_pred->expr_join_nest->nested_join)
2800
 
    {
2801
 
      /*
2802
 
        We're dealing with
2803
 
 
2804
 
          ... [LEFT] JOIN  ( ... ) ON (subquery AND whatever) ...
2805
 
 
2806
 
        The sj-nest will be inserted into the brackets nest.
2807
 
      */
2808
 
      emb_tbl_nest=  subq_pred->expr_join_nest;
2809
 
      emb_join_list= &emb_tbl_nest->nested_join->join_list;
2810
 
    }
2811
 
    else if (!subq_pred->expr_join_nest->outer_join)
2812
 
    {
2813
 
      /*
2814
 
        We're dealing with
2815
 
 
2816
 
          ... INNER JOIN tblX ON (subquery AND whatever) ...
2817
 
 
2818
 
        The sj-nest will be tblX's "sibling", i.e. another child of its
2819
 
        parent. This is ok because tblX is joined as an inner join.
2820
 
      */
2821
 
      emb_tbl_nest= subq_pred->expr_join_nest->embedding;
2822
 
      if (emb_tbl_nest)
2823
 
        emb_join_list= &emb_tbl_nest->nested_join->join_list;
2824
 
    }
2825
 
    else if (!subq_pred->expr_join_nest->nested_join)
2826
 
    {
2827
 
      TABLE_LIST *outer_tbl= subq_pred->expr_join_nest;      
2828
 
      TABLE_LIST *wrap_nest;
2829
 
      /*
2830
 
        We're dealing with
2831
 
 
2832
 
          ... LEFT JOIN tbl ON (on_expr AND subq_pred) ...
2833
 
 
2834
 
        we'll need to convert it into:
2835
 
 
2836
 
          ... LEFT JOIN ( tbl SJ (subq_tables) ) ON (on_expr AND subq_pred) ...
2837
 
                        |                      |
2838
 
                        |<----- wrap_nest ---->|
2839
 
        
2840
 
        Q:  other subqueries may be pointing to this element. What to do?
2841
 
        A1: simple solution: copy *subq_pred->expr_join_nest= *parent_nest.
2842
 
            But we'll need to fix other pointers.
2843
 
        A2: Another way: have TABLE_LIST::next_ptr so the following
2844
 
            subqueries know the table has been nested.
2845
 
        A3: changes in the TABLE_LIST::outer_join will make everything work
2846
 
            automatically.
2847
 
      */
2848
 
      if (!(wrap_nest= alloc_join_nest(parent_join->thd)))
2849
 
      {
2850
 
        return(true);
2851
 
      }
2852
 
      wrap_nest->embedding= outer_tbl->embedding;
2853
 
      wrap_nest->join_list= outer_tbl->join_list;
2854
 
      wrap_nest->alias= (char*) "(sj-wrap)";
2855
 
 
2856
 
      wrap_nest->nested_join->join_list.empty();
2857
 
      wrap_nest->nested_join->join_list.push_back(outer_tbl);
2858
 
 
2859
 
      outer_tbl->embedding= wrap_nest;
2860
 
      outer_tbl->join_list= &wrap_nest->nested_join->join_list;
2861
 
 
2862
 
      /*
2863
 
        wrap_nest will take place of outer_tbl, so move the outer join flag
2864
 
        and on_expr
2865
 
      */
2866
 
      wrap_nest->outer_join= outer_tbl->outer_join;
2867
 
      outer_tbl->outer_join= 0;
2868
 
 
2869
 
      wrap_nest->on_expr= outer_tbl->on_expr;
2870
 
      outer_tbl->on_expr= NULL;
2871
 
 
2872
 
      List_iterator<TABLE_LIST> li(*wrap_nest->join_list);
2873
 
      TABLE_LIST *tbl;
2874
 
      while ((tbl= li++))
2875
 
      {
2876
 
        if (tbl == outer_tbl)
2877
 
        {
2878
 
          li.replace(wrap_nest);
2879
 
          break;
2880
 
        }
2881
 
      }
2882
 
      /*
2883
 
        Ok now wrap_nest 'contains' outer_tbl and we're ready to add the 
2884
 
        semi-join nest into it
2885
 
      */
2886
 
      emb_join_list= &wrap_nest->nested_join->join_list;
2887
 
      emb_tbl_nest=  wrap_nest;
2888
 
    }
2889
 
  }
2890
 
 
2891
 
  TABLE_LIST *sj_nest;
2892
 
  NESTED_JOIN *nested_join;
2893
 
  if (!(sj_nest= alloc_join_nest(parent_join->thd)))
2894
 
  {
2895
 
    return(true);
2896
 
  }
2897
 
  nested_join= sj_nest->nested_join;
2898
 
 
2899
 
  sj_nest->join_list= emb_join_list;
2900
 
  sj_nest->embedding= emb_tbl_nest;
2901
 
  sj_nest->alias= (char*) "(sj-nest)";
2902
 
  /* Nests do not participate in those 'chains', so: */
2903
 
  /* sj_nest->next_leaf= sj_nest->next_local= sj_nest->next_global == NULL*/
2904
 
  emb_join_list->push_back(sj_nest);
2905
 
 
2906
 
  /* 
2907
 
    nested_join->used_tables and nested_join->not_null_tables are
2908
 
    initialized in simplify_joins().
2909
 
  */
2910
 
  
2911
 
  /* 
2912
 
    2. Walk through subquery's top list and set 'embedding' to point to the
2913
 
       sj-nest.
2914
 
  */
2915
 
  st_select_lex *subq_lex= subq_pred->unit->first_select();
2916
 
  nested_join->join_list.empty();
2917
 
  List_iterator_fast<TABLE_LIST> li(subq_lex->top_join_list);
2918
 
  TABLE_LIST *tl, *last_leaf;
2919
 
  while ((tl= li++))
2920
 
  {
2921
 
    tl->embedding= sj_nest;
2922
 
    tl->join_list= &nested_join->join_list;
2923
 
    nested_join->join_list.push_back(tl);
2924
 
  }
2925
 
  
2926
 
  /*
2927
 
    Reconnect the next_leaf chain.
2928
 
    TODO: Do we have to put subquery's tables at the end of the chain?
2929
 
          Inserting them at the beginning would be a bit faster.
2930
 
    NOTE: We actually insert them at the front! That's because the order is
2931
 
          reversed in this list.
2932
 
  */
2933
 
  for (tl= parent_lex->leaf_tables; tl->next_leaf; tl= tl->next_leaf) {};
2934
 
  tl->next_leaf= subq_lex->leaf_tables;
2935
 
  last_leaf= tl;
2936
 
 
2937
 
  /*
2938
 
    Same as above for next_local chain
2939
 
    (a theory: a next_local chain always starts with ::leaf_tables
2940
 
     because view's tables are inserted after the view)
2941
 
  */
2942
 
  for (tl= parent_lex->leaf_tables; tl->next_local; tl= tl->next_local) {};
2943
 
  tl->next_local= subq_lex->leaf_tables;
2944
 
 
2945
 
  /* A theory: no need to re-connect the next_global chain */
2946
 
 
2947
 
  /* 3. Remove the original subquery predicate from the WHERE/ON */
2948
 
 
2949
 
  // The subqueries were replaced for Item_int(1) earlier
2950
 
  subq_pred->exec_method= Item_in_subselect::SEMI_JOIN; // for subsequent executions
2951
 
  /*TODO: also reset the 'with_subselect' there. */
2952
 
 
2953
 
  /* n. Adjust the parent_join->tables counter */
2954
 
  uint table_no= parent_join->tables;
2955
 
  /* n. Walk through child's tables and adjust table->map */
2956
 
  for (tl= subq_lex->leaf_tables; tl; tl= tl->next_leaf, table_no++)
2957
 
  {
2958
 
    tl->table->tablenr= table_no;
2959
 
    tl->table->map= ((table_map)1) << table_no;
2960
 
    SELECT_LEX *old_sl= tl->select_lex;
2961
 
    tl->select_lex= parent_join->select_lex; 
2962
 
    for(TABLE_LIST *emb= tl->embedding; emb && emb->select_lex == old_sl; emb= emb->embedding)
2963
 
      emb->select_lex= parent_join->select_lex;
2964
 
  }
2965
 
  parent_join->tables += subq_lex->join->tables;
2966
 
 
2967
 
  /* 
2968
 
    Put the subquery's WHERE into semi-join's sj_on_expr
2969
 
    Add the subquery-induced equalities too.
2970
 
  */
2971
 
  SELECT_LEX *save_lex= thd->lex->current_select;
2972
 
  thd->lex->current_select=subq_lex;
2973
 
  if (!subq_pred->left_expr->fixed &&
2974
 
       subq_pred->left_expr->fix_fields(thd, &subq_pred->left_expr))
2975
 
    return(true);
2976
 
  thd->lex->current_select=save_lex;
2977
 
 
2978
 
  sj_nest->nested_join->sj_corr_tables= subq_pred->used_tables();
2979
 
  sj_nest->nested_join->sj_depends_on=  subq_pred->used_tables() |
2980
 
                                        subq_pred->left_expr->used_tables();
2981
 
  sj_nest->sj_on_expr= subq_lex->where;
2982
 
 
2983
 
  /*
2984
 
    Create the IN-equalities and inject them into semi-join's ON expression.
2985
 
    Additionally, for InsideOut strategy
2986
 
     - Record the number of IN-equalities.
2987
 
     - Create list of pointers to (oe1, ..., ieN). We'll need the list to
2988
 
       see which of the expressions are bound and which are not (for those
2989
 
       we'll produce a distinct stream of (ie_i1,...ie_ik).
2990
 
 
2991
 
       (TODO: can we just create a list of pointers and hope the expressions
2992
 
       will not substitute themselves on fix_fields()? or we need to wrap
2993
 
       them into Item_direct_view_refs and store pointers to those. The
2994
 
       pointers to Item_direct_view_refs are guaranteed to be stable as 
2995
 
       Item_direct_view_refs doesn't substitute itself with anything in 
2996
 
       Item_direct_view_ref::fix_fields.
2997
 
  */
2998
 
  sj_nest->sj_in_exprs= subq_pred->left_expr->cols();
2999
 
  sj_nest->nested_join->sj_outer_expr_list.empty();
3000
 
 
3001
 
  if (subq_pred->left_expr->cols() == 1)
3002
 
  {
3003
 
    nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr);
3004
 
 
3005
 
    Item *item_eq= new Item_func_eq(subq_pred->left_expr, 
3006
 
                                    subq_lex->ref_pointer_array[0]);
3007
 
    item_eq->name= (char*)subq_sj_cond_name;
3008
 
    sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3009
 
  }
3010
 
  else
3011
 
  {
3012
 
    for (uint i= 0; i < subq_pred->left_expr->cols(); i++)
3013
 
    {
3014
 
      nested_join->sj_outer_expr_list.push_back(subq_pred->left_expr->
3015
 
                                                element_index(i));
3016
 
      Item *item_eq= 
3017
 
        new Item_func_eq(subq_pred->left_expr->element_index(i), 
3018
 
                         subq_lex->ref_pointer_array[i]);
3019
 
      item_eq->name= (char*)subq_sj_cond_name + (i % 64);
3020
 
      sj_nest->sj_on_expr= and_items(sj_nest->sj_on_expr, item_eq);
3021
 
    }
3022
 
  }
3023
 
  /* Fix the created equality and AND */
3024
 
  sj_nest->sj_on_expr->fix_fields(parent_join->thd, &sj_nest->sj_on_expr);
3025
 
 
3026
 
  /*
3027
 
    Walk through sj nest's WHERE and ON expressions and call
3028
 
    item->fix_table_changes() for all items.
3029
 
  */
3030
 
  sj_nest->sj_on_expr->fix_after_pullout(parent_lex, &sj_nest->sj_on_expr);
3031
 
  fix_list_after_tbl_changes(parent_lex, &sj_nest->nested_join->join_list);
3032
 
 
3033
 
 
3034
 
  /* Unlink the child select_lex so it doesn't show up in EXPLAIN: */
3035
 
  subq_lex->master_unit()->exclude_level();
3036
 
 
3037
 
  /* Inject sj_on_expr into the parent's WHERE or ON */
3038
 
  if (emb_tbl_nest)
3039
 
  {
3040
 
    emb_tbl_nest->on_expr= and_items(emb_tbl_nest->on_expr, 
3041
 
                                     sj_nest->sj_on_expr);
3042
 
    emb_tbl_nest->on_expr->fix_fields(parent_join->thd, &emb_tbl_nest->on_expr);
3043
 
  }
3044
 
  else
3045
 
  {
3046
 
    /* Inject into the WHERE */
3047
 
    parent_join->conds= and_items(parent_join->conds, sj_nest->sj_on_expr);
3048
 
    parent_join->conds->fix_fields(parent_join->thd, &parent_join->conds);
3049
 
    parent_join->select_lex->where= parent_join->conds;
3050
 
  }
3051
 
 
3052
 
  return(false);
3053
 
}
3054
 
 
3055
 
 
3056
 
/*
3057
 
  Convert candidate subquery predicates to semi-joins
3058
 
 
3059
 
  SYNOPSIS
3060
 
    JOIN::flatten_subqueries()
3061
 
 
3062
 
  DESCRIPTION
3063
 
    Convert candidate subquery predicates to semi-joins.
3064
 
 
3065
 
  RETURN 
3066
 
    false  OK
3067
 
    true   Error
3068
 
*/
3069
 
 
3070
 
bool JOIN::flatten_subqueries()
3071
 
{
3072
 
  Item_in_subselect **in_subq;
3073
 
  Item_in_subselect **in_subq_end;
3074
 
 
3075
 
  if (sj_subselects.elements() == 0)
3076
 
    return(false);
3077
 
 
3078
 
  /* 1. Fix children subqueries */
3079
 
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back(); 
3080
 
       in_subq != in_subq_end; in_subq++)
3081
 
  {
3082
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3083
 
    child_join->outer_tables = child_join->tables;
3084
 
    if (child_join->flatten_subqueries())
3085
 
      return(true);
3086
 
    (*in_subq)->sj_convert_priority= 
3087
 
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3088
 
  }
3089
 
 
3090
 
  //dump_TABLE_LIST_struct(select_lex, select_lex->leaf_tables);
3091
 
  /* 
3092
 
    2. Pick which subqueries to convert:
3093
 
      sort the subquery array
3094
 
      - prefer correlated subqueries over uncorrelated;
3095
 
      - prefer subqueries that have greater number of outer tables;
3096
 
  */
3097
 
  sj_subselects.sort(subq_sj_candidate_cmp);
3098
 
  // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3099
 
  /* Replace all subqueries to be flattened with Item_int(1) */
3100
 
  for (in_subq= sj_subselects.front(); 
3101
 
       in_subq != in_subq_end && 
3102
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3103
 
       in_subq++)
3104
 
  {
3105
 
    if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3106
 
      return(true);
3107
 
  }
3108
 
 
3109
 
  for (in_subq= sj_subselects.front(); 
3110
 
       in_subq != in_subq_end && 
3111
 
       tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3112
 
       in_subq++)
3113
 
  {
3114
 
    if (convert_subq_to_sj(this, *in_subq))
3115
 
      return(true);
3116
 
  }
3117
 
 
3118
 
  /* 3. Finalize those we didn't convert */
3119
 
  for (; in_subq!= in_subq_end; in_subq++)
3120
 
  {
3121
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3122
 
    Item_subselect::trans_res res;
3123
 
    (*in_subq)->changed= 0;
3124
 
    (*in_subq)->fixed= 0;
3125
 
    res= (*in_subq)->select_transformer(child_join);
3126
 
    if (res == Item_subselect::RES_ERROR)
3127
 
      return(true);
3128
 
 
3129
 
    (*in_subq)->changed= 1;
3130
 
    (*in_subq)->fixed= 1;
3131
 
 
3132
 
    Item *substitute= (*in_subq)->substitution;
3133
 
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3134
 
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3135
 
      return(true);
3136
 
 
3137
 
    //if ((*in_subq)->fix_fields(thd, (*in_subq)->ref_ptr))
3138
 
    //  return(true);
3139
 
  }
3140
 
  sj_subselects.clear();
3141
 
  return(false);
3142
 
}
3143
 
 
3144
 
 
3145
 
/**
3146
 
  Setup for execution all subqueries of a query, for which the optimizer
3147
 
  chose hash semi-join.
3148
 
 
3149
 
  @details Iterate over all subqueries of the query, and if they are under an
3150
 
  IN predicate, and the optimizer chose to compute it via hash semi-join:
3151
 
  - try to initialize all data structures needed for the materialized execution
3152
 
    of the IN predicate,
3153
 
  - if this fails, then perform the IN=>EXISTS transformation which was
3154
 
    previously blocked during JOIN::prepare.
3155
 
 
3156
 
  This method is part of the "code generation" query processing phase.
3157
 
 
3158
 
  This phase must be called after substitute_for_best_equal_field() because
3159
 
  that function may replace items with other items from a multiple equality,
3160
 
  and we need to reference the correct items in the index access method of the
3161
 
  IN predicate.
3162
 
 
3163
 
  @return Operation status
3164
 
  @retval false     success.
3165
 
  @retval true      error occurred.
3166
 
*/
3167
 
 
3168
 
bool JOIN::setup_subquery_materialization()
3169
 
{
3170
 
  for (SELECT_LEX_UNIT *un= select_lex->first_inner_unit(); un;
3171
 
       un= un->next_unit())
3172
 
  {
3173
 
    for (SELECT_LEX *sl= un->first_select(); sl; sl= sl->next_select())
3174
 
    {
3175
 
      Item_subselect *subquery_predicate= sl->master_unit()->item;
3176
 
      if (subquery_predicate &&
3177
 
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
3178
 
      {
3179
 
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3180
 
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3181
 
            in_subs->setup_engine())
3182
 
          return true;
3183
 
      }
3184
 
    }
3185
 
  }
3186
 
  return false;
3187
 
}
3188
 
 
3189
 
 
3190
 
/*
3191
 
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3192
 
 
3193
 
  SYNOPSIS
3194
 
    find_eq_ref_candidate()
3195
 
      table             Table to be checked
3196
 
      sj_inner_tables   Bitmap of inner tables. eq_ref(inner_table) doesn't
3197
 
                        count.
3198
 
 
3199
 
  DESCRIPTION
3200
 
    Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3201
 
 
3202
 
  TODO
3203
 
    Check again if it is feasible to factor common parts with constant table
3204
 
    search
3205
 
 
3206
 
  RETURN
3207
 
    true  - There exists an eq_ref(outer-tables) candidate
3208
 
    false - Otherwise
3209
 
*/
3210
 
 
3211
 
bool find_eq_ref_candidate(TABLE *table, table_map sj_inner_tables)
3212
 
{
3213
 
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3214
 
  uint key;
3215
 
 
3216
 
  if (keyuse)
3217
 
  {
3218
 
    while (1) /* For each key */
3219
 
    {
3220
 
      key= keyuse->key;
3221
 
      KEY *keyinfo= table->key_info + key;
3222
 
      key_part_map bound_parts= 0;
3223
 
      if ((keyinfo->flags & HA_NOSAME) == HA_NOSAME)
3224
 
      {
3225
 
        do  /* For all equalities on all key parts */
3226
 
        {
3227
 
          /* Check if this is "t.keypart = expr(outer_tables) */
3228
 
          if (!(keyuse->used_tables & sj_inner_tables) &&
3229
 
              !(keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL))
3230
 
          {
3231
 
            bound_parts |= 1 << keyuse->keypart;
3232
 
          }
3233
 
          keyuse++;
3234
 
        } while (keyuse->key == key && keyuse->table == table);
3235
 
 
3236
 
        if (bound_parts == PREV_BITS(uint, keyinfo->key_parts))
3237
 
          return true;
3238
 
        if (keyuse->table != table)
3239
 
          return false;
3240
 
      }
3241
 
      else
3242
 
      {
3243
 
        do
3244
 
        {
3245
 
          keyuse++;
3246
 
          if (keyuse->table != table)
3247
 
            return false;
3248
 
        }
3249
 
        while (keyuse->key == key);
3250
 
      }
3251
 
    }
3252
 
  }
3253
 
  return false;
3254
 
}
3255
 
 
3256
 
 
3257
 
/*
3258
 
  Pull tables out of semi-join nests, if possible
3259
 
 
3260
 
  SYNOPSIS
3261
 
    pull_out_semijoin_tables()
3262
 
      join  The join where to do the semi-join flattening
3263
 
 
3264
 
  DESCRIPTION
3265
 
    Try to pull tables out of semi-join nests.
3266
 
     
3267
 
    PRECONDITIONS
3268
 
    When this function is called, the join may have several semi-join nests
3269
 
    (possibly within different semi-join nests), but it is guaranteed that
3270
 
    one semi-join nest does not contain another.
3271
 
   
3272
 
    ACTION
3273
 
    A table can be pulled out of the semi-join nest if
3274
 
     - It is a constant table
3275
 
     - It is accessed 
3276
 
 
3277
 
    POSTCONDITIONS
3278
 
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3279
 
       tables)
3280
 
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3281
 
     * Semi-join nests TABLE_LIST::sj_inner_tables
3282
 
 
3283
 
    This operation is (and should be) performed at each PS execution since
3284
 
    tables may become/cease to be constant across PS reexecutions.
3285
 
 
3286
 
  RETURN 
3287
 
    0 - OK
3288
 
    1 - Out of memory error
3289
 
*/
3290
 
 
3291
 
int pull_out_semijoin_tables(JOIN *join)
3292
 
{
3293
 
  TABLE_LIST *sj_nest;
3294
 
  List_iterator<TABLE_LIST> sj_list_it(join->select_lex->sj_nests);
3295
 
   
3296
 
  /* Try pulling out of the each of the semi-joins */
3297
 
  while ((sj_nest= sj_list_it++))
3298
 
  {
3299
 
    /* Action #1: Mark the constant tables to be pulled out */
3300
 
    table_map pulled_tables= 0;
3301
 
     
3302
 
    List_iterator<TABLE_LIST> child_li(sj_nest->nested_join->join_list);
3303
 
    TABLE_LIST *tbl;
3304
 
    while ((tbl= child_li++))
3305
 
    {
3306
 
      if (tbl->table)
3307
 
      {
3308
 
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3309
 
        if (tbl->table->map & join->const_table_map)
3310
 
        {
3311
 
          pulled_tables |= tbl->table->map;
3312
 
        }
3313
 
      }
3314
 
    }
3315
 
    
3316
 
    /*
3317
 
      Action #2: Find which tables we can pull out based on
3318
 
      update_ref_and_keys() data. Note that pulling one table out can allow
3319
 
      us to pull out some other tables too.
3320
 
    */
3321
 
    bool pulled_a_table;
3322
 
    do 
3323
 
    {
3324
 
      pulled_a_table= false;
3325
 
      child_li.rewind();
3326
 
      while ((tbl= child_li++))
3327
 
      {
3328
 
        if (tbl->table && !(pulled_tables & tbl->table->map))
3329
 
        {
3330
 
          if (find_eq_ref_candidate(tbl->table, 
3331
 
                                    sj_nest->nested_join->used_tables & 
3332
 
                                    ~pulled_tables))
3333
 
          {
3334
 
            pulled_a_table= true;
3335
 
            pulled_tables |= tbl->table->map;
3336
 
          }
3337
 
        }
3338
 
      }
3339
 
    } while (pulled_a_table);
3340
 
 
3341
 
    child_li.rewind();
3342
 
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3343
 
    {
3344
 
      (sj_nest)->sj_inner_tables= 0;
3345
 
      while ((tbl= child_li++))
3346
 
      {
3347
 
        if (tbl->table)
3348
 
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3349
 
      }
3350
 
    }
3351
 
    else
3352
 
    {
3353
 
      /* Record the bitmap of inner tables, mark the inner tables */
3354
 
      table_map inner_tables=(sj_nest)->nested_join->used_tables & 
3355
 
                             ~pulled_tables;
3356
 
      (sj_nest)->sj_inner_tables= inner_tables;
3357
 
      while ((tbl= child_li++))
3358
 
      {
3359
 
        if (tbl->table)
3360
 
        {
3361
 
          if (inner_tables & tbl->table->map)
3362
 
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3363
 
          else
3364
 
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3365
 
        }
3366
 
      }
3367
 
    }
3368
 
  }
3369
 
  return(0);
3370
 
}
3371
 
 
3372
462
/*****************************************************************************
3373
 
  Create JOIN_TABS, make a guess about the table types,
 
463
  Create JoinTableS, make a guess about the table types,
3374
464
  Approximate how many records will be used in each table
3375
465
*****************************************************************************/
3376
 
 
3377
 
 
3378
 
static ha_rows get_quick_record_count(THD *thd, SQL_SELECT *select,
3379
 
                                      TABLE *table,
3380
 
                                      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)
3381
467
{
3382
468
  int error;
3383
 
  if (check_stack_overrun(thd, STACK_MIN_SIZE, NULL))
 
469
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3384
470
    return(0);                           // Fatal error flag is set
3385
471
  if (select)
3386
472
  {
3387
473
    select->head=table;
3388
474
    table->reginfo.impossible_range=0;
3389
 
    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,
3390
476
                                          limit, 0, false)) == 1)
3391
477
      return(select->quick->records);
3392
478
    if (error == -1)
3398
484
  return(HA_POS_ERROR);                 /* This shouldn't happend */
3399
485
}
3400
486
 
3401
 
/*
3402
 
   This structure is used to collect info on potentially sargable
3403
 
   predicates in order to check whether they become sargable after
3404
 
   reading const tables.
3405
 
   We form a bitmap of indexes that can be used for sargable predicates.
3406
 
   Only such indexes are involved in range analysis.
3407
 
*/
3408
 
typedef struct st_sargable_param
3409
 
{
3410
 
  Field *field;              /* field against which to check sargability */
3411
 
  Item **arg_value;          /* values of potential keys for lookups     */
3412
 
  uint num_values;           /* number of values in the above array      */
3413
 
} SARGABLE_PARAM;  
3414
 
 
3415
 
/**
3416
 
  Calculate the best possible join and initialize the join structure.
3417
 
 
3418
 
  @retval
3419
 
    0   ok
3420
 
  @retval
3421
 
    1   Fatal error
3422
 
*/
3423
 
 
3424
 
static bool
3425
 
make_join_statistics(JOIN *join, TABLE_LIST *tables, COND *conds,
3426
 
                     DYNAMIC_ARRAY *keyuse_array)
3427
 
{
3428
 
  int error;
3429
 
  TABLE *table;
3430
 
  uint i,table_count,const_count,key;
3431
 
  table_map found_const_table_map, all_table_map, found_ref, refs;
3432
 
  key_map const_ref, eq_part;
3433
 
  TABLE **table_vector;
3434
 
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3435
 
  KEYUSE *keyuse,*start_keyuse;
3436
 
  table_map outer_join=0;
3437
 
  SARGABLE_PARAM *sargables= 0;
3438
 
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3439
 
 
3440
 
  table_count=join->tables;
3441
 
  stat=(JOIN_TAB*) join->thd->calloc(sizeof(JOIN_TAB)*table_count);
3442
 
  stat_ref=(JOIN_TAB**) join->thd->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3443
 
  table_vector=(TABLE**) join->thd->alloc(sizeof(TABLE*)*(table_count*2));
3444
 
  if (!stat || !stat_ref || !table_vector)
3445
 
    return(1);                          // Eom /* purecov: inspected */
3446
 
 
3447
 
  join->best_ref=stat_vector;
3448
 
 
3449
 
  stat_end=stat+table_count;
3450
 
  found_const_table_map= all_table_map=0;
3451
 
  const_count=0;
3452
 
 
3453
 
  for (s= stat, i= 0;
3454
 
       tables;
3455
 
       s++, tables= tables->next_leaf, i++)
3456
 
  {
3457
 
    TABLE_LIST *embedding= tables->embedding;
3458
 
    stat_vector[i]=s;
3459
 
    s->keys.init();
3460
 
    s->const_keys.init();
3461
 
    s->checked_keys.init();
3462
 
    s->needed_reg.init();
3463
 
    table_vector[i]=s->table=table=tables->table;
3464
 
    table->pos_in_table_list= tables;
3465
 
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3466
 
    if(error)
3467
 
    {
3468
 
        table->file->print_error(error, MYF(0));
3469
 
        return(1);
3470
 
    }
3471
 
    table->quick_keys.clear_all();
3472
 
    table->reginfo.join_tab=s;
3473
 
    table->reginfo.not_exists_optimize=0;
3474
 
    memset(table->const_key_parts, 0,
3475
 
           sizeof(key_part_map)*table->s->keys);
3476
 
    all_table_map|= table->map;
3477
 
    s->join=join;
3478
 
    s->info=0;                                  // For describe
3479
 
 
3480
 
    s->dependent= tables->dep_tables;
3481
 
    s->key_dependent= 0;
3482
 
    if (tables->schema_table)
3483
 
      table->file->stats.records= 2;
3484
 
    table->quick_condition_rows= table->file->stats.records;
3485
 
 
3486
 
    s->on_expr_ref= &tables->on_expr;
3487
 
    if (*s->on_expr_ref)
3488
 
    {
3489
 
      /* s is the only inner table of an outer join */
3490
 
      if (!table->file->stats.records && !embedding)
3491
 
      {                                         // Empty table
3492
 
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
3493
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3494
 
        continue;
3495
 
      }
3496
 
      outer_join|= table->map;
3497
 
      s->embedding_map= 0;
3498
 
      for (;embedding; embedding= embedding->embedding)
3499
 
        s->embedding_map|= embedding->nested_join->nj_map;
3500
 
      continue;
3501
 
    }
3502
 
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3503
 
    {
3504
 
      /* s belongs to a nested join, maybe to several embedded joins */
3505
 
      s->embedding_map= 0;
3506
 
      do
3507
 
      {
3508
 
        NESTED_JOIN *nested_join= embedding->nested_join;
3509
 
        s->embedding_map|=nested_join->nj_map;
3510
 
        s->dependent|= embedding->dep_tables;
3511
 
        embedding= embedding->embedding;
3512
 
        outer_join|= nested_join->used_tables;
3513
 
      }
3514
 
      while (embedding);
3515
 
      continue;
3516
 
    }
3517
 
    if ((table->s->system || table->file->stats.records <= 1) &&
3518
 
        !s->dependent &&
3519
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3520
 
    {
3521
 
      set_position(join,const_count++,s,(KEYUSE*) 0);
3522
 
    }
3523
 
  }
3524
 
  stat_vector[i]=0;
3525
 
  join->outer_join=outer_join;
3526
 
 
3527
 
  if (join->outer_join)
3528
 
  {
3529
 
    /* 
3530
 
       Build transitive closure for relation 'to be dependent on'.
3531
 
       This will speed up the plan search for many cases with outer joins,
3532
 
       as well as allow us to catch illegal cross references/
3533
 
       Warshall's algorithm is used to build the transitive closure.
3534
 
       As we use bitmaps to represent the relation the complexity
3535
 
       of the algorithm is O((number of tables)^2). 
3536
 
    */
3537
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3538
 
    {
3539
 
      for (uint j= 0 ; j < table_count ; j++)
3540
 
      {
3541
 
        table= stat[j].table;
3542
 
        if (s->dependent & table->map)
3543
 
          s->dependent |= table->reginfo.join_tab->dependent;
3544
 
      }
3545
 
      if (s->dependent)
3546
 
        s->table->maybe_null= 1;
3547
 
    }
3548
 
    /* Catch illegal cross references for outer joins */
3549
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3550
 
    {
3551
 
      if (s->dependent & s->table->map)
3552
 
      {
3553
 
        join->tables=0;                 // Don't use join->table
3554
 
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3555
 
        return(1);
3556
 
      }
3557
 
      s->key_dependent= s->dependent;
3558
 
    }
3559
 
  }
3560
 
 
3561
 
  if (conds || outer_join)
3562
 
    if (update_ref_and_keys(join->thd, keyuse_array, stat, join->tables,
3563
 
                            conds, join->cond_equal,
3564
 
                            ~outer_join, join->select_lex, &sargables))
3565
 
      return(1);
3566
 
 
3567
 
  /* Read tables with 0 or 1 rows (system tables) */
3568
 
  join->const_table_map= 0;
3569
 
 
3570
 
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3571
 
       p_pos < p_end ;
3572
 
       p_pos++)
3573
 
  {
3574
 
    int tmp;
3575
 
    s= p_pos->table;
3576
 
    s->type=JT_SYSTEM;
3577
 
    join->const_table_map|=s->table->map;
3578
 
    if ((tmp=join_read_const_table(s, p_pos)))
3579
 
    {
3580
 
      if (tmp > 0)
3581
 
        return(1);                      // Fatal error
3582
 
    }
3583
 
    else
3584
 
      found_const_table_map|= s->table->map;
3585
 
  }
3586
 
 
3587
 
  /* loop until no more const tables are found */
3588
 
  int ref_changed;
3589
 
  do
3590
 
  {
3591
 
  more_const_tables_found:
3592
 
    ref_changed = 0;
3593
 
    found_ref=0;
3594
 
 
3595
 
    /*
3596
 
      We only have to loop from stat_vector + const_count as
3597
 
      set_position() will move all const_tables first in stat_vector
3598
 
    */
3599
 
 
3600
 
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3601
 
    {
3602
 
      table=s->table;
3603
 
 
3604
 
      /* 
3605
 
        If equi-join condition by a key is null rejecting and after a
3606
 
        substitution of a const table the key value happens to be null
3607
 
        then we can state that there are no matches for this equi-join.
3608
 
      */  
3609
 
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3610
 
      {
3611
 
        /* 
3612
 
          When performing an outer join operation if there are no matching rows
3613
 
          for the single row of the outer table all the inner tables are to be
3614
 
          null complemented and thus considered as constant tables.
3615
 
          Here we apply this consideration to the case of outer join operations 
3616
 
          with a single inner table only because the case with nested tables
3617
 
          would require a more thorough analysis.
3618
 
          TODO. Apply single row substitution to null complemented inner tables
3619
 
          for nested outer join operations. 
3620
 
        */              
3621
 
        while (keyuse->table == table)
3622
 
        {
3623
 
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3624
 
              keyuse->val->is_null() && keyuse->null_rejecting)
3625
 
          {
3626
 
            s->type= JT_CONST;
3627
 
            mark_as_null_row(table);
3628
 
            found_const_table_map|= table->map;
3629
 
            join->const_table_map|= table->map;
3630
 
            set_position(join,const_count++,s,(KEYUSE*) 0);
3631
 
            goto more_const_tables_found;
3632
 
           }
3633
 
          keyuse++;
3634
 
        }
3635
 
      }
3636
 
 
3637
 
      if (s->dependent)                         // If dependent on some table
3638
 
      {
3639
 
        // All dep. must be constants
3640
 
        if (s->dependent & ~(found_const_table_map))
3641
 
          continue;
3642
 
        if (table->file->stats.records <= 1L &&
3643
 
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3644
 
            !table->pos_in_table_list->embedding)
3645
 
        {                                       // system table
3646
 
          int tmp= 0;
3647
 
          s->type=JT_SYSTEM;
3648
 
          join->const_table_map|=table->map;
3649
 
          set_position(join,const_count++,s,(KEYUSE*) 0);
3650
 
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3651
 
          {
3652
 
            if (tmp > 0)
3653
 
              return(1);                        // Fatal error
3654
 
          }
3655
 
          else
3656
 
            found_const_table_map|= table->map;
3657
 
          continue;
3658
 
        }
3659
 
      }
3660
 
      /* check if table can be read by key or table only uses const refs */
3661
 
      if ((keyuse=s->keyuse))
3662
 
      {
3663
 
        s->type= JT_REF;
3664
 
        while (keyuse->table == table)
3665
 
        {
3666
 
          start_keyuse=keyuse;
3667
 
          key=keyuse->key;
3668
 
          s->keys.set_bit(key);               // QQ: remove this ?
3669
 
 
3670
 
          refs=0;
3671
 
          const_ref.clear_all();
3672
 
          eq_part.clear_all();
3673
 
          do
3674
 
          {
3675
 
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3676
 
            {
3677
 
              if (!((~found_const_table_map) & keyuse->used_tables))
3678
 
                const_ref.set_bit(keyuse->keypart);
3679
 
              else
3680
 
                refs|=keyuse->used_tables;
3681
 
              eq_part.set_bit(keyuse->keypart);
3682
 
            }
3683
 
            keyuse++;
3684
 
          } while (keyuse->table == table && keyuse->key == key);
3685
 
 
3686
 
          if (eq_part.is_prefix(table->key_info[key].key_parts) &&
3687
 
              !table->pos_in_table_list->embedding)
3688
 
          {
3689
 
            if ((table->key_info[key].flags & (HA_NOSAME))
3690
 
                 == HA_NOSAME)
3691
 
            {
3692
 
              if (const_ref == eq_part)
3693
 
              {                                 // Found everything for ref.
3694
 
                int tmp;
3695
 
                ref_changed = 1;
3696
 
                s->type= JT_CONST;
3697
 
                join->const_table_map|=table->map;
3698
 
                set_position(join,const_count++,s,start_keyuse);
3699
 
                if (create_ref_for_key(join, s, start_keyuse,
3700
 
                                       found_const_table_map))
3701
 
                  return(1);
3702
 
                if ((tmp=join_read_const_table(s,
3703
 
                                               join->positions+const_count-1)))
3704
 
                {
3705
 
                  if (tmp > 0)
3706
 
                    return(1);                  // Fatal error
3707
 
                }
3708
 
                else
3709
 
                  found_const_table_map|= table->map;
3710
 
                break;
3711
 
              }
3712
 
              else
3713
 
                found_ref|= refs;      // Table is const if all refs are const
3714
 
            }
3715
 
            else if (const_ref == eq_part)
3716
 
              s->const_keys.set_bit(key);
3717
 
          }
3718
 
        }
3719
 
      }
3720
 
    }
3721
 
  } while (join->const_table_map & found_ref && ref_changed);
3722
 
 
3723
 
  /* 
3724
 
    Update info on indexes that can be used for search lookups as
3725
 
    reading const tables may has added new sargable predicates. 
3726
 
  */
3727
 
  if (const_count && sargables)
3728
 
  {
3729
 
    for( ; sargables->field ; sargables++)
3730
 
    {
3731
 
      Field *field= sargables->field;
3732
 
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3733
 
      key_map possible_keys= field->key_start;
3734
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
3735
 
      bool is_const= 1;
3736
 
      for (uint j=0; j < sargables->num_values; j++)
3737
 
        is_const&= sargables->arg_value[j]->const_item();
3738
 
      if (is_const)
3739
 
        join_tab[0].const_keys.merge(possible_keys);
3740
 
    }
3741
 
  }
3742
 
 
3743
 
  if (pull_out_semijoin_tables(join))
3744
 
    return(true);
3745
 
 
3746
 
  /* Calc how many (possible) matched records in each table */
3747
 
 
3748
 
  for (s=stat ; s < stat_end ; s++)
3749
 
  {
3750
 
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
3751
 
    {
3752
 
      /* Only one matching row */
3753
 
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3754
 
      continue;
3755
 
    }
3756
 
    /* Approximate found rows and time to read them */
3757
 
    s->found_records=s->records=s->table->file->stats.records;
3758
 
    s->read_time=(ha_rows) s->table->file->scan_time();
3759
 
 
3760
 
    /*
3761
 
      Set a max range of how many seeks we can expect when using keys
3762
 
      This is can't be to high as otherwise we are likely to use
3763
 
      table scan.
3764
 
    */
3765
 
    s->worst_seeks= min((double) s->found_records / 10,
3766
 
                        (double) s->read_time*3);
3767
 
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3768
 
      s->worst_seeks=2.0;
3769
 
 
3770
 
    /*
3771
 
      Add to stat->const_keys those indexes for which all group fields or
3772
 
      all select distinct fields participate in one index.
3773
 
    */
3774
 
    add_group_and_distinct_keys(join, s);
3775
 
 
3776
 
    if (!s->const_keys.is_clear_all() &&
3777
 
        !s->table->pos_in_table_list->embedding)
3778
 
    {
3779
 
      ha_rows records;
3780
 
      SQL_SELECT *select;
3781
 
      select= make_select(s->table, found_const_table_map,
3782
 
                          found_const_table_map,
3783
 
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3784
 
                          1, &error);
3785
 
      if (!select)
3786
 
        return(1);
3787
 
      records= get_quick_record_count(join->thd, select, s->table,
3788
 
                                      &s->const_keys, join->row_limit);
3789
 
      s->quick=select->quick;
3790
 
      s->needed_reg=select->needed_reg;
3791
 
      select->quick=0;
3792
 
      if (records == 0 && s->table->reginfo.impossible_range)
3793
 
      {
3794
 
        /*
3795
 
          Impossible WHERE or ON expression
3796
 
          In case of ON, we mark that the we match one empty NULL row.
3797
 
          In case of WHERE, don't set found_const_table_map to get the
3798
 
          caller to abort with a zero row result.
3799
 
        */
3800
 
        join->const_table_map|= s->table->map;
3801
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3802
 
        s->type= JT_CONST;
3803
 
        if (*s->on_expr_ref)
3804
 
        {
3805
 
          /* Generate empty row */
3806
 
          s->info= "Impossible ON condition";
3807
 
          found_const_table_map|= s->table->map;
3808
 
          s->type= JT_CONST;
3809
 
          mark_as_null_row(s->table);           // All fields are NULL
3810
 
        }
3811
 
      }
3812
 
      if (records != HA_POS_ERROR)
3813
 
      {
3814
 
        s->found_records=records;
3815
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3816
 
      }
3817
 
      delete select;
3818
 
    }
3819
 
  }
3820
 
 
3821
 
  join->join_tab=stat;
3822
 
  join->map2table=stat_ref;
3823
 
  join->table= join->all_tables=table_vector;
3824
 
  join->const_tables=const_count;
3825
 
  join->found_const_table_map=found_const_table_map;
3826
 
 
3827
 
  /* Find an optimal join order of the non-constant tables. */
3828
 
  if (join->const_tables != join->tables)
3829
 
  {
3830
 
    optimize_keyuse(join, keyuse_array);
3831
 
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3832
 
      return(true);
3833
 
  }
3834
 
  else
3835
 
  {
3836
 
    memcpy(join->best_positions, join->positions,
3837
 
           sizeof(POSITION)*join->const_tables);
3838
 
    join->best_read=1.0;
3839
 
  }
3840
 
  /* Generate an execution plan from the found optimal join order. */
3841
 
  return(join->thd->killed || get_best_combination(join));
3842
 
}
3843
 
 
3844
 
 
3845
487
/*****************************************************************************
3846
488
  Check with keys are used and with tables references with tables
3847
489
  Updates in stat:
3850
492
          keyuse     Pointer to possible keys
3851
493
*****************************************************************************/
3852
494
 
3853
 
/// Used when finding key fields
3854
 
typedef struct key_field_t {
3855
 
  Field         *field;
3856
 
  Item          *val;                   ///< May be empty if diff constant
3857
 
  uint          level;
3858
 
  uint          optimize; // KEY_OPTIMIZE_*
3859
 
  bool          eq_func;
3860
 
  /**
3861
 
    If true, the condition this struct represents will not be satisfied
3862
 
    when val IS NULL.
3863
 
  */
3864
 
  bool          null_rejecting; 
3865
 
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3866
 
  uint          sj_pred_no; /* See KEYUSE::sj_pred_no */
3867
 
} KEY_FIELD;
3868
 
 
3869
 
/**
3870
 
  Merge new key definitions to old ones, remove those not used in both.
3871
 
 
3872
 
  This is called for OR between different levels.
3873
 
 
3874
 
  To be able to do 'ref_or_null' we merge a comparison of a column
3875
 
  and 'column IS NULL' to one test.  This is useful for sub select queries
3876
 
  that are internally transformed to something like:.
3877
 
 
3878
 
  @code
3879
 
  SELECT * FROM t1 WHERE t1.key=outer_ref_field or t1.key IS NULL 
3880
 
  @endcode
3881
 
 
3882
 
  KEY_FIELD::null_rejecting is processed as follows: @n
3883
 
  result has null_rejecting=true if it is set for both ORed references.
3884
 
  for example:
3885
 
  -   (t2.key = t1.field OR t2.key  =  t1.field) -> null_rejecting=true
3886
 
  -   (t2.key = t1.field OR t2.key <=> t1.field) -> null_rejecting=false
3887
 
 
3888
 
  @todo
3889
 
    The result of this is that we're missing some 'ref' accesses.
3890
 
    OptimizerTeam: Fix this
3891
 
*/
3892
 
 
3893
 
static KEY_FIELD *
3894
 
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3895
 
                 uint and_level)
3896
 
{
3897
 
  if (start == new_fields)
3898
 
    return start;                               // Impossible or
3899
 
  if (new_fields == end)
3900
 
    return start;                               // No new fields, skip all
3901
 
 
3902
 
  KEY_FIELD *first_free=new_fields;
3903
 
 
3904
 
  /* Mark all found fields in old array */
3905
 
  for (; new_fields != end ; new_fields++)
3906
 
  {
3907
 
    for (KEY_FIELD *old=start ; old != first_free ; old++)
3908
 
    {
3909
 
      if (old->field == new_fields->field)
3910
 
      {
3911
 
        /*
3912
 
          NOTE: below const_item() call really works as "!used_tables()", i.e.
3913
 
          it can return false where it is feasible to make it return true.
3914
 
          
3915
 
          The cause is as follows: Some of the tables are already known to be
3916
 
          const tables (the detection code is in make_join_statistics(),
3917
 
          above the update_ref_and_keys() call), but we didn't propagate 
3918
 
          information about this: TABLE::const_table is not set to true, and
3919
 
          Item::update_used_tables() hasn't been called for each item.
3920
 
          The result of this is that we're missing some 'ref' accesses.
3921
 
          TODO: OptimizerTeam: Fix this
3922
 
        */
3923
 
        if (!new_fields->val->const_item())
3924
 
        {
3925
 
          /*
3926
 
            If the value matches, we can use the key reference.
3927
 
            If not, we keep it until we have examined all new values
3928
 
          */
3929
 
          if (old->val->eq(new_fields->val, old->field->binary()))
3930
 
          {
3931
 
            old->level= and_level;
3932
 
            old->optimize= ((old->optimize & new_fields->optimize &
3933
 
                             KEY_OPTIMIZE_EXISTS) |
3934
 
                            ((old->optimize | new_fields->optimize) &
3935
 
                             KEY_OPTIMIZE_REF_OR_NULL));
3936
 
            old->null_rejecting= (old->null_rejecting &&
3937
 
                                  new_fields->null_rejecting);
3938
 
          }
3939
 
        }
3940
 
        else if (old->eq_func && new_fields->eq_func &&
3941
 
                 old->val->eq_by_collation(new_fields->val, 
3942
 
                                           old->field->binary(),
3943
 
                                           old->field->charset()))
3944
 
 
3945
 
        {
3946
 
          old->level= and_level;
3947
 
          old->optimize= ((old->optimize & new_fields->optimize &
3948
 
                           KEY_OPTIMIZE_EXISTS) |
3949
 
                          ((old->optimize | new_fields->optimize) &
3950
 
                           KEY_OPTIMIZE_REF_OR_NULL));
3951
 
          old->null_rejecting= (old->null_rejecting &&
3952
 
                                new_fields->null_rejecting);
3953
 
        }
3954
 
        else if (old->eq_func && new_fields->eq_func &&
3955
 
                 ((old->val->const_item() && old->val->is_null()) || 
3956
 
                  new_fields->val->is_null()))
3957
 
        {
3958
 
          /* field = expression OR field IS NULL */
3959
 
          old->level= and_level;
3960
 
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3961
 
          /*
3962
 
            Remember the NOT NULL value unless the value does not depend
3963
 
            on other tables.
3964
 
          */
3965
 
          if (!old->val->used_tables() && old->val->is_null())
3966
 
            old->val= new_fields->val;
3967
 
          /* The referred expression can be NULL: */ 
3968
 
          old->null_rejecting= 0;
3969
 
        }
3970
 
        else
3971
 
        {
3972
 
          /*
3973
 
            We are comparing two different const.  In this case we can't
3974
 
            use a key-lookup on this so it's better to remove the value
3975
 
            and let the range optimzier handle it
3976
 
          */
3977
 
          if (old == --first_free)              // If last item
3978
 
            break;
3979
 
          *old= *first_free;                    // Remove old value
3980
 
          old--;                                // Retry this value
3981
 
        }
3982
 
      }
3983
 
    }
3984
 
  }
3985
 
  /* Remove all not used items */
3986
 
  for (KEY_FIELD *old=start ; old != first_free ;)
3987
 
  {
3988
 
    if (old->level != and_level)
3989
 
    {                                           // Not used in all levels
3990
 
      if (old == --first_free)
3991
 
        break;
3992
 
      *old= *first_free;                        // Remove old value
3993
 
      continue;
3994
 
    }
3995
 
    old++;
3996
 
  }
3997
 
  return first_free;
3998
 
}
3999
 
 
4000
 
 
4001
 
/**
4002
 
  Add a possible key to array of possible keys if it's usable as a key
4003
 
 
4004
 
    @param key_fields      Pointer to add key, if usable
4005
 
    @param and_level       And level, to be stored in KEY_FIELD
4006
 
    @param cond            Condition predicate
4007
 
    @param field           Field used in comparision
4008
 
    @param eq_func         True if we used =, <=> or IS NULL
4009
 
    @param value           Value used for comparison with field
4010
 
    @param usable_tables   Tables which can be used for key optimization
4011
 
    @param sargables       IN/OUT Array of found sargable candidates
4012
 
 
4013
 
  @note
4014
 
    If we are doing a NOT NULL comparison on a NOT NULL field in a outer join
4015
 
    table, we store this to be able to do not exists optimization later.
4016
 
 
4017
 
  @returns
4018
 
    *key_fields is incremented if we stored a key in the array
4019
 
*/
4020
 
 
4021
 
static void
4022
 
add_key_field(KEY_FIELD **key_fields,uint and_level, Item_func *cond,
4023
 
              Field *field, bool eq_func, Item **value, uint num_values,
4024
 
              table_map usable_tables, SARGABLE_PARAM **sargables)
4025
 
{
4026
 
  uint exists_optimize= 0;
4027
 
  if (!(field->flags & PART_KEY_FLAG))
4028
 
  {
4029
 
    // Don't remove column IS NULL on a LEFT JOIN table
4030
 
    if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4031
 
        !field->table->maybe_null || field->null_ptr)
4032
 
      return;                                   // Not a key. Skip it
4033
 
    exists_optimize= KEY_OPTIMIZE_EXISTS;
4034
 
    assert(num_values == 1);
4035
 
  }
4036
 
  else
4037
 
  {
4038
 
    table_map used_tables=0;
4039
 
    bool optimizable=0;
4040
 
    for (uint i=0; i<num_values; i++)
4041
 
    {
4042
 
      used_tables|=(value[i])->used_tables();
4043
 
      if (!((value[i])->used_tables() & (field->table->map | RAND_TABLE_BIT)))
4044
 
        optimizable=1;
4045
 
    }
4046
 
    if (!optimizable)
4047
 
      return;
4048
 
    if (!(usable_tables & field->table->map))
4049
 
    {
4050
 
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4051
 
          !field->table->maybe_null || field->null_ptr)
4052
 
        return;                                 // Can't use left join optimize
4053
 
      exists_optimize= KEY_OPTIMIZE_EXISTS;
4054
 
    }
4055
 
    else
4056
 
    {
4057
 
      JOIN_TAB *stat=field->table->reginfo.join_tab;
4058
 
      key_map possible_keys=field->key_start;
4059
 
      possible_keys.intersect(field->table->keys_in_use_for_query);
4060
 
      stat[0].keys.merge(possible_keys);             // Add possible keys
4061
 
 
4062
 
      /*
4063
 
        Save the following cases:
4064
 
        Field op constant
4065
 
        Field LIKE constant where constant doesn't start with a wildcard
4066
 
        Field = field2 where field2 is in a different table
4067
 
        Field op formula
4068
 
        Field IS NULL
4069
 
        Field IS NOT NULL
4070
 
         Field BETWEEN ...
4071
 
         Field IN ...
4072
 
      */
4073
 
      stat[0].key_dependent|=used_tables;
4074
 
 
4075
 
      bool is_const=1;
4076
 
      for (uint i=0; i<num_values; i++)
4077
 
      {
4078
 
        if (!(is_const&= value[i]->const_item()))
4079
 
          break;
4080
 
      }
4081
 
      if (is_const)
4082
 
        stat[0].const_keys.merge(possible_keys);
4083
 
      else if (!eq_func)
4084
 
      {
4085
 
        /* 
4086
 
          Save info to be able check whether this predicate can be 
4087
 
          considered as sargable for range analisis after reading const tables.
4088
 
          We do not save info about equalities as update_const_equal_items
4089
 
          will take care of updating info on keys from sargable equalities. 
4090
 
        */
4091
 
        (*sargables)--;
4092
 
        (*sargables)->field= field;
4093
 
        (*sargables)->arg_value= value;
4094
 
        (*sargables)->num_values= num_values;
4095
 
      }
4096
 
      /*
4097
 
        We can't always use indexes when comparing a string index to a
4098
 
        number. cmp_type() is checked to allow compare of dates to numbers.
4099
 
        eq_func is NEVER true when num_values > 1
4100
 
       */
4101
 
      if (!eq_func)
4102
 
      {
4103
 
        /* 
4104
 
          Additional optimization: if we're processing
4105
 
          "t.key BETWEEN c1 AND c1" then proceed as if we were processing
4106
 
          "t.key = c1".
4107
 
          TODO: This is a very limited fix. A more generic fix is possible. 
4108
 
          There are 2 options:
4109
 
          A) Make equality propagation code be able to handle BETWEEN
4110
 
             (including cases like t1.key BETWEEN t2.key AND t3.key)
4111
 
          B) Make range optimizer to infer additional "t.key = c" equalities
4112
 
             and use them in equality propagation process (see details in
4113
 
             OptimizerKBAndTodo)
4114
 
        */
4115
 
        if ((cond->functype() != Item_func::BETWEEN) ||
4116
 
            ((Item_func_between*) cond)->negated ||
4117
 
            !value[0]->eq(value[1], field->binary()))
4118
 
          return;
4119
 
        eq_func= true;
4120
 
      }
4121
 
 
4122
 
      if (field->result_type() == STRING_RESULT)
4123
 
      {
4124
 
        if ((*value)->result_type() != STRING_RESULT)
4125
 
        {
4126
 
          if (field->cmp_type() != (*value)->result_type())
4127
 
            return;
4128
 
        }
4129
 
        else
4130
 
        {
4131
 
          /*
4132
 
            We can't use indexes if the effective collation
4133
 
            of the operation differ from the field collation.
4134
 
          */
4135
 
          if (field->cmp_type() == STRING_RESULT &&
4136
 
              ((Field_str*)field)->charset() != cond->compare_collation())
4137
 
            return;
4138
 
        }
4139
 
      }
4140
 
    }
4141
 
  }
4142
 
  /*
4143
 
    For the moment eq_func is always true. This slot is reserved for future
4144
 
    extensions where we want to remembers other things than just eq comparisons
4145
 
  */
4146
 
  assert(eq_func);
4147
 
  /* Store possible eq field */
4148
 
  (*key_fields)->field=         field;
4149
 
  (*key_fields)->eq_func=       eq_func;
4150
 
  (*key_fields)->val=           *value;
4151
 
  (*key_fields)->level=         and_level;
4152
 
  (*key_fields)->optimize=      exists_optimize;
4153
 
  /*
4154
 
    If the condition has form "tbl.keypart = othertbl.field" and 
4155
 
    othertbl.field can be NULL, there will be no matches if othertbl.field 
4156
 
    has NULL value.
4157
 
    We use null_rejecting in add_not_null_conds() to add
4158
 
    'othertbl.field IS NOT NULL' to tab->select_cond.
4159
 
  */
4160
 
  (*key_fields)->null_rejecting= ((cond->functype() == Item_func::EQ_FUNC ||
4161
 
                                   cond->functype() == Item_func::MULT_EQUAL_FUNC) &&
4162
 
                                  ((*value)->type() == Item::FIELD_ITEM) &&
4163
 
                                  ((Item_field*)*value)->field->maybe_null());
4164
 
  (*key_fields)->cond_guard= NULL;
4165
 
  (*key_fields)->sj_pred_no= (cond->name >= subq_sj_cond_name && 
4166
 
                              cond->name < subq_sj_cond_name + 64)? 
4167
 
                              cond->name - subq_sj_cond_name: UINT_MAX;
4168
 
  (*key_fields)++;
4169
 
}
4170
 
 
4171
 
/**
4172
 
  Add possible keys to array of possible keys originated from a simple
4173
 
  predicate.
4174
 
 
4175
 
    @param  key_fields     Pointer to add key, if usable
4176
 
    @param  and_level      And level, to be stored in KEY_FIELD
4177
 
    @param  cond           Condition predicate
4178
 
    @param  field          Field used in comparision
4179
 
    @param  eq_func        True if we used =, <=> or IS NULL
4180
 
    @param  value          Value used for comparison with field
4181
 
                           Is NULL for BETWEEN and IN    
4182
 
    @param  usable_tables  Tables which can be used for key optimization
4183
 
    @param  sargables      IN/OUT Array of found sargable candidates
4184
 
 
4185
 
  @note
4186
 
    If field items f1 and f2 belong to the same multiple equality and
4187
 
    a key is added for f1, the the same key is added for f2.
4188
 
 
4189
 
  @returns
4190
 
    *key_fields is incremented if we stored a key in the array
4191
 
*/
4192
 
 
4193
 
static void
4194
 
add_key_equal_fields(KEY_FIELD **key_fields, uint and_level,
4195
 
                     Item_func *cond, Item_field *field_item,
4196
 
                     bool eq_func, Item **val,
4197
 
                     uint num_values, table_map usable_tables,
4198
 
                     SARGABLE_PARAM **sargables)
4199
 
{
4200
 
  Field *field= field_item->field;
4201
 
  add_key_field(key_fields, and_level, cond, field,
4202
 
                eq_func, val, num_values, usable_tables, sargables);
4203
 
  Item_equal *item_equal= field_item->item_equal;
4204
 
  if (item_equal)
4205
 
  { 
4206
 
    /*
4207
 
      Add to the set of possible key values every substitution of
4208
 
      the field for an equal field included into item_equal
4209
 
    */
4210
 
    Item_equal_iterator it(*item_equal);
4211
 
    Item_field *item;
4212
 
    while ((item= it++))
4213
 
    {
4214
 
      if (!field->eq(item->field))
4215
 
      {
4216
 
        add_key_field(key_fields, and_level, cond, item->field,
4217
 
                      eq_func, val, num_values, usable_tables,
4218
 
                      sargables);
4219
 
      }
4220
 
    }
4221
 
  }
4222
 
}
4223
 
 
4224
 
static void
4225
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint *and_level,
4226
 
               COND *cond, table_map usable_tables,
4227
 
               SARGABLE_PARAM **sargables)
4228
 
{
4229
 
  if (cond->type() == Item_func::COND_ITEM)
4230
 
  {
4231
 
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
4232
 
    KEY_FIELD *org_key_fields= *key_fields;
4233
 
 
4234
 
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
4235
 
    {
4236
 
      Item *item;
4237
 
      while ((item=li++))
4238
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4239
 
                       sargables);
4240
 
      for (; org_key_fields != *key_fields ; org_key_fields++)
4241
 
        org_key_fields->level= *and_level;
4242
 
    }
4243
 
    else
4244
 
    {
4245
 
      (*and_level)++;
4246
 
      add_key_fields(join, key_fields, and_level, li++, usable_tables,
4247
 
                     sargables);
4248
 
      Item *item;
4249
 
      while ((item=li++))
4250
 
      {
4251
 
        KEY_FIELD *start_key_fields= *key_fields;
4252
 
        (*and_level)++;
4253
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4254
 
                       sargables);
4255
 
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
4256
 
                                     *key_fields,++(*and_level));
4257
 
      }
4258
 
    }
4259
 
    return;
4260
 
  }
4261
 
 
4262
 
  /* 
4263
 
    Subquery optimization: Conditions that are pushed down into subqueries
4264
 
    are wrapped into Item_func_trig_cond. We process the wrapped condition
4265
 
    but need to set cond_guard for KEYUSE elements generated from it.
4266
 
  */
4267
 
  {
4268
 
    if (cond->type() == Item::FUNC_ITEM &&
4269
 
        ((Item_func*)cond)->functype() == Item_func::TRIG_COND_FUNC)
4270
 
    {
4271
 
      Item *cond_arg= ((Item_func*)cond)->arguments()[0];
4272
 
      if (!join->group_list && !join->order &&
4273
 
          join->unit->item && 
4274
 
          join->unit->item->substype() == Item_subselect::IN_SUBS &&
4275
 
          !join->unit->is_union())
4276
 
      {
4277
 
        KEY_FIELD *save= *key_fields;
4278
 
        add_key_fields(join, key_fields, and_level, cond_arg, usable_tables,
4279
 
                       sargables);
4280
 
        // Indicate that this ref access candidate is for subquery lookup:
4281
 
        for (; save != *key_fields; save++)
4282
 
          save->cond_guard= ((Item_func_trig_cond*)cond)->get_trig_var();
4283
 
      }
4284
 
      return;
4285
 
    }
4286
 
  }
4287
 
 
4288
 
  /* If item is of type 'field op field/constant' add it to key_fields */
4289
 
  if (cond->type() != Item::FUNC_ITEM)
4290
 
    return;
4291
 
  Item_func *cond_func= (Item_func*) cond;
4292
 
  switch (cond_func->select_optimize()) {
4293
 
  case Item_func::OPTIMIZE_NONE:
4294
 
    break;
4295
 
  case Item_func::OPTIMIZE_KEY:
4296
 
  {
4297
 
    Item **values;
4298
 
    // BETWEEN, IN, NE
4299
 
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4300
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4301
 
    {
4302
 
      values= cond_func->arguments()+1;
4303
 
      if (cond_func->functype() == Item_func::NE_FUNC &&
4304
 
        cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4305
 
             !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4306
 
        values--;
4307
 
      assert(cond_func->functype() != Item_func::IN_FUNC ||
4308
 
                  cond_func->argument_count() != 2);
4309
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4310
 
                           (Item_field*) (cond_func->key_item()->real_item()),
4311
 
                           0, values, 
4312
 
                           cond_func->argument_count()-1,
4313
 
                           usable_tables, sargables);
4314
 
    }
4315
 
    if (cond_func->functype() == Item_func::BETWEEN)
4316
 
    {
4317
 
      values= cond_func->arguments();
4318
 
      for (uint i= 1 ; i < cond_func->argument_count() ; i++)
4319
 
      {
4320
 
        Item_field *field_item;
4321
 
        if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM
4322
 
            &&
4323
 
            !(cond_func->arguments()[i]->used_tables() & OUTER_REF_TABLE_BIT))
4324
 
        {
4325
 
          field_item= (Item_field *) (cond_func->arguments()[i]->real_item());
4326
 
          add_key_equal_fields(key_fields, *and_level, cond_func,
4327
 
                               field_item, 0, values, 1, usable_tables, 
4328
 
                               sargables);
4329
 
        }
4330
 
      }  
4331
 
    }
4332
 
    break;
4333
 
  }
4334
 
  case Item_func::OPTIMIZE_OP:
4335
 
  {
4336
 
    bool equal_func=(cond_func->functype() == Item_func::EQ_FUNC ||
4337
 
                     cond_func->functype() == Item_func::EQUAL_FUNC);
4338
 
 
4339
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4340
 
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4341
 
    {
4342
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4343
 
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
4344
 
                           equal_func,
4345
 
                           cond_func->arguments()+1, 1, usable_tables,
4346
 
                           sargables);
4347
 
    }
4348
 
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4349
 
        cond_func->functype() != Item_func::LIKE_FUNC &&
4350
 
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4351
 
    {
4352
 
      add_key_equal_fields(key_fields, *and_level, cond_func, 
4353
 
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
4354
 
                           equal_func,
4355
 
                           cond_func->arguments(),1,usable_tables,
4356
 
                           sargables);
4357
 
    }
4358
 
    break;
4359
 
  }
4360
 
  case Item_func::OPTIMIZE_NULL:
4361
 
    /* column_name IS [NOT] NULL */
4362
 
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4363
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4364
 
    {
4365
 
      Item *tmp=new Item_null;
4366
 
      if (unlikely(!tmp))                       // Should never be true
4367
 
        return;
4368
 
      add_key_equal_fields(key_fields, *and_level, cond_func,
4369
 
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
4370
 
                    cond_func->functype() == Item_func::ISNULL_FUNC,
4371
 
                           &tmp, 1, usable_tables, sargables);
4372
 
    }
4373
 
    break;
4374
 
  case Item_func::OPTIMIZE_EQUAL:
4375
 
    Item_equal *item_equal= (Item_equal *) cond;
4376
 
    Item *const_item= item_equal->get_const();
4377
 
    Item_equal_iterator it(*item_equal);
4378
 
    Item_field *item;
4379
 
    if (const_item)
4380
 
    {
4381
 
      /*
4382
 
        For each field field1 from item_equal consider the equality 
4383
 
        field1=const_item as a condition allowing an index access of the table
4384
 
        with field1 by the keys value of field1.
4385
 
      */   
4386
 
      while ((item= it++))
4387
 
      {
4388
 
        add_key_field(key_fields, *and_level, cond_func, item->field,
4389
 
                      true, &const_item, 1, usable_tables, sargables);
4390
 
      }
4391
 
    }
4392
 
    else 
4393
 
    {
4394
 
      /*
4395
 
        Consider all pairs of different fields included into item_equal.
4396
 
        For each of them (field1, field1) consider the equality 
4397
 
        field1=field2 as a condition allowing an index access of the table
4398
 
        with field1 by the keys value of field2.
4399
 
      */   
4400
 
      Item_equal_iterator fi(*item_equal);
4401
 
      while ((item= fi++))
4402
 
      {
4403
 
        Field *field= item->field;
4404
 
        while ((item= it++))
4405
 
        {
4406
 
          if (!field->eq(item->field))
4407
 
          {
4408
 
            add_key_field(key_fields, *and_level, cond_func, field,
4409
 
                          true, (Item **) &item, 1, usable_tables,
4410
 
                          sargables);
4411
 
          }
4412
 
        }
4413
 
        it.rewind();
4414
 
      }
4415
 
    }
4416
 
    break;
4417
 
  }
4418
 
}
4419
495
 
4420
496
/**
4421
497
  Add all keys with uses 'field' for some keypart.
4422
498
 
4423
499
  If field->and_level != and_level then only mark key_part as const_part.
4424
500
*/
4425
 
 
4426
 
static uint
4427
 
max_part_bit(key_part_map bits)
 
501
uint32_t max_part_bit(key_part_map bits)
4428
502
{
4429
 
  uint found;
 
503
  uint32_t found;
4430
504
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4431
505
  return found;
4432
506
}
4433
507
 
4434
 
static void
4435
 
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4436
 
{
4437
 
  Field *field=key_field->field;
4438
 
  TABLE *form= field->table;
4439
 
  KEYUSE keyuse;
4440
 
 
4441
 
  if (key_field->eq_func && !(key_field->optimize & KEY_OPTIMIZE_EXISTS))
4442
 
  {
4443
 
    for (uint key=0 ; key < form->s->keys ; key++)
4444
 
    {
4445
 
      if (!(form->keys_in_use_for_query.is_set(key)))
4446
 
        continue;
4447
 
 
4448
 
      uint key_parts= (uint) form->key_info[key].key_parts;
4449
 
      for (uint part=0 ; part <  key_parts ; part++)
4450
 
      {
4451
 
        if (field->eq(form->key_info[key].key_part[part].field))
4452
 
        {
4453
 
          keyuse.table= field->table;
4454
 
          keyuse.val =  key_field->val;
4455
 
          keyuse.key =  key;
4456
 
          keyuse.keypart=part;
4457
 
          keyuse.keypart_map= (key_part_map) 1 << part;
4458
 
          keyuse.used_tables=key_field->val->used_tables();
4459
 
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4460
 
          keyuse.null_rejecting= key_field->null_rejecting;
4461
 
          keyuse.cond_guard= key_field->cond_guard;
4462
 
          keyuse.sj_pred_no= key_field->sj_pred_no;
4463
 
          VOID(insert_dynamic(keyuse_array,(uchar*) &keyuse));
4464
 
        }
4465
 
      }
4466
 
    }
4467
 
  }
4468
 
}
4469
 
 
4470
 
static int
4471
 
sort_keyuse(KEYUSE *a,KEYUSE *b)
 
508
static int sort_keyuse(optimizer::KeyUse *a, optimizer::KeyUse *b)
4472
509
{
4473
510
  int res;
4474
 
  if (a->table->tablenr != b->table->tablenr)
4475
 
    return (int) (a->table->tablenr - b->table->tablenr);
4476
 
  if (a->key != b->key)
4477
 
    return (int) (a->key - b->key);
4478
 
  if (a->keypart != b->keypart)
4479
 
    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()));
4480
517
  // Place const values before other ones
4481
 
  if ((res= test((a->used_tables & ~OUTER_REF_TABLE_BIT)) -
4482
 
       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))))
4483
520
    return res;
4484
521
  /* Place rows that are not 'OPTIMIZE_REF_OR_NULL' first */
4485
 
  return (int) ((a->optimize & KEY_OPTIMIZE_REF_OR_NULL) -
4486
 
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4487
 
}
4488
 
 
4489
 
 
4490
 
/*
4491
 
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
4492
 
 
4493
 
    This function populates KEY_FIELD array with entries generated from the 
4494
 
    ON condition of the given nested join, and does the same for nested joins 
4495
 
    contained within this nested join.
4496
 
 
4497
 
  @param[in]      nested_join_table   Nested join pseudo-table to process
4498
 
  @param[in,out]  end                 End of the key field array
4499
 
  @param[in,out]  and_level           And-level
4500
 
  @param[in,out]  sargables           Array of found sargable candidates
4501
 
 
4502
 
 
4503
 
  @note
4504
 
    We can add accesses to the tables that are direct children of this nested 
4505
 
    join (1), and are not inner tables w.r.t their neighbours (2).
4506
 
    
4507
 
    Example for #1 (outer brackets pair denotes nested join this function is 
4508
 
    invoked for):
4509
 
    @code
4510
 
     ... LEFT JOIN (t1 LEFT JOIN (t2 ... ) ) ON cond
4511
 
    @endcode
4512
 
    Example for #2:
4513
 
    @code
4514
 
     ... LEFT JOIN (t1 LEFT JOIN t2 ) ON cond
4515
 
    @endcode
4516
 
    In examples 1-2 for condition cond, we can add 'ref' access candidates to 
4517
 
    t1 only.
4518
 
    Example #3:
4519
 
    @code
4520
 
     ... LEFT JOIN (t1, t2 LEFT JOIN t3 ON inner_cond) ON cond
4521
 
    @endcode
4522
 
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4523
 
*/
4524
 
 
4525
 
static void add_key_fields_for_nj(JOIN *join, TABLE_LIST *nested_join_table,
4526
 
                                  KEY_FIELD **end, uint *and_level,
4527
 
                                  SARGABLE_PARAM **sargables)
4528
 
{
4529
 
  List_iterator<TABLE_LIST> li(nested_join_table->nested_join->join_list);
4530
 
  List_iterator<TABLE_LIST> li2(nested_join_table->nested_join->join_list);
4531
 
  bool have_another = false;
4532
 
  table_map tables= 0;
4533
 
  TABLE_LIST *table;
4534
 
  assert(nested_join_table->nested_join);
4535
 
 
4536
 
  while ((table= li++) || (have_another && (li=li2, have_another=false,
4537
 
                                            (table= li++))))
4538
 
  {
4539
 
    if (table->nested_join)
4540
 
    {
4541
 
      if (!table->on_expr)
4542
 
      {
4543
 
        /* It's a semi-join nest. Walk into it as if it wasn't a nest */
4544
 
        have_another= true;
4545
 
        li2= li;
4546
 
        li= List_iterator<TABLE_LIST>(table->nested_join->join_list); 
4547
 
      }
4548
 
      else
4549
 
        add_key_fields_for_nj(join, table, end, and_level, sargables);
4550
 
    }
4551
 
    else
4552
 
      if (!table->on_expr)
4553
 
        tables |= table->table->map;
4554
 
  }
4555
 
  if (nested_join_table->on_expr)
4556
 
    add_key_fields(join, end, and_level, nested_join_table->on_expr, tables,
4557
 
                   sargables);
 
522
  return static_cast<int>(((a->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) -
 
523
                          (b->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL)));
4558
524
}
4559
525
 
4560
526
 
4561
527
/**
4562
528
  Update keyuse array with all possible keys we can use to fetch rows.
4563
 
  
4564
 
  @param       thd 
4565
 
  @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
4566
532
  @param       join_tab       Array in tablenr_order
4567
533
  @param       tables         Number of tables in join
4568
534
  @param       cond           WHERE condition (note that the function analyzes
4571
537
                              for which we can make ref access based the WHERE
4572
538
                              clause)
4573
539
  @param       select_lex     current SELECT
4574
 
  @param[out]  sargables      Array of found sargable candidates
4575
 
      
 
540
  @param[out]  sargables      std::vector of found sargable candidates
 
541
 
4576
542
   @retval
4577
543
     0  OK
4578
544
   @retval
4579
545
     1  Out of memory.
4580
546
*/
4581
 
 
4582
 
static bool
4583
 
update_ref_and_keys(THD *thd, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4584
 
                    uint tables, COND *cond,
4585
 
                    COND_EQUAL *cond_equal __attribute__((unused)),
4586
 
                    table_map normal_tables, SELECT_LEX *select_lex,
4587
 
                    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)
4588
556
{
4589
557
  uint  and_level,i,found_eq_constant;
4590
 
  KEY_FIELD *key_fields, *end, *field;
4591
 
  uint sz;
4592
 
  uint m= max(select_lex->max_equal_elems,(uint32_t)1);
4593
 
  
4594
 
  /* 
4595
 
    We use the same piece of memory to store both  KEY_FIELD 
4596
 
    and SARGABLE_PARAM structure.
4597
 
    KEY_FIELD values are placed at the beginning this memory
4598
 
    while  SARGABLE_PARAM values are put at the end.
4599
 
    All predicates that are used to fill arrays of KEY_FIELD
4600
 
    and SARGABLE_PARAM structures have at most 2 arguments
4601
 
    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
4602
566
    IN predicates.
4603
 
    This any predicate if it's not BETWEEN/IN can be used 
4604
 
    directly to fill at most 2 array elements, either of KEY_FIELD
4605
 
    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
4606
570
    can be filled as this predicate is considered as
4607
571
    saragable with respect to each of its argument.
4608
572
    An IN predicate can require at most 1 element as currently
4609
573
    it is considered as sargable only for its first argument.
4610
574
    Multiple equality can add  elements that are filled after
4611
575
    substitution of field arguments by equal fields. There
4612
 
    can be not more than select_lex->max_equal_elems such 
 
576
    can be not more than select_lex->max_equal_elems such
4613
577
    substitutions.
4614
 
  */ 
4615
 
  sz= max(sizeof(KEY_FIELD),sizeof(SARGABLE_PARAM))*
4616
 
      (((thd->lex->current_select->cond_count+1)*2 +
4617
 
        thd->lex->current_select->between_count)*m+1);
4618
 
  if (!(key_fields=(KEY_FIELD*) thd->alloc(sz)))
4619
 
    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;
4620
584
  and_level= 0;
4621
585
  field= end= key_fields;
4622
 
  *sargables= (SARGABLE_PARAM *) key_fields + 
4623
 
                (sz - sizeof((*sargables)[0].field))/sizeof(SARGABLE_PARAM);
4624
 
  /* set a barrier for the array of SARGABLE_PARAM */
4625
 
  (*sargables)[0].field= 0; 
4626
586
 
4627
 
  if (my_init_dynamic_array(keyuse,sizeof(KEYUSE),20,64))
 
587
  if (my_init_dynamic_array(keyuse, sizeof(optimizer::KeyUse), 20, 64))
4628
588
    return true;
4629
589
  if (cond)
4630
590
  {
4631
591
    add_key_fields(join_tab->join, &end, &and_level, cond, normal_tables,
4632
592
                   sargables);
4633
 
    for (; field != end ; field++)
 
593
    for (; field != end; field++)
4634
594
    {
4635
 
      add_key_part(keyuse,field);
 
595
      add_key_part(keyuse, field);
4636
596
      /* Mark that we can optimize LEFT JOIN */
4637
 
      if (field->val->type() == Item::NULL_ITEM &&
4638
 
          !field->field->real_maybe_null())
4639
 
        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
      }
4640
602
    }
4641
603
  }
4642
 
  for (i=0 ; i < tables ; i++)
 
604
  for (i= 0; i < tables; i++)
4643
605
  {
4644
606
    /*
4645
607
      Block the creation of keys for inner tables of outer joins.
4649
611
      In the future when we introduce conditional accesses
4650
612
      for inner tables in outer joins these keys will be taken
4651
613
      into account as well.
4652
 
    */ 
 
614
    */
4653
615
    if (*join_tab[i].on_expr_ref)
4654
 
      add_key_fields(join_tab->join, &end, &and_level, 
 
616
      add_key_fields(join_tab->join, &end, &and_level,
4655
617
                     *join_tab[i].on_expr_ref,
4656
618
                     join_tab[i].table->map, sargables);
4657
619
  }
4658
620
 
4659
621
  /* Process ON conditions for the nested joins */
4660
622
  {
4661
 
    List_iterator<TABLE_LIST> li(*join_tab->join->join_list);
4662
 
    TABLE_LIST *table;
 
623
    List_iterator<TableList> li(*join_tab->join->join_list);
 
624
    TableList *table;
4663
625
    while ((table= li++))
4664
626
    {
4665
627
      if (table->nested_join)
4666
 
        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,
4667
629
                              sargables);
4668
630
    }
4669
631
  }
4683
645
  */
4684
646
  if (keyuse->elements)
4685
647
  {
4686
 
    KEYUSE key_end,*prev,*save_pos,*use;
 
648
    optimizer::KeyUse key_end,*prev,*save_pos,*use;
4687
649
 
4688
 
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(KEYUSE),
 
650
    my_qsort(keyuse->buffer,keyuse->elements,sizeof(optimizer::KeyUse),
4689
651
          (qsort_cmp) sort_keyuse);
4690
652
 
4691
653
    memset(&key_end, 0, sizeof(key_end)); /* Add for easy testing */
4692
 
    VOID(insert_dynamic(keyuse,(uchar*) &key_end));
 
654
    insert_dynamic(keyuse,(unsigned char*) &key_end);
4693
655
 
4694
 
    use=save_pos=dynamic_element(keyuse,0,KEYUSE*);
 
656
    use= save_pos= dynamic_element(keyuse, 0, optimizer::KeyUse*);
4695
657
    prev= &key_end;
4696
 
    found_eq_constant=0;
4697
 
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
 
658
    found_eq_constant= 0;
 
659
    for (i= 0; i < keyuse->elements-1; i++, use++)
4698
660
    {
4699
 
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4700
 
        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())
4701
664
      {
4702
 
        if (use->key == prev->key && use->table == prev->table)
4703
 
        {
4704
 
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4705
 
            continue;                           /* remove */
4706
 
        }
4707
 
        else if (use->keypart != 0)             // First found must be 0
4708
 
          continue;
 
665
        if (prev->getKeypart() + 1 < use->getKeypart() || 
 
666
            ((prev->getKeypart() == use->getKeypart()) && found_eq_constant))
 
667
          continue;                             /* remove */
4709
668
      }
 
669
      else if (use->getKeypart() != 0)          // First found must be 0
 
670
        continue;
4710
671
 
4711
672
#ifdef HAVE_purify
4712
673
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
4714
675
#endif
4715
676
        *save_pos= *use;
4716
677
      prev=use;
4717
 
      found_eq_constant= !use->used_tables;
 
678
      found_eq_constant= ! use->getUsedTables();
4718
679
      /* Save ptr to first use */
4719
 
      if (!use->table->reginfo.join_tab->keyuse)
4720
 
        use->table->reginfo.join_tab->keyuse=save_pos;
4721
 
      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());
4722
683
      save_pos++;
4723
684
    }
4724
 
    i=(uint) (save_pos-(KEYUSE*) keyuse->buffer);
4725
 
    VOID(set_dynamic(keyuse,(uchar*) &key_end,i));
4726
 
    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;
4727
688
  }
4728
689
  return false;
4729
690
}
4731
692
/**
4732
693
  Update some values in keyuse for faster choose_plan() loop.
4733
694
*/
4734
 
 
4735
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
695
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
4736
696
{
4737
 
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
 
697
  optimizer::KeyUse *end,*keyuse= dynamic_element(keyuse_array, 
 
698
                                                  0, 
 
699
                                                  optimizer::KeyUse*);
4738
700
 
4739
701
  for (end= keyuse+ keyuse_array->elements ; keyuse < end ; keyuse++)
4740
702
  {
4747
709
      Constant tables are ignored.
4748
710
      To avoid bad matches, we don't make ref_table_rows less than 100.
4749
711
    */
4750
 
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
4751
 
    if (keyuse->used_tables &
4752
 
        (map= (keyuse->used_tables & ~join->const_table_map &
4753
 
               ~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)))
4754
714
    {
4755
 
      uint tablenr;
 
715
      uint32_t tablenr;
4756
716
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4757
717
      if (map == 1)                     // Only one table
4758
718
      {
4759
 
        TABLE *tmp_table=join->all_tables[tablenr];
4760
 
        keyuse->ref_table_rows= max(tmp_table->file->stats.records, (ha_rows)100);
 
719
        Table *tmp_table=join->all_tables[tablenr];
 
720
        keyuse->setTableRows(max(tmp_table->cursor->stats.records, (ha_rows)100));
4761
721
      }
4762
722
    }
4763
723
    /*
4764
724
      Outer reference (external field) is constant for single executing
4765
725
      of subquery
4766
726
    */
4767
 
    if (keyuse->used_tables == OUTER_REF_TABLE_BIT)
4768
 
      keyuse->ref_table_rows= 1;
 
727
    if (keyuse->getUsedTables() == OUTER_REF_TABLE_BIT)
 
728
      keyuse->setTableRows(1);
4769
729
  }
4770
730
}
4771
731
 
4787
747
  @return
4788
748
    None
4789
749
*/
4790
 
 
4791
 
static void
4792
 
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
 
750
void add_group_and_distinct_keys(JOIN *join, JoinTable *join_tab)
4793
751
{
4794
752
  List<Item_field> indexed_fields;
4795
753
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4796
 
  ORDER      *cur_group;
 
754
  order_st      *cur_group;
4797
755
  Item_field *cur_item;
4798
756
  key_map possible_keys(0);
4799
757
 
4801
759
  { /* Collect all query fields referenced in the GROUP clause. */
4802
760
    for (cur_group= join->group_list; cur_group; cur_group= cur_group->next)
4803
761
      (*cur_group->item)->walk(&Item::collect_item_field_processor, 0,
4804
 
                               (uchar*) &indexed_fields);
 
762
                               (unsigned char*) &indexed_fields);
4805
763
  }
4806
764
  else if (join->select_distinct)
4807
765
  { /* Collect all query fields referenced in the SELECT clause. */
4810
768
    Item *item;
4811
769
    while ((item= select_items_it++))
4812
770
      item->walk(&Item::collect_item_field_processor, 0,
4813
 
                 (uchar*) &indexed_fields);
 
771
                 (unsigned char*) &indexed_fields);
4814
772
  }
4815
773
  else
4816
774
    return;
4820
778
 
4821
779
  /* Intersect the keys of all group fields. */
4822
780
  cur_item= indexed_fields_it++;
4823
 
  possible_keys.merge(cur_item->field->part_of_key);
 
781
  possible_keys|= cur_item->field->part_of_key;
4824
782
  while ((cur_item= indexed_fields_it++))
4825
783
  {
4826
 
    possible_keys.intersect(cur_item->field->part_of_key);
4827
 
  }
4828
 
 
4829
 
  if (!possible_keys.is_clear_all())
4830
 
    join_tab->const_keys.merge(possible_keys);
4831
 
}
4832
 
 
4833
 
 
4834
 
/*****************************************************************************
4835
 
  Go through all combinations of not marked tables and find the one
4836
 
  which uses least records
4837
 
*****************************************************************************/
4838
 
 
4839
 
/** Save const tables first as used tables. */
4840
 
 
4841
 
static void
4842
 
set_position(JOIN *join,uint idx,JOIN_TAB *table,KEYUSE *key)
4843
 
{
4844
 
  join->positions[idx].table= table;
4845
 
  join->positions[idx].key=key;
4846
 
  join->positions[idx].records_read=1.0;        /* This is a const table */
4847
 
  join->positions[idx].ref_depend_map= 0;
4848
 
 
4849
 
  /* Move the const table as down as possible in best_ref */
4850
 
  JOIN_TAB **pos=join->best_ref+idx+1;
4851
 
  JOIN_TAB *next=join->best_ref[idx];
4852
 
  for (;next != table ; pos++)
4853
 
  {
4854
 
    JOIN_TAB *tmp=pos[0];
4855
 
    pos[0]=next;
4856
 
    next=tmp;
4857
 
  }
4858
 
  join->best_ref[idx]=table;
4859
 
}
4860
 
 
4861
 
 
4862
 
/*
4863
 
  Given a semi-join nest, find out which of the IN-equalities are bound
4864
 
 
4865
 
  SYNOPSIS
4866
 
    get_bound_sj_equalities()
4867
 
      sj_nest           Semi-join nest
4868
 
      remaining_tables  Tables that are not yet bound
4869
 
 
4870
 
  DESCRIPTION
4871
 
    Given a semi-join nest, find out which of the IN-equalities have their
4872
 
    left part expression bound (i.e. the said expression doesn't refer to
4873
 
    any of remaining_tables and can be evaluated).
4874
 
 
4875
 
  RETURN
4876
 
    Bitmap of bound IN-equalities.
4877
 
*/
4878
 
 
4879
 
uint64_t get_bound_sj_equalities(TABLE_LIST *sj_nest, 
4880
 
                                  table_map remaining_tables)
4881
 
{
4882
 
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4883
 
  Item *item;
4884
 
  uint i= 0;
4885
 
  uint64_t res= 0;
4886
 
  while ((item= li++))
4887
 
  {
4888
 
    /*
4889
 
      Q: should this take into account equality propagation and how?
4890
 
      A: If e->outer_side is an Item_field, walk over the equality
4891
 
         class and see if there is an element that is bound?
4892
 
      (this is an optional feature)
4893
 
    */
4894
 
    if (!(item->used_tables() & remaining_tables))
4895
 
    {
4896
 
      res |= 1ULL < i;
4897
 
    }
4898
 
  }
4899
 
  return res;
4900
 
}
4901
 
 
4902
 
 
4903
 
/**
4904
 
  Find the best access path for an extension of a partial execution
4905
 
  plan and add this path to the plan.
4906
 
 
4907
 
  The function finds the best access path to table 's' from the passed
4908
 
  partial plan where an access path is the general term for any means to
4909
 
  access the data in 's'. An access path may use either an index or a scan,
4910
 
  whichever is cheaper. The input partial plan is passed via the array
4911
 
  'join->positions' of length 'idx'. The chosen access method for 's' and its
4912
 
  cost are stored in 'join->positions[idx]'.
4913
 
 
4914
 
  @param join             pointer to the structure providing all context info
4915
 
                          for the query
4916
 
  @param s                the table to be joined by the function
4917
 
  @param thd              thread for the connection that submitted the query
4918
 
  @param remaining_tables set of tables not included into the partial plan yet
4919
 
  @param idx              the length of the partial plan
4920
 
  @param record_count     estimate for the number of records returned by the
4921
 
                          partial plan
4922
 
  @param read_time        the cost of the partial plan
4923
 
 
4924
 
  @return
4925
 
    None
4926
 
*/
4927
 
 
4928
 
static void
4929
 
best_access_path(JOIN      *join,
4930
 
                 JOIN_TAB  *s,
4931
 
                 THD       *thd,
4932
 
                 table_map remaining_tables,
4933
 
                 uint      idx,
4934
 
                 double    record_count,
4935
 
                 double    read_time __attribute__((unused)))
4936
 
{
4937
 
  KEYUSE *best_key=         0;
4938
 
  uint best_max_key_part=   0;
4939
 
  bool found_constraint= 0;
4940
 
  double best=              DBL_MAX;
4941
 
  double best_time=         DBL_MAX;
4942
 
  double records=           DBL_MAX;
4943
 
  table_map best_ref_depends_map= 0;
4944
 
  double tmp;
4945
 
  ha_rows rec;
4946
 
  uint best_is_sj_inside_out=    0;
4947
 
 
4948
 
  if (s->keyuse)
4949
 
  {                                            /* Use key if possible */
4950
 
    TABLE *table= s->table;
4951
 
    KEYUSE *keyuse,*start_key=0;
4952
 
    double best_records= DBL_MAX;
4953
 
    uint max_key_part=0;
4954
 
    uint64_t bound_sj_equalities= 0;
4955
 
    bool try_sj_inside_out= false;
4956
 
    /*
4957
 
      Discover the bound equalites. We need to do this, if
4958
 
        1. The next table is an SJ-inner table, and
4959
 
        2. It is the first table from that semijoin, and
4960
 
        3. We're not within a semi-join range (i.e. all semi-joins either have
4961
 
           all or none of their tables in join_table_map), except
4962
 
           s->emb_sj_nest (which we've just entered).
4963
 
        3. All correlation references from this sj-nest are bound
4964
 
    */
4965
 
    if (s->emb_sj_nest &&                                                 // (1)
4966
 
        s->emb_sj_nest->sj_in_exprs < 64 && 
4967
 
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
4968
 
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
4969
 
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
4970
 
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4971
 
    {
4972
 
      /* This table is an InsideOut scan candidate */
4973
 
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest, 
4974
 
                                                   remaining_tables);
4975
 
      try_sj_inside_out= true;
4976
 
    }
4977
 
 
4978
 
    /* Test how we can use keys */
4979
 
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
4980
 
    for (keyuse=s->keyuse ; keyuse->table == table ;)
4981
 
    {
4982
 
      key_part_map found_part= 0;
4983
 
      table_map found_ref= 0;
4984
 
      uint key= keyuse->key;
4985
 
      KEY *keyinfo= table->key_info+key;
4986
 
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4987
 
      key_part_map const_part= 0;
4988
 
      /* The or-null keypart in ref-or-null access: */
4989
 
      key_part_map ref_or_null_part= 0;
4990
 
 
4991
 
      /* Calculate how many key segments of the current key we can use */
4992
 
      start_key= keyuse;
4993
 
      uint64_t handled_sj_equalities=0;
4994
 
      key_part_map sj_insideout_map= 0;
4995
 
 
4996
 
      do /* For each keypart */
4997
 
      {
4998
 
        uint keypart= keyuse->keypart;
4999
 
        table_map best_part_found_ref= 0;
5000
 
        double best_prev_record_reads= DBL_MAX;
5001
 
        
5002
 
        do /* For each way to access the keypart */
5003
 
        {
5004
 
 
5005
 
          /*
5006
 
            if 1. expression doesn't refer to forward tables
5007
 
               2. we won't get two ref-or-null's
5008
 
          */
5009
 
          if (!(remaining_tables & keyuse->used_tables) &&
5010
 
              !(ref_or_null_part && (keyuse->optimize &
5011
 
                                     KEY_OPTIMIZE_REF_OR_NULL)))
5012
 
          {
5013
 
            found_part|= keyuse->keypart_map;
5014
 
            if (!(keyuse->used_tables & ~join->const_table_map))
5015
 
              const_part|= keyuse->keypart_map;
5016
 
 
5017
 
            double tmp2= prev_record_reads(join, idx, (found_ref |
5018
 
                                                      keyuse->used_tables));
5019
 
            if (tmp2 < best_prev_record_reads)
5020
 
            {
5021
 
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5022
 
              best_prev_record_reads= tmp2;
5023
 
            }
5024
 
            if (rec > keyuse->ref_table_rows)
5025
 
              rec= keyuse->ref_table_rows;
5026
 
            /*
5027
 
              If there is one 'key_column IS NULL' expression, we can
5028
 
              use this ref_or_null optimisation of this field
5029
 
            */
5030
 
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5031
 
              ref_or_null_part |= keyuse->keypart_map;
5032
 
          }
5033
 
 
5034
 
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5035
 
          {
5036
 
            if (!(remaining_tables & keyuse->used_tables))
5037
 
              bound_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5038
 
            else
5039
 
            {
5040
 
              handled_sj_equalities |= 1ULL << keyuse->sj_pred_no;
5041
 
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5042
 
            }
5043
 
          }
5044
 
 
5045
 
          keyuse++;
5046
 
        } while (keyuse->table == table && keyuse->key == key &&
5047
 
                 keyuse->keypart == keypart);
5048
 
        found_ref|= best_part_found_ref;
5049
 
      } while (keyuse->table == table && keyuse->key == key);
5050
 
 
5051
 
      /*
5052
 
        Assume that that each key matches a proportional part of table.
5053
 
      */
5054
 
      if (!found_part && !handled_sj_equalities)
5055
 
        continue;                               // Nothing usable found
5056
 
 
5057
 
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5058
 
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
5059
 
 
5060
 
      bool sj_inside_out_scan= false;
5061
 
      {
5062
 
        found_constraint= 1;
5063
 
        /*
5064
 
          Check if InsideOut scan is applicable:
5065
 
          1. All IN-equalities are either "bound" or "handled"
5066
 
          2. Index keyparts are 
5067
 
             ...
5068
 
        */
5069
 
        if (try_sj_inside_out && 
5070
 
            table->covering_keys.is_set(key) &&
5071
 
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5072
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5073
 
        {
5074
 
          uint n_fixed_parts= max_part_bit(found_part);
5075
 
          if (n_fixed_parts != keyinfo->key_parts &&
5076
 
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5077
 
               PREV_BITS(uint, keyinfo->key_parts))
5078
 
          {
5079
 
            /*
5080
 
              Not all parts are fixed. Produce bitmap of remaining bits and
5081
 
              check if all of them are covered.
5082
 
            */
5083
 
            sj_inside_out_scan= true;
5084
 
            if (!n_fixed_parts)
5085
 
            {
5086
 
              /*
5087
 
                It's a confluent ref scan.
5088
 
 
5089
 
                That is, all found KEYUSE elements refer to IN-equalities,
5090
 
                and there is really no ref access because there is no
5091
 
                  t.keypart0 = {bound expression}
5092
 
 
5093
 
                Calculate the cost of complete loose index scan.
5094
 
              */
5095
 
              records= (double)s->table->file->stats.records;
5096
 
 
5097
 
              /* The cost is entire index scan cost (divided by 2) */
5098
 
              best_time= s->table->file->index_only_read_time(key, records);
5099
 
 
5100
 
              /* Now figure how many different keys we will get */
5101
 
              ulong rpc;
5102
 
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5103
 
                records= records / rpc;
5104
 
              start_key= NULL;
5105
 
            }
5106
 
          }
5107
 
        }
5108
 
 
5109
 
        /*
5110
 
          Check if we found full key
5111
 
        */
5112
 
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5113
 
            !ref_or_null_part)
5114
 
        {                                         /* use eq key */
5115
 
          max_key_part= (uint) ~0;
5116
 
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5117
 
          {
5118
 
            tmp = prev_record_reads(join, idx, found_ref);
5119
 
            records=1.0;
5120
 
          }
5121
 
          else
5122
 
          {
5123
 
            if (!found_ref)
5124
 
            {                                     /* We found a const key */
5125
 
              /*
5126
 
                ReuseRangeEstimateForRef-1:
5127
 
                We get here if we've found a ref(const) (c_i are constants):
5128
 
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
5129
 
                
5130
 
                If range optimizer was able to construct a "range" 
5131
 
                access on this index, then its condition "quick_cond" was
5132
 
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5133
 
                from the range optimizer.
5134
 
                
5135
 
                Proof of (*): By properties of range and ref optimizers 
5136
 
                quick_cond will be equal or tighther than ref_const_cond. 
5137
 
                ref_const_cond already covers "smallest" possible interval - 
5138
 
                a singlepoint interval over all keyparts. Therefore, 
5139
 
                quick_cond is equivalent to ref_const_cond (if it was an 
5140
 
                empty interval we wouldn't have got here).
5141
 
              */
5142
 
              if (table->quick_keys.is_set(key))
5143
 
                records= (double) table->quick_rows[key];
5144
 
              else
5145
 
              {
5146
 
                /* quick_range couldn't use key! */
5147
 
                records= (double) s->records/rec;
5148
 
              }
5149
 
            }
5150
 
            else
5151
 
            {
5152
 
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5153
 
              {                                   /* Prefer longer keys */
5154
 
                records=
5155
 
                  ((double) s->records / (double) rec *
5156
 
                   (1.0 +
5157
 
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
5158
 
                     (double) table->s->max_key_length)));
5159
 
                if (records < 2.0)
5160
 
                  records=2.0;               /* Can't be as good as a unique */
5161
 
              }
5162
 
              /*
5163
 
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
5164
 
                E(#rows) from range optimizer. Make another try:
5165
 
                
5166
 
                If range optimizer produced E(#rows) for a prefix of the ref
5167
 
                access we're considering, and that E(#rows) is lower then our
5168
 
                current estimate, make an adjustment. The criteria of when we
5169
 
                can make an adjustment is a special case of the criteria used
5170
 
                in ReuseRangeEstimateForRef-3.
5171
 
              */
5172
 
              if (table->quick_keys.is_set(key) &&
5173
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5174
 
                  table->quick_n_ranges[key] == 1 &&
5175
 
                  records > (double) table->quick_rows[key])
5176
 
              {
5177
 
                records= (double) table->quick_rows[key];
5178
 
              }
5179
 
            }
5180
 
            /* Limit the number of matched rows */
5181
 
            tmp= records;
5182
 
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5183
 
            if (table->covering_keys.is_set(key))
5184
 
            {
5185
 
              /* we can use only index tree */
5186
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5187
 
            }
5188
 
            else
5189
 
              tmp= record_count*min(tmp,s->worst_seeks);
5190
 
          }
5191
 
        }
5192
 
        else
5193
 
        {
5194
 
          /*
5195
 
            Use as much key-parts as possible and a uniq key is better
5196
 
            than a not unique key
5197
 
            Set tmp to (previous record count) * (records / combination)
5198
 
          */
5199
 
          if ((found_part & 1) &&
5200
 
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5201
 
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
5202
 
          {
5203
 
            max_key_part= max_part_bit(found_part);
5204
 
            /*
5205
 
              ReuseRangeEstimateForRef-3:
5206
 
              We're now considering a ref[or_null] access via
5207
 
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR  
5208
 
              (same-as-above but with one cond replaced 
5209
 
               with "t.keypart_i IS NULL")]  (**)
5210
 
              
5211
 
              Try re-using E(#rows) from "range" optimizer:
5212
 
              We can do so if "range" optimizer used the same intervals as
5213
 
              in (**). The intervals used by range optimizer may be not 
5214
 
              available at this point (as "range" access might have choosen to
5215
 
              create quick select over another index), so we can't compare
5216
 
              them to (**). We'll make indirect judgements instead.
5217
 
              The sufficient conditions for re-use are:
5218
 
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5219
 
                   this is not satisfied we have no way to know which ranges
5220
 
                   will be actually scanned by 'ref' until we execute the 
5221
 
                   join)
5222
 
              (C2) max #key parts in 'range' access == K == max_key_part (this
5223
 
                   is apparently a necessary requirement)
5224
 
 
5225
 
              We also have a property that "range optimizer produces equal or 
5226
 
              tighter set of scan intervals than ref(const) optimizer". Each
5227
 
              of the intervals in (**) are "tightest possible" intervals when 
5228
 
              one limits itself to using keyparts 1..K (which we do in #2).              
5229
 
              From here it follows that range access used either one, or
5230
 
              both of the (I1) and (I2) intervals:
5231
 
              
5232
 
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1) 
5233
 
               (same-as-above but with one cond replaced  
5234
 
                with "t.keypart_i IS NULL")               (I2)
5235
 
 
5236
 
              The remaining part is to exclude the situation where range
5237
 
              optimizer used one interval while we're considering
5238
 
              ref-or-null and looking for estimate for two intervals. This
5239
 
              is done by last limitation:
5240
 
 
5241
 
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5242
 
            */
5243
 
            if (table->quick_keys.is_set(key) && !found_ref &&          //(C1)
5244
 
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5245
 
                table->quick_n_ranges[key] == 1+test(ref_or_null_part)) //(C3)
5246
 
            {
5247
 
              tmp= records= (double) table->quick_rows[key];
5248
 
            }
5249
 
            else
5250
 
            {
5251
 
              /* Check if we have statistic about the distribution */
5252
 
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
5253
 
              {
5254
 
                /* 
5255
 
                  Fix for the case where the index statistics is too
5256
 
                  optimistic: If 
5257
 
                  (1) We're considering ref(const) and there is quick select
5258
 
                      on the same index, 
5259
 
                  (2) and that quick select uses more keyparts (i.e. it will
5260
 
                      scan equal/smaller interval then this ref(const))
5261
 
                  (3) and E(#rows) for quick select is higher then our
5262
 
                      estimate,
5263
 
                  Then 
5264
 
                    We'll use E(#rows) from quick select.
5265
 
 
5266
 
                  Q: Why do we choose to use 'ref'? Won't quick select be
5267
 
                  cheaper in some cases ?
5268
 
                  TODO: figure this out and adjust the plan choice if needed.
5269
 
                */
5270
 
                if (!found_ref && table->quick_keys.is_set(key) &&    // (1)
5271
 
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5272
 
                    records < (double)table->quick_rows[key])         // (3)
5273
 
                  records= (double)table->quick_rows[key];
5274
 
 
5275
 
                tmp= records;
5276
 
              }
5277
 
              else
5278
 
              {
5279
 
                /*
5280
 
                  Assume that the first key part matches 1% of the file
5281
 
                  and that the whole key matches 10 (duplicates) or 1
5282
 
                  (unique) records.
5283
 
                  Assume also that more key matches proportionally more
5284
 
                  records
5285
 
                  This gives the formula:
5286
 
                  records = (x * (b-a) + a*c-b)/(c-1)
5287
 
 
5288
 
                  b = records matched by whole key
5289
 
                  a = records matched by first key part (1% of all records?)
5290
 
                  c = number of key parts in key
5291
 
                  x = used key parts (1 <= x <= c)
5292
 
                */
5293
 
                double rec_per_key;
5294
 
                if (!(rec_per_key=(double)
5295
 
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
5296
 
                  rec_per_key=(double) s->records/rec+1;
5297
 
 
5298
 
                if (!s->records)
5299
 
                  tmp = 0;
5300
 
                else if (rec_per_key/(double) s->records >= 0.01)
5301
 
                  tmp = rec_per_key;
5302
 
                else
5303
 
                {
5304
 
                  double a=s->records*0.01;
5305
 
                  if (keyinfo->key_parts > 1)
5306
 
                    tmp= (max_key_part * (rec_per_key - a) +
5307
 
                          a*keyinfo->key_parts - rec_per_key)/
5308
 
                         (keyinfo->key_parts-1);
5309
 
                  else
5310
 
                    tmp= a;
5311
 
                  set_if_bigger(tmp,1.0);
5312
 
                }
5313
 
                records = (ulong) tmp;
5314
 
              }
5315
 
 
5316
 
              if (ref_or_null_part)
5317
 
              {
5318
 
                /* We need to do two key searches to find key */
5319
 
                tmp *= 2.0;
5320
 
                records *= 2.0;
5321
 
              }
5322
 
 
5323
 
              /*
5324
 
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
5325
 
                E(#rows) from range optimizer. Make another try:
5326
 
                
5327
 
                If range optimizer produced E(#rows) for a prefix of the ref 
5328
 
                access we're considering, and that E(#rows) is lower then our
5329
 
                current estimate, make the adjustment.
5330
 
 
5331
 
                The decision whether we can re-use the estimate from the range
5332
 
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5333
 
                applied to first table->quick_key_parts[key] key parts.
5334
 
              */
5335
 
              if (table->quick_keys.is_set(key) &&
5336
 
                  table->quick_key_parts[key] <= max_key_part &&
5337
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5338
 
                  table->quick_n_ranges[key] == 1 + test(ref_or_null_part &
5339
 
                                                         const_part) &&
5340
 
                  records > (double) table->quick_rows[key])
5341
 
              {
5342
 
                tmp= records= (double) table->quick_rows[key];
5343
 
              }
5344
 
            }
5345
 
 
5346
 
            /* Limit the number of matched rows */
5347
 
            set_if_smaller(tmp, (double) thd->variables.max_seeks_for_key);
5348
 
            if (table->covering_keys.is_set(key))
5349
 
            {
5350
 
              /* we can use only index tree */
5351
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5352
 
            }
5353
 
            else
5354
 
              tmp= record_count * min(tmp,s->worst_seeks);
5355
 
          }
5356
 
          else
5357
 
            tmp= best_time;                    // Do nothing
5358
 
        }
5359
 
 
5360
 
        if (sj_inside_out_scan && !start_key)
5361
 
        {
5362
 
          tmp= tmp/2;
5363
 
          if (records)
5364
 
            records= records/2;
5365
 
        }
5366
 
 
5367
 
      }
5368
 
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5369
 
      {
5370
 
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
5371
 
        best= tmp;
5372
 
        best_records= records;
5373
 
        best_key= start_key;
5374
 
        best_max_key_part= max_key_part;
5375
 
        best_ref_depends_map= found_ref;
5376
 
        best_is_sj_inside_out= sj_inside_out_scan;
5377
 
      }
5378
 
    }
5379
 
    records= best_records;
5380
 
  }
5381
 
 
5382
 
  /*
5383
 
    Don't test table scan if it can't be better.
5384
 
    Prefer key lookup if we would use the same key for scanning.
5385
 
 
5386
 
    Don't do a table scan on InnoDB tables, if we can read the used
5387
 
    parts of the row from any of the used index.
5388
 
    This is because table scans uses index and we would not win
5389
 
    anything by using a table scan.
5390
 
 
5391
 
    A word for word translation of the below if-statement in sergefp's
5392
 
    understanding: we check if we should use table scan if:
5393
 
    (1) The found 'ref' access produces more records than a table scan
5394
 
        (or index scan, or quick select), or 'ref' is more expensive than
5395
 
        any of them.
5396
 
    (2) This doesn't hold: the best way to perform table scan is to to perform
5397
 
        'range' access using index IDX, and the best way to perform 'ref' 
5398
 
        access is to use the same index IDX, with the same or more key parts.
5399
 
        (note: it is not clear how this rule is/should be extended to 
5400
 
        index_merge quick selects)
5401
 
    (3) See above note about InnoDB.
5402
 
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5403
 
             path, but there is no quick select)
5404
 
        If the condition in the above brackets holds, then the only possible
5405
 
        "table scan" access method is ALL/index (there is no quick select).
5406
 
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
5407
 
        choose it over ALL/index, there is no need to consider a full table
5408
 
        scan.
5409
 
  */
5410
 
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
5411
 
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5412
 
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5413
 
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5414
 
        ! s->table->covering_keys.is_clear_all() && best_key && !s->quick) &&// (3)
5415
 
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5416
 
  {                                             // Check full join
5417
 
    ha_rows rnd_records= s->found_records;
5418
 
    /*
5419
 
      If there is a filtering condition on the table (i.e. ref analyzer found
5420
 
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5421
 
      preceding this table in the join order we're now considering), then 
5422
 
      assume that 25% of the rows will be filtered out by this condition.
5423
 
 
5424
 
      This heuristic is supposed to force tables used in exprZ to be before
5425
 
      this table in join order.
5426
 
    */
5427
 
    if (found_constraint)
5428
 
      rnd_records-= rnd_records/4;
5429
 
 
5430
 
    /*
5431
 
      If applicable, get a more accurate estimate. Don't use the two
5432
 
      heuristics at once.
5433
 
    */
5434
 
    if (s->table->quick_condition_rows != s->found_records)
5435
 
      rnd_records= s->table->quick_condition_rows;
5436
 
 
5437
 
    /*
5438
 
      Range optimizer never proposes a RANGE if it isn't better
5439
 
      than FULL: so if RANGE is present, it's always preferred to FULL.
5440
 
      Here we estimate its cost.
5441
 
    */
5442
 
    if (s->quick)
5443
 
    {
5444
 
      /*
5445
 
        For each record we:
5446
 
        - read record range through 'quick'
5447
 
        - skip rows which does not satisfy WHERE constraints
5448
 
        TODO: 
5449
 
        We take into account possible use of join cache for ALL/index
5450
 
        access (see first else-branch below), but we don't take it into 
5451
 
        account here for range/index_merge access. Find out why this is so.
5452
 
      */
5453
 
      tmp= record_count *
5454
 
        (s->quick->read_time +
5455
 
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5456
 
    }
5457
 
    else
5458
 
    {
5459
 
      /* Estimate cost of reading table. */
5460
 
      tmp= s->table->file->scan_time();
5461
 
      if (s->table->map & join->outer_join)     // Can't use join cache
5462
 
      {
5463
 
        /*
5464
 
          For each record we have to:
5465
 
          - read the whole table record 
5466
 
          - skip rows which does not satisfy join condition
5467
 
        */
5468
 
        tmp= record_count *
5469
 
          (tmp +
5470
 
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5471
 
      }
5472
 
      else
5473
 
      {
5474
 
        /* We read the table as many times as join buffer becomes full. */
5475
 
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5476
 
                           record_count /
5477
 
                           (double) thd->variables.join_buff_size));
5478
 
        /* 
5479
 
            We don't make full cartesian product between rows in the scanned
5480
 
           table and existing records because we skip all rows from the
5481
 
           scanned table, which does not satisfy join condition when 
5482
 
           we read the table (see flush_cached_records for details). Here we
5483
 
           take into account cost to read and skip these records.
5484
 
        */
5485
 
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5486
 
      }
5487
 
    }
5488
 
 
5489
 
    /*
5490
 
      We estimate the cost of evaluating WHERE clause for found records
5491
 
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5492
 
      tmp give us total cost of using TABLE SCAN
5493
 
    */
5494
 
    if (best == DBL_MAX ||
5495
 
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5496
 
         best + record_count/(double) TIME_FOR_COMPARE*records))
5497
 
    {
5498
 
      /*
5499
 
        If the table has a range (s->quick is set) make_join_select()
5500
 
        will ensure that this will be used
5501
 
      */
5502
 
      best= tmp;
5503
 
      records= rows2double(rnd_records);
5504
 
      best_key= 0;
5505
 
      /* range/index_merge/ALL/index access method are "independent", so: */
5506
 
      best_ref_depends_map= 0;
5507
 
      best_is_sj_inside_out= false;
5508
 
    }
5509
 
  }
5510
 
 
5511
 
  /* Update the cost information for the current partial plan */
5512
 
  join->positions[idx].records_read= records;
5513
 
  join->positions[idx].read_time=    best;
5514
 
  join->positions[idx].key=          best_key;
5515
 
  join->positions[idx].table=        s;
5516
 
  join->positions[idx].ref_depend_map= best_ref_depends_map;
5517
 
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5518
 
 
5519
 
  if (!best_key &&
5520
 
      idx == join->const_tables &&
5521
 
      s->table == join->sort_by_table &&
5522
 
      join->unit->select_limit_cnt >= records)
5523
 
    join->sort_by_table= (TABLE*) 1;  // Must use temporary table
5524
 
 
5525
 
  return;
5526
 
}
5527
 
 
5528
 
 
5529
 
/**
5530
 
  Selects and invokes a search strategy for an optimal query plan.
5531
 
 
5532
 
  The function checks user-configurable parameters that control the search
5533
 
  strategy for an optimal plan, selects the search method and then invokes
5534
 
  it. Each specific optimization procedure stores the final optimal plan in
5535
 
  the array 'join->best_positions', and the cost of the plan in
5536
 
  'join->best_read'.
5537
 
 
5538
 
  @param join         pointer to the structure providing all context info for
5539
 
                      the query
5540
 
  @param join_tables  set of the tables in the query
5541
 
 
5542
 
  @todo
5543
 
    'MAX_TABLES+2' denotes the old implementation of find_best before
5544
 
    the greedy version. Will be removed when greedy_search is approved.
5545
 
 
5546
 
  @retval
5547
 
    false       ok
5548
 
  @retval
5549
 
    true        Fatal error
5550
 
*/
5551
 
 
5552
 
static bool
5553
 
choose_plan(JOIN *join, table_map join_tables)
5554
 
{
5555
 
  uint search_depth= join->thd->variables.optimizer_search_depth;
5556
 
  uint prune_level=  join->thd->variables.optimizer_prune_level;
5557
 
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5558
 
 
5559
 
  join->cur_embedding_map= 0;
5560
 
  reset_nj_counters(join->join_list);
5561
 
  /*
5562
 
    if (SELECT_STRAIGHT_JOIN option is set)
5563
 
      reorder tables so dependent tables come after tables they depend 
5564
 
      on, otherwise keep tables in the order they were specified in the query 
5565
 
    else
5566
 
      Apply heuristic: pre-sort all access plans with respect to the number of
5567
 
      records accessed.
5568
 
  */
5569
 
  my_qsort(join->best_ref + join->const_tables,
5570
 
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
5571
 
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
5572
 
  join->cur_emb_sj_nests= 0;
5573
 
  if (straight_join)
5574
 
  {
5575
 
    optimize_straight_join(join, join_tables);
5576
 
  }
5577
 
  else
5578
 
  {
5579
 
    if (search_depth == MAX_TABLES+2)
5580
 
    { /*
5581
 
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5582
 
        the greedy version. Will be removed when greedy_search is approved.
5583
 
      */
5584
 
      join->best_read= DBL_MAX;
5585
 
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5586
 
        return(true);
5587
 
    } 
5588
 
    else
5589
 
    {
5590
 
      if (search_depth == 0)
5591
 
        /* Automatically determine a reasonable value for 'search_depth' */
5592
 
        search_depth= determine_search_depth(join);
5593
 
      if (greedy_search(join, join_tables, search_depth, prune_level))
5594
 
        return(true);
5595
 
    }
5596
 
  }
5597
 
 
5598
 
  /* 
5599
 
    Store the cost of this query into a user variable
5600
 
    Don't update last_query_cost for statements that are not "flat joins" :
5601
 
    i.e. they have subqueries, unions or call stored procedures.
5602
 
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
5603
 
  */
5604
 
  if (join->thd->lex->is_single_level_stmt())
5605
 
    join->thd->status_var.last_query_cost= join->best_read;
5606
 
  return(false);
5607
 
}
5608
 
 
5609
 
 
5610
 
/**
5611
 
  Compare two JOIN_TAB objects based on the number of accessed records.
5612
 
 
5613
 
  @param ptr1 pointer to first JOIN_TAB object
5614
 
  @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
5615
796
 
5616
797
  NOTES
5617
798
    The order relation implemented by join_tab_cmp() is not transitive,
5623
804
      a: dependent = 0x0 table->map = 0x1 found_records = 3 ptr = 0x907e6b0
5624
805
      b: dependent = 0x0 table->map = 0x2 found_records = 3 ptr = 0x907e838
5625
806
      c: dependent = 0x6 table->map = 0x10 found_records = 2 ptr = 0x907ecd0
5626
 
     
 
807
 
5627
808
  @retval
5628
809
    1  if first is bigger
5629
810
  @retval
5631
812
  @retval
5632
813
    0  if equal
5633
814
*/
5634
 
 
5635
 
static int
5636
 
join_tab_cmp(const void* ptr1, const void* ptr2)
 
815
int join_tab_cmp(const void* ptr1, const void* ptr2)
5637
816
{
5638
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5639
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
817
  JoinTable *jt1= *(JoinTable**) ptr1;
 
818
  JoinTable *jt2= *(JoinTable**) ptr2;
5640
819
 
5641
820
  if (jt1->dependent & jt2->table->map)
5642
821
    return 1;
5643
822
  if (jt2->dependent & jt1->table->map)
5644
 
    return -1;  
 
823
    return -1;
5645
824
  if (jt1->found_records > jt2->found_records)
5646
825
    return 1;
5647
826
  if (jt1->found_records < jt2->found_records)
5648
 
    return -1; 
 
827
    return -1;
5649
828
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5650
829
}
5651
830
 
5652
 
 
5653
831
/**
5654
832
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5655
833
*/
5656
 
 
5657
 
static int
5658
 
join_tab_cmp_straight(const void* ptr1, const void* ptr2)
 
834
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5659
835
{
5660
 
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5661
 
  JOIN_TAB *jt2= *(JOIN_TAB**) ptr2;
 
836
  JoinTable *jt1= *(JoinTable**) ptr1;
 
837
  JoinTable *jt2= *(JoinTable**) ptr2;
5662
838
 
5663
839
  if (jt1->dependent & jt2->table->map)
5664
840
    return 1;
5668
844
}
5669
845
 
5670
846
/**
5671
 
  Heuristic procedure to automatically guess a reasonable degree of
5672
 
  exhaustiveness for the greedy search procedure.
5673
 
 
5674
 
  The procedure estimates the optimization time and selects a search depth
5675
 
  big enough to result in a near-optimal QEP, that doesn't take too long to
5676
 
  find. If the number of tables in the query exceeds some constant, then
5677
 
  search_depth is set to this constant.
5678
 
 
5679
 
  @param join   pointer to the structure providing all context info for
5680
 
                the query
5681
 
 
5682
 
  @note
5683
 
    This is an extremely simplistic implementation that serves as a stub for a
5684
 
    more advanced analysis of the join. Ideally the search depth should be
5685
 
    determined by learning from previous query optimizations, because it will
5686
 
    depend on the CPU power (and other factors).
5687
 
 
5688
 
  @todo
5689
 
    this value should be determined dynamically, based on statistics:
5690
 
    uint max_tables_for_exhaustive_opt= 7;
5691
 
 
5692
 
  @todo
5693
 
    this value could be determined by some mapping of the form:
5694
 
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5695
 
 
5696
 
  @return
5697
 
    A positive integer that specifies the search depth (and thus the
5698
 
    exhaustiveness) of the depth-first search algorithm used by
5699
 
    'greedy_search'.
5700
 
*/
5701
 
 
5702
 
static uint
5703
 
determine_search_depth(JOIN *join)
5704
 
{
5705
 
  uint table_count=  join->tables - join->const_tables;
5706
 
  uint search_depth;
5707
 
  /* TODO: this value should be determined dynamically, based on statistics: */
5708
 
  uint max_tables_for_exhaustive_opt= 7;
5709
 
 
5710
 
  if (table_count <= max_tables_for_exhaustive_opt)
5711
 
    search_depth= table_count+1; // use exhaustive for small number of tables
5712
 
  else
5713
 
    /*
5714
 
      TODO: this value could be determined by some mapping of the form:
5715
 
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5716
 
    */
5717
 
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
5718
 
 
5719
 
  return search_depth;
5720
 
}
5721
 
 
5722
 
 
5723
 
/**
5724
 
  Select the best ways to access the tables in a query without reordering them.
5725
 
 
5726
 
    Find the best access paths for each query table and compute their costs
5727
 
    according to their order in the array 'join->best_ref' (thus without
5728
 
    reordering the join tables). The function calls sequentially
5729
 
    'best_access_path' for each table in the query to select the best table
5730
 
    access method. The final optimal plan is stored in the array
5731
 
    'join->best_positions', and the corresponding cost in 'join->best_read'.
5732
 
 
5733
 
  @param join          pointer to the structure providing all context info for
5734
 
                       the query
5735
 
  @param join_tables   set of the tables in the query
5736
 
 
5737
 
  @note
5738
 
    This function can be applied to:
5739
 
    - queries with STRAIGHT_JOIN
5740
 
    - internally to compute the cost of an arbitrary QEP
5741
 
  @par
5742
 
    Thus 'optimize_straight_join' can be used at any stage of the query
5743
 
    optimization process to finalize a QEP as it is.
5744
 
*/
5745
 
 
5746
 
static void
5747
 
optimize_straight_join(JOIN *join, table_map join_tables)
5748
 
{
5749
 
  JOIN_TAB *s;
5750
 
  uint idx= join->const_tables;
5751
 
  double    record_count= 1.0;
5752
 
  double    read_time=    0.0;
5753
 
 
5754
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5755
 
  {
5756
 
    /* Find the best access method from 's' to the current partial plan */
5757
 
    advance_sj_state(join_tables, s);
5758
 
    best_access_path(join, s, join->thd, join_tables, idx,
5759
 
                     record_count, read_time);
5760
 
    /* compute the cost of the new plan extended with 's' */
5761
 
    record_count*= join->positions[idx].records_read;
5762
 
    read_time+=    join->positions[idx].read_time;
5763
 
    join_tables&= ~(s->table->map);
5764
 
    ++idx;
5765
 
  }
5766
 
 
5767
 
  read_time+= record_count / (double) TIME_FOR_COMPARE;
5768
 
  if (join->sort_by_table &&
5769
 
      join->sort_by_table != join->positions[join->const_tables].table->table)
5770
 
    read_time+= record_count;  // We have to make a temp table
5771
 
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
5772
 
  join->best_read= read_time;
5773
 
}
5774
 
 
5775
 
 
5776
 
/**
5777
 
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5778
 
 
5779
 
    The search procedure uses a hybrid greedy/exhaustive search with controlled
5780
 
    exhaustiveness. The search is performed in N = card(remaining_tables)
5781
 
    steps. Each step evaluates how promising is each of the unoptimized tables,
5782
 
    selects the most promising table, and extends the current partial QEP with
5783
 
    that table.  Currenly the most 'promising' table is the one with least
5784
 
    expensive extension.\
5785
 
 
5786
 
    There are two extreme cases:
5787
 
    -# When (card(remaining_tables) < search_depth), the estimate finds the
5788
 
    best complete continuation of the partial QEP. This continuation can be
5789
 
    used directly as a result of the search.
5790
 
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
5791
 
    consideres the extension of the current QEP with each of the remaining
5792
 
    unoptimized tables.
5793
 
 
5794
 
    All other cases are in-between these two extremes. Thus the parameter
5795
 
    'search_depth' controlls the exhaustiveness of the search. The higher the
5796
 
    value, the longer the optimizaton time and possibly the better the
5797
 
    resulting plan. The lower the value, the fewer alternative plans are
5798
 
    estimated, but the more likely to get a bad QEP.
5799
 
 
5800
 
    All intermediate and final results of the procedure are stored in 'join':
5801
 
    - join->positions     : modified for every partial QEP that is explored
5802
 
    - join->best_positions: modified for the current best complete QEP
5803
 
    - join->best_read     : modified for the current best complete QEP
5804
 
    - join->best_ref      : might be partially reordered
5805
 
 
5806
 
    The final optimal plan is stored in 'join->best_positions', and its
5807
 
    corresponding cost in 'join->best_read'.
5808
 
 
5809
 
  @note
5810
 
    The following pseudocode describes the algorithm of 'greedy_search':
5811
 
 
5812
 
    @code
5813
 
    procedure greedy_search
5814
 
    input: remaining_tables
5815
 
    output: pplan;
5816
 
    {
5817
 
      pplan = <>;
5818
 
      do {
5819
 
        (t, a) = best_extension(pplan, remaining_tables);
5820
 
        pplan = concat(pplan, (t, a));
5821
 
        remaining_tables = remaining_tables - t;
5822
 
      } while (remaining_tables != {})
5823
 
      return pplan;
5824
 
    }
5825
 
 
5826
 
  @endcode
5827
 
    where 'best_extension' is a placeholder for a procedure that selects the
5828
 
    most "promising" of all tables in 'remaining_tables'.
5829
 
    Currently this estimate is performed by calling
5830
 
    'best_extension_by_limited_search' to evaluate all extensions of the
5831
 
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5832
 
    mainly depends on that of 'best_extension_by_limited_search'.
5833
 
 
5834
 
  @par
5835
 
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
5836
 
    worst-case complexity of this algorithm is <=
5837
 
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5838
 
    complexity of greedy_search is O(N!).
5839
 
 
5840
 
  @par
5841
 
    In the future, 'greedy_search' might be extended to support other
5842
 
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
5843
 
 
5844
 
  @param join             pointer to the structure providing all context info
5845
 
                          for the query
5846
 
  @param remaining_tables set of tables not included into the partial plan yet
5847
 
  @param search_depth     controlls the exhaustiveness of the search
5848
 
  @param prune_level      the pruning heuristics that should be applied during
5849
 
                          search
5850
 
 
5851
 
  @retval
5852
 
    false       ok
5853
 
  @retval
5854
 
    true        Fatal error
5855
 
*/
5856
 
 
5857
 
static bool
5858
 
greedy_search(JOIN      *join,
5859
 
              table_map remaining_tables,
5860
 
              uint      search_depth,
5861
 
              uint      prune_level)
5862
 
{
5863
 
  double    record_count= 1.0;
5864
 
  double    read_time=    0.0;
5865
 
  uint      idx= join->const_tables; // index into 'join->best_ref'
5866
 
  uint      best_idx;
5867
 
  uint      size_remain;    // cardinality of remaining_tables
5868
 
  POSITION  best_pos;
5869
 
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5870
 
 
5871
 
  /* number of tables that remain to be optimized */
5872
 
  size_remain= my_count_bits(remaining_tables);
5873
 
 
5874
 
  do {
5875
 
    /* Find the extension of the current QEP with the lowest cost */
5876
 
    join->best_read= DBL_MAX;
5877
 
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5878
 
                                         read_time, search_depth, prune_level))
5879
 
      return(true);
5880
 
 
5881
 
    if (size_remain <= search_depth)
5882
 
    {
5883
 
      /*
5884
 
        'join->best_positions' contains a complete optimal extension of the
5885
 
        current partial QEP.
5886
 
      */
5887
 
      return(false);
5888
 
    }
5889
 
 
5890
 
    /* select the first table in the optimal extension as most promising */
5891
 
    best_pos= join->best_positions[idx];
5892
 
    best_table= best_pos.table;
5893
 
    /*
5894
 
      Each subsequent loop of 'best_extension_by_limited_search' uses
5895
 
      'join->positions' for cost estimates, therefore we have to update its
5896
 
      value.
5897
 
    */
5898
 
    join->positions[idx]= best_pos;
5899
 
 
5900
 
    /* find the position of 'best_table' in 'join->best_ref' */
5901
 
    best_idx= idx;
5902
 
    JOIN_TAB *pos= join->best_ref[best_idx];
5903
 
    while (pos && best_table != pos)
5904
 
      pos= join->best_ref[++best_idx];
5905
 
    assert((pos != NULL)); // should always find 'best_table'
5906
 
    /* move 'best_table' at the first free position in the array of joins */
5907
 
    swap_variables(JOIN_TAB*, join->best_ref[idx], join->best_ref[best_idx]);
5908
 
 
5909
 
    /* compute the cost of the new plan extended with 'best_table' */
5910
 
    record_count*= join->positions[idx].records_read;
5911
 
    read_time+=    join->positions[idx].read_time;
5912
 
 
5913
 
    remaining_tables&= ~(best_table->table->map);
5914
 
    --size_remain;
5915
 
    ++idx;
5916
 
  } while (true);
5917
 
}
5918
 
 
5919
 
 
5920
 
/**
5921
 
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
5922
 
  exhaustive search.
5923
 
 
5924
 
    The procedure searches for the optimal ordering of the query tables in set
5925
 
    'remaining_tables' of size N, and the corresponding optimal access paths to
5926
 
    each table. The choice of a table order and an access path for each table
5927
 
    constitutes a query execution plan (QEP) that fully specifies how to
5928
 
    execute the query.
5929
 
   
5930
 
    The maximal size of the found plan is controlled by the parameter
5931
 
    'search_depth'. When search_depth == N, the resulting plan is complete and
5932
 
    can be used directly as a QEP. If search_depth < N, the found plan consists
5933
 
    of only some of the query tables. Such "partial" optimal plans are useful
5934
 
    only as input to query optimization procedures, and cannot be used directly
5935
 
    to execute a query.
5936
 
 
5937
 
    The algorithm begins with an empty partial plan stored in 'join->positions'
5938
 
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
5939
 
    evaluates the cost of the partial plan extended by all access plans for
5940
 
    each of the relations in 'remaining_tables', expands the current partial
5941
 
    plan with the access plan that results in lowest cost of the expanded
5942
 
    partial plan, and removes the corresponding relation from
5943
 
    'remaining_tables'. The algorithm continues until it either constructs a
5944
 
    complete optimal plan, or constructs an optimal plartial plan with size =
5945
 
    search_depth.
5946
 
 
5947
 
    The final optimal plan is stored in 'join->best_positions'. The
5948
 
    corresponding cost of the optimal plan is in 'join->best_read'.
5949
 
 
5950
 
  @note
5951
 
    The procedure uses a recursive depth-first search where the depth of the
5952
 
    recursion (and thus the exhaustiveness of the search) is controlled by the
5953
 
    parameter 'search_depth'.
5954
 
 
5955
 
  @note
5956
 
    The pseudocode below describes the algorithm of
5957
 
    'best_extension_by_limited_search'. The worst-case complexity of this
5958
 
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5959
 
    the complexity of greedy_search is O(N!).
5960
 
 
5961
 
    @code
5962
 
    procedure best_extension_by_limited_search(
5963
 
      pplan in,             // in, partial plan of tables-joined-so-far
5964
 
      pplan_cost,           // in, cost of pplan
5965
 
      remaining_tables,     // in, set of tables not referenced in pplan
5966
 
      best_plan_so_far,     // in/out, best plan found so far
5967
 
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5968
 
      search_depth)         // in, maximum size of the plans being considered
5969
 
    {
5970
 
      for each table T from remaining_tables
5971
 
      {
5972
 
        // Calculate the cost of using table T as above
5973
 
        cost = complex-series-of-calculations;
5974
 
 
5975
 
        // Add the cost to the cost so far.
5976
 
        pplan_cost+= cost;
5977
 
 
5978
 
        if (pplan_cost >= best_plan_so_far_cost)
5979
 
          // pplan_cost already too great, stop search
5980
 
          continue;
5981
 
 
5982
 
        pplan= expand pplan by best_access_method;
5983
 
        remaining_tables= remaining_tables - table T;
5984
 
        if (remaining_tables is not an empty set
5985
 
            and
5986
 
            search_depth > 1)
5987
 
        {
5988
 
          best_extension_by_limited_search(pplan, pplan_cost,
5989
 
                                           remaining_tables,
5990
 
                                           best_plan_so_far,
5991
 
                                           best_plan_so_far_cost,
5992
 
                                           search_depth - 1);
5993
 
        }
5994
 
        else
5995
 
        {
5996
 
          best_plan_so_far_cost= pplan_cost;
5997
 
          best_plan_so_far= pplan;
5998
 
        }
5999
 
      }
6000
 
    }
6001
 
    @endcode
6002
 
 
6003
 
  @note
6004
 
    When 'best_extension_by_limited_search' is called for the first time,
6005
 
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
6006
 
    The actual implementation provides a way to optionally use pruning
6007
 
    heuristic (controlled by the parameter 'prune_level') to reduce the search
6008
 
    space by skipping some partial plans.
6009
 
 
6010
 
  @note
6011
 
    The parameter 'search_depth' provides control over the recursion
6012
 
    depth, and thus the size of the resulting optimal plan.
6013
 
 
6014
 
  @param join             pointer to the structure providing all context info
6015
 
                          for the query
6016
 
  @param remaining_tables set of tables not included into the partial plan yet
6017
 
  @param idx              length of the partial QEP in 'join->positions';
6018
 
                          since a depth-first search is used, also corresponds
6019
 
                          to the current depth of the search tree;
6020
 
                          also an index in the array 'join->best_ref';
6021
 
  @param record_count     estimate for the number of records returned by the
6022
 
                          best partial plan
6023
 
  @param read_time        the cost of the best partial plan
6024
 
  @param search_depth     maximum depth of the recursion and thus size of the
6025
 
                          found optimal plan
6026
 
                          (0 < search_depth <= join->tables+1).
6027
 
  @param prune_level      pruning heuristics that should be applied during
6028
 
                          optimization
6029
 
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6030
 
 
6031
 
  @retval
6032
 
    false       ok
6033
 
  @retval
6034
 
    true        Fatal error
6035
 
*/
6036
 
 
6037
 
static bool
6038
 
best_extension_by_limited_search(JOIN      *join,
6039
 
                                 table_map remaining_tables,
6040
 
                                 uint      idx,
6041
 
                                 double    record_count,
6042
 
                                 double    read_time,
6043
 
                                 uint      search_depth,
6044
 
                                 uint      prune_level)
6045
 
{
6046
 
  THD *thd= join->thd;
6047
 
  if (thd->killed)  // Abort
6048
 
    return(true);
6049
 
 
6050
 
  /* 
6051
 
     'join' is a partial plan with lower cost than the best plan so far,
6052
 
     so continue expanding it further with the tables in 'remaining_tables'.
6053
 
  */
6054
 
  JOIN_TAB *s;
6055
 
  double best_record_count= DBL_MAX;
6056
 
  double best_read_time=    DBL_MAX;
6057
 
 
6058
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6059
 
  {
6060
 
    table_map real_table_bit= s->table->map;
6061
 
    if ((remaining_tables & real_table_bit) && 
6062
 
        !(remaining_tables & s->dependent) && 
6063
 
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6064
 
    {
6065
 
      double current_record_count, current_read_time;
6066
 
      advance_sj_state(remaining_tables, s);
6067
 
 
6068
 
      /*
6069
 
        psergey-insideout-todo: 
6070
 
          when best_access_path() detects it could do an InsideOut scan or 
6071
 
          some other scan, have it return an insideout scan and a flag that 
6072
 
          requests to "fork" this loop iteration. (Q: how does that behave 
6073
 
          when the depth is insufficient??)
6074
 
      */
6075
 
      /* Find the best access method from 's' to the current partial plan */
6076
 
      best_access_path(join, s, thd, remaining_tables, idx,
6077
 
                       record_count, read_time);
6078
 
      /* Compute the cost of extending the plan with 's' */
6079
 
      current_record_count= record_count * join->positions[idx].records_read;
6080
 
      current_read_time=    read_time + join->positions[idx].read_time;
6081
 
 
6082
 
      /* Expand only partial plans with lower cost than the best QEP so far */
6083
 
      if ((current_read_time +
6084
 
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6085
 
      {
6086
 
        restore_prev_nj_state(s);
6087
 
        restore_prev_sj_state(remaining_tables, s);
6088
 
        continue;
6089
 
      }
6090
 
 
6091
 
      /*
6092
 
        Prune some less promising partial plans. This heuristic may miss
6093
 
        the optimal QEPs, thus it results in a non-exhaustive search.
6094
 
      */
6095
 
      if (prune_level == 1)
6096
 
      {
6097
 
        if (best_record_count > current_record_count ||
6098
 
            best_read_time > current_read_time ||
6099
 
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6100
 
        {
6101
 
          if (best_record_count >= current_record_count &&
6102
 
              best_read_time >= current_read_time &&
6103
 
              /* TODO: What is the reasoning behind this condition? */
6104
 
              (!(s->key_dependent & remaining_tables) ||
6105
 
               join->positions[idx].records_read < 2.0))
6106
 
          {
6107
 
            best_record_count= current_record_count;
6108
 
            best_read_time=    current_read_time;
6109
 
          }
6110
 
        }
6111
 
        else
6112
 
        {
6113
 
          restore_prev_nj_state(s);
6114
 
          restore_prev_sj_state(remaining_tables, s);
6115
 
          continue;
6116
 
        }
6117
 
      }
6118
 
 
6119
 
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6120
 
      { /* Recursively expand the current partial plan */
6121
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6122
 
        if (best_extension_by_limited_search(join,
6123
 
                                             remaining_tables & ~real_table_bit,
6124
 
                                             idx + 1,
6125
 
                                             current_record_count,
6126
 
                                             current_read_time,
6127
 
                                             search_depth - 1,
6128
 
                                             prune_level))
6129
 
          return(true);
6130
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6131
 
      }
6132
 
      else
6133
 
      { /*
6134
 
          'join' is either the best partial QEP with 'search_depth' relations,
6135
 
          or the best complete QEP so far, whichever is smaller.
6136
 
        */
6137
 
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6138
 
        if (join->sort_by_table &&
6139
 
            join->sort_by_table !=
6140
 
            join->positions[join->const_tables].table->table)
6141
 
          /* We have to make a temp table */
6142
 
          current_read_time+= current_record_count;
6143
 
        if ((search_depth == 1) || (current_read_time < join->best_read))
6144
 
        {
6145
 
          memcpy(join->best_positions, join->positions,
6146
 
                 sizeof(POSITION) * (idx + 1));
6147
 
          join->best_read= current_read_time - 0.001;
6148
 
        }
6149
 
      }
6150
 
      restore_prev_nj_state(s);
6151
 
      restore_prev_sj_state(remaining_tables, s);
6152
 
    }
6153
 
  }
6154
 
  return(false);
6155
 
}
6156
 
 
6157
 
 
6158
 
/**
6159
 
  @todo
6160
 
  - TODO: this function is here only temporarily until 'greedy_search' is
6161
 
  tested and accepted.
6162
 
 
6163
 
  RETURN VALUES
6164
 
    false       ok
6165
 
    true        Fatal error
6166
 
*/
6167
 
static bool
6168
 
find_best(JOIN *join,table_map rest_tables,uint idx,double record_count,
6169
 
          double read_time)
6170
 
{
6171
 
  THD *thd= join->thd;
6172
 
  if (thd->killed)
6173
 
    return(true);
6174
 
  if (!rest_tables)
6175
 
  {
6176
 
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6177
 
    if (join->sort_by_table &&
6178
 
        join->sort_by_table !=
6179
 
        join->positions[join->const_tables].table->table)
6180
 
      read_time+=record_count;                  // We have to make a temp table
6181
 
    if (read_time < join->best_read)
6182
 
    {
6183
 
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
6184
 
      join->best_read= read_time - 0.001;
6185
 
    }
6186
 
    return(false);
6187
 
  }
6188
 
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6189
 
    return(false);                                      /* Found better before */
6190
 
 
6191
 
  JOIN_TAB *s;
6192
 
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6193
 
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6194
 
  {
6195
 
    table_map real_table_bit=s->table->map;
6196
 
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6197
 
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6198
 
    {
6199
 
      double records, best;
6200
 
      advance_sj_state(rest_tables, s);
6201
 
      best_access_path(join, s, thd, rest_tables, idx, record_count, 
6202
 
                       read_time);
6203
 
      records= join->positions[idx].records_read;
6204
 
      best= join->positions[idx].read_time;
6205
 
      /*
6206
 
        Go to the next level only if there hasn't been a better key on
6207
 
        this level! This will cut down the search for a lot simple cases!
6208
 
      */
6209
 
      double current_record_count=record_count*records;
6210
 
      double current_read_time=read_time+best;
6211
 
      if (best_record_count > current_record_count ||
6212
 
          best_read_time > current_read_time ||
6213
 
          (idx == join->const_tables && s->table == join->sort_by_table))
6214
 
      {
6215
 
        if (best_record_count >= current_record_count &&
6216
 
            best_read_time >= current_read_time &&
6217
 
            (!(s->key_dependent & rest_tables) || records < 2.0))
6218
 
        {
6219
 
          best_record_count=current_record_count;
6220
 
          best_read_time=current_read_time;
6221
 
        }
6222
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6223
 
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6224
 
                      current_record_count,current_read_time))
6225
 
          return(true);
6226
 
        swap_variables(JOIN_TAB*, join->best_ref[idx], *pos);
6227
 
      }
6228
 
      restore_prev_nj_state(s);
6229
 
      restore_prev_sj_state(rest_tables, s);
6230
 
      if (join->select_options & SELECT_STRAIGHT_JOIN)
6231
 
        break;                          // Don't test all combinations
6232
 
    }
6233
 
  }
6234
 
  return(false);
6235
 
}
6236
 
 
6237
 
 
6238
 
/**
6239
847
  Find how much space the prevous read not const tables takes in cache.
6240
848
*/
6241
 
 
6242
 
static void calc_used_field_length(THD *thd __attribute__((unused)),
6243
 
                                   JOIN_TAB *join_tab)
 
849
void calc_used_field_length(Session *, JoinTable *join_tab)
6244
850
{
6245
 
  uint null_fields,blobs,fields,rec_length;
 
851
  uint32_t null_fields,blobs,fields,rec_length;
6246
852
  Field **f_ptr,*field;
6247
 
  MY_BITMAP *read_set= join_tab->table->read_set;;
6248
853
 
6249
854
  null_fields= blobs= fields= rec_length=0;
6250
855
  for (f_ptr=join_tab->table->field ; (field= *f_ptr) ; f_ptr++)
6251
856
  {
6252
 
    if (bitmap_is_set(read_set, field->field_index))
 
857
    if (field->isReadSet())
6253
858
    {
6254
 
      uint flags=field->flags;
 
859
      uint32_t flags=field->flags;
6255
860
      fields++;
6256
861
      rec_length+=field->pack_length();
6257
862
      if (flags & BLOB_FLAG)
6258
 
        blobs++;
 
863
        blobs++;
6259
864
      if (!(flags & NOT_NULL_FLAG))
6260
 
        null_fields++;
 
865
        null_fields++;
6261
866
    }
6262
867
  }
6263
868
  if (null_fields)
6264
 
    rec_length+=(join_tab->table->s->null_fields+7)/8;
 
869
    rec_length+=(join_tab->table->getNullFields() + 7)/8;
6265
870
  if (join_tab->table->maybe_null)
6266
871
    rec_length+=sizeof(bool);
6267
872
  if (blobs)
6268
873
  {
6269
 
    uint blob_length=(uint) (join_tab->table->file->stats.mean_rec_length-
6270
 
                             (join_tab->table->s->reclength- rec_length));
6271
 
    rec_length+=(uint) max((uint)4,blob_length);
6272
 
  }
6273
 
  join_tab->used_fields=fields;
6274
 
  join_tab->used_fieldlength=rec_length;
6275
 
  join_tab->used_blobs=blobs;
6276
 
}
6277
 
 
6278
 
 
6279
 
static uint
6280
 
cache_record_length(JOIN *join,uint idx)
6281
 
{
6282
 
  uint length=0;
6283
 
  JOIN_TAB **pos,**end;
6284
 
  THD *thd=join->thd;
6285
 
 
6286
 
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6287
 
       pos != end ;
6288
 
       pos++)
6289
 
  {
6290
 
    JOIN_TAB *join_tab= *pos;
6291
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
6292
 
      calc_used_field_length(thd, join_tab);
6293
 
    length+=join_tab->used_fieldlength;
6294
 
  }
6295
 
  return length;
6296
 
}
6297
 
 
6298
 
 
6299
 
/*
6300
 
  Get the number of different row combinations for subset of partial join
6301
 
 
6302
 
  SYNOPSIS
6303
 
    prev_record_reads()
6304
 
      join       The join structure
6305
 
      idx        Number of tables in the partial join order (i.e. the
6306
 
                 partial join order is in join->positions[0..idx-1])
6307
 
      found_ref  Bitmap of tables for which we need to find # of distinct
6308
 
                 row combinations.
6309
 
 
6310
 
  DESCRIPTION
6311
 
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
6312
 
    tables within that join order (specified in found_ref), find out how many
6313
 
    distinct row combinations of subset tables will be in the result of the
6314
 
    partial join order.
6315
 
     
6316
 
    This is used as follows: Suppose we have a table accessed with a ref-based
6317
 
    method. The ref access depends on current rows of tables in found_ref.
6318
 
    We want to count # of different ref accesses. We assume two ref accesses
6319
 
    will be different if at least one of access parameters is different.
6320
 
    Example: consider a query
6321
 
 
6322
 
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6323
 
 
6324
 
    and a join order:
6325
 
      t1,  ref access on t1.key=c1
6326
 
      t2,  ref access on t2.key=c2       
6327
 
      t3,  ref access on t3.key=t1.field 
6328
 
    
6329
 
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6330
 
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6331
 
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
6332
 
            n_distinct_ref_scans = #records_read(t1)
6333
 
    
6334
 
    The reason for having this function (at least the latest version of it)
6335
 
    is that we need to account for buffering in join execution. 
6336
 
    
6337
 
    An edge-case example: if we have a non-first table in join accessed via
6338
 
    ref(const) or ref(param) where there is a small number of different
6339
 
    values of param, then the access will likely hit the disk cache and will
6340
 
    not require any disk seeks.
6341
 
    
6342
 
    The proper solution would be to assume an LRU disk cache of some size,
6343
 
    calculate probability of cache hits, etc. For now we just count
6344
 
    identical ref accesses as one.
6345
 
 
6346
 
  RETURN 
6347
 
    Expected number of row combinations
6348
 
*/
6349
 
 
6350
 
static double
6351
 
prev_record_reads(JOIN *join, uint idx, table_map found_ref)
6352
 
{
6353
 
  double found=1.0;
6354
 
  POSITION *pos_end= join->positions - 1;
6355
 
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6356
 
  {
6357
 
    if (pos->table->table->map & found_ref)
6358
 
    {
6359
 
      found_ref|= pos->ref_depend_map;
6360
 
      /* 
6361
 
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table 
6362
 
        with no matching row we will get position[t2].records_read==0. 
6363
 
        Actually the size of output is one null-complemented row, therefore 
6364
 
        we will use value of 1 whenever we get records_read==0.
6365
 
 
6366
 
        Note
6367
 
        - the above case can't occur if inner part of outer join has more 
6368
 
          than one table: table with no matches will not be marked as const.
6369
 
 
6370
 
        - Ideally we should add 1 to records_read for every possible null-
6371
 
          complemented row. We're not doing it because: 1. it will require
6372
 
          non-trivial code and add overhead. 2. The value of records_read
6373
 
          is an inprecise estimate and adding 1 (or, in the worst case,
6374
 
          #max_nested_outer_joins=64-1) will not make it any more precise.
6375
 
      */
6376
 
      if (pos->records_read > DBL_EPSILON)
6377
 
        found*= pos->records_read;
6378
 
    }
6379
 
  }
6380
 
  return found;
6381
 
}
6382
 
 
 
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
}
6383
921
 
6384
922
/**
6385
 
  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.
6386
927
*/
6387
 
 
6388
 
static bool
6389
 
get_best_combination(JOIN *join)
6390
 
{
6391
 
  uint i,tablenr;
6392
 
  table_map used_tables;
6393
 
  JOIN_TAB *join_tab,*j;
6394
 
  KEYUSE *keyuse;
6395
 
  uint table_count;
6396
 
  THD *thd=join->thd;
6397
 
 
6398
 
  table_count=join->tables;
6399
 
  if (!(join->join_tab=join_tab=
6400
 
        (JOIN_TAB*) thd->alloc(sizeof(JOIN_TAB)*table_count)))
6401
 
    return(true);
6402
 
 
6403
 
  join->full_join=0;
6404
 
 
6405
 
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6406
 
  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)
6407
950
  {
6408
 
    TABLE *form;
6409
 
    *j= *join->best_positions[tablenr].table;
6410
 
    form=join->table[tablenr]=j->table;
6411
 
    used_tables|= form->map;
6412
 
    form->reginfo.join_tab=j;
6413
 
    if (!*j->on_expr_ref)
6414
 
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
6415
 
    if (j->type == JT_CONST)
6416
 
      continue;                                 // Handled in make_join_stat..
6417
 
 
6418
 
    j->ref.key = -1;
6419
 
    j->ref.key_parts=0;
6420
 
 
6421
 
    if (j->type == JT_SYSTEM)
6422
 
      continue;
6423
 
    if (j->keys.is_clear_all() || !(keyuse= join->best_positions[tablenr].key))
 
951
    Item *res;
 
952
    if ((res= new Item_cond_and(*e1, e2)))
6424
953
    {
6425
 
      j->type=JT_ALL;
6426
 
      if (tablenr != join->const_tables)
6427
 
        join->full_join=1;
 
954
      *e1= res;
 
955
      res->quick_fix_field();
6428
956
    }
6429
 
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6430
 
      return(true);                        // Something went wrong
6431
957
  }
6432
 
 
6433
 
  for (i=0 ; i < table_count ; i++)
6434
 
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6435
 
  update_depend_map(join);
6436
 
  return(0);
 
958
  else
 
959
    *e1= e2;
6437
960
}
6438
961
 
6439
 
 
6440
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6441
 
                               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)
6442
966
{
6443
 
  KEYUSE *keyuse=org_keyuse;
6444
 
  THD  *thd= join->thd;
6445
 
  uint keyparts,length,key;
6446
 
  TABLE *table;
6447
 
  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;
6448
974
 
6449
975
  /*  Use best key from find_best */
6450
 
  table=j->table;
6451
 
  key=keyuse->key;
6452
 
  keyinfo=table->key_info+key;
 
976
  table= j->table;
 
977
  key= keyuse->getKey();
 
978
  keyinfo= table->key_info + key;
6453
979
 
6454
980
  {
6455
 
    keyparts=length=0;
6456
 
    uint found_part_ref_or_null= 0;
 
981
    keyparts= length= 0;
 
982
    uint32_t found_part_ref_or_null= 0;
6457
983
    /*
6458
984
      Calculate length for the used key
6459
985
      Stop if there is a missing key part or when we find second key_part
6461
987
    */
6462
988
    do
6463
989
    {
6464
 
      if (!(~used_tables & keyuse->used_tables))
 
990
      if (! (~used_tables & keyuse->getUsedTables()))
6465
991
      {
6466
 
        if (keyparts == keyuse->keypart &&
6467
 
            !(found_part_ref_or_null & keyuse->optimize))
6468
 
        {
6469
 
          keyparts++;
6470
 
          length+= keyinfo->key_part[keyuse->keypart].store_length;
6471
 
          found_part_ref_or_null|= keyuse->optimize;
6472
 
        }
 
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
        }
6473
999
      }
6474
1000
      keyuse++;
6475
 
    } while (keyuse->table == table && keyuse->key == key);
 
1001
    } while (keyuse->getTable() == table && keyuse->getKey() == key);
6476
1002
  }
6477
1003
 
6478
1004
  /* set up fieldref */
6480
1006
  j->ref.key_parts=keyparts;
6481
1007
  j->ref.key_length=length;
6482
1008
  j->ref.key=(int) key;
6483
 
  if (!(j->ref.key_buff= (uchar*) thd->calloc(ALIGN_SIZE(length)*2)) ||
6484
 
      !(j->ref.key_copy= (store_key**) thd->alloc((sizeof(store_key*) *
6485
 
                                                   (keyparts+1)))) ||
6486
 
      !(j->ref.items=    (Item**) thd->alloc(sizeof(Item*)*keyparts)) ||
6487
 
      !(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)))
6488
1014
  {
6489
1015
    return(true);
6490
1016
  }
6494
1020
  j->ref.disable_cache= false;
6495
1021
  keyuse=org_keyuse;
6496
1022
 
6497
 
  store_key **ref_key= j->ref.key_copy;
6498
 
  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;
6499
1025
  bool keyuse_uses_no_tables= true;
6500
1026
  {
6501
 
    uint i;
6502
 
    for (i=0 ; i < keyparts ; keyuse++,i++)
 
1027
    for (uint32_t i= 0; i < keyparts; keyuse++, i++)
6503
1028
    {
6504
 
      while (keyuse->keypart != i ||
6505
 
             ((~used_tables) & keyuse->used_tables))
6506
 
        keyuse++;                               /* Skip other parts */
 
1029
      while (keyuse->getKeypart() != i ||
 
1030
             ((~used_tables) & keyuse->getUsedTables()))
 
1031
        keyuse++;       /* Skip other parts */
6507
1032
 
6508
 
      uint maybe_null= test(keyinfo->key_part[i].null_bit);
6509
 
      j->ref.items[i]=keyuse->val;              // Save for cond removal
6510
 
      j->ref.cond_guards[i]= keyuse->cond_guard;
6511
 
      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())
6512
1037
        j->ref.null_rejecting |= 1 << i;
6513
 
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6514
 
      if (!keyuse->used_tables &&
6515
 
          !(join->select_options & SELECT_DESCRIBE))
6516
 
      {                                 // Compare against constant
6517
 
        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,
6518
1042
                           key_buff + maybe_null,
6519
1043
                           maybe_null ?  key_buff : 0,
6520
 
                           keyinfo->key_part[i].length, keyuse->val);
6521
 
        if (thd->is_fatal_error)
6522
 
          return(true);
6523
 
        tmp.copy();
 
1044
                           keyinfo->key_part[i].length, keyuse->getVal());
 
1045
        if (session->is_fatal_error)
 
1046
          return(true);
 
1047
        tmp.copy();
6524
1048
      }
6525
1049
      else
6526
 
        *ref_key++= get_store_key(thd,
6527
 
                                  keyuse,join->const_table_map,
6528
 
                                  &keyinfo->key_part[i],
6529
 
                                  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);
6530
1054
      /*
6531
 
        Remember if we are going to use REF_OR_NULL
6532
 
        But only if field _really_ can be null i.e. we force JT_REF
6533
 
        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
6534
1058
      */
6535
 
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6536
 
        null_ref_key= key_buff;
 
1059
      if ((keyuse->getOptimizeFlags() & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
 
1060
        null_ref_key= key_buff;
6537
1061
      key_buff+=keyinfo->key_part[i].store_length;
6538
1062
    }
6539
1063
  }
6540
 
  *ref_key=0;                           // end_marker
6541
 
  if (j->type == JT_CONST)
 
1064
  *ref_key= 0;       // end_marker
 
1065
  if (j->type == AM_CONST)
6542
1066
    j->table->const_table= 1;
6543
1067
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
6544
1068
           keyparts != keyinfo->key_parts || null_ref_key)
6545
1069
  {
6546
1070
    /* Must read with repeat */
6547
 
    j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF;
 
1071
    j->type= null_ref_key ? AM_REF_OR_NULL : AM_REF;
6548
1072
    j->ref.null_ref_key= null_ref_key;
6549
1073
  }
6550
1074
  else if (keyuse_uses_no_tables)
6556
1080
      Here we should not mark the table as a 'const' as a field may
6557
1081
      have a 'normal' value or a NULL value.
6558
1082
    */
6559
 
    j->type=JT_CONST;
6560
 
  }
6561
 
  else
6562
 
    j->type=JT_EQ_REF;
6563
 
  return(0);
6564
 
}
6565
 
 
6566
 
 
6567
 
 
6568
 
static store_key *
6569
 
get_store_key(THD *thd, KEYUSE *keyuse, table_map used_tables,
6570
 
              KEY_PART_INFO *key_part, uchar *key_buff, uint maybe_null)
6571
 
{
6572
 
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6573
 
  {
6574
 
    return new store_key_const_item(thd,
6575
 
                                    key_part->field,
6576
 
                                    key_buff + maybe_null,
6577
 
                                    maybe_null ? key_buff : 0,
6578
 
                                    key_part->length,
6579
 
                                    keyuse->val);
6580
 
  }
6581
 
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
6582
 
           (keyuse->val->type() == Item::REF_ITEM &&
6583
 
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6584
 
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6585
 
             Item_ref::DIRECT_REF && 
6586
 
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6587
 
    return new store_key_field(thd,
6588
 
                               key_part->field,
6589
 
                               key_buff + maybe_null,
6590
 
                               maybe_null ? key_buff : 0,
6591
 
                               key_part->length,
6592
 
                               ((Item_field*) keyuse->val->real_item())->field,
6593
 
                               keyuse->val->full_name());
6594
 
  return new store_key_item(thd,
6595
 
                            key_part->field,
6596
 
                            key_buff + maybe_null,
6597
 
                            maybe_null ? key_buff : 0,
6598
 
                            key_part->length,
6599
 
                            keyuse->val);
6600
 
}
6601
 
 
6602
 
/**
6603
 
  This function is only called for const items on fields which are keys.
6604
 
 
6605
 
  @return
6606
 
    returns 1 if there was some conversion made when the field was stored.
6607
 
*/
6608
 
 
6609
 
bool
6610
 
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6611
 
{
6612
 
  bool error;
6613
 
  TABLE *table= field->table;
6614
 
  THD *thd= table->in_use;
6615
 
  ha_rows cuted_fields=thd->cuted_fields;
6616
 
  my_bitmap_map *old_map= dbug_tmp_use_all_columns(table,
6617
 
                                                   table->write_set);
6618
 
 
6619
 
  /*
6620
 
    we should restore old value of count_cuted_fields because
6621
 
    store_val_in_field can be called from mysql_insert 
6622
 
    with select_insert, which make count_cuted_fields= 1
6623
 
   */
6624
 
  enum_check_fields old_count_cuted_fields= thd->count_cuted_fields;
6625
 
  thd->count_cuted_fields= check_flag;
6626
 
  error= item->save_in_field(field, 1);
6627
 
  thd->count_cuted_fields= old_count_cuted_fields;
6628
 
  dbug_tmp_restore_column_map(table->write_set, old_map);
6629
 
  return error || cuted_fields != thd->cuted_fields;
6630
 
}
6631
 
 
6632
 
 
6633
 
static bool
6634
 
make_simple_join(JOIN *join,TABLE *tmp_table)
6635
 
{
6636
 
  TABLE **tableptr;
6637
 
  JOIN_TAB *join_tab;
6638
 
 
6639
 
  /*
6640
 
    Reuse TABLE * and JOIN_TAB if already allocated by a previous call
6641
 
    to this function through JOIN::exec (may happen for sub-queries).
6642
 
  */
6643
 
  if (!join->table_reexec)
6644
 
  {
6645
 
    if (!(join->table_reexec= (TABLE**) join->thd->alloc(sizeof(TABLE*))))
6646
 
      return(true);                        /* purecov: inspected */
6647
 
    if (join->tmp_join)
6648
 
      join->tmp_join->table_reexec= join->table_reexec;
6649
 
  }
6650
 
  if (!join->join_tab_reexec)
6651
 
  {
6652
 
    if (!(join->join_tab_reexec=
6653
 
          (JOIN_TAB*) join->thd->alloc(sizeof(JOIN_TAB))))
6654
 
      return(true);                        /* purecov: inspected */
6655
 
    if (join->tmp_join)
6656
 
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6657
 
  }
6658
 
  tableptr= join->table_reexec;
6659
 
  join_tab= join->join_tab_reexec;
6660
 
 
6661
 
  join->join_tab=join_tab;
6662
 
  join->table=tableptr; tableptr[0]=tmp_table;
6663
 
  join->tables=1;
6664
 
  join->const_tables=0;
6665
 
  join->const_table_map=0;
6666
 
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6667
 
    join->tmp_table_param.func_count=0;
6668
 
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6669
 
  join->first_record=join->sort_and_group=0;
6670
 
  join->send_records=(ha_rows) 0;
6671
 
  join->group=0;
6672
 
  join->row_limit=join->unit->select_limit_cnt;
6673
 
  join->do_send_rows = (join->row_limit) ? 1 : 0;
6674
 
 
6675
 
  join_tab->cache.buff=0;                       /* No caching */
6676
 
  join_tab->table=tmp_table;
6677
 
  join_tab->select=0;
6678
 
  join_tab->select_cond=0;
6679
 
  join_tab->quick=0;
6680
 
  join_tab->type= JT_ALL;                       /* Map through all records */
6681
 
  join_tab->keys.init();
6682
 
  join_tab->keys.set_all();                     /* test everything in quick */
6683
 
  join_tab->info=0;
6684
 
  join_tab->on_expr_ref=0;
6685
 
  join_tab->last_inner= 0;
6686
 
  join_tab->first_unmatched= 0;
6687
 
  join_tab->ref.key = -1;
6688
 
  join_tab->not_used_in_distinct=0;
6689
 
  join_tab->read_first_record= join_init_read_record;
6690
 
  join_tab->join=join;
6691
 
  join_tab->ref.key_parts= 0;
6692
 
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6693
 
  join_tab->do_firstmatch= NULL;
6694
 
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
6695
 
  tmp_table->status=0;
6696
 
  tmp_table->null_row=0;
6697
 
  return(false);
6698
 
}
6699
 
 
6700
 
 
6701
 
inline void add_cond_and_fix(Item **e1, Item *e2)
6702
 
{
6703
 
  if (*e1)
6704
 
  {
6705
 
    Item *res;
6706
 
    if ((res= new Item_cond_and(*e1, e2)))
6707
 
    {
6708
 
      *e1= res;
6709
 
      res->quick_fix_field();
6710
 
    }
6711
 
  }
6712
 
  else
6713
 
    *e1= e2;
6714
 
}
6715
 
 
 
1083
    j->type= AM_CONST;
 
1084
  }
 
1085
  else
 
1086
    j->type= AM_EQ_REF;
 
1087
  return 0;
 
1088
}
6716
1089
 
6717
1090
/**
6718
1091
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6727
1100
    add "t1.field IS NOT NULL" to t1's table condition. @n
6728
1101
 
6729
1102
    Description of the optimization:
6730
 
    
 
1103
 
6731
1104
      We look through equalities choosen to perform ref/eq_ref access,
6732
1105
      pick equalities that have form "tbl.part_of_key = othertbl.field"
6733
1106
      (where othertbl is a non-const table and othertbl.field may be NULL)
6755
1128
      This optimization doesn't affect the choices that ref, range, or join
6756
1129
      optimizer make. This was intentional because this was added after 4.1
6757
1130
      was GA.
6758
 
      
 
1131
 
6759
1132
    Implementation overview
6760
1133
      1. update_ref_and_keys() accumulates info about null-rejecting
6761
 
         predicates in in KEY_FIELD::null_rejecting
6762
 
      1.1 add_key_part saves these to KEYUSE.
6763
 
      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.
6764
1137
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6765
 
         appropiate JOIN_TAB members.
 
1138
         appropiate JoinTable members.
6766
1139
*/
6767
 
 
6768
 
static void add_not_null_conds(JOIN *join)
 
1140
void add_not_null_conds(JOIN *join)
6769
1141
{
6770
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
 
1142
  for (uint32_t i= join->const_tables; i < join->tables; i++)
6771
1143
  {
6772
 
    JOIN_TAB *tab=join->join_tab+i;
6773
 
    if ((tab->type == JT_REF || tab->type == JT_EQ_REF || 
6774
 
         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) &&
6775
1147
        !tab->table->maybe_null)
6776
1148
    {
6777
 
      for (uint keypart= 0; keypart < tab->ref.key_parts; keypart++)
 
1149
      for (uint32_t keypart= 0; keypart < tab->ref.key_parts; keypart++)
6778
1150
      {
6779
1151
        if (tab->ref.null_rejecting & (1 << keypart))
6780
1152
        {
6782
1154
          Item *notnull;
6783
1155
          assert(item->type() == Item::FIELD_ITEM);
6784
1156
          Item_field *not_null_item= (Item_field*)item;
6785
 
          JOIN_TAB *referred_tab= not_null_item->field->table->reginfo.join_tab;
 
1157
          JoinTable *referred_tab= not_null_item->field->table->reginfo.join_tab;
6786
1158
          /*
6787
1159
            For UPDATE queries such as:
6788
1160
            UPDATE t1 SET t1.f2=(SELECT MAX(t2.f4) FROM t2 WHERE t2.f3=t1.f1);
6794
1166
            return;
6795
1167
          /*
6796
1168
            We need to do full fix_fields() call here in order to have correct
6797
 
            notnull->const_item(). This is needed e.g. by test_quick_select 
6798
 
            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
6799
1171
            called.
6800
1172
          */
6801
 
          if (notnull->fix_fields(join->thd, &notnull))
 
1173
          if (notnull->fix_fields(join->session, &notnull))
6802
1174
            return;
6803
1175
          add_cond_and_fix(&referred_tab->select_cond, notnull);
6804
1176
        }
6823
1195
    -  pointer to the guarded predicate, if success
6824
1196
    -  0, otherwise
6825
1197
*/
6826
 
 
6827
 
static COND*
6828
 
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)
6829
1199
{
6830
1200
  COND *tmp;
6831
1201
  assert(cond != 0);
6841
1211
  return tmp;
6842
1212
}
6843
1213
 
6844
 
 
6845
 
/**
6846
 
  Fill in outer join related info for the execution plan structure.
6847
 
 
6848
 
    For each outer join operation left after simplification of the
6849
 
    original query the function set up the following pointers in the linear
6850
 
    structure join->join_tab representing the selected execution plan.
6851
 
    The first inner table t0 for the operation is set to refer to the last
6852
 
    inner table tk through the field t0->last_inner.
6853
 
    Any inner table ti for the operation are set to refer to the first
6854
 
    inner table ti->first_inner.
6855
 
    The first inner table t0 for the operation is set to refer to the
6856
 
    first inner table of the embedding outer join operation, if there is any,
6857
 
    through the field t0->first_upper.
6858
 
    The on expression for the outer join operation is attached to the
6859
 
    corresponding first inner table through the field t0->on_expr_ref.
6860
 
    Here ti are structures of the JOIN_TAB type.
6861
 
 
6862
 
  EXAMPLE. For the query: 
6863
 
  @code
6864
 
        SELECT * FROM t1
6865
 
                      LEFT JOIN
6866
 
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6867
 
                      ON (t1.a=t2.a AND t1.b=t3.b)
6868
 
          WHERE t1.c > 5,
6869
 
  @endcode
6870
 
 
6871
 
    given the execution plan with the table order t1,t2,t3,t4
6872
 
    is selected, the following references will be set;
6873
 
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6874
 
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6875
 
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to 
6876
 
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6877
 
 
6878
 
  @param join   reference to the info fully describing the query
6879
 
 
6880
 
  @note
6881
 
    The function assumes that the simplification procedure has been
6882
 
    already applied to the join query (see simplify_joins).
6883
 
    This function can be called only after the execution plan
6884
 
    has been chosen.
6885
 
*/
6886
 
 
6887
 
static void
6888
 
make_outerjoin_info(JOIN *join)
6889
 
{
6890
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
6891
 
  {
6892
 
    JOIN_TAB *tab=join->join_tab+i;
6893
 
    TABLE *table=tab->table;
6894
 
    TABLE_LIST *tbl= table->pos_in_table_list;
6895
 
    TABLE_LIST *embedding= tbl->embedding;
6896
 
 
6897
 
    if (tbl->outer_join)
6898
 
    {
6899
 
      /* 
6900
 
        Table tab is the only one inner table for outer join.
6901
 
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6902
 
        is in the query above.)
6903
 
      */
6904
 
      tab->last_inner= tab->first_inner= tab;
6905
 
      tab->on_expr_ref= &tbl->on_expr;
6906
 
      tab->cond_equal= tbl->cond_equal;
6907
 
      if (embedding)
6908
 
        tab->first_upper= embedding->nested_join->first_nested;
6909
 
    }    
6910
 
    for ( ; embedding ; embedding= embedding->embedding)
6911
 
    {
6912
 
      /* Ignore sj-nests: */
6913
 
      if (!embedding->on_expr)
6914
 
        continue;
6915
 
      NESTED_JOIN *nested_join= embedding->nested_join;
6916
 
      if (!nested_join->counter_)
6917
 
      {
6918
 
        /* 
6919
 
          Table tab is the first inner table for nested_join.
6920
 
          Save reference to it in the nested join structure.
6921
 
        */ 
6922
 
        nested_join->first_nested= tab;
6923
 
        tab->on_expr_ref= &embedding->on_expr;
6924
 
        tab->cond_equal= tbl->cond_equal;
6925
 
        if (embedding->embedding)
6926
 
          tab->first_upper= embedding->embedding->nested_join->first_nested;
6927
 
      }
6928
 
      if (!tab->first_inner)  
6929
 
        tab->first_inner= nested_join->first_nested;
6930
 
      if (++nested_join->counter_ < nested_join->join_list.elements)
6931
 
        break;
6932
 
      /* Table tab is the last inner table for nested join. */
6933
 
      nested_join->first_nested->last_inner= tab;
6934
 
    }
6935
 
  }
6936
 
  return;
6937
 
}
6938
 
 
6939
 
 
6940
 
static bool
6941
 
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6942
 
{
6943
 
  THD *thd= join->thd;
6944
 
  if (select)
6945
 
  {
6946
 
    add_not_null_conds(join);
6947
 
    table_map used_tables;
6948
 
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6949
 
    {                        /* there may be a select without a cond. */    
6950
 
      if (join->tables > 1)
6951
 
        cond->update_used_tables();             // Tablenr may have changed
6952
 
      if (join->const_tables == join->tables &&
6953
 
          thd->lex->current_select->master_unit() ==
6954
 
          &thd->lex->unit)              // not upper level SELECT
6955
 
        join->const_table_map|=RAND_TABLE_BIT;
6956
 
      {                                         // Check const tables
6957
 
        COND *const_cond=
6958
 
          make_cond_for_table(cond,
6959
 
                              join->const_table_map,
6960
 
                              (table_map) 0, 1);
6961
 
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6962
 
             tab < join->join_tab+join->tables ; tab++)
6963
 
        {
6964
 
          if (*tab->on_expr_ref)
6965
 
          {
6966
 
            JOIN_TAB *cond_tab= tab->first_inner;
6967
 
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6968
 
                                           join->const_table_map,
6969
 
                                           (  table_map) 0, 0);
6970
 
            if (!tmp)
6971
 
              continue;
6972
 
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6973
 
            if (!tmp)
6974
 
              return(1);
6975
 
            tmp->quick_fix_field();
6976
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6977
 
                                    new Item_cond_and(cond_tab->select_cond,
6978
 
                                                      tmp);
6979
 
            if (!cond_tab->select_cond)
6980
 
              return(1);
6981
 
            cond_tab->select_cond->quick_fix_field();
6982
 
          }       
6983
 
        }
6984
 
        if (const_cond && !const_cond->val_int())
6985
 
        {
6986
 
          return(1);     // Impossible const condition
6987
 
        }
6988
 
      }
6989
 
    }
6990
 
    used_tables=((select->const_tables=join->const_table_map) |
6991
 
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6992
 
    for (uint i=join->const_tables ; i < join->tables ; i++)
6993
 
    {
6994
 
      JOIN_TAB *tab=join->join_tab+i;
6995
 
      /*
6996
 
        first_inner is the X in queries like:
6997
 
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
6998
 
      */
6999
 
      JOIN_TAB *first_inner_tab= tab->first_inner; 
7000
 
      table_map current_map= tab->table->map;
7001
 
      bool use_quick_range=0;
7002
 
      COND *tmp;
7003
 
 
7004
 
      /*
7005
 
        Following force including random expression in last table condition.
7006
 
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
7007
 
      */
7008
 
      if (i == join->tables-1)
7009
 
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
7010
 
      used_tables|=current_map;
7011
 
 
7012
 
      if (tab->type == JT_REF && tab->quick &&
7013
 
          (uint) tab->ref.key == tab->quick->index &&
7014
 
          tab->ref.key_length < tab->quick->max_used_key_length)
7015
 
      {
7016
 
        /* Range uses longer key;  Use this instead of ref on key */
7017
 
        tab->type=JT_ALL;
7018
 
        use_quick_range=1;
7019
 
        tab->use_quick=1;
7020
 
        tab->ref.key= -1;
7021
 
        tab->ref.key_parts=0;           // Don't use ref key.
7022
 
        join->best_positions[i].records_read= rows2double(tab->quick->records);
7023
 
        /* 
7024
 
          We will use join cache here : prevent sorting of the first
7025
 
          table only and sort at the end.
7026
 
        */
7027
 
        if (i != join->const_tables && join->tables > join->const_tables + 1)
7028
 
          join->full_join= 1;
7029
 
      }
7030
 
 
7031
 
      tmp= NULL;
7032
 
      if (cond)
7033
 
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7034
 
      if (cond && !tmp && tab->quick)
7035
 
      {                                         // Outer join
7036
 
        if (tab->type != JT_ALL)
7037
 
        {
7038
 
          /*
7039
 
            Don't use the quick method
7040
 
            We come here in the case where we have 'key=constant' and
7041
 
            the test is removed by make_cond_for_table()
7042
 
          */
7043
 
          delete tab->quick;
7044
 
          tab->quick= 0;
7045
 
        }
7046
 
        else
7047
 
        {
7048
 
          /*
7049
 
            Hack to handle the case where we only refer to a table
7050
 
            in the ON part of an OUTER JOIN. In this case we want the code
7051
 
            below to check if we should use 'quick' instead.
7052
 
          */
7053
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
7054
 
        }
7055
 
 
7056
 
      }
7057
 
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7058
 
          tab->type == JT_EQ_REF)
7059
 
      {
7060
 
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7061
 
                                       thd->memdup((uchar*) select,
7062
 
                                                   sizeof(*select)));
7063
 
        if (!sel)
7064
 
          return(1);                    // End of memory
7065
 
        /*
7066
 
          If tab is an inner table of an outer join operation,
7067
 
          add a match guard to the pushed down predicate.
7068
 
          The guard will turn the predicate on only after
7069
 
          the first match for outer tables is encountered.
7070
 
        */        
7071
 
        if (cond && tmp)
7072
 
        {
7073
 
          /*
7074
 
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7075
 
            a cond, so neutralize the hack above.
7076
 
          */
7077
 
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7078
 
            return(1);
7079
 
          tab->select_cond=sel->cond=tmp;
7080
 
          /* Push condition to storage engine if this is enabled
7081
 
             and the condition is not guarded */
7082
 
          tab->table->file->pushed_cond= NULL;
7083
 
          if (thd->variables.engine_condition_pushdown)
7084
 
          {
7085
 
            COND *push_cond= 
7086
 
              make_cond_for_table(tmp, current_map, current_map, 0);
7087
 
            if (push_cond)
7088
 
            {
7089
 
              /* Push condition to handler */
7090
 
              if (!tab->table->file->cond_push(push_cond))
7091
 
                tab->table->file->pushed_cond= push_cond;
7092
 
            }
7093
 
          }
7094
 
        }
7095
 
        else
7096
 
          tab->select_cond= sel->cond= NULL;
7097
 
 
7098
 
        sel->head=tab->table;
7099
 
        if (tab->quick)
7100
 
        {
7101
 
          /* Use quick key read if it's a constant and it's not used
7102
 
             with key reading */
7103
 
          if (tab->needed_reg.is_clear_all() && tab->type != JT_EQ_REF
7104
 
              && (tab->type != JT_REF || (uint) tab->ref.key == tab->quick->index))
7105
 
          {
7106
 
            sel->quick=tab->quick;              // Use value from get_quick_...
7107
 
            sel->quick_keys.clear_all();
7108
 
            sel->needed_reg.clear_all();
7109
 
          }
7110
 
          else
7111
 
          {
7112
 
            delete tab->quick;
7113
 
          }
7114
 
          tab->quick=0;
7115
 
        }
7116
 
        uint ref_key=(uint) sel->head->reginfo.join_tab->ref.key+1;
7117
 
        if (i == join->const_tables && ref_key)
7118
 
        {
7119
 
          if (!tab->const_keys.is_clear_all() &&
7120
 
              tab->table->reginfo.impossible_range)
7121
 
            return(1);
7122
 
        }
7123
 
        else if (tab->type == JT_ALL && ! use_quick_range)
7124
 
        {
7125
 
          if (!tab->const_keys.is_clear_all() &&
7126
 
              tab->table->reginfo.impossible_range)
7127
 
            return(1);                          // Impossible range
7128
 
          /*
7129
 
            We plan to scan all rows.
7130
 
            Check again if we should use an index.
7131
 
            We could have used an column from a previous table in
7132
 
            the index if we are using limit and this is the first table
7133
 
          */
7134
 
 
7135
 
          if ((cond && (!tab->keys.is_subset(tab->const_keys) && i > 0)) ||
7136
 
              (!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)))
7137
 
          {
7138
 
            /* Join with outer join condition */
7139
 
            COND *orig_cond=sel->cond;
7140
 
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7141
 
 
7142
 
            /*
7143
 
              We can't call sel->cond->fix_fields,
7144
 
              as it will break tab->on_expr if it's AND condition
7145
 
              (fix_fields currently removes extra AND/OR levels).
7146
 
              Yet attributes of the just built condition are not needed.
7147
 
              Thus we call sel->cond->quick_fix_field for safety.
7148
 
            */
7149
 
            if (sel->cond && !sel->cond->fixed)
7150
 
              sel->cond->quick_fix_field();
7151
 
 
7152
 
            if (sel->test_quick_select(thd, tab->keys,
7153
 
                                       used_tables & ~ current_map,
7154
 
                                       (join->select_options &
7155
 
                                        OPTION_FOUND_ROWS ?
7156
 
                                        HA_POS_ERROR :
7157
 
                                        join->unit->select_limit_cnt), 0,
7158
 
                                        false) < 0)
7159
 
            {
7160
 
              /*
7161
 
                Before reporting "Impossible WHERE" for the whole query
7162
 
                we have to check isn't it only "impossible ON" instead
7163
 
              */
7164
 
              sel->cond=orig_cond;
7165
 
              if (!*tab->on_expr_ref ||
7166
 
                  sel->test_quick_select(thd, tab->keys,
7167
 
                                         used_tables & ~ current_map,
7168
 
                                         (join->select_options &
7169
 
                                          OPTION_FOUND_ROWS ?
7170
 
                                          HA_POS_ERROR :
7171
 
                                          join->unit->select_limit_cnt),0,
7172
 
                                          false) < 0)
7173
 
                return(1);                      // Impossible WHERE
7174
 
            }
7175
 
            else
7176
 
              sel->cond=orig_cond;
7177
 
 
7178
 
            /* Fix for EXPLAIN */
7179
 
            if (sel->quick)
7180
 
              join->best_positions[i].records_read= (double)sel->quick->records;
7181
 
          }
7182
 
          else
7183
 
          {
7184
 
            sel->needed_reg=tab->needed_reg;
7185
 
            sel->quick_keys.clear_all();
7186
 
          }
7187
 
          if (!sel->quick_keys.is_subset(tab->checked_keys) ||
7188
 
              !sel->needed_reg.is_subset(tab->checked_keys))
7189
 
          {
7190
 
            tab->keys=sel->quick_keys;
7191
 
            tab->keys.merge(sel->needed_reg);
7192
 
            tab->use_quick= (!sel->needed_reg.is_clear_all() &&
7193
 
                             (select->quick_keys.is_clear_all() ||
7194
 
                              (select->quick &&
7195
 
                               (select->quick->records >= 100L)))) ?
7196
 
              2 : 1;
7197
 
            sel->read_tables= used_tables & ~current_map;
7198
 
          }
7199
 
          if (i != join->const_tables && tab->use_quick != 2)
7200
 
          {                                     /* Read with cache */
7201
 
            if (cond &&
7202
 
                (tmp=make_cond_for_table(cond,
7203
 
                                         join->const_table_map |
7204
 
                                         current_map,
7205
 
                                         current_map, 0)))
7206
 
            {
7207
 
              tab->cache.select=(SQL_SELECT*)
7208
 
                thd->memdup((uchar*) sel, sizeof(SQL_SELECT));
7209
 
              tab->cache.select->cond=tmp;
7210
 
              tab->cache.select->read_tables=join->const_table_map;
7211
 
            }
7212
 
          }
7213
 
        }
7214
 
      }
7215
 
      
7216
 
      /* 
7217
 
        Push down conditions from all on expressions.
7218
 
        Each of these conditions are guarded by a variable
7219
 
        that turns if off just before null complemented row for
7220
 
        outer joins is formed. Thus, the condition from an
7221
 
        'on expression' are guaranteed not to be checked for
7222
 
        the null complemented row.
7223
 
      */ 
7224
 
 
7225
 
      /* First push down constant conditions from on expressions */
7226
 
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7227
 
           join_tab < join->join_tab+join->tables ; join_tab++)
7228
 
      {
7229
 
        if (*join_tab->on_expr_ref)
7230
 
        {
7231
 
          JOIN_TAB *cond_tab= join_tab->first_inner;
7232
 
          COND *tmp= make_cond_for_table(*join_tab->on_expr_ref,
7233
 
                                         join->const_table_map,
7234
 
                                         (table_map) 0, 0);
7235
 
          if (!tmp)
7236
 
            continue;
7237
 
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7238
 
          if (!tmp)
7239
 
            return(1);
7240
 
          tmp->quick_fix_field();
7241
 
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7242
 
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7243
 
          if (!cond_tab->select_cond)
7244
 
            return(1);
7245
 
          cond_tab->select_cond->quick_fix_field();
7246
 
        }       
7247
 
      }
7248
 
 
7249
 
      /* Push down non-constant conditions from on expressions */
7250
 
      JOIN_TAB *last_tab= tab;
7251
 
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7252
 
      {  
7253
 
        /* 
7254
 
          Table tab is the last inner table of an outer join.
7255
 
          An on expression is always attached to it.
7256
 
        */     
7257
 
        COND *on_expr= *first_inner_tab->on_expr_ref;
7258
 
 
7259
 
        table_map used_tables2= (join->const_table_map |
7260
 
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7261
 
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7262
 
        {
7263
 
          current_map= tab->table->map;
7264
 
          used_tables2|= current_map;
7265
 
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7266
 
                                              current_map, 0);
7267
 
          if (tmp_cond)
7268
 
          {
7269
 
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7270
 
            /*
7271
 
              First add the guards for match variables of
7272
 
              all embedding outer join operations.
7273
 
            */
7274
 
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7275
 
                                                     tmp_cond,
7276
 
                                                     first_inner_tab)))
7277
 
              return(1);
7278
 
            /* 
7279
 
              Now add the guard turning the predicate off for 
7280
 
              the null complemented row.
7281
 
            */ 
7282
 
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7283
 
                                              &first_inner_tab->
7284
 
                                              not_null_compl);
7285
 
            if (tmp_cond)
7286
 
              tmp_cond->quick_fix_field();
7287
 
            /* Add the predicate to other pushed down predicates */
7288
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7289
 
                                  new Item_cond_and(cond_tab->select_cond,
7290
 
                                                    tmp_cond);
7291
 
            if (!cond_tab->select_cond)
7292
 
              return(1);
7293
 
            cond_tab->select_cond->quick_fix_field();
7294
 
          }              
7295
 
        }
7296
 
        first_inner_tab= first_inner_tab->first_upper;       
7297
 
      }
7298
 
    }
7299
 
  }
7300
 
  return(0);
7301
 
}
7302
 
 
7303
 
 
7304
 
/* 
 
1214
/*
7305
1215
  Check if given expression uses only table fields covered by the given index
7306
1216
 
7307
1217
  SYNOPSIS
7314
1224
  DESCRIPTION
7315
1225
    Check if given expression only uses fields covered by index #keyno in the
7316
1226
    table tbl. The expression can use any fields in any other tables.
7317
 
    
7318
 
    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
7319
1229
    handled outside of this function.
7320
1230
 
7321
1231
  RETURN
7322
1232
    true   Yes
7323
1233
    false  No
7324
1234
*/
7325
 
 
7326
 
bool uses_index_fields_only(Item *item, TABLE *tbl, uint keyno, 
7327
 
                            bool other_tbls_ok)
 
1235
static bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
7328
1236
{
7329
1237
  if (item->const_item())
7330
1238
    return true;
7331
1239
 
7332
 
  /* 
7333
 
    Don't push down the triggered conditions. Nested outer joins execution 
 
1240
  /*
 
1241
    Don't push down the triggered conditions. Nested outer joins execution
7334
1242
    code may need to evaluate a condition several times (both triggered and
7335
1243
    untriggered), and there is no way to put thi
7336
1244
    TODO: Consider cloning the triggered condition and using the copies for:
7337
1245
      1. push the first copy down, to have most restrictive index condition
7338
1246
         possible
7339
 
      2. Put the second copy into tab->select_cond. 
 
1247
      2. Put the second copy into tab->select_cond.
7340
1248
  */
7341
 
  if (item->type() == Item::FUNC_ITEM && 
 
1249
  if (item->type() == Item::FUNC_ITEM &&
7342
1250
      ((Item_func*)item)->functype() == Item_func::TRIG_COND_FUNC)
7343
1251
    return false;
7344
1252
 
7364
1272
    {
7365
1273
      /* This is a function, apply condition recursively to arguments */
7366
1274
      List_iterator<Item> li(*((Item_cond*)item)->argument_list());
7367
 
      Item *item;
7368
 
      while ((item=li++))
 
1275
      Item *list_item;
 
1276
      while ((list_item=li++))
7369
1277
      {
7370
1278
        if (!uses_index_fields_only(item, tbl, keyno, other_tbls_ok))
7371
1279
          return false;
7375
1283
  case Item::FIELD_ITEM:
7376
1284
    {
7377
1285
      Item_field *item_field= (Item_field*)item;
7378
 
      if (item_field->field->table != tbl) 
 
1286
      if (item_field->field->table != tbl)
7379
1287
        return true;
7380
 
      return item_field->field->part_of_key.is_set(keyno);
 
1288
      return item_field->field->part_of_key.test(keyno);
7381
1289
    }
7382
1290
  case Item::REF_ITEM:
7383
1291
    return uses_index_fields_only(item->real_item(), tbl, keyno,
7387
1295
  }
7388
1296
}
7389
1297
 
7390
 
 
7391
1298
#define ICP_COND_USES_INDEX_ONLY 10
7392
1299
 
7393
1300
/*
7402
1309
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7403
1310
 
7404
1311
  DESCRIPTION
7405
 
    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
7406
1313
    we have values only of fields covered by some index. The condition may
7407
 
    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
7408
1315
    fields.
7409
1316
 
7410
1317
    Example:
7411
1318
      make_cond_for_index(
7412
1319
         "cond(t1.field) AND cond(t2.key1) AND cond(t2.non_key) AND cond(t2.key2)",
7413
 
          t2, keyno(t2.key1)) 
 
1320
          t2, keyno(t2.key1))
7414
1321
      will return
7415
1322
        "cond(t1.field) AND cond(t2.key2)"
7416
1323
 
7417
1324
  RETURN
7418
1325
    Index condition, or NULL if no condition could be inferred.
7419
1326
*/
7420
 
 
7421
 
Item *make_cond_for_index(Item *cond, TABLE *table, uint keyno,
7422
 
                          bool other_tbls_ok)
 
1327
static Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
7423
1328
{
7424
1329
  if (!cond)
7425
1330
    return NULL;
7426
1331
  if (cond->type() == Item::COND_ITEM)
7427
1332
  {
7428
 
    uint n_marked= 0;
 
1333
    uint32_t n_marked= 0;
7429
1334
    if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
7430
1335
    {
7431
1336
      Item_cond_and *new_cond=new Item_cond_and;
7432
1337
      if (!new_cond)
7433
 
        return (COND*) 0;
 
1338
        return (COND*) 0;
7434
1339
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7435
1340
      Item *item;
7436
1341
      while ((item=li++))
7437
1342
      {
7438
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7439
 
        if (fix)
7440
 
          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);
7441
1346
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7442
1347
      }
7443
1348
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7444
1349
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7445
1350
      switch (new_cond->argument_list()->elements) {
7446
1351
      case 0:
7447
 
        return (COND*) 0;
 
1352
        return (COND*) 0;
7448
1353
      case 1:
7449
 
        return new_cond->argument_list()->head();
 
1354
        return new_cond->argument_list()->head();
7450
1355
      default:
7451
 
        new_cond->quick_fix_field();
7452
 
        return new_cond;
 
1356
        new_cond->quick_fix_field();
 
1357
        return new_cond;
7453
1358
      }
7454
1359
    }
7455
1360
    else /* It's OR */
7456
1361
    {
7457
1362
      Item_cond_or *new_cond=new Item_cond_or;
7458
1363
      if (!new_cond)
7459
 
        return (COND*) 0;
 
1364
        return (COND*) 0;
7460
1365
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7461
1366
      Item *item;
7462
1367
      while ((item=li++))
7463
1368
      {
7464
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7465
 
        if (!fix)
7466
 
          return (COND*) 0;
7467
 
        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);
7468
1373
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7469
1374
      }
7470
1375
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7482
1387
}
7483
1388
 
7484
1389
 
7485
 
Item *make_cond_remainder(Item *cond, bool exclude_index)
 
1390
static Item *make_cond_remainder(Item *cond, bool exclude_index)
7486
1391
{
7487
1392
  if (exclude_index && cond->marker == ICP_COND_USES_INDEX_ONLY)
7488
1393
    return 0; /* Already checked */
7495
1400
      /* Create new top level AND item */
7496
1401
      Item_cond_and *new_cond=new Item_cond_and;
7497
1402
      if (!new_cond)
7498
 
        return (COND*) 0;
 
1403
        return (COND*) 0;
7499
1404
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7500
1405
      Item *item;
7501
1406
      while ((item=li++))
7502
1407
      {
7503
 
        Item *fix= make_cond_remainder(item, exclude_index);
7504
 
        if (fix)
 
1408
        Item *fix= make_cond_remainder(item, exclude_index);
 
1409
        if (fix)
7505
1410
        {
7506
 
          new_cond->argument_list()->push_back(fix);
 
1411
          new_cond->argument_list()->push_back(fix);
7507
1412
          tbl_map |= fix->used_tables();
7508
1413
        }
7509
1414
      }
7510
1415
      switch (new_cond->argument_list()->elements) {
7511
1416
      case 0:
7512
 
        return (COND*) 0;
 
1417
        return (COND*) 0;
7513
1418
      case 1:
7514
 
        return new_cond->argument_list()->head();
 
1419
        return new_cond->argument_list()->head();
7515
1420
      default:
7516
 
        new_cond->quick_fix_field();
 
1421
        new_cond->quick_fix_field();
7517
1422
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7518
 
        return new_cond;
 
1423
        return new_cond;
7519
1424
      }
7520
1425
    }
7521
1426
    else /* It's OR */
7522
1427
    {
7523
1428
      Item_cond_or *new_cond=new Item_cond_or;
7524
1429
      if (!new_cond)
7525
 
        return (COND*) 0;
 
1430
        return (COND*) 0;
7526
1431
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7527
1432
      Item *item;
7528
1433
      while ((item=li++))
7529
1434
      {
7530
 
        Item *fix= make_cond_remainder(item, false);
7531
 
        if (!fix)
7532
 
          return (COND*) 0;
7533
 
        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);
7534
1439
        tbl_map |= fix->used_tables();
7535
1440
      }
7536
1441
      new_cond->quick_fix_field();
7542
1447
  return cond;
7543
1448
}
7544
1449
 
7545
 
 
7546
 
/*
7547
 
  Try to extract and push the index condition
7548
 
 
7549
 
  SYNOPSIS
7550
 
    push_index_cond()
7551
 
      tab            A join tab that has tab->table->file and its condition
7552
 
                     in tab->select_cond
7553
 
      keyno          Index for which extract and push the condition
7554
 
      other_tbls_ok  true <=> Fields of other non-const tables are allowed
7555
 
 
7556
 
  DESCRIPTION
7557
 
    Try to extract and push the index condition down to table handler
7558
 
*/
7559
 
 
7560
 
static void push_index_cond(JOIN_TAB *tab, uint keyno, bool other_tbls_ok)
7561
 
{
7562
 
  Item *idx_cond;
7563
 
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7564
 
      tab->join->thd->variables.engine_condition_pushdown)
7565
 
  {
7566
 
    idx_cond= make_cond_for_index(tab->select_cond, tab->table, keyno,
7567
 
                                  other_tbls_ok);
7568
 
 
7569
 
    if (idx_cond)
7570
 
    {
7571
 
      tab->pre_idx_push_select_cond= tab->select_cond;
7572
 
      Item *idx_remainder_cond= 
7573
 
        tab->table->file->idx_cond_push(keyno, idx_cond);
7574
 
 
7575
 
      /*
7576
 
        Disable eq_ref's "lookup cache" if we've pushed down an index
7577
 
        condition. 
7578
 
        TODO: This check happens to work on current ICP implementations, but
7579
 
        there may exist a compliant implementation that will not work 
7580
 
        correctly with it. Sort this out when we stabilize the condition
7581
 
        pushdown APIs.
7582
 
      */
7583
 
      if (idx_remainder_cond != idx_cond)
7584
 
        tab->ref.disable_cache= true;
7585
 
 
7586
 
      Item *row_cond= make_cond_remainder(tab->select_cond, true);
7587
 
 
7588
 
      if (row_cond)
7589
 
      {
7590
 
        if (!idx_remainder_cond)
7591
 
          tab->select_cond= row_cond;
7592
 
        else
7593
 
        {
7594
 
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7595
 
          tab->select_cond->quick_fix_field();
7596
 
          ((Item_cond_and*)tab->select_cond)->used_tables_cache= 
7597
 
            row_cond->used_tables() | idx_remainder_cond->used_tables();
7598
 
        }
7599
 
      }
7600
 
      else
7601
 
        tab->select_cond= idx_remainder_cond;
7602
 
      if (tab->select)
7603
 
      {
7604
 
        tab->select->cond= tab->select_cond;
7605
 
      }
7606
 
    }
7607
 
  }
7608
 
  return;
7609
 
}
7610
 
 
7611
 
 
7612
 
 
7613
 
    /*
7614
 
      Determine if the set is already ordered for ORDER BY, so it can 
7615
 
      disable join cache because it will change the ordering of the results.
7616
 
      Code handles sort table that is at any location (not only first after 
7617
 
      the const tables) despite the fact that it's currently prohibited.
7618
 
      We must disable join cache if the first non-const table alone is
7619
 
      ordered. If there is a temp table the ordering is done as a last
7620
 
      operation and doesn't prevent join cache usage.
7621
 
    */
7622
 
uint make_join_orderinfo(JOIN *join)
7623
 
{
7624
 
  uint i;
7625
 
  if (join->need_tmp)
7626
 
    return join->tables;
7627
 
 
7628
 
  for (i=join->const_tables ; i < join->tables ; i++)
7629
 
  {
7630
 
    JOIN_TAB *tab=join->join_tab+i;
7631
 
    TABLE *table=tab->table;
7632
 
    if ((table == join->sort_by_table && 
7633
 
         (!join->order || join->skip_sort_order)) ||
7634
 
        (join->sort_by_table == (TABLE *) 1 && i != join->const_tables))
7635
 
    {
7636
 
      break;
7637
 
    }
7638
 
  }
7639
 
  return i;
7640
 
}
7641
 
 
7642
 
 
7643
 
/*
7644
 
  Plan refinement stage: do various set ups for the executioner
7645
 
 
7646
 
  SYNOPSIS
7647
 
    make_join_readinfo()
7648
 
      join           Join being processed
7649
 
      options        Join's options (checking for SELECT_DESCRIBE, 
7650
 
                     SELECT_NO_JOIN_CACHE)
7651
 
      no_jbuf_after  Don't use join buffering after table with this number.
7652
 
 
7653
 
  DESCRIPTION
7654
 
    Plan refinement stage: do various set ups for the executioner
7655
 
      - set up use of join buffering
7656
 
      - push index conditions
7657
 
      - increment counters
7658
 
      - etc
7659
 
 
7660
 
  RETURN 
7661
 
    false - OK
7662
 
    true  - Out of memory
7663
 
*/
7664
 
 
7665
 
static bool
7666
 
make_join_readinfo(JOIN *join, uint64_t options, uint no_jbuf_after)
7667
 
{
7668
 
  uint i;
7669
 
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7670
 
  bool sorted= 1;
7671
 
 
7672
 
  for (i=join->const_tables ; i < join->tables ; i++)
7673
 
  {
7674
 
    JOIN_TAB *tab=join->join_tab+i;
7675
 
    TABLE *table=tab->table;
7676
 
    bool using_join_cache;
7677
 
    tab->read_record.table= table;
7678
 
    tab->read_record.file=table->file;
7679
 
    tab->next_select=sub_select;                /* normal select */
7680
 
    /* 
7681
 
      TODO: don't always instruct first table's ref/range access method to 
7682
 
      produce sorted output.
7683
 
    */
7684
 
    tab->sorted= sorted;
7685
 
    sorted= 0;                                  // only first must be sorted
7686
 
    if (tab->insideout_match_tab)
7687
 
    {
7688
 
      if (!(tab->insideout_buf= (uchar*)join->thd->alloc(tab->table->key_info
7689
 
                                                         [tab->index].
7690
 
                                                         key_length)))
7691
 
        return true;
7692
 
    }
7693
 
    switch (tab->type) {
7694
 
    case JT_SYSTEM:                             // Only happens with left join
7695
 
      table->status=STATUS_NO_RECORD;
7696
 
      tab->read_first_record= join_read_system;
7697
 
      tab->read_record.read_record= join_no_more_records;
7698
 
      break;
7699
 
    case JT_CONST:                              // Only happens with left join
7700
 
      table->status=STATUS_NO_RECORD;
7701
 
      tab->read_first_record= join_read_const;
7702
 
      tab->read_record.read_record= join_no_more_records;
7703
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7704
 
          !table->no_keyread)
7705
 
      {
7706
 
        table->key_read=1;
7707
 
        table->file->extra(HA_EXTRA_KEYREAD);
7708
 
      }
7709
 
      break;
7710
 
    case JT_EQ_REF:
7711
 
      table->status=STATUS_NO_RECORD;
7712
 
      if (tab->select)
7713
 
      {
7714
 
        delete tab->select->quick;
7715
 
        tab->select->quick=0;
7716
 
      }
7717
 
      delete tab->quick;
7718
 
      tab->quick=0;
7719
 
      tab->read_first_record= join_read_key;
7720
 
      tab->read_record.read_record= join_no_more_records;
7721
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7722
 
          !table->no_keyread)
7723
 
      {
7724
 
        table->key_read=1;
7725
 
        table->file->extra(HA_EXTRA_KEYREAD);
7726
 
      }
7727
 
      else
7728
 
        push_index_cond(tab, tab->ref.key, true);
7729
 
      break;
7730
 
    case JT_REF_OR_NULL:
7731
 
    case JT_REF:
7732
 
      table->status=STATUS_NO_RECORD;
7733
 
      if (tab->select)
7734
 
      {
7735
 
        delete tab->select->quick;
7736
 
        tab->select->quick=0;
7737
 
      }
7738
 
      delete tab->quick;
7739
 
      tab->quick=0;
7740
 
      if (table->covering_keys.is_set(tab->ref.key) &&
7741
 
          !table->no_keyread)
7742
 
      {
7743
 
        table->key_read=1;
7744
 
        table->file->extra(HA_EXTRA_KEYREAD);
7745
 
      }
7746
 
      else
7747
 
        push_index_cond(tab, tab->ref.key, true);
7748
 
      if (tab->type == JT_REF)
7749
 
      {
7750
 
        tab->read_first_record= join_read_always_key;
7751
 
        tab->read_record.read_record= tab->insideout_match_tab? 
7752
 
           join_read_next_same_diff : join_read_next_same;
7753
 
      }
7754
 
      else
7755
 
      {
7756
 
        tab->read_first_record= join_read_always_key_or_null;
7757
 
        tab->read_record.read_record= join_read_next_same_or_null;
7758
 
      }
7759
 
      break;
7760
 
    case JT_ALL:
7761
 
      /*
7762
 
        If previous table use cache
7763
 
        If the incoming data set is already sorted don't use cache.
7764
 
      */
7765
 
      table->status=STATUS_NO_RECORD;
7766
 
      using_join_cache= false;
7767
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7768
 
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7769
 
          !tab->insideout_match_tab)
7770
 
      {
7771
 
        if ((options & SELECT_DESCRIBE) ||
7772
 
            !join_init_cache(join->thd,join->join_tab+join->const_tables,
7773
 
                             i-join->const_tables))
7774
 
        {
7775
 
          using_join_cache= true;
7776
 
          tab[-1].next_select=sub_select_cache; /* Patch previous */
7777
 
        }
7778
 
      }
7779
 
      /* These init changes read_record */
7780
 
      if (tab->use_quick == 2)
7781
 
      {
7782
 
        join->thd->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7783
 
        tab->read_first_record= join_init_quick_read_record;
7784
 
        if (statistics)
7785
 
          status_var_increment(join->thd->status_var.select_range_check_count);
7786
 
      }
7787
 
      else
7788
 
      {
7789
 
        tab->read_first_record= join_init_read_record;
7790
 
        if (i == join->const_tables)
7791
 
        {
7792
 
          if (tab->select && tab->select->quick)
7793
 
          {
7794
 
            if (statistics)
7795
 
              status_var_increment(join->thd->status_var.select_range_count);
7796
 
          }
7797
 
          else
7798
 
          {
7799
 
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7800
 
            if (statistics)
7801
 
              status_var_increment(join->thd->status_var.select_scan_count);
7802
 
          }
7803
 
        }
7804
 
        else
7805
 
        {
7806
 
          if (tab->select && tab->select->quick)
7807
 
          {
7808
 
            if (statistics)
7809
 
              status_var_increment(join->thd->status_var.select_full_range_join_count);
7810
 
          }
7811
 
          else
7812
 
          {
7813
 
            join->thd->server_status|=SERVER_QUERY_NO_INDEX_USED;
7814
 
            if (statistics)
7815
 
              status_var_increment(join->thd->status_var.select_full_join_count);
7816
 
          }
7817
 
        }
7818
 
        if (!table->no_keyread)
7819
 
        {
7820
 
          if (tab->select && tab->select->quick &&
7821
 
              tab->select->quick->index != MAX_KEY && //not index_merge
7822
 
              table->covering_keys.is_set(tab->select->quick->index))
7823
 
          {
7824
 
            table->key_read=1;
7825
 
            table->file->extra(HA_EXTRA_KEYREAD);
7826
 
          }
7827
 
          else if (!table->covering_keys.is_clear_all() &&
7828
 
                   !(tab->select && tab->select->quick))
7829
 
          {                                     // Only read index tree
7830
 
            if (!tab->insideout_match_tab)
7831
 
            {
7832
 
              /*
7833
 
                See bug #26447: "Using the clustered index for a table scan
7834
 
                is always faster than using a secondary index".
7835
 
              */
7836
 
              if (table->s->primary_key != MAX_KEY &&
7837
 
                  table->file->primary_key_is_clustered())
7838
 
                tab->index= table->s->primary_key;
7839
 
              else
7840
 
                tab->index=find_shortest_key(table, & table->covering_keys);
7841
 
            }
7842
 
            tab->read_first_record= join_read_first;
7843
 
            tab->type=JT_NEXT;          // Read with index_first / index_next
7844
 
          }
7845
 
        }
7846
 
        if (tab->select && tab->select->quick &&
7847
 
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7848
 
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7849
 
      }
7850
 
      break;
7851
 
    default:
7852
 
      break;                                    /* purecov: deadcode */
7853
 
    case JT_UNKNOWN:
7854
 
    case JT_MAYBE_REF:
7855
 
      abort();                                  /* purecov: deadcode */
7856
 
    }
7857
 
  }
7858
 
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7859
 
  return(false);
7860
 
}
7861
 
 
7862
 
 
7863
 
/**
7864
 
  Give error if we some tables are done with a full join.
7865
 
 
7866
 
  This is used by multi_table_update and multi_table_delete when running
7867
 
  in safe mode.
7868
 
 
7869
 
  @param join           Join condition
7870
 
 
7871
 
  @retval
7872
 
    0   ok
7873
 
  @retval
7874
 
    1   Error (full join used)
7875
 
*/
7876
 
 
7877
 
bool error_if_full_join(JOIN *join)
7878
 
{
7879
 
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7880
 
       tab < end;
7881
 
       tab++)
7882
 
  {
7883
 
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7884
 
    {
7885
 
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7886
 
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7887
 
      return(1);
7888
 
    }
7889
 
  }
7890
 
  return(0);
7891
 
}
7892
 
 
7893
 
 
7894
 
/**
7895
 
  cleanup JOIN_TAB.
7896
 
*/
7897
 
 
7898
 
void JOIN_TAB::cleanup()
 
1450
/**
 
1451
  cleanup JoinTable.
 
1452
*/
 
1453
void JoinTable::cleanup()
7899
1454
{
7900
1455
  delete select;
7901
1456
  select= 0;
7902
1457
  delete quick;
7903
1458
  quick= 0;
7904
 
  x_free(cache.buff);
 
1459
  if (cache.buff)
 
1460
    free(cache.buff);
7905
1461
  cache.buff= 0;
7906
1462
  limit= 0;
7907
1463
  if (table)
7909
1465
    if (table->key_read)
7910
1466
    {
7911
1467
      table->key_read= 0;
7912
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
1468
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
7913
1469
    }
7914
 
    table->file->ha_index_or_rnd_end();
 
1470
    table->cursor->ha_index_or_rnd_end();
7915
1471
    /*
7916
1472
      We need to reset this for next select
7917
1473
      (Tested in part_of_refkey)
7921
1477
  end_read_record(&read_record);
7922
1478
}
7923
1479
 
7924
 
 
7925
 
/**
7926
 
  Partially cleanup JOIN after it has executed: close index or rnd read
7927
 
  (table cursors), free quick selects.
7928
 
 
7929
 
    This function is called in the end of execution of a JOIN, before the used
7930
 
    tables are unlocked and closed.
7931
 
 
7932
 
    For a join that is resolved using a temporary table, the first sweep is
7933
 
    performed against actual tables and an intermediate result is inserted
7934
 
    into the temprorary table.
7935
 
    The last sweep is performed against the temporary table. Therefore,
7936
 
    the base tables and associated buffers used to fill the temporary table
7937
 
    are no longer needed, and this function is called to free them.
7938
 
 
7939
 
    For a join that is performed without a temporary table, this function
7940
 
    is called after all rows are sent, but before EOF packet is sent.
7941
 
 
7942
 
    For a simple SELECT with no subqueries this function performs a full
7943
 
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
7944
 
    tables.
7945
 
 
7946
 
    If a JOIN is executed for a subquery or if it has a subquery, we can't
7947
 
    do the full cleanup and need to do a partial cleanup only.
7948
 
    - If a JOIN is not the top level join, we must not unlock the tables
7949
 
    because the outer select may not have been evaluated yet, and we
7950
 
    can't unlock only selected tables of a query.
7951
 
    - Additionally, if this JOIN corresponds to a correlated subquery, we
7952
 
    should not free quick selects and join buffers because they will be
7953
 
    needed for the next execution of the correlated subquery.
7954
 
    - However, if this is a JOIN for a [sub]select, which is not
7955
 
    a correlated subquery itself, but has subqueries, we can free it
7956
 
    fully and also free JOINs of all its subqueries. The exception
7957
 
    is a subquery in SELECT list, e.g: @n
7958
 
    SELECT a, (select max(b) from t1) group by c @n
7959
 
    This subquery will not be evaluated at first sweep and its value will
7960
 
    not be inserted into the temporary table. Instead, it's evaluated
7961
 
    when selecting from the temporary table. Therefore, it can't be freed
7962
 
    here even though it's not correlated.
7963
 
 
7964
 
  @todo
7965
 
    Unlock tables even if the join isn't top level select in the tree
7966
 
*/
7967
 
 
7968
 
void JOIN::join_free()
7969
 
{
7970
 
  SELECT_LEX_UNIT *tmp_unit;
7971
 
  SELECT_LEX *sl;
7972
 
  /*
7973
 
    Optimization: if not EXPLAIN and we are done with the JOIN,
7974
 
    free all tables.
7975
 
  */
7976
 
  bool full= (!select_lex->uncacheable && !thd->lex->describe);
7977
 
  bool can_unlock= full;
7978
 
 
7979
 
  cleanup(full);
7980
 
 
7981
 
  for (tmp_unit= select_lex->first_inner_unit();
7982
 
       tmp_unit;
7983
 
       tmp_unit= tmp_unit->next_unit())
7984
 
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
7985
 
    {
7986
 
      Item_subselect *subselect= sl->master_unit()->item;
7987
 
      bool full_local= full && (!subselect || subselect->is_evaluated());
7988
 
      /*
7989
 
        If this join is evaluated, we can fully clean it up and clean up all
7990
 
        its underlying joins even if they are correlated -- they will not be
7991
 
        used any more anyway.
7992
 
        If this join is not yet evaluated, we still must clean it up to
7993
 
        close its table cursors -- it may never get evaluated, as in case of
7994
 
        ... HAVING false OR a IN (SELECT ...))
7995
 
        but all table cursors must be closed before the unlock.
7996
 
      */
7997
 
      sl->cleanup_all_joins(full_local);
7998
 
      /* Can't unlock if at least one JOIN is still needed */
7999
 
      can_unlock= can_unlock && full_local;
8000
 
    }
8001
 
 
8002
 
  /*
8003
 
    We are not using tables anymore
8004
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8005
 
  */
8006
 
  if (can_unlock && lock && thd->lock &&
8007
 
      !(select_options & SELECT_NO_UNLOCK) &&
8008
 
      !select_lex->subquery_in_having &&
8009
 
      (select_lex == (thd->lex->unit.fake_select_lex ?
8010
 
                      thd->lex->unit.fake_select_lex : &thd->lex->select_lex)))
8011
 
  {
8012
 
    /*
8013
 
      TODO: unlock tables even if the join isn't top level select in the
8014
 
      tree.
8015
 
    */
8016
 
    mysql_unlock_read_tables(thd, lock);           // Don't free join->lock
8017
 
    lock= 0;
8018
 
  }
8019
 
 
8020
 
  return;
8021
 
}
8022
 
 
8023
 
 
8024
 
/**
8025
 
  Free resources of given join.
8026
 
 
8027
 
  @param fill   true if we should free all resources, call with full==1
8028
 
                should be last, before it this function can be called with
8029
 
                full==0
8030
 
 
8031
 
  @note
8032
 
    With subquery this function definitely will be called several times,
8033
 
    but even for simple query it can be called several times.
8034
 
*/
8035
 
 
8036
 
void JOIN::cleanup(bool full)
8037
 
{
8038
 
  if (table)
8039
 
  {
8040
 
    JOIN_TAB *tab,*end;
8041
 
    /*
8042
 
      Only a sorted table may be cached.  This sorted table is always the
8043
 
      first non const table in join->table
8044
 
    */
8045
 
    if (tables > const_tables) // Test for not-const tables
8046
 
    {
8047
 
      free_io_cache(table[const_tables]);
8048
 
      filesort_free_buffers(table[const_tables],full);
8049
 
    }
8050
 
 
8051
 
    if (full)
8052
 
    {
8053
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8054
 
        tab->cleanup();
8055
 
      table= 0;
8056
 
    }
8057
 
    else
8058
 
    {
8059
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8060
 
      {
8061
 
        if (tab->table)
8062
 
          tab->table->file->ha_index_or_rnd_end();
8063
 
      }
8064
 
    }
8065
 
    cleanup_sj_tmp_tables(this);//
8066
 
  }
8067
 
  /*
8068
 
    We are not using tables anymore
8069
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8070
 
  */
8071
 
  if (full)
8072
 
  {
8073
 
    if (tmp_join)
8074
 
      tmp_table_param.copy_field= 0;
8075
 
    group_fields.delete_elements();
8076
 
    /*
8077
 
      We can't call delete_elements() on copy_funcs as this will cause
8078
 
      problems in free_elements() as some of the elements are then deleted.
8079
 
    */
8080
 
    tmp_table_param.copy_funcs.empty();
8081
 
    /*
8082
 
      If we have tmp_join and 'this' JOIN is not tmp_join and
8083
 
      tmp_table_param.copy_field's  of them are equal then we have to remove
8084
 
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
8085
 
      be removed in tmp_table_param.cleanup().
8086
 
    */
8087
 
    if (tmp_join &&
8088
 
        tmp_join != this &&
8089
 
        tmp_join->tmp_table_param.copy_field ==
8090
 
        tmp_table_param.copy_field)
8091
 
    {
8092
 
      tmp_join->tmp_table_param.copy_field=
8093
 
        tmp_join->tmp_table_param.save_copy_field= 0;
8094
 
    }
8095
 
    tmp_table_param.cleanup();
8096
 
  }
8097
 
  return;
8098
 
}
8099
 
 
8100
 
 
8101
 
/**
8102
 
  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:
8103
1492
  Constant expressions @n
8104
1493
  Expression that only uses tables that are of type EQ_REF and the reference
8105
 
  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.
8106
1495
 
8107
1496
  In the following, the X field can be removed:
8108
1497
  @code
8109
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t1.a,t2.X
8110
 
  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
8111
1500
  @endcode
8112
1501
 
8113
1502
  These can't be optimized:
8114
1503
  @code
8115
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a ORDER BY t2.X,t1.a
8116
 
  SELECT * FROM t1,t2 WHERE t1.a=t2.a AND t1.b=t2.b ORDER BY t1.a,t2.c
8117
 
  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
8118
1507
  @endcode
8119
1508
*/
8120
 
 
8121
 
static bool
8122
 
eq_ref_table(JOIN *join, ORDER *start_order, JOIN_TAB *tab)
 
1509
bool eq_ref_table(JOIN *join, order_st *start_order, JoinTable *tab)
8123
1510
{
8124
1511
  if (tab->cached_eq_ref_table)                 // If cached
8125
1512
    return tab->eq_ref_table;
8126
1513
  tab->cached_eq_ref_table=1;
8127
1514
  /* We can skip const tables only if not an outer table */
8128
 
  if (tab->type == JT_CONST && !tab->first_inner)
8129
 
    return (tab->eq_ref_table=1);               /* purecov: inspected */
8130
 
  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)
8131
1518
    return (tab->eq_ref_table=0);               // We must use this
8132
1519
  Item **ref_item=tab->ref.items;
8133
1520
  Item **end=ref_item+tab->ref.key_parts;
8134
 
  uint found=0;
 
1521
  uint32_t found=0;
8135
1522
  table_map map=tab->table->map;
8136
1523
 
8137
1524
  for (; ref_item != end ; ref_item++)
8138
1525
  {
8139
1526
    if (! (*ref_item)->const_item())
8140
1527
    {                                           // Not a const ref
8141
 
      ORDER *order;
 
1528
      order_st *order;
8142
1529
      for (order=start_order ; order ; order=order->next)
8143
1530
      {
8144
 
        if ((*ref_item)->eq(order->item[0],0))
8145
 
          break;
 
1531
        if ((*ref_item)->eq(order->item[0],0))
 
1532
          break;
8146
1533
      }
8147
1534
      if (order)
8148
1535
      {
8149
 
        found++;
8150
 
        assert(!(order->used & map));
8151
 
        order->used|=map;
8152
 
        continue;                               // Used in ORDER BY
 
1536
        found++;
 
1537
        assert(!(order->used & map));
 
1538
        order->used|=map;
 
1539
        continue;                               // Used in order_st BY
8153
1540
      }
8154
1541
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8155
 
        return (tab->eq_ref_table=0);
 
1542
        return (tab->eq_ref_table= 0);
8156
1543
    }
8157
1544
  }
8158
1545
  /* Check that there was no reference to table before sort order */
8164
1551
      continue;
8165
1552
    }
8166
1553
    if (start_order->depend_map & map)
8167
 
      return (tab->eq_ref_table=0);
8168
 
  }
8169
 
  return tab->eq_ref_table=1;
8170
 
}
8171
 
 
8172
 
 
8173
 
static bool
8174
 
only_eq_ref_tables(JOIN *join,ORDER *order,table_map tables)
8175
 
{
8176
 
  if (specialflag &  SPECIAL_SAFE_MODE)
8177
 
    return 0;                   // skip this optimize /* purecov: inspected */
8178
 
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8179
 
  {
8180
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8181
 
      return 0;
8182
 
  }
8183
 
  return 1;
8184
 
}
8185
 
 
8186
 
 
8187
 
/** Update the dependency map for the tables. */
8188
 
 
8189
 
static void update_depend_map(JOIN *join)
8190
 
{
8191
 
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8192
 
 
8193
 
  for (; join_tab != end ; join_tab++)
8194
 
  {
8195
 
    TABLE_REF *ref= &join_tab->ref;
8196
 
    table_map depend_map=0;
8197
 
    Item **item=ref->items;
8198
 
    uint i;
8199
 
    for (i=0 ; i < ref->key_parts ; i++,item++)
8200
 
      depend_map|=(*item)->used_tables();
8201
 
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8202
 
    depend_map&= ~OUTER_REF_TABLE_BIT;
8203
 
    for (JOIN_TAB **tab=join->map2table;
8204
 
         depend_map ;
8205
 
         tab++,depend_map>>=1 )
8206
 
    {
8207
 
      if (depend_map & 1)
8208
 
        ref->depend_map|=(*tab)->ref.depend_map;
8209
 
    }
8210
 
  }
8211
 
}
8212
 
 
8213
 
 
8214
 
/** Update the dependency map for the sort order. */
8215
 
 
8216
 
static void update_depend_map(JOIN *join, ORDER *order)
8217
 
{
8218
 
  for (; order ; order=order->next)
8219
 
  {
8220
 
    table_map depend_map;
8221
 
    order->item[0]->update_used_tables();
8222
 
    order->depend_map=depend_map=order->item[0]->used_tables();
8223
 
    // Not item_sum(), RAND() and no reference to table outside of sub select
8224
 
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8225
 
        && !order->item[0]->with_sum_func)
8226
 
    {
8227
 
      for (JOIN_TAB **tab=join->map2table;
8228
 
           depend_map ;
8229
 
           tab++, depend_map>>=1)
8230
 
      {
8231
 
        if (depend_map & 1)
8232
 
          order->depend_map|=(*tab)->ref.depend_map;
8233
 
      }
8234
 
    }
8235
 
  }
8236
 
}
8237
 
 
8238
 
 
8239
 
/**
8240
 
  Remove all constants and check if ORDER only contains simple
8241
 
  expressions.
8242
 
 
8243
 
  simple_order is set to 1 if sort_order only uses fields from head table
8244
 
  and the head table is not a LEFT JOIN table.
8245
 
 
8246
 
  @param join                   Join handler
8247
 
  @param first_order            List of SORT or GROUP order
8248
 
  @param cond                   WHERE statement
8249
 
  @param change_list            Set to 1 if we should remove things from list.
8250
 
                               If this is not set, then only simple_order is
8251
 
                               calculated.
8252
 
  @param simple_order           Set to 1 if we are only using simple expressions
8253
 
 
8254
 
  @return
8255
 
    Returns new sort order
8256
 
*/
8257
 
 
8258
 
static ORDER *
8259
 
remove_const(JOIN *join,ORDER *first_order, COND *cond,
8260
 
             bool change_list, bool *simple_order)
8261
 
{
8262
 
  if (join->tables == join->const_tables)
8263
 
    return change_list ? 0 : first_order;               // No need to sort
8264
 
 
8265
 
  ORDER *order,**prev_ptr;
8266
 
  table_map first_table= join->join_tab[join->const_tables].table->map;
8267
 
  table_map not_const_tables= ~join->const_table_map;
8268
 
  table_map ref;
8269
 
 
8270
 
  prev_ptr= &first_order;
8271
 
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8272
 
 
8273
 
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8274
 
 
8275
 
  update_depend_map(join, first_order);
8276
 
  for (order=first_order; order ; order=order->next)
8277
 
  {
8278
 
    table_map order_tables=order->item[0]->used_tables();
8279
 
    if (order->item[0]->with_sum_func)
8280
 
      *simple_order=0;                          // Must do a temp table to sort
8281
 
    else if (!(order_tables & not_const_tables))
8282
 
    {
8283
 
      if (order->item[0]->with_subselect)
8284
 
        order->item[0]->val_str(&order->item[0]->str_value);
8285
 
      continue;                                 // skip const item
8286
 
    }
8287
 
    else
8288
 
    {
8289
 
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8290
 
        *simple_order=0;
8291
 
      else
8292
 
      {
8293
 
        Item *comp_item=0;
8294
 
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8295
 
        {
8296
 
          continue;
8297
 
        }
8298
 
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8299
 
        {
8300
 
          if (!(order_tables & first_table) &&
8301
 
              only_eq_ref_tables(join,first_order, ref))
8302
 
          {
8303
 
            continue;
8304
 
          }
8305
 
          *simple_order=0;                      // Must do a temp table to sort
8306
 
        }
8307
 
      }
8308
 
    }
8309
 
    if (change_list)
8310
 
      *prev_ptr= order;                         // use this entry
8311
 
    prev_ptr= &order->next;
8312
 
  }
8313
 
  if (change_list)
8314
 
    *prev_ptr=0;
8315
 
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8316
 
    *simple_order=1;
8317
 
  return(first_order);
8318
 
}
8319
 
 
8320
 
 
8321
 
static int
8322
 
return_zero_rows(JOIN *join, select_result *result,TABLE_LIST *tables,
8323
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
8324
 
                 const char *info, Item *having)
8325
 
{
8326
 
  if (select_options & SELECT_DESCRIBE)
8327
 
  {
8328
 
    select_describe(join, false, false, false, info);
8329
 
    return(0);
8330
 
  }
8331
 
 
8332
 
  join->join_free();
8333
 
 
8334
 
  if (send_row)
8335
 
  {
8336
 
    for (TABLE_LIST *table= tables; table; table= table->next_leaf)
8337
 
      mark_as_null_row(table->table);           // All fields are NULL
8338
 
    if (having && having->val_int() == 0)
8339
 
      send_row=0;
8340
 
  }
8341
 
  if (!(result->send_fields(fields,
8342
 
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8343
 
  {
8344
 
    if (send_row)
8345
 
    {
8346
 
      List_iterator_fast<Item> it(fields);
8347
 
      Item *item;
8348
 
      while ((item= it++))
8349
 
        item->no_rows_in_result();
8350
 
      result->send_data(fields);
8351
 
    }
8352
 
    result->send_eof();                         // Should be safe
8353
 
  }
8354
 
  /* Update results for FOUND_ROWS */
8355
 
  join->thd->limit_found_rows= join->thd->examined_row_count= 0;
8356
 
  return(0);
8357
 
}
8358
 
 
8359
 
/*
8360
 
  used only in JOIN::clear
8361
 
*/
8362
 
static void clear_tables(JOIN *join)
8363
 
{
8364
 
  /* 
8365
 
    must clear only the non-const tables, as const tables
8366
 
    are not re-calculated.
8367
 
  */
8368
 
  for (uint i=join->const_tables ; i < join->tables ; i++)
8369
 
    mark_as_null_row(join->table[i]);           // All fields are NULL
8370
 
}
8371
 
 
8372
 
/*****************************************************************************
8373
 
  Make som simple condition optimization:
8374
 
  If there is a test 'field = const' change all refs to 'field' to 'const'
8375
 
  Remove all dummy tests 'item = item', 'const op const'.
8376
 
  Remove all 'item is NULL', when item can never be null!
8377
 
  item->marker should be 0 for all items on entry
8378
 
  Return in cond_value false if condition is impossible (1 = 2)
8379
 
*****************************************************************************/
8380
 
 
8381
 
class COND_CMP :public ilink {
8382
 
public:
8383
 
  static void *operator new(size_t size)
8384
 
  {
8385
 
    return (void*) sql_alloc((uint) size);
8386
 
  }
8387
 
  static void operator delete(void *ptr __attribute__((unused)),
8388
 
                              size_t size __attribute__((unused)))
8389
 
  { TRASH(ptr, size); }
8390
 
 
8391
 
  Item *and_level;
8392
 
  Item_func *cmp_func;
8393
 
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8394
 
};
8395
 
 
8396
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8397
 
template class I_List<COND_CMP>;
8398
 
template class I_List_iterator<COND_CMP>;
8399
 
#endif
8400
 
 
 
1554
      return (tab->eq_ref_table= 0);
 
1555
  }
 
1556
  return tab->eq_ref_table= 1;
 
1557
}
8401
1558
 
8402
1559
/**
8403
1560
  Find the multiple equality predicate containing a field.
8417
1574
    - Item_equal for the found multiple equality predicate if a success;
8418
1575
    - NULL otherwise.
8419
1576
*/
8420
 
 
8421
 
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8422
 
                            bool *inherited_fl)
 
1577
static Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
8423
1578
{
8424
1579
  Item_equal *item= 0;
8425
1580
  bool in_upper_level= false;
8440
1595
  return item;
8441
1596
}
8442
1597
 
8443
 
  
8444
1598
/**
8445
1599
  Check whether an equality can be used to build multiple equalities.
8446
1600
 
8467
1621
    the check_equality will be called for the following equality
8468
1622
    predicates a=b, b=c, b=2 and f=e.
8469
1623
    - For a=b it will be called with *cond_equal=(0,[]) and will transform
8470
 
      *cond_equal into (0,[Item_equal(a,b)]). 
 
1624
      *cond_equal into (0,[Item_equal(a,b)]).
8471
1625
    - For b=c it will be called with *cond_equal=(0,[Item_equal(a,b)])
8472
1626
      and will transform *cond_equal into CE=(0,[Item_equal(a,b,c)]).
8473
1627
    - For b=2 it will be called with *cond_equal=(ptr(CE),[])
8480
1634
    the Field::eq_def method) are placed to the same multiple equalities.
8481
1635
    Because of this some equality predicates are not eliminated and
8482
1636
    can be used in the constant propagation procedure.
8483
 
    We could weeken the equlity test as soon as at least one of the 
8484
 
    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
8485
1639
    more complicated implementation: we would have to store, in
8486
1640
    general case, its own constant for each fields from the multiple
8487
1641
    equality. But at the same time it would allow us to get rid
8499
1653
    containing just field1 and field2 is added to the existing
8500
1654
    multiple equalities.
8501
1655
    If the function processes the predicate of the form field1=const,
8502
 
    it looks for a multiple equality containing field1. If found, the 
 
1656
    it looks for a multiple equality containing field1. If found, the
8503
1657
    function checks the constant of the multiple equality. If the value
8504
1658
    is unknown, it is setup to const. Otherwise the value is compared with
8505
1659
    const and the evaluation of the equality predicate is performed.
8522
1676
  @retval
8523
1677
    false   otherwise
8524
1678
*/
8525
 
 
8526
 
static bool check_simple_equality(Item *left_item, Item *right_item,
8527
 
                                  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)
8528
1683
{
8529
 
  if (left_item->type() == Item::REF_ITEM &&
8530
 
      ((Item_ref*)left_item)->ref_type() == Item_ref::VIEW_REF)
8531
 
  {
8532
 
    if (((Item_ref*)left_item)->depended_from)
8533
 
      return false;
8534
 
    left_item= left_item->real_item();
8535
 
  }
8536
 
  if (right_item->type() == Item::REF_ITEM &&
8537
 
      ((Item_ref*)right_item)->ref_type() == Item_ref::VIEW_REF)
8538
 
  {
8539
 
    if (((Item_ref*)right_item)->depended_from)
8540
 
      return false;
8541
 
    right_item= right_item->real_item();
8542
 
  }
8543
1684
  if (left_item->type() == Item::FIELD_ITEM &&
8544
1685
      right_item->type() == Item::FIELD_ITEM &&
8545
1686
      !((Item_field*)left_item)->depended_from &&
8557
1698
    bool left_copyfl, right_copyfl;
8558
1699
    Item_equal *left_item_equal=
8559
1700
               find_item_equal(cond_equal, left_field, &left_copyfl);
8560
 
    Item_equal *right_item_equal= 
 
1701
    Item_equal *right_item_equal=
8561
1702
               find_item_equal(cond_equal, right_field, &right_copyfl);
8562
1703
 
8563
1704
    /* As (NULL=NULL) != true we can't just remove the predicate f=f */
8564
1705
    if (left_field->eq(right_field)) /* f = f */
8565
 
      return (!(left_field->maybe_null() && !left_item_equal)); 
 
1706
      return (!(left_field->maybe_null() && !left_item_equal));
8566
1707
 
8567
1708
    if (left_item_equal && left_item_equal == right_item_equal)
8568
1709
    {
8569
 
      /* 
 
1710
      /*
8570
1711
        The equality predicate is inference of one of the existing
8571
1712
        multiple equalities, i.e the condition is already covered
8572
1713
        by upper level equalities
8573
1714
      */
8574
1715
       return true;
8575
1716
    }
8576
 
    
8577
 
    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 &&
8578
1719
                              item->name < subq_sj_cond_name + 64);
8579
1720
    /* Copy the found multiple equalities at the current level if needed */
8580
1721
    if (left_copyfl)
8595
1736
    }
8596
1737
 
8597
1738
    if (left_item_equal)
8598
 
    { 
 
1739
    {
8599
1740
      /* left item was found in the current or one of the upper levels */
8600
1741
      if (! right_item_equal)
8601
1742
        left_item_equal->add((Item_field *) right_item);
8610
1751
      }
8611
1752
    }
8612
1753
    else
8613
 
    { 
 
1754
    {
8614
1755
      /* left item was not found neither the current nor in upper levels  */
8615
1756
      if (right_item_equal)
8616
1757
      {
8618
1759
        if (copy_item_name)
8619
1760
          right_item_equal->name = item->name;
8620
1761
      }
8621
 
      else 
 
1762
      else
8622
1763
      {
8623
1764
        /* None of the fields was found in multiple equalities */
8624
1765
        Item_equal *item_equal= new Item_equal((Item_field *) left_item,
8666
1807
          eq_item->set_cmp_func();
8667
1808
          eq_item->quick_fix_field();
8668
1809
          item= eq_item;
8669
 
        }  
 
1810
        }
8670
1811
        if ((cs != ((Item_func *) item)->compare_collation()) ||
8671
1812
            !cs->coll->propagate(cs, 0, 0))
8672
1813
          return false;
8681
1822
      }
8682
1823
      if (item_equal)
8683
1824
      {
8684
 
        /* 
 
1825
        /*
8685
1826
          The flag cond_false will be set to 1 after this, if item_equal
8686
1827
          already contains a constant and its value is  not equal to
8687
1828
          the value of const_item.
8699
1840
  return false;
8700
1841
}
8701
1842
 
8702
 
 
8703
1843
/**
8704
1844
  Convert row equalities into a conjunction of regular equalities.
8705
1845
 
8712
1852
    simple equality nor a row equality the item for this predicate is added
8713
1853
    to eq_list.
8714
1854
 
8715
 
  @param thd        thread handle
 
1855
  @param session        thread handle
8716
1856
  @param left_row   left term of the row equality to be processed
8717
1857
  @param right_row  right term of the row equality to be processed
8718
1858
  @param cond_equal multiple equalities that must hold together with the
8725
1865
  @retval
8726
1866
    false   otherwise
8727
1867
*/
8728
 
 
8729
 
static bool check_row_equality(THD *thd, Item *left_row, Item_row *right_row,
8730
 
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
8731
 
8732
 
  uint n= left_row->cols();
8733
 
  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++)
8734
1876
  {
8735
1877
    bool is_converted;
8736
1878
    Item *left_item= left_row->element_index(i);
8738
1880
    if (left_item->type() == Item::ROW_ITEM &&
8739
1881
        right_item->type() == Item::ROW_ITEM)
8740
1882
    {
8741
 
      is_converted= check_row_equality(thd, 
 
1883
      is_converted= check_row_equality(session,
8742
1884
                                       (Item_row *) left_item,
8743
1885
                                       (Item_row *) right_item,
8744
1886
                                       cond_equal, eq_list);
8745
1887
      if (!is_converted)
8746
 
        thd->lex->current_select->cond_count++;      
 
1888
        session->lex->current_select->cond_count++;
8747
1889
    }
8748
1890
    else
8749
 
    { 
 
1891
    {
8750
1892
      is_converted= check_simple_equality(left_item, right_item, 0, cond_equal);
8751
 
      thd->lex->current_select->cond_count++;
8752
 
    }  
8753
 
 
 
1893
      session->lex->current_select->cond_count++;
 
1894
    }
 
1895
 
8754
1896
    if (!is_converted)
8755
1897
    {
8756
1898
      Item_func_eq *eq_item;
8764
1906
  return true;
8765
1907
}
8766
1908
 
8767
 
 
8768
1909
/**
8769
1910
  Eliminate row equalities and form multiple equalities predicates.
8770
1911
 
8779
1920
    equalities which are treated in the same way as original equality
8780
1921
    predicates.
8781
1922
 
8782
 
  @param thd        thread handle
 
1923
  @param session        thread handle
8783
1924
  @param item       predicate to process
8784
1925
  @param cond_equal multiple equalities that must hold together with the
8785
1926
                    predicate
8794
1935
           or, if the equality is neither a simple one nor a row equality,
8795
1936
           or, if the procedure fails by a fatal error.
8796
1937
*/
8797
 
 
8798
 
static bool check_equality(THD *thd, Item *item, COND_EQUAL *cond_equal,
8799
 
                           List<Item> *eq_list)
 
1938
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
8800
1939
{
8801
1940
  if (item->type() == Item::FUNC_ITEM &&
8802
1941
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
8807
1946
    if (left_item->type() == Item::ROW_ITEM &&
8808
1947
        right_item->type() == Item::ROW_ITEM)
8809
1948
    {
8810
 
      thd->lex->current_select->cond_count--;
8811
 
      return check_row_equality(thd,
 
1949
      session->lex->current_select->cond_count--;
 
1950
      return check_row_equality(session,
8812
1951
                                (Item_row *) left_item,
8813
1952
                                (Item_row *) right_item,
8814
1953
                                cond_equal, eq_list);
8815
1954
    }
8816
 
    else 
 
1955
    else
8817
1956
      return check_simple_equality(left_item, right_item, item, cond_equal);
8818
 
  } 
 
1957
  }
8819
1958
  return false;
8820
1959
}
8821
1960
 
8822
 
                          
8823
1961
/**
8824
1962
  Replace all equality predicates in a condition by multiple equality items.
8825
1963
 
8826
1964
    At each 'and' level the function detects items for equality predicates
8827
1965
    and replaced them by a set of multiple equality items of class Item_equal,
8828
 
    taking into account inherited equalities from upper levels. 
 
1966
    taking into account inherited equalities from upper levels.
8829
1967
    If an equality predicate is used not in a conjunction it's just
8830
1968
    replaced by a multiple equality predicate.
8831
1969
    For each 'and' level the function set a pointer to the inherited
8832
1970
    multiple equalities in the cond_equal field of the associated
8833
 
    object of the type Item_cond_and.   
 
1971
    object of the type Item_cond_and.
8834
1972
    The function also traverses the cond tree and and for each field reference
8835
1973
    sets a pointer to the multiple equality item containing the field, if there
8836
1974
    is any. If this multiple equality equates fields to a constant the
8837
 
    function replaces the field reference by the constant in the cases 
 
1975
    function replaces the field reference by the constant in the cases
8838
1976
    when the field is not of a string type or when the field reference is
8839
1977
    just an argument of a comparison predicate.
8840
 
    The function also determines the maximum number of members in 
 
1978
    The function also determines the maximum number of members in
8841
1979
    equality lists of each Item_cond_and object assigning it to
8842
 
    thd->lex->current_select->max_equal_elems.
 
1980
    session->lex->current_select->max_equal_elems.
8843
1981
 
8844
1982
  @note
8845
1983
    Multiple equality predicate =(f1,..fn) is equivalent to the conjuction of
8851
1989
    in a conjuction for a minimal set of multiple equality predicates.
8852
1990
    This set can be considered as a canonical representation of the
8853
1991
    sub-conjunction of the equality predicates.
8854
 
    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
8855
1993
    (=(t1.a,t2.b,t3.c) AND t2.b>5), not by
8856
1994
    (=(t1.a,t2.b) AND =(t1.a,t3.c) AND t2.b>5);
8857
1995
    while (t1.a=t2.b AND t2.b>5 AND t3.c=t4.d) is replaced by
8862
2000
    The function performs the substitution in a recursive descent by
8863
2001
    the condtion tree, passing to the next AND level a chain of multiple
8864
2002
    equality predicates which have been built at the upper levels.
8865
 
    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
8866
2004
    non-equality conjucts as a sublist. The pointer to the inherited
8867
2005
    multiple equalities is saved in the and condition object (Item_cond_and).
8868
 
    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
8869
2007
    multiple equality that must be held for this occurence.
8870
2008
    For each AND level we do the following:
8871
2009
    - scan it for all equality predicate (=) items
8872
2010
    - join them into disjoint Item_equal() groups
8873
 
    - process the included OR conditions recursively to do the same for 
8874
 
      lower AND levels. 
 
2011
    - process the included OR conditions recursively to do the same for
 
2012
      lower AND levels.
8875
2013
 
8876
2014
    We need to do things in this order as lower AND levels need to know about
8877
2015
    all possible Item_equal objects in upper levels.
8878
2016
 
8879
 
  @param thd        thread handle
 
2017
  @param session        thread handle
8880
2018
  @param cond       condition(expression) where to make replacement
8881
2019
  @param inherited  path to all inherited multiple equality items
8882
2020
 
8883
2021
  @return
8884
2022
    pointer to the transformed condition
8885
2023
*/
8886
 
 
8887
 
static COND *build_equal_items_for_cond(THD *thd, COND *cond,
8888
 
                                        COND_EQUAL *inherited)
 
2024
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
8889
2025
{
8890
2026
  Item_equal *item_equal;
8891
2027
  COND_EQUAL cond_equal;
8897
2033
    bool and_level= ((Item_cond*) cond)->functype() ==
8898
2034
      Item_func::COND_AND_FUNC;
8899
2035
    List<Item> *args= ((Item_cond*) cond)->argument_list();
8900
 
    
 
2036
 
8901
2037
    List_iterator<Item> li(*args);
8902
2038
    Item *item;
8903
2039
 
8906
2042
      /*
8907
2043
         Retrieve all conjucts of this level detecting the equality
8908
2044
         that are subject to substitution by multiple equality items and
8909
 
         removing each such predicate from the conjunction after having 
 
2045
         removing each such predicate from the conjunction after having
8910
2046
         found/created a multiple equality whose inference the predicate is.
8911
 
     */      
 
2047
     */
8912
2048
      while ((item= li++))
8913
2049
      {
8914
2050
        /*
8916
2052
          structure here because it's restored before each
8917
2053
          re-execution of any prepared statement/stored procedure.
8918
2054
        */
8919
 
        if (check_equality(thd, item, &cond_equal, &eq_list))
 
2055
        if (check_equality(session, item, &cond_equal, &eq_list))
8920
2056
          li.remove();
8921
2057
      }
8922
2058
 
8925
2061
      {
8926
2062
        item_equal->fix_length_and_dec();
8927
2063
        item_equal->update_used_tables();
8928
 
        set_if_bigger(thd->lex->current_select->max_equal_elems,
8929
 
                      item_equal->members());  
 
2064
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
2065
                      item_equal->members());
8930
2066
      }
8931
2067
 
8932
2068
      ((Item_cond_and*)cond)->cond_equal= cond_equal;
8938
2074
    */
8939
2075
    li.rewind();
8940
2076
    while ((item= li++))
8941
 
    { 
 
2077
    {
8942
2078
      Item *new_item;
8943
 
      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)
8944
2080
      {
8945
2081
        /* This replacement happens only for standalone equalities */
8946
2082
        /*
8968
2104
      (b=5) and (a=c) are standalone equalities.
8969
2105
      In general we can't leave alone standalone eqalities:
8970
2106
      for WHERE a=b AND c=d AND (b=c OR d=5)
8971
 
      b=c is replaced by =(a,b,c,d).  
 
2107
      b=c is replaced by =(a,b,c,d).
8972
2108
     */
8973
 
    if (check_equality(thd, cond, &cond_equal, &eq_list))
 
2109
    if (check_equality(session, cond, &cond_equal, &eq_list))
8974
2110
    {
8975
2111
      int n= cond_equal.current_level.elements + eq_list.elements;
8976
2112
      if (n == 0)
8981
2117
        {
8982
2118
          item_equal->fix_length_and_dec();
8983
2119
          item_equal->update_used_tables();
8984
 
        }
 
2120
        }
8985
2121
        else
8986
2122
          item_equal= (Item_equal *) eq_list.pop();
8987
 
        set_if_bigger(thd->lex->current_select->max_equal_elems,
8988
 
                      item_equal->members());  
 
2123
        set_if_bigger(session->lex->current_select->max_equal_elems,
 
2124
                      item_equal->members());
8989
2125
        return item_equal;
8990
2126
      }
8991
2127
      else
8992
2128
      {
8993
 
        /* 
 
2129
        /*
8994
2130
          Here a new AND level must be created. It can happen only
8995
2131
          when a row equality is processed as a standalone predicate.
8996
 
        */
 
2132
        */
8997
2133
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
8998
2134
        and_cond->quick_fix_field();
8999
2135
        List<Item> *args= and_cond->argument_list();
9002
2138
        {
9003
2139
          item_equal->fix_length_and_dec();
9004
2140
          item_equal->update_used_tables();
9005
 
          set_if_bigger(thd->lex->current_select->max_equal_elems,
9006
 
                        item_equal->members());  
 
2141
          set_if_bigger(session->lex->current_select->max_equal_elems,
 
2142
                        item_equal->members());
9007
2143
        }
9008
2144
        and_cond->cond_equal= cond_equal;
9009
2145
        args->concat((List<Item> *)&cond_equal.current_level);
9010
 
        
 
2146
 
9011
2147
        return and_cond;
9012
2148
      }
9013
2149
    }
9014
 
    /* 
 
2150
    /*
9015
2151
      For each field reference in cond, not from equal item predicates,
9016
2152
      set a pointer to the multiple equality it belongs to (if there is any)
9017
2153
      as soon the field is not of a string type or the field reference is
9018
 
      an argument of a comparison predicate. 
9019
 
    */ 
9020
 
    uchar *is_subst_valid= (uchar *) 1;
 
2154
      an argument of a comparison predicate.
 
2155
    */
 
2156
    unsigned char *is_subst_valid= (unsigned char *) 1;
9021
2157
    cond= cond->compile(&Item::subst_argument_checker,
9022
 
                        &is_subst_valid, 
 
2158
                        &is_subst_valid,
9023
2159
                        &Item::equal_fields_propagator,
9024
 
                        (uchar *) inherited);
 
2160
                        (unsigned char *) inherited);
9025
2161
    cond->update_used_tables();
9026
2162
  }
9027
2163
  return cond;
9028
2164
}
9029
2165
 
9030
 
 
9031
2166
/**
9032
2167
  Build multiple equalities for a condition and all on expressions that
9033
2168
  inherit these multiple equalities.
9073
2208
      SELECT * FROM (t1,t2) LEFT JOIN (t3,t4) ON t2.a=t4.a AND t3.a=t4.a
9074
2209
        WHERE t1.a=t2.a
9075
2210
    @endcode
9076
 
    that is equivalent to:   
 
2211
    that is equivalent to:
9077
2212
    @code
9078
2213
      SELECT * FROM (t2 LEFT JOIN (t3,t4)ON t2.a=t4.a AND t3.a=t4.a), t1
9079
2214
        WHERE t1.a=t2.a
9080
2215
    @endcode
9081
2216
    Thus, applying equalities from the where condition we basically
9082
2217
    can get more freedom in performing join operations.
9083
 
    Althogh we don't use this property now, it probably makes sense to use 
9084
 
    it in the future.    
9085
 
  @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
9086
2221
  @param cond                condition to build the multiple equalities for
9087
2222
  @param inherited           path to all inherited multiple equality items
9088
2223
  @param join_list           list of join tables to which the condition
9093
2228
  @return
9094
2229
    pointer to the transformed condition containing multiple equalities
9095
2230
*/
9096
 
   
9097
 
static COND *build_equal_items(THD *thd, COND *cond,
 
2231
static COND *build_equal_items(Session *session, COND *cond,
9098
2232
                               COND_EQUAL *inherited,
9099
 
                               List<TABLE_LIST> *join_list,
 
2233
                               List<TableList> *join_list,
9100
2234
                               COND_EQUAL **cond_equal_ref)
9101
2235
{
9102
2236
  COND_EQUAL *cond_equal= 0;
9103
2237
 
9104
 
  if (cond) 
 
2238
  if (cond)
9105
2239
  {
9106
 
    cond= build_equal_items_for_cond(thd, cond, inherited);
 
2240
    cond= build_equal_items_for_cond(session, cond, inherited);
9107
2241
    cond->update_used_tables();
9108
2242
    if (cond->type() == Item::COND_ITEM &&
9109
2243
        ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
9124
2258
 
9125
2259
  if (join_list)
9126
2260
  {
9127
 
    TABLE_LIST *table;
9128
 
    List_iterator<TABLE_LIST> li(*join_list);
 
2261
    TableList *table;
 
2262
    List_iterator<TableList> li(*join_list);
9129
2263
 
9130
2264
    while ((table= li++))
9131
2265
    {
9132
2266
      if (table->on_expr)
9133
2267
      {
9134
 
        List<TABLE_LIST> *nested_join_list= table->nested_join ?
 
2268
        List<TableList> *nested_join_list= table->nested_join ?
9135
2269
          &table->nested_join->join_list : NULL;
9136
2270
        /*
9137
2271
          We can modify table->on_expr because its old value will
9138
2272
          be restored before re-execution of PS/SP.
9139
2273
        */
9140
 
        table->on_expr= build_equal_items(thd, table->on_expr, inherited,
 
2274
        table->on_expr= build_equal_items(session, table->on_expr, inherited,
9141
2275
                                          nested_join_list,
9142
2276
                                          &table->cond_equal);
9143
2277
      }
9145
2279
  }
9146
2280
 
9147
2281
  return cond;
9148
 
}    
9149
 
 
 
2282
}
9150
2283
 
9151
2284
/**
9152
2285
  Compare field items by table order in the execution plan.
9153
2286
 
9154
2287
    field1 considered as better than field2 if the table containing
9155
 
    field1 is accessed earlier than the table containing field2.   
 
2288
    field1 is accessed earlier than the table containing field2.
9156
2289
    The function finds out what of two fields is better according
9157
2290
    this criteria.
9158
2291
 
9167
2300
  @retval
9168
2301
    0  otherwise
9169
2302
*/
9170
 
 
9171
2303
static int compare_fields_by_table_order(Item_field *field1,
9172
 
                                  Item_field *field2,
9173
 
                                  void *table_join_idx)
 
2304
                                         Item_field *field2,
 
2305
                                         void *table_join_idx)
9174
2306
{
9175
2307
  int cmp= 0;
9176
2308
  bool outer_ref= 0;
9177
2309
  if (field2->used_tables() & OUTER_REF_TABLE_BIT)
9178
 
  {  
 
2310
  {
9179
2311
    outer_ref= 1;
9180
2312
    cmp= -1;
9181
2313
  }
9186
2318
  }
9187
2319
  if (outer_ref)
9188
2320
    return cmp;
9189
 
  JOIN_TAB **idx= (JOIN_TAB **) table_join_idx;
 
2321
  JoinTable **idx= (JoinTable **) table_join_idx;
9190
2322
  cmp= idx[field2->field->table->tablenr]-idx[field1->field->table->tablenr];
9191
2323
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
9192
2324
}
9193
2325
 
9194
 
 
9195
2326
/**
9196
2327
  Generate minimal set of simple equalities equivalent to a multiple equality.
9197
2328
 
9231
2362
    a pointer to the simple generated equality, if success.
9232
2363
    - 0, otherwise.
9233
2364
*/
9234
 
 
9235
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9236
 
                                  Item_equal *item_equal)
 
2365
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
9237
2366
{
9238
2367
  List<Item> eq_list;
9239
2368
  Item_func_eq *eq_item= 0;
9240
2369
  if (((Item *) item_equal)->const_item() && !item_equal->val_int())
9241
 
    return new Item_int((int64_t) 0,1); 
 
2370
    return new Item_int((int64_t) 0,1);
9242
2371
  Item *item_const= item_equal->get_const();
9243
2372
  Item_equal_iterator it(*item_equal);
9244
2373
  Item *head;
9255
2384
    Item_equal *upper= item_field->find_item_equal(upper_levels);
9256
2385
    Item_field *item= item_field;
9257
2386
    if (upper)
9258
 
    { 
 
2387
    {
9259
2388
      if (item_const && upper->get_const())
9260
2389
        item= 0;
9261
2390
      else
9299
2428
 
9300
2429
  cond->quick_fix_field();
9301
2430
  cond->update_used_tables();
9302
 
   
 
2431
 
9303
2432
  return cond;
9304
2433
}
9305
2434
 
9306
 
 
9307
2435
/**
9308
2436
  Substitute every field reference in a condition by the best equal field
9309
2437
  and eliminate all multiple equality predicates.
9312
2440
    multiple equality predicate it sorts the field references in it
9313
2441
    according to the order of tables specified by the table_join_idx
9314
2442
    parameter. Then it eliminates the multiple equality predicate it
9315
 
    replacing it by the conjunction of simple equality predicates 
 
2443
    replacing it by the conjunction of simple equality predicates
9316
2444
    equating every field from the multiple equality to the first
9317
2445
    field in it, or to the constant, if there is any.
9318
2446
    After this the function retrieves all other conjuncted
9331
2459
  @return
9332
2460
    The transformed condition
9333
2461
*/
9334
 
 
9335
 
static COND* substitute_for_best_equal_field(COND *cond,
9336
 
                                             COND_EQUAL *cond_equal,
9337
 
                                             void *table_join_idx)
 
2462
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
9338
2463
{
9339
2464
  Item_equal *item_equal;
9340
2465
 
9349
2474
      cond_equal= &((Item_cond_and *) cond)->cond_equal;
9350
2475
      cond_list->disjoin((List<Item> *) &cond_equal->current_level);
9351
2476
 
9352
 
      List_iterator_fast<Item_equal> it(cond_equal->current_level);      
 
2477
      List_iterator_fast<Item_equal> it(cond_equal->current_level);
9353
2478
      while ((item_equal= it++))
9354
2479
      {
9355
2480
        item_equal->sort(&compare_fields_by_table_order, table_join_idx);
9356
2481
      }
9357
2482
    }
9358
 
    
 
2483
 
9359
2484
    List_iterator<Item> li(*cond_list);
9360
2485
    Item *item;
9361
2486
    while ((item= li++))
9388
2513
      cond= new Item_int((int32_t)cond->val_bool());
9389
2514
 
9390
2515
  }
9391
 
  else if (cond->type() == Item::FUNC_ITEM && 
 
2516
  else if (cond->type() == Item::FUNC_ITEM &&
9392
2517
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9393
2518
  {
9394
2519
    item_equal= (Item_equal *) cond;
9402
2527
  return cond;
9403
2528
}
9404
2529
 
9405
 
 
9406
2530
/**
9407
2531
  Check appearance of new constant items in multiple equalities
9408
2532
  of a condition after reading a constant table.
9415
2539
  @param cond       condition whose multiple equalities are to be checked
9416
2540
  @param table      constant table that has been read
9417
2541
*/
9418
 
 
9419
 
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
 
2542
static void update_const_equal_items(COND *cond, JoinTable *tab)
9420
2543
{
9421
2544
  if (!(cond->used_tables() & tab->table->map))
9422
2545
    return;
9423
2546
 
9424
2547
  if (cond->type() == Item::COND_ITEM)
9425
2548
  {
9426
 
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list(); 
 
2549
    List<Item> *cond_list= ((Item_cond*) cond)->argument_list();
9427
2550
    List_iterator_fast<Item> li(*cond_list);
9428
2551
    Item *item;
9429
2552
    while ((item= li++))
9430
2553
      update_const_equal_items(item, tab);
9431
2554
  }
9432
 
  else if (cond->type() == Item::FUNC_ITEM && 
 
2555
  else if (cond->type() == Item::FUNC_ITEM &&
9433
2556
           ((Item_cond*) cond)->functype() == Item_func::MULT_EQUAL_FUNC)
9434
2557
  {
9435
2558
    Item_equal *item_equal= (Item_equal *) cond;
9443
2566
      while ((item_field= it++))
9444
2567
      {
9445
2568
        Field *field= item_field->field;
9446
 
        JOIN_TAB *stat= field->table->reginfo.join_tab;
 
2569
        JoinTable *stat= field->table->reginfo.join_tab;
9447
2570
        key_map possible_keys= field->key_start;
9448
 
        possible_keys.intersect(field->table->keys_in_use_for_query);
9449
 
        stat[0].const_keys.merge(possible_keys);
 
2571
        possible_keys&= field->table->keys_in_use_for_query;
 
2572
        stat[0].const_keys|= possible_keys;
9450
2573
 
9451
2574
        /*
9452
 
          For each field in the multiple equality (for which we know that it 
9453
 
          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
9454
2577
          that key part in const_key_parts.
9455
 
        */  
9456
 
        if (!possible_keys.is_clear_all())
 
2578
        */
 
2579
        if (possible_keys.any())
9457
2580
        {
9458
 
          TABLE *tab= field->table;
9459
 
          KEYUSE *use;
9460
 
          for (use= stat->keyuse; use && use->table == tab; use++)
9461
 
            if (possible_keys.is_set(use->key) && 
9462
 
                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 ==
9463
2586
                field)
9464
 
              tab->const_key_parts[use->key]|= use->keypart_map;
 
2587
              field_tab->const_key_parts[use->getKey()]|= use->getKeypartMap();
9465
2588
        }
9466
2589
      }
9467
2590
    }
9468
2591
  }
9469
2592
}
9470
2593
 
9471
 
 
9472
2594
/*
9473
2595
  change field = field to field = const for each found field = const in the
9474
2596
  and_level
9475
2597
*/
9476
 
 
9477
 
static void
9478
 
change_cond_ref_to_const(THD *thd, I_List<COND_CMP> *save_list,
9479
 
                         Item *and_father, Item *cond,
9480
 
                         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)
9481
2604
{
9482
2605
  if (cond->type() == Item::COND_ITEM)
9483
2606
  {
9484
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9485
 
      Item_func::COND_AND_FUNC;
 
2607
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
9486
2608
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
9487
2609
    Item *item;
9488
2610
    while ((item=li++))
9489
 
      change_cond_ref_to_const(thd, save_list,and_level ? cond : item, item,
9490
 
                               field, value);
 
2611
      change_cond_ref_to_const(session, save_list, and_level ? cond : item, item, field, value);
9491
2612
    return;
9492
2613
  }
9493
2614
  if (cond->eq_cmp_result() == Item::COND_OK)
9507
2628
  {
9508
2629
    Item *tmp=value->clone_item();
9509
2630
    tmp->collation.set(right_item->collation);
9510
 
    
 
2631
 
9511
2632
    if (tmp)
9512
2633
    {
9513
 
      thd->change_item_tree(args + 1, tmp);
 
2634
      session->change_item_tree(args + 1, tmp);
9514
2635
      func->update_used_tables();
9515
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9516
 
          && 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())
9517
2639
      {
9518
 
        cond->marker=1;
9519
 
        COND_CMP *tmp2;
9520
 
        if ((tmp2=new COND_CMP(and_father,func)))
9521
 
          save_list->push_back(tmp2);
 
2640
        cond->marker=1;
 
2641
        save_list.push_back( COND_CMP(and_father, func) );
9522
2642
      }
9523
2643
      func->set_cmp_func();
9524
2644
    }
9531
2651
  {
9532
2652
    Item *tmp= value->clone_item();
9533
2653
    tmp->collation.set(left_item->collation);
9534
 
    
 
2654
 
9535
2655
    if (tmp)
9536
2656
    {
9537
 
      thd->change_item_tree(args, tmp);
 
2657
      session->change_item_tree(args, tmp);
9538
2658
      value= tmp;
9539
2659
      func->update_used_tables();
9540
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9541
 
          && 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())
9542
2663
      {
9543
2664
        args[0]= args[1];                       // For easy check
9544
 
        thd->change_item_tree(args + 1, value);
9545
 
        cond->marker=1;
9546
 
        COND_CMP *tmp2;
9547
 
        if ((tmp2=new COND_CMP(and_father,func)))
9548
 
          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) );
9549
2668
      }
9550
2669
      func->set_cmp_func();
9551
2670
    }
9560
2679
  @return
9561
2680
    new conditions
9562
2681
*/
9563
 
 
9564
 
static Item *remove_additional_cond(Item* conds)
 
2682
Item *remove_additional_cond(Item* conds)
9565
2683
{
9566
2684
  if (conds->name == in_additional_cond)
9567
2685
    return 0;
9584
2702
  return conds;
9585
2703
}
9586
2704
 
9587
 
static void
9588
 
propagate_cond_constants(THD *thd, I_List<COND_CMP> *save_list,
9589
 
                         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)
9590
2709
{
9591
2710
  if (cond->type() == Item::COND_ITEM)
9592
2711
  {
9593
 
    bool and_level= ((Item_cond*) cond)->functype() ==
9594
 
      Item_func::COND_AND_FUNC;
 
2712
    bool and_level= ((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC;
9595
2713
    List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
9596
2714
    Item *item;
9597
 
    I_List<COND_CMP> save;
 
2715
    vector<COND_CMP> save;
9598
2716
    while ((item=li++))
9599
2717
    {
9600
 
      propagate_cond_constants(thd, &save,and_level ? cond : item, item);
 
2718
      propagate_cond_constants(session, save, and_level ? cond : item, item);
9601
2719
    }
9602
2720
    if (and_level)
9603
 
    {                                           // Handle other found items
9604
 
      I_List_iterator<COND_CMP> cond_itr(save);
9605
 
      COND_CMP *cond_cmp;
9606
 
      while ((cond_cmp=cond_itr++))
 
2721
    {
 
2722
      // Handle other found items
 
2723
      for (vector<COND_CMP>::iterator iter= save.begin(); iter != save.end(); ++iter)
9607
2724
      {
9608
 
        Item **args= cond_cmp->cmp_func->arguments();
 
2725
        Item **args= iter->cmp_func->arguments();
9609
2726
        if (!args[0]->const_item())
9610
 
          change_cond_ref_to_const(thd, &save,cond_cmp->and_level,
9611
 
                                   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
        }
9612
2731
      }
9613
2732
    }
9614
2733
  }
9615
2734
  else if (and_father != cond && !cond->marker)         // In a AND group
9616
2735
  {
9617
2736
    if (cond->type() == Item::FUNC_ITEM &&
9618
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9619
 
         ((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))
9620
2739
    {
9621
2740
      Item_func_eq *func=(Item_func_eq*) cond;
9622
2741
      Item **args= func->arguments();
9625
2744
      if (!(left_const && right_const) &&
9626
2745
          args[0]->result_type() == args[1]->result_type())
9627
2746
      {
9628
 
        if (right_const)
9629
 
        {
9630
 
          resolve_const_item(thd, &args[1], args[0]);
9631
 
          func->update_used_tables();
9632
 
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9633
 
                                   args[0], args[1]);
9634
 
        }
9635
 
        else if (left_const)
9636
 
        {
9637
 
          resolve_const_item(thd, &args[0], args[1]);
9638
 
          func->update_used_tables();
9639
 
          change_cond_ref_to_const(thd, save_list, and_father, and_father,
9640
 
                                   args[1], args[0]);
9641
 
        }
9642
 
      }
9643
 
    }
9644
 
  }
9645
 
}
9646
 
 
9647
 
 
9648
 
/**
9649
 
  Simplify joins replacing outer joins by inner joins whenever it's
9650
 
  possible.
9651
 
 
9652
 
    The function, during a retrieval of join_list,  eliminates those
9653
 
    outer joins that can be converted into inner join, possibly nested.
9654
 
    It also moves the on expressions for the converted outer joins
9655
 
    and from inner joins to conds.
9656
 
    The function also calculates some attributes for nested joins:
9657
 
    - used_tables    
9658
 
    - not_null_tables
9659
 
    - dep_tables.
9660
 
    - on_expr_dep_tables
9661
 
    The first two attributes are used to test whether an outer join can
9662
 
    be substituted for an inner join. The third attribute represents the
9663
 
    relation 'to be dependent on' for tables. If table t2 is dependent
9664
 
    on table t1, then in any evaluated execution plan table access to
9665
 
    table t2 must precede access to table t2. This relation is used also
9666
 
    to check whether the query contains  invalid cross-references.
9667
 
    The forth attribute is an auxiliary one and is used to calculate
9668
 
    dep_tables.
9669
 
    As the attribute dep_tables qualifies possibles orders of tables in the
9670
 
    execution plan, the dependencies required by the straight join
9671
 
    modifiers are reflected in this attribute as well.
9672
 
    The function also removes all braces that can be removed from the join
9673
 
    expression without changing its meaning.
9674
 
 
9675
 
  @note
9676
 
    An outer join can be replaced by an inner join if the where condition
9677
 
    or the on expression for an embedding nested join contains a conjunctive
9678
 
    predicate rejecting null values for some attribute of the inner tables.
9679
 
 
9680
 
    E.g. in the query:    
9681
 
    @code
9682
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9683
 
    @endcode
9684
 
    the predicate t2.b < 5 rejects nulls.
9685
 
    The query is converted first to:
9686
 
    @code
9687
 
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9688
 
    @endcode
9689
 
    then to the equivalent form:
9690
 
    @code
9691
 
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9692
 
    @endcode
9693
 
 
9694
 
 
9695
 
    Similarly the following query:
9696
 
    @code
9697
 
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9698
 
        WHERE t2.c < 5  
9699
 
    @endcode
9700
 
    is converted to:
9701
 
    @code
9702
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b 
9703
 
 
9704
 
    @endcode
9705
 
 
9706
 
    One conversion might trigger another:
9707
 
    @code
9708
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9709
 
                       LEFT JOIN t3 ON t3.b=t2.b
9710
 
        WHERE t3 IS NOT NULL =>
9711
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9712
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b => 
9713
 
      SELECT * FROM t1, t2, t3
9714
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9715
 
  @endcode
9716
 
 
9717
 
    The function removes all unnecessary braces from the expression
9718
 
    produced by the conversions.
9719
 
    E.g.
9720
 
    @code
9721
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9722
 
    @endcode
9723
 
    finally is converted to: 
9724
 
    @code
9725
 
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9726
 
 
9727
 
    @endcode
9728
 
 
9729
 
 
9730
 
    It also will remove braces from the following queries:
9731
 
    @code
9732
 
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9733
 
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9734
 
    @endcode
9735
 
 
9736
 
    The benefit of this simplification procedure is that it might return 
9737
 
    a query for which the optimizer can evaluate execution plan with more
9738
 
    join orders. With a left join operation the optimizer does not
9739
 
    consider any plan where one of the inner tables is before some of outer
9740
 
    tables.
9741
 
 
9742
 
  IMPLEMENTATION
9743
 
    The function is implemented by a recursive procedure.  On the recursive
9744
 
    ascent all attributes are calculated, all outer joins that can be
9745
 
    converted are replaced and then all unnecessary braces are removed.
9746
 
    As join list contains join tables in the reverse order sequential
9747
 
    elimination of outer joins does not require extra recursive calls.
9748
 
 
9749
 
  SEMI-JOIN NOTES
9750
 
    Remove all semi-joins that have are within another semi-join (i.e. have
9751
 
    an "ancestor" semi-join nest)
9752
 
 
9753
 
  EXAMPLES
9754
 
    Here is an example of a join query with invalid cross references:
9755
 
    @code
9756
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b 
9757
 
    @endcode
9758
 
 
9759
 
  @param join        reference to the query info
9760
 
  @param join_list   list representation of the join to be converted
9761
 
  @param conds       conditions to add on expressions for converted joins
9762
 
  @param top         true <=> conds is the where condition
9763
 
 
9764
 
  @return
9765
 
    - The new condition, if success
9766
 
    - 0, otherwise
9767
 
*/
9768
 
 
9769
 
static COND *
9770
 
simplify_joins(JOIN *join, List<TABLE_LIST> *join_list, COND *conds, bool top,
9771
 
               bool in_sj)
9772
 
{
9773
 
  TABLE_LIST *table;
9774
 
  NESTED_JOIN *nested_join;
9775
 
  TABLE_LIST *prev_table= 0;
9776
 
  List_iterator<TABLE_LIST> li(*join_list);
9777
 
 
9778
 
  /* 
9779
 
    Try to simplify join operations from join_list.
9780
 
    The most outer join operation is checked for conversion first. 
9781
 
  */
9782
 
  while ((table= li++))
9783
 
  {
9784
 
    table_map used_tables;
9785
 
    table_map not_null_tables= (table_map) 0;
9786
 
 
9787
 
    if ((nested_join= table->nested_join))
9788
 
    {
9789
 
      /* 
9790
 
         If the element of join_list is a nested join apply
9791
 
         the procedure to its nested join list first.
9792
 
      */
9793
 
      if (table->on_expr)
9794
 
      {
9795
 
        Item *expr= table->on_expr;
9796
 
        /* 
9797
 
           If an on expression E is attached to the table, 
9798
 
           check all null rejected predicates in this expression.
9799
 
           If such a predicate over an attribute belonging to
9800
 
           an inner table of an embedded outer join is found,
9801
 
           the outer join is converted to an inner join and
9802
 
           the corresponding on expression is added to E. 
9803
 
        */ 
9804
 
        expr= simplify_joins(join, &nested_join->join_list,
9805
 
                             expr, false, in_sj || table->sj_on_expr);
9806
 
 
9807
 
        if (!table->prep_on_expr || expr != table->on_expr)
9808
 
        {
9809
 
          assert(expr);
9810
 
 
9811
 
          table->on_expr= expr;
9812
 
          table->prep_on_expr= expr->copy_andor_structure(join->thd);
9813
 
        }
9814
 
      }
9815
 
      nested_join->used_tables= (table_map) 0;
9816
 
      nested_join->not_null_tables=(table_map) 0;
9817
 
      conds= simplify_joins(join, &nested_join->join_list, conds, top, 
9818
 
                            in_sj || table->sj_on_expr);
9819
 
      used_tables= nested_join->used_tables;
9820
 
      not_null_tables= nested_join->not_null_tables;  
9821
 
    }
9822
 
    else
9823
 
    {
9824
 
      if (!table->prep_on_expr)
9825
 
        table->prep_on_expr= table->on_expr;
9826
 
      used_tables= table->table->map;
9827
 
      if (conds)
9828
 
        not_null_tables= conds->not_null_tables();
9829
 
    }
9830
 
      
9831
 
    if (table->embedding)
9832
 
    {
9833
 
      table->embedding->nested_join->used_tables|= used_tables;
9834
 
      table->embedding->nested_join->not_null_tables|= not_null_tables;
9835
 
    }
9836
 
 
9837
 
    if (!table->outer_join || (used_tables & not_null_tables))
9838
 
    {
9839
 
      /* 
9840
 
        For some of the inner tables there are conjunctive predicates
9841
 
        that reject nulls => the outer join can be replaced by an inner join.
9842
 
      */
9843
 
      table->outer_join= 0;
9844
 
      if (table->on_expr)
9845
 
      {
9846
 
        /* Add ON expression to the WHERE or upper-level ON condition. */
9847
 
        if (conds)
9848
 
        {
9849
 
          conds= and_conds(conds, table->on_expr);
9850
 
          conds->top_level_item();
9851
 
          /* conds is always a new item as both cond and on_expr existed */
9852
 
          assert(!conds->fixed);
9853
 
          conds->fix_fields(join->thd, &conds);
9854
 
        }
9855
 
        else
9856
 
          conds= table->on_expr; 
9857
 
        table->prep_on_expr= table->on_expr= 0;
9858
 
      }
9859
 
    }
9860
 
    
9861
 
    if (!top)
9862
 
      continue;
9863
 
 
9864
 
    /* 
9865
 
      Only inner tables of non-convertible outer joins
9866
 
      remain with on_expr.
9867
 
    */ 
9868
 
    if (table->on_expr)
9869
 
    {
9870
 
      table->dep_tables|= table->on_expr->used_tables(); 
9871
 
      if (table->embedding)
9872
 
      {
9873
 
        table->dep_tables&= ~table->embedding->nested_join->used_tables;   
9874
 
        /*
9875
 
           Embedding table depends on tables used
9876
 
           in embedded on expressions. 
9877
 
        */
9878
 
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9879
 
      }
9880
 
      else
9881
 
        table->dep_tables&= ~table->table->map;
9882
 
    }
9883
 
 
9884
 
    if (prev_table)
9885
 
    {
9886
 
      /* The order of tables is reverse: prev_table follows table */
9887
 
      if (prev_table->straight)
9888
 
        prev_table->dep_tables|= used_tables;
9889
 
      if (prev_table->on_expr)
9890
 
      {
9891
 
        prev_table->dep_tables|= table->on_expr_dep_tables;
9892
 
        table_map prev_used_tables= prev_table->nested_join ?
9893
 
                                    prev_table->nested_join->used_tables :
9894
 
                                    prev_table->table->map;
9895
 
        /* 
9896
 
          If on expression contains only references to inner tables
9897
 
          we still make the inner tables dependent on the outer tables.
9898
 
          It would be enough to set dependency only on one outer table
9899
 
          for them. Yet this is really a rare case.
9900
 
        */  
9901
 
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9902
 
          prev_table->dep_tables|= used_tables;
9903
 
      }
9904
 
    }
9905
 
    prev_table= table;
9906
 
  }
9907
 
    
9908
 
  /* 
9909
 
    Flatten nested joins that can be flattened.
9910
 
    no ON expression and not a semi-join => can be flattened.
9911
 
  */
9912
 
  li.rewind();
9913
 
  while ((table= li++))
9914
 
  {
9915
 
    nested_join= table->nested_join;
9916
 
    if (table->sj_on_expr && !in_sj)
9917
 
    {
9918
 
       /*
9919
 
         If this is a semi-join that is not contained within another semi-join, 
9920
 
         leave it intact (otherwise it is flattened)
9921
 
       */
9922
 
      join->select_lex->sj_nests.push_back(table);
9923
 
    }
9924
 
    else if (nested_join && !table->on_expr)
9925
 
    {
9926
 
      TABLE_LIST *tbl;
9927
 
      List_iterator<TABLE_LIST> it(nested_join->join_list);
9928
 
      while ((tbl= it++))
9929
 
      {
9930
 
        tbl->embedding= table->embedding;
9931
 
        tbl->join_list= table->join_list;
9932
 
      }      
9933
 
      li.replace(nested_join->join_list);
9934
 
    }
9935
 
  }
9936
 
  return(conds); 
9937
 
}
9938
 
 
9939
 
 
9940
 
/**
9941
 
  Assign each nested join structure a bit in nested_join_map.
9942
 
 
9943
 
    Assign each nested join structure (except "confluent" ones - those that
9944
 
    embed only one element) a bit in nested_join_map.
9945
 
 
9946
 
  @param join          Join being processed
9947
 
  @param join_list     List of tables
9948
 
  @param first_unused  Number of first unused bit in nested_join_map before the
9949
 
                       call
9950
 
 
9951
 
  @note
9952
 
    This function is called after simplify_joins(), when there are no
9953
 
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9954
 
    we will not run out of bits in nested_join_map.
9955
 
 
9956
 
  @return
9957
 
    First unused bit in nested_join_map after the call.
9958
 
*/
9959
 
 
9960
 
static uint build_bitmap_for_nested_joins(List<TABLE_LIST> *join_list, 
9961
 
                                          uint first_unused)
9962
 
{
9963
 
  List_iterator<TABLE_LIST> li(*join_list);
9964
 
  TABLE_LIST *table;
9965
 
  while ((table= li++))
9966
 
  {
9967
 
    NESTED_JOIN *nested_join;
9968
 
    if ((nested_join= table->nested_join))
9969
 
    {
9970
 
      /*
9971
 
        It is guaranteed by simplify_joins() function that a nested join
9972
 
        that has only one child is either
9973
 
         - a single-table view (the child is the underlying table), or 
9974
 
         - a single-table semi-join nest
9975
 
 
9976
 
        We don't assign bits to such sj-nests because 
9977
 
        1. it is redundant (a "sequence" of one table cannot be interleaved 
9978
 
            with anything)
9979
 
        2. we could run out bits in nested_join_map otherwise.
9980
 
      */
9981
 
      if (nested_join->join_list.elements != 1)
9982
 
      {
9983
 
        /* Don't assign bits to sj-nests */
9984
 
        if (table->on_expr)
9985
 
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9986
 
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9987
 
                                                    first_unused);
9988
 
      }
9989
 
    }
9990
 
  }
9991
 
  return(first_unused);
9992
 
}
9993
 
 
9994
 
 
9995
 
/**
9996
 
  Set NESTED_JOIN::counter=0 in all nested joins in passed list.
9997
 
 
9998
 
    Recursively set NESTED_JOIN::counter=0 for all nested joins contained in
9999
 
    the passed join_list.
10000
 
 
10001
 
  @param join_list  List of nested joins to process. It may also contain base
10002
 
                    tables which will be ignored.
10003
 
*/
10004
 
 
10005
 
static void reset_nj_counters(List<TABLE_LIST> *join_list)
10006
 
{
10007
 
  List_iterator<TABLE_LIST> li(*join_list);
10008
 
  TABLE_LIST *table;
10009
 
  while ((table= li++))
10010
 
  {
10011
 
    NESTED_JOIN *nested_join;
10012
 
    if ((nested_join= table->nested_join))
10013
 
    {
10014
 
      nested_join->counter_= 0;
10015
 
      reset_nj_counters(&nested_join->join_list);
10016
 
    }
10017
 
  }
10018
 
  return;
10019
 
}
10020
 
 
 
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
}
10021
2765
 
10022
2766
/**
10023
2767
  Check interleaving with an inner tables of an outer join for
10024
2768
  extension table.
10025
2769
 
10026
 
    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
10027
2771
    if yes, record that it has been added.
10028
2772
 
10029
2773
    The function assumes that both current partial join order and its
10030
2774
    extension with next_tab are valid wrt table dependencies.
10031
2775
 
10032
2776
  @verbatim
10033
 
     IMPLEMENTATION 
10034
 
       LIMITATIONS ON JOIN ORDER
 
2777
     IMPLEMENTATION
 
2778
       LIMITATIONS ON JOIN order_st
10035
2779
         The nested [outer] joins executioner algorithm imposes these limitations
10036
2780
         on join order:
10037
 
         1. "Outer tables first" -  any "outer" table must be before any 
 
2781
         1. "Outer tables first" -  any "outer" table must be before any
10038
2782
             corresponding "inner" table.
10039
2783
         2. "No interleaving" - tables inside a nested join must form a continuous
10040
 
            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
10041
2785
            tables that are outside of this nested join).
10042
2786
 
10043
2787
         #1 is checked elsewhere, this function checks #2 provided that #1 has
10044
2788
         been already checked.
10045
2789
 
10046
2790
       WHY NEED NON-INTERLEAVING
10047
 
         Consider an example: 
 
2791
         Consider an example:
10048
2792
 
10049
2793
           select * from t0 join t1 left join (t2 join t3) on cond1
10050
2794
 
10068
2812
         The limitations on join order can be rephrased as follows: for valid
10069
2813
         join order one must be able to:
10070
2814
           1. write down the used tables in the join order on one line.
10071
 
           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
10072
2816
           3. write "LEFT JOIN" and "ON (...)" where appropriate
10073
2817
           4. get a query equivalent to the query we're trying to execute.
10074
2818
 
10075
2819
         Calls to check_interleaving_with_nj() are equivalent to writing the
10076
 
         above described line from left to right. 
10077
 
         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
10078
2822
         table B and appropriate brackets on condition that table A and
10079
2823
         appropriate brackets is the last what was written. Graphically the
10080
2824
         transition is as follows:
10087
2831
                                                      position.
10088
2832
 
10089
2833
         Notes about the position:
10090
 
           The caller guarantees that there is no more then one X-bracket by 
10091
 
           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
10092
2836
           function. X-bracket may have a pair in Y-bracket.
10093
2837
 
10094
2838
         When "writing" we store/update this auxilary info about the current
10095
2839
         position:
10096
2840
          1. join->cur_embedding_map - bitmap of pairs of brackets (aka nested
10097
2841
             joins) we've opened but didn't close.
10098
 
          2. {each NESTED_JOIN structure not simplified away}->counter - number
 
2842
          2. {each nested_join_st structure not simplified away}->counter - number
10099
2843
             of this nested join's children that have already been added to to
10100
2844
             the partial join order.
10101
2845
  @endverbatim
10111
2855
  @retval
10112
2856
    true   Requested join order extension not allowed.
10113
2857
*/
10114
 
 
10115
 
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)
10116
2859
{
10117
 
  TABLE_LIST *next_emb= next_tab->table->pos_in_table_list->embedding;
 
2860
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
10118
2861
  JOIN *join= last_tab->join;
10119
2862
 
10120
 
  if (join->cur_embedding_map & ~next_tab->embedding_map)
 
2863
  if ((join->cur_embedding_map & ~next_tab->embedding_map).any())
10121
2864
  {
10122
 
    /* 
 
2865
    /*
10123
2866
      next_tab is outside of the "pair of brackets" we're currently in.
10124
2867
      Cannot add it.
10125
2868
    */
10126
2869
    return true;
10127
2870
  }
10128
 
   
 
2871
 
10129
2872
  /*
10130
2873
    Do update counters for "pairs of brackets" that we've left (marked as
10131
2874
    X,Y,Z in the above picture)
10135
2878
    next_emb->nested_join->counter_++;
10136
2879
    if (next_emb->nested_join->counter_ == 1)
10137
2880
    {
10138
 
      /* 
 
2881
      /*
10139
2882
        next_emb is the first table inside a nested join we've "entered". In
10140
2883
        the picture above, we're looking at the 'X' bracket. Don't exit yet as
10141
2884
        X bracket might have Y pair bracket.
10142
2885
      */
10143
2886
      join->cur_embedding_map |= next_emb->nested_join->nj_map;
10144
2887
    }
10145
 
    
 
2888
 
10146
2889
    if (next_emb->nested_join->join_list.elements !=
10147
2890
        next_emb->nested_join->counter_)
10148
2891
      break;
10156
2899
  return false;
10157
2900
}
10158
2901
 
10159
 
 
10160
 
/**
10161
 
  Nested joins perspective: Remove the last table from the join order.
10162
 
 
10163
 
    Remove the last table from the partial join order and update the nested
10164
 
    joins counters and join->cur_embedding_map. It is ok to call this 
10165
 
    function for the first table in join order (for which 
10166
 
    check_interleaving_with_nj has not been called)
10167
 
 
10168
 
  @param last  join table to remove, it is assumed to be the last in current
10169
 
               partial join order.
10170
 
*/
10171
 
 
10172
 
static void restore_prev_nj_state(JOIN_TAB *last)
10173
 
{
10174
 
  TABLE_LIST *last_emb= last->table->pos_in_table_list->embedding;
10175
 
  JOIN *join= last->join;
10176
 
  while (last_emb)
10177
 
  {
10178
 
    if (last_emb->on_expr)
10179
 
    {
10180
 
      if (!(--last_emb->nested_join->counter_))
10181
 
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10182
 
      else if (last_emb->nested_join->join_list.elements-1 ==
10183
 
               last_emb->nested_join->counter_) 
10184
 
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
10185
 
      else
10186
 
        break;
10187
 
    }
10188
 
    last_emb= last_emb->embedding;
10189
 
  }
10190
 
}
10191
 
 
10192
 
 
10193
 
 
10194
 
static 
10195
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10196
 
{
10197
 
  TABLE_LIST *emb_sj_nest;
10198
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10199
 
  {
10200
 
    tab->join->cur_emb_sj_nests |= emb_sj_nest->sj_inner_tables;
10201
 
    /* Remove the sj_nest if all of its SJ-inner tables are in cur_table_map */
10202
 
    if (!(remaining_tables & emb_sj_nest->sj_inner_tables))
10203
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10204
 
  }
10205
 
}
10206
 
 
10207
 
 
10208
 
/*
10209
 
  we assume remaining_tables doesnt contain @tab.
10210
 
*/
10211
 
 
10212
 
static void restore_prev_sj_state(const table_map remaining_tables, 
10213
 
                                  const JOIN_TAB *tab)
10214
 
{
10215
 
  TABLE_LIST *emb_sj_nest;
10216
 
  if ((emb_sj_nest= tab->emb_sj_nest))
10217
 
  {
10218
 
    /* If we're removing the last SJ-inner table, remove the sj-nest */
10219
 
    if ((remaining_tables & emb_sj_nest->sj_inner_tables) == 
10220
 
        (emb_sj_nest->sj_inner_tables & ~tab->table->map))
10221
 
    {
10222
 
      tab->join->cur_emb_sj_nests &= ~emb_sj_nest->sj_inner_tables;
10223
 
    }
10224
 
  }
10225
 
}
10226
 
 
10227
 
 
10228
 
static COND *
10229
 
optimize_cond(JOIN *join, COND *conds, List<TABLE_LIST> *join_list,
10230
 
              Item::cond_result *cond_value)
10231
 
{
10232
 
  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;
10233
2905
 
10234
2906
  if (!conds)
10235
2907
    *cond_value= Item::COND_TRUE;
10236
2908
  else
10237
2909
  {
10238
 
    /* 
 
2910
    /*
10239
2911
      Build all multiple equality predicates and eliminate equality
10240
2912
      predicates that can be inferred from these multiple equalities.
10241
2913
      For each reference of a field included into a multiple equality
10242
2914
      that occurs in a function set a pointer to the multiple equality
10243
2915
      predicate. Substitute a constant instead of this field if the
10244
2916
      multiple equality contains a constant.
10245
 
    */ 
10246
 
    conds= build_equal_items(join->thd, conds, NULL, join_list,
 
2917
    */
 
2918
    conds= build_equal_items(join->session, conds, NULL, join_list,
10247
2919
                             &join->cond_equal);
10248
2920
 
10249
2921
    /* change field = field to field = const for each found field = const */
10250
 
    propagate_cond_constants(thd, (I_List<COND_CMP> *) 0, conds, conds);
 
2922
    vector<COND_CMP> temp;
 
2923
    propagate_cond_constants(session, temp, conds, conds);
10251
2924
    /*
10252
2925
      Remove all instances of item == item
10253
2926
      Remove all and-levels where CONST item != CONST item
10254
2927
    */
10255
 
    conds= remove_eq_conds(thd, conds, cond_value) ;
 
2928
    conds= remove_eq_conds(session, conds, cond_value) ;
10256
2929
  }
10257
2930
  return(conds);
10258
2931
}
10259
2932
 
10260
 
 
10261
2933
/**
10262
2934
  Remove const and eq items.
10263
2935
 
10268
2940
    - COND_TRUE   : always true ( 1 = 1 )
10269
2941
    - COND_FALSE  : always false        ( 1 = 2 )
10270
2942
*/
10271
 
 
10272
 
COND *
10273
 
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)
10274
2944
{
10275
2945
  if (cond->type() == Item::COND_ITEM)
10276
2946
  {
10277
 
    bool and_level= ((Item_cond*) cond)->functype()
10278
 
      == Item_func::COND_AND_FUNC;
 
2947
    bool and_level= (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC);
 
2948
 
10279
2949
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
10280
2950
    Item::cond_result tmp_cond_value;
10281
 
    bool should_fix_fields=0;
 
2951
    bool should_fix_fields= false;
10282
2952
 
10283
 
    *cond_value=Item::COND_UNDEF;
 
2953
    *cond_value= Item::COND_UNDEF;
10284
2954
    Item *item;
10285
 
    while ((item=li++))
 
2955
    while ((item= li++))
10286
2956
    {
10287
 
      Item *new_item=remove_eq_conds(thd, item, &tmp_cond_value);
10288
 
      if (!new_item)
10289
 
        li.remove();
 
2957
      Item *new_item= remove_eq_conds(session, item, &tmp_cond_value);
 
2958
      if (! new_item)
 
2959
              li.remove();
10290
2960
      else if (item != new_item)
10291
2961
      {
10292
 
        VOID(li.replace(new_item));
10293
 
        should_fix_fields=1;
 
2962
        li.replace(new_item);
 
2963
        should_fix_fields= true;
10294
2964
      }
10295
2965
      if (*cond_value == Item::COND_UNDEF)
10296
 
        *cond_value=tmp_cond_value;
10297
 
      switch (tmp_cond_value) {
10298
 
      case Item::COND_OK:                       // Not true or false
10299
 
        if (and_level || *cond_value == Item::COND_FALSE)
10300
 
          *cond_value=tmp_cond_value;
10301
 
        break;
10302
 
      case Item::COND_FALSE:
10303
 
        if (and_level)
10304
 
        {
10305
 
          *cond_value=tmp_cond_value;
10306
 
          return (COND*) 0;                     // Always false
10307
 
        }
10308
 
        break;
10309
 
      case Item::COND_TRUE:
10310
 
        if (!and_level)
10311
 
        {
10312
 
          *cond_value= tmp_cond_value;
10313
 
          return (COND*) 0;                     // Always true
10314
 
        }
10315
 
        break;
10316
 
      case Item::COND_UNDEF:                    // Impossible
10317
 
        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;
10318
2990
      }
10319
2991
    }
 
2992
 
10320
2993
    if (should_fix_fields)
10321
2994
      cond->update_used_tables();
10322
2995
 
10323
 
    if (!((Item_cond*) cond)->argument_list()->elements ||
10324
 
        *cond_value != Item::COND_OK)
10325
 
      return (COND*) 0;
 
2996
    if (! ((Item_cond*) cond)->argument_list()->elements || *cond_value != Item::COND_OK)
 
2997
      return (COND*) NULL;
 
2998
 
10326
2999
    if (((Item_cond*) cond)->argument_list()->elements == 1)
10327
 
    {                                           // Remove list
 
3000
    {                                           
 
3001
      /* Argument list contains only one element, so reduce it so a single item, then remove list */
10328
3002
      item= ((Item_cond*) cond)->argument_list()->head();
10329
3003
      ((Item_cond*) cond)->argument_list()->empty();
10330
3004
      return item;
10331
3005
    }
10332
3006
  }
10333
 
  else if (cond->type() == Item::FUNC_ITEM &&
10334
 
           ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
 
3007
  else if (cond->type() == Item::FUNC_ITEM && ((Item_func*) cond)->functype() == Item_func::ISNULL_FUNC)
10335
3008
  {
10336
3009
    /*
10337
3010
      Handles this special case for some ODBC applications:
10343
3016
      SELECT * from table_name where auto_increment_column = LAST_INSERT_ID
10344
3017
    */
10345
3018
 
10346
 
    Item_func_isnull *func=(Item_func_isnull*) cond;
 
3019
    Item_func_isnull *func= (Item_func_isnull*) cond;
10347
3020
    Item **args= func->arguments();
10348
3021
    if (args[0]->type() == Item::FIELD_ITEM)
10349
3022
    {
10350
 
      Field *field=((Item_field*) args[0])->field;
10351
 
      if (field->flags & AUTO_INCREMENT_FLAG && !field->table->maybe_null &&
10352
 
          (thd->options & OPTION_AUTO_IS_NULL) &&
10353
 
          (thd->first_successful_insert_id_in_prev_stmt > 0 &&
10354
 
           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
          )
10355
3032
      {
10356
 
        COND *new_cond;
10357
 
        if ((new_cond= new Item_func_eq(args[0],
10358
 
                                        new Item_int("last_insert_id()",
10359
 
                                                     thd->read_first_successful_insert_id_in_prev_stmt(),
10360
 
                                                     MY_INT64_NUM_DECIMAL_DIGITS))))
10361
 
        {
10362
 
          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;
10363
3039
          /*
10364
3040
            Item_func_eq can't be fixed after creation so we do not check
10365
3041
            cond->fixed, also it do not need tables so we use 0 as second
10366
3042
            argument.
10367
3043
          */
10368
 
          cond->fix_fields(thd, &cond);
10369
 
        }
 
3044
          cond->fix_fields(session, &cond);
 
3045
        }
10370
3046
        /*
10371
3047
          IS NULL should be mapped to LAST_INSERT_ID only for first row, so
10372
3048
          clear for next row
10373
3049
        */
10374
 
        thd->substitute_null_with_insert_id= false;
 
3050
        session->substitute_null_with_insert_id= false;
10375
3051
      }
 
3052
#ifdef NOTDEFINED
10376
3053
      /* fix to replace 'NULL' dates with '0' (shreeve@uci.edu) */
10377
 
      else if (((field->type() == DRIZZLE_TYPE_NEWDATE) ||
10378
 
                (field->type() == DRIZZLE_TYPE_DATETIME)) &&
10379
 
                (field->flags & NOT_NULL_FLAG) &&
10380
 
               !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)
10381
3058
      {
10382
 
        COND *new_cond;
10383
 
        if ((new_cond= new Item_func_eq(args[0],new Item_int("0", 0, 2))))
10384
 
        {
10385
 
          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;
10386
3063
          /*
10387
3064
            Item_func_eq can't be fixed after creation so we do not check
10388
3065
            cond->fixed, also it do not need tables so we use 0 as second
10389
3066
            argument.
10390
3067
          */
10391
 
          cond->fix_fields(thd, &cond);
10392
 
        }
 
3068
          cond->fix_fields(session, &cond);
 
3069
        }
10393
3070
      }
 
3071
#endif /* NOTDEFINED */
10394
3072
    }
10395
3073
    if (cond->const_item())
10396
3074
    {
10397
3075
      *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10398
 
      return (COND*) 0;
 
3076
      return (COND *) NULL;
10399
3077
    }
10400
3078
  }
10401
3079
  else if (cond->const_item() && !cond->is_expensive())
10411
3089
  */
10412
3090
  {
10413
3091
    *cond_value= eval_const_cond(cond) ? Item::COND_TRUE : Item::COND_FALSE;
10414
 
    return (COND*) 0;
 
3092
    return (COND *) NULL;
10415
3093
  }
10416
3094
  else if ((*cond_value= cond->eq_cmp_result()) != Item::COND_OK)
10417
 
  {                                             // boolan compare function
 
3095
  {                                             
 
3096
    /* boolan compare function */
10418
3097
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10419
3098
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10420
3099
    if (left_item->eq(right_item,1))
10421
3100
    {
10422
 
      if (!left_item->maybe_null ||
10423
 
          ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC)
10424
 
        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 */
10425
3103
    }
10426
3104
  }
10427
 
  *cond_value=Item::COND_OK;
10428
 
  return cond;                                  // Point at next and level
 
3105
  *cond_value= Item::COND_OK;
 
3106
  return cond;                                  /* Point at next and return into recursion */
10429
3107
}
10430
3108
 
10431
 
/* 
 
3109
/*
10432
3110
  Check if equality can be used in removing components of GROUP BY/DISTINCT
10433
 
  
 
3111
 
10434
3112
  SYNOPSIS
10435
3113
    test_if_equality_guarantees_uniqueness()
10436
3114
      l          the left comparison argument (a field if any)
10437
3115
      r          the right comparison argument (a const of any)
10438
 
  
10439
 
  DESCRIPTION    
10440
 
    Checks if an equality predicate can be used to take away 
10441
 
    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
10442
3120
    distinct value (e.g. <expr> == <const>).
10443
 
    Arguments must be of the same type because e.g. 
10444
 
    <string_field> = <int_const> may match more than 1 distinct value from 
10445
 
    the column. 
10446
 
    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
10447
3125
    string constants when compared to dates etc (see Item_int_with_ref) as
10448
3126
    well as the collation of the arguments.
10449
 
  
10450
 
  RETURN VALUE  
 
3127
 
 
3128
  RETURN VALUE
10451
3129
    true    can be used
10452
3130
    false   cannot be used
10453
3131
*/
10454
 
static bool
10455
 
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
3132
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10456
3133
{
10457
3134
  return r->const_item() &&
10458
3135
    /* elements must be compared as dates */
10467
3144
/**
10468
3145
  Return true if the item is a const value in all the WHERE clause.
10469
3146
*/
10470
 
 
10471
 
static bool
10472
 
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)
10473
3148
{
10474
3149
  if (cond->type() == Item::COND_ITEM)
10475
3150
  {
10482
3157
      bool res=const_expression_in_where(item, comp_item, const_item);
10483
3158
      if (res)                                  // Is a const value
10484
3159
      {
10485
 
        if (and_level)
10486
 
          return 1;
 
3160
        if (and_level)
 
3161
          return 1;
10487
3162
      }
10488
3163
      else if (!and_level)
10489
 
        return 0;
 
3164
        return 0;
10490
3165
    }
10491
3166
    return and_level ? 0 : 1;
10492
3167
  }
10494
3169
  {                                             // boolan compare function
10495
3170
    Item_func* func= (Item_func*) cond;
10496
3171
    if (func->functype() != Item_func::EQUAL_FUNC &&
10497
 
        func->functype() != Item_func::EQ_FUNC)
 
3172
              func->functype() != Item_func::EQ_FUNC)
10498
3173
      return 0;
10499
3174
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10500
3175
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10502
3177
    {
10503
3178
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10504
3179
      {
10505
 
        if (*const_item)
10506
 
          return right_item->eq(*const_item, 1);
10507
 
        *const_item=right_item;
10508
 
        return 1;
 
3180
        if (*const_item)
 
3181
          return right_item->eq(*const_item, 1);
 
3182
        *const_item=right_item;
 
3183
        return 1;
10509
3184
      }
10510
3185
    }
10511
3186
    else if (right_item->eq(comp_item,1))
10512
3187
    {
10513
3188
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10514
3189
      {
10515
 
        if (*const_item)
10516
 
          return left_item->eq(*const_item, 1);
10517
 
        *const_item=left_item;
10518
 
        return 1;
10519
 
      }
10520
 
    }
10521
 
  }
10522
 
  return 0;
10523
 
}
10524
 
 
10525
 
/****************************************************************************
10526
 
  Create internal temporary table
10527
 
****************************************************************************/
10528
 
 
10529
 
/**
10530
 
  Create field for temporary table from given field.
10531
 
 
10532
 
  @param thd           Thread handler
10533
 
  @param org_field    field from which new field will be created
10534
 
  @param name         New field name
10535
 
  @param table         Temporary table
10536
 
  @param item          !=NULL if item->result_field should point to new field.
10537
 
                      This is relevant for how fill_record() is going to work:
10538
 
                      If item != NULL then fill_record() will update
10539
 
                      the record in the original table.
10540
 
                      If item == NULL then fill_record() will update
10541
 
                      the temporary table
10542
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10543
 
                               field instead of blob.
10544
 
 
10545
 
  @retval
10546
 
    NULL                on error
10547
 
  @retval
10548
 
    new_created field
10549
 
*/
10550
 
 
10551
 
Field *create_tmp_field_from_field(THD *thd, Field *org_field,
10552
 
                                   const char *name, TABLE *table,
10553
 
                                   Item_field *item, uint convert_blob_length)
10554
 
{
10555
 
  Field *new_field;
10556
 
 
10557
 
  /* 
10558
 
    Make sure that the blob fits into a Field_varstring which has 
10559
 
    2-byte lenght. 
10560
 
  */
10561
 
  if (convert_blob_length && convert_blob_length <= Field_varstring::MAX_SIZE &&
10562
 
      (org_field->flags & BLOB_FLAG))
10563
 
    new_field= new Field_varstring(convert_blob_length,
10564
 
                                   org_field->maybe_null(),
10565
 
                                   org_field->field_name, table->s,
10566
 
                                   org_field->charset());
10567
 
  else
10568
 
    new_field= org_field->new_field(thd->mem_root, table,
10569
 
                                    table == org_field->table);
10570
 
  if (new_field)
10571
 
  {
10572
 
    new_field->init(table);
10573
 
    new_field->orig_table= org_field->orig_table;
10574
 
    if (item)
10575
 
      item->result_field= new_field;
10576
 
    else
10577
 
      new_field->field_name= name;
10578
 
    new_field->flags|= (org_field->flags & NO_DEFAULT_VALUE_FLAG);
10579
 
    if (org_field->maybe_null() || (item && item->maybe_null))
10580
 
      new_field->flags&= ~NOT_NULL_FLAG;        // Because of outer join
10581
 
    if (org_field->type() == DRIZZLE_TYPE_VARCHAR)
10582
 
      table->s->db_create_options|= HA_OPTION_PACK_RECORD;
10583
 
    else if (org_field->type() == DRIZZLE_TYPE_DOUBLE)
10584
 
      ((Field_double *) new_field)->not_fixed= true;
10585
 
  }
10586
 
  return new_field;
10587
 
}
10588
 
 
10589
 
/**
10590
 
  Create field for temporary table using type of given item.
10591
 
 
10592
 
  @param thd                   Thread handler
10593
 
  @param item                  Item to create a field for
10594
 
  @param table                 Temporary table
10595
 
  @param copy_func             If set and item is a function, store copy of
10596
 
                               item in this array
10597
 
  @param modify_item           1 if item->result_field should point to new
10598
 
                               item. This is relevent for how fill_record()
10599
 
                               is going to work:
10600
 
                               If modify_item is 1 then fill_record() will
10601
 
                               update the record in the original table.
10602
 
                               If modify_item is 0 then fill_record() will
10603
 
                               update the temporary table
10604
 
  @param convert_blob_length   If >0 create a varstring(convert_blob_length)
10605
 
                               field instead of blob.
10606
 
 
10607
 
  @retval
10608
 
    0  on error
10609
 
  @retval
10610
 
    new_created field
10611
 
*/
10612
 
 
10613
 
static Field *create_tmp_field_from_item(THD *thd __attribute__((unused)),
10614
 
                                         Item *item, TABLE *table,
10615
 
                                         Item ***copy_func, bool modify_item,
10616
 
                                         uint convert_blob_length)
10617
 
{
10618
 
  bool maybe_null= item->maybe_null;
10619
 
  Field *new_field;
10620
 
 
10621
 
  switch (item->result_type()) {
10622
 
  case REAL_RESULT:
10623
 
    new_field= new Field_double(item->max_length, maybe_null,
10624
 
                                item->name, item->decimals, true);
10625
 
    break;
10626
 
  case INT_RESULT:
10627
 
    /* 
10628
 
      Select an integer type with the minimal fit precision.
10629
 
      MY_INT32_NUM_DECIMAL_DIGITS is sign inclusive, don't consider the sign.
10630
 
      Values with MY_INT32_NUM_DECIMAL_DIGITS digits may or may not fit into 
10631
 
      Field_long : make them Field_int64_t.  
10632
 
    */
10633
 
    if (item->max_length >= (MY_INT32_NUM_DECIMAL_DIGITS - 1))
10634
 
      new_field=new Field_int64_t(item->max_length, maybe_null,
10635
 
                                   item->name, item->unsigned_flag);
10636
 
    else
10637
 
      new_field=new Field_long(item->max_length, maybe_null,
10638
 
                               item->name, item->unsigned_flag);
10639
 
    break;
10640
 
  case STRING_RESULT:
10641
 
    assert(item->collation.collation);
10642
 
  
10643
 
    enum enum_field_types type;
10644
 
    /*
10645
 
      DATE/TIME fields have STRING_RESULT result type. 
10646
 
      To preserve type they needed to be handled separately.
10647
 
    */
10648
 
    if ((type= item->field_type()) == DRIZZLE_TYPE_DATETIME ||
10649
 
        type == DRIZZLE_TYPE_TIME || type == DRIZZLE_TYPE_NEWDATE ||
10650
 
        type == DRIZZLE_TYPE_TIMESTAMP)
10651
 
      new_field= item->tmp_table_field_from_field_type(table, 1);
10652
 
    /* 
10653
 
      Make sure that the blob fits into a Field_varstring which has 
10654
 
      2-byte lenght. 
10655
 
    */
10656
 
    else if (item->max_length/item->collation.collation->mbmaxlen > 255 &&
10657
 
             convert_blob_length <= Field_varstring::MAX_SIZE && 
10658
 
             convert_blob_length)
10659
 
      new_field= new Field_varstring(convert_blob_length, maybe_null,
10660
 
                                     item->name, table->s,
10661
 
                                     item->collation.collation);
10662
 
    else
10663
 
      new_field= item->make_string_field(table);
10664
 
    new_field->set_derivation(item->collation.derivation);
10665
 
    break;
10666
 
  case DECIMAL_RESULT:
10667
 
  {
10668
 
    uint8_t dec= item->decimals;
10669
 
    uint8_t intg= ((Item_decimal *) item)->decimal_precision() - dec;
10670
 
    uint32_t len= item->max_length;
10671
 
 
10672
 
    /*
10673
 
      Trying to put too many digits overall in a DECIMAL(prec,dec)
10674
 
      will always throw a warning. We must limit dec to
10675
 
      DECIMAL_MAX_SCALE however to prevent an assert() later.
10676
 
    */
10677
 
 
10678
 
    if (dec > 0)
10679
 
    {
10680
 
      signed int overflow;
10681
 
 
10682
 
      dec= min(dec, (uint8_t)DECIMAL_MAX_SCALE);
10683
 
 
10684
 
      /*
10685
 
        If the value still overflows the field with the corrected dec,
10686
 
        we'll throw out decimals rather than integers. This is still
10687
 
        bad and of course throws a truncation warning.
10688
 
        +1: for decimal point
10689
 
      */
10690
 
 
10691
 
      overflow= my_decimal_precision_to_length(intg + dec, dec,
10692
 
                                               item->unsigned_flag) - len;
10693
 
 
10694
 
      if (overflow > 0)
10695
 
        dec= max(0, dec - overflow);            // too long, discard fract
10696
 
      else
10697
 
        len -= item->decimals - dec;            // corrected value fits
10698
 
    }
10699
 
 
10700
 
    new_field= new Field_new_decimal(len, maybe_null, item->name,
10701
 
                                     dec, item->unsigned_flag);
10702
 
    break;
10703
 
  }
10704
 
  case ROW_RESULT:
10705
 
  default:
10706
 
    // This case should never be choosen
10707
 
    assert(0);
10708
 
    new_field= 0;
10709
 
    break;
10710
 
  }
10711
 
  if (new_field)
10712
 
    new_field->init(table);
10713
 
    
10714
 
  if (copy_func && item->is_result_field())
10715
 
    *((*copy_func)++) = item;                   // Save for copy_funcs
10716
 
  if (modify_item)
10717
 
    item->set_result_field(new_field);
10718
 
  if (item->type() == Item::NULL_ITEM)
10719
 
    new_field->is_created_from_null_item= true;
10720
 
  return new_field;
10721
 
}
10722
 
 
10723
 
 
10724
 
/**
10725
 
  Create field for information schema table.
10726
 
 
10727
 
  @param thd            Thread handler
10728
 
  @param table          Temporary table
10729
 
  @param item           Item to create a field for
10730
 
 
10731
 
  @retval
10732
 
    0                   on error
10733
 
  @retval
10734
 
    new_created field
10735
 
*/
10736
 
 
10737
 
Field *create_tmp_field_for_schema(THD *thd __attribute__((unused)),
10738
 
                                   Item *item, TABLE *table)
10739
 
{
10740
 
  if (item->field_type() == DRIZZLE_TYPE_VARCHAR)
10741
 
  {
10742
 
    Field *field;
10743
 
    if (item->max_length > MAX_FIELD_VARCHARLENGTH)
10744
 
      field= new Field_blob(item->max_length, item->maybe_null,
10745
 
                            item->name, item->collation.collation);
10746
 
    else
10747
 
      field= new Field_varstring(item->max_length, item->maybe_null,
10748
 
                                 item->name,
10749
 
                                 table->s, item->collation.collation);
10750
 
    if (field)
10751
 
      field->init(table);
10752
 
    return field;
10753
 
  }
10754
 
  return item->tmp_table_field_from_field_type(table, 0);
10755
 
}
10756
 
 
10757
 
 
10758
 
/**
10759
 
  Create field for temporary table.
10760
 
 
10761
 
  @param thd            Thread handler
10762
 
  @param table          Temporary table
10763
 
  @param item           Item to create a field for
10764
 
  @param type           Type of item (normally item->type)
10765
 
  @param copy_func      If set and item is a function, store copy of item
10766
 
                       in this array
10767
 
  @param from_field    if field will be created using other field as example,
10768
 
                       pointer example field will be written here
10769
 
  @param default_field  If field has a default value field, store it here
10770
 
  @param group          1 if we are going to do a relative group by on result
10771
 
  @param modify_item    1 if item->result_field should point to new item.
10772
 
                       This is relevent for how fill_record() is going to
10773
 
                       work:
10774
 
                       If modify_item is 1 then fill_record() will update
10775
 
                       the record in the original table.
10776
 
                       If modify_item is 0 then fill_record() will update
10777
 
                       the temporary table
10778
 
  @param convert_blob_length If >0 create a varstring(convert_blob_length)
10779
 
                             field instead of blob.
10780
 
 
10781
 
  @retval
10782
 
    0                   on error
10783
 
  @retval
10784
 
    new_created field
10785
 
*/
10786
 
 
10787
 
Field *create_tmp_field(THD *thd, TABLE *table,Item *item, Item::Type type,
10788
 
                        Item ***copy_func, Field **from_field,
10789
 
                        Field **default_field,
10790
 
                        bool group, bool modify_item,
10791
 
                        bool table_cant_handle_bit_fields __attribute__((unused)),
10792
 
                        bool make_copy_field,
10793
 
                        uint convert_blob_length)
10794
 
{
10795
 
  Field *result;
10796
 
  Item::Type orig_type= type;
10797
 
  Item *orig_item= 0;
10798
 
 
10799
 
  if (type != Item::FIELD_ITEM &&
10800
 
      item->real_item()->type() == Item::FIELD_ITEM)
10801
 
  {
10802
 
    orig_item= item;
10803
 
    item= item->real_item();
10804
 
    type= Item::FIELD_ITEM;
10805
 
  }
10806
 
 
10807
 
  switch (type) {
10808
 
  case Item::SUM_FUNC_ITEM:
10809
 
  {
10810
 
    Item_sum *item_sum=(Item_sum*) item;
10811
 
    result= item_sum->create_tmp_field(group, table, convert_blob_length);
10812
 
    if (!result)
10813
 
      my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
10814
 
    return result;
10815
 
  }
10816
 
  case Item::FIELD_ITEM:
10817
 
  case Item::DEFAULT_VALUE_ITEM:
10818
 
  {
10819
 
    Item_field *field= (Item_field*) item;
10820
 
    bool orig_modify= modify_item;
10821
 
    if (orig_type == Item::REF_ITEM)
10822
 
      modify_item= 0;
10823
 
    /*
10824
 
      If item have to be able to store NULLs but underlaid field can't do it,
10825
 
      create_tmp_field_from_field() can't be used for tmp field creation.
10826
 
    */
10827
 
    if (field->maybe_null && !field->field->maybe_null())
10828
 
    {
10829
 
      result= create_tmp_field_from_item(thd, item, table, NULL,
10830
 
                                         modify_item, convert_blob_length);
10831
 
      *from_field= field->field;
10832
 
      if (result && modify_item)
10833
 
        field->result_field= result;
10834
 
    } 
10835
 
    else
10836
 
      result= create_tmp_field_from_field(thd, (*from_field= field->field),
10837
 
                                          orig_item ? orig_item->name :
10838
 
                                          item->name,
10839
 
                                          table,
10840
 
                                          modify_item ? field :
10841
 
                                          NULL,
10842
 
                                          convert_blob_length);
10843
 
    if (orig_type == Item::REF_ITEM && orig_modify)
10844
 
      ((Item_ref*)orig_item)->set_result_field(result);
10845
 
    if (field->field->eq_def(result))
10846
 
      *default_field= field->field;
10847
 
    return result;
10848
 
  }
10849
 
  /* Fall through */
10850
 
  case Item::FUNC_ITEM:
10851
 
    /* Fall through */
10852
 
  case Item::COND_ITEM:
10853
 
  case Item::FIELD_AVG_ITEM:
10854
 
  case Item::FIELD_STD_ITEM:
10855
 
  case Item::SUBSELECT_ITEM:
10856
 
    /* The following can only happen with 'CREATE TABLE ... SELECT' */
10857
 
  case Item::PROC_ITEM:
10858
 
  case Item::INT_ITEM:
10859
 
  case Item::REAL_ITEM:
10860
 
  case Item::DECIMAL_ITEM:
10861
 
  case Item::STRING_ITEM:
10862
 
  case Item::REF_ITEM:
10863
 
  case Item::NULL_ITEM:
10864
 
  case Item::VARBIN_ITEM:
10865
 
    if (make_copy_field)
10866
 
    {
10867
 
      assert(((Item_result_field*)item)->result_field);
10868
 
      *from_field= ((Item_result_field*)item)->result_field;
10869
 
    }
10870
 
    return create_tmp_field_from_item(thd, item, table,
10871
 
                                      (make_copy_field ? 0 : copy_func),
10872
 
                                       modify_item, convert_blob_length);
10873
 
  case Item::TYPE_HOLDER:  
10874
 
    result= ((Item_type_holder *)item)->make_field_by_type(table);
10875
 
    result->set_derivation(item->collation.derivation);
10876
 
    return result;
10877
 
  default:                                      // Dosen't have to be stored
10878
 
    return 0;
10879
 
  }
10880
 
}
10881
 
 
10882
 
/*
10883
 
  Set up column usage bitmaps for a temporary table
10884
 
 
10885
 
  IMPLEMENTATION
10886
 
    For temporary tables, we need one bitmap with all columns set and
10887
 
    a tmp_set bitmap to be used by things like filesort.
10888
 
*/
10889
 
 
10890
 
void setup_tmp_table_column_bitmaps(TABLE *table, uchar *bitmaps)
10891
 
{
10892
 
  uint field_count= table->s->fields;
10893
 
  bitmap_init(&table->def_read_set, (my_bitmap_map*) bitmaps, field_count,
10894
 
              false);
10895
 
  bitmap_init(&table->tmp_set,
10896
 
              (my_bitmap_map*) (bitmaps+ bitmap_buffer_size(field_count)),
10897
 
              field_count, false);
10898
 
  /* write_set and all_set are copies of read_set */
10899
 
  table->def_write_set= table->def_read_set;
10900
 
  table->s->all_set= table->def_read_set;
10901
 
  bitmap_set_all(&table->s->all_set);
10902
 
  table->default_column_bitmaps();
10903
 
}
10904
 
 
10905
 
 
10906
 
/**
10907
 
  Create a temp table according to a field list.
10908
 
 
10909
 
  Given field pointers are changed to point at tmp_table for
10910
 
  send_fields. The table object is self contained: it's
10911
 
  allocated in its own memory root, as well as Field objects
10912
 
  created for table columns.
10913
 
  This function will replace Item_sum items in 'fields' list with
10914
 
  corresponding Item_field items, pointing at the fields in the
10915
 
  temporary table, unless this was prohibited by true
10916
 
  value of argument save_sum_fields. The Item_field objects
10917
 
  are created in THD memory root.
10918
 
 
10919
 
  @param thd                  thread handle
10920
 
  @param param                a description used as input to create the table
10921
 
  @param fields               list of items that will be used to define
10922
 
                              column types of the table (also see NOTES)
10923
 
  @param group                TODO document
10924
 
  @param distinct             should table rows be distinct
10925
 
  @param save_sum_fields      see NOTES
10926
 
  @param select_options
10927
 
  @param rows_limit
10928
 
  @param table_alias          possible name of the temporary table that can
10929
 
                              be used for name resolving; can be "".
10930
 
*/
10931
 
 
10932
 
#define STRING_TOTAL_LENGTH_TO_PACK_ROWS 128
10933
 
#define AVG_STRING_LENGTH_TO_PACK_ROWS   64
10934
 
#define RATIO_TO_PACK_ROWS             2
10935
 
#define MIN_STRING_LENGTH_TO_PACK_ROWS   10
10936
 
 
10937
 
TABLE *
10938
 
create_tmp_table(THD *thd,TMP_TABLE_PARAM *param,List<Item> &fields,
10939
 
                 ORDER *group, bool distinct, bool save_sum_fields,
10940
 
                 uint64_t select_options, ha_rows rows_limit,
10941
 
                 char *table_alias)
10942
 
{
10943
 
  MEM_ROOT *mem_root_save, own_root;
10944
 
  TABLE *table;
10945
 
  TABLE_SHARE *share;
10946
 
  uint  i,field_count,null_count,null_pack_length;
10947
 
  uint  copy_func_count= param->func_count;
10948
 
  uint  hidden_null_count, hidden_null_pack_length, hidden_field_count;
10949
 
  uint  blob_count,group_null_items, string_count;
10950
 
  uint  temp_pool_slot=MY_BIT_NONE;
10951
 
  uint fieldnr= 0;
10952
 
  ulong reclength, string_total_length;
10953
 
  bool  using_unique_constraint= 0;
10954
 
  bool  use_packed_rows= 0;
10955
 
  bool  not_all_columns= !(select_options & TMP_TABLE_ALL_COLUMNS);
10956
 
  char  *tmpname,path[FN_REFLEN];
10957
 
  uchar *pos, *group_buff, *bitmaps;
10958
 
  uchar *null_flags;
10959
 
  Field **reg_field, **from_field, **default_field;
10960
 
  uint *blob_field;
10961
 
  Copy_field *copy=0;
10962
 
  KEY *keyinfo;
10963
 
  KEY_PART_INFO *key_part_info;
10964
 
  Item **copy_func;
10965
 
  MI_COLUMNDEF *recinfo;
10966
 
  uint total_uneven_bit_length= 0;
10967
 
  bool force_copy_fields= param->force_copy_fields;
10968
 
 
10969
 
  status_var_increment(thd->status_var.created_tmp_tables);
10970
 
 
10971
 
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
10972
 
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
10973
 
 
10974
 
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
10975
 
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
10976
 
            current_pid, temp_pool_slot);
10977
 
  else
10978
 
  {
10979
 
    /* if we run out of slots or we are not using tempool */
10980
 
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
10981
 
            thd->thread_id, thd->tmp_table++);
10982
 
  }
10983
 
 
10984
 
  /*
10985
 
    No need to change table name to lower case as we are only creating
10986
 
    MyISAM or HEAP tables here
10987
 
  */
10988
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
10989
 
 
10990
 
 
10991
 
  if (group)
10992
 
  {
10993
 
    if (!param->quick_group)
10994
 
      group=0;                                  // Can't use group key
10995
 
    else for (ORDER *tmp=group ; tmp ; tmp=tmp->next)
10996
 
    {
10997
 
      /*
10998
 
        marker == 4 means two things:
10999
 
        - store NULLs in the key, and
11000
 
        - convert BIT fields to 64-bit long, needed because MEMORY tables
11001
 
          can't index BIT fields.
11002
 
      */
11003
 
      (*tmp->item)->marker= 4;
11004
 
      if ((*tmp->item)->max_length >= CONVERT_IF_BIGGER_TO_BLOB)
11005
 
        using_unique_constraint=1;
11006
 
    }
11007
 
    if (param->group_length >= MAX_BLOB_WIDTH)
11008
 
      using_unique_constraint=1;
11009
 
    if (group)
11010
 
      distinct=0;                               // Can't use distinct
11011
 
  }
11012
 
 
11013
 
  field_count=param->field_count+param->func_count+param->sum_func_count;
11014
 
  hidden_field_count=param->hidden_field_count;
11015
 
 
11016
 
  /*
11017
 
    When loose index scan is employed as access method, it already
11018
 
    computes all groups and the result of all aggregate functions. We
11019
 
    make space for the items of the aggregate function in the list of
11020
 
    functions TMP_TABLE_PARAM::items_to_copy, so that the values of
11021
 
    these items are stored in the temporary table.
11022
 
  */
11023
 
  if (param->precomputed_group_by)
11024
 
    copy_func_count+= param->sum_func_count;
11025
 
  
11026
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11027
 
 
11028
 
  if (!multi_alloc_root(&own_root,
11029
 
                        &table, sizeof(*table),
11030
 
                        &share, sizeof(*share),
11031
 
                        &reg_field, sizeof(Field*) * (field_count+1),
11032
 
                        &default_field, sizeof(Field*) * (field_count),
11033
 
                        &blob_field, sizeof(uint)*(field_count+1),
11034
 
                        &from_field, sizeof(Field*)*field_count,
11035
 
                        &copy_func, sizeof(*copy_func)*(copy_func_count+1),
11036
 
                        &param->keyinfo, sizeof(*param->keyinfo),
11037
 
                        &key_part_info,
11038
 
                        sizeof(*key_part_info)*(param->group_parts+1),
11039
 
                        &param->start_recinfo,
11040
 
                        sizeof(*param->recinfo)*(field_count*2+4),
11041
 
                        &tmpname, (uint) strlen(path)+1,
11042
 
                        &group_buff, (group && ! using_unique_constraint ?
11043
 
                                      param->group_length : 0),
11044
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
11045
 
                        NullS))
11046
 
  {
11047
 
    if (temp_pool_slot != MY_BIT_NONE)
11048
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11049
 
    return(NULL);                               /* purecov: inspected */
11050
 
  }
11051
 
  /* Copy_field belongs to TMP_TABLE_PARAM, allocate it in THD mem_root */
11052
 
  if (!(param->copy_field= copy= new (thd->mem_root) Copy_field[field_count]))
11053
 
  {
11054
 
    if (temp_pool_slot != MY_BIT_NONE)
11055
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11056
 
    free_root(&own_root, MYF(0));               /* purecov: inspected */
11057
 
    return(NULL);                               /* purecov: inspected */
11058
 
  }
11059
 
  param->items_to_copy= copy_func;
11060
 
  stpcpy(tmpname,path);
11061
 
  /* make table according to fields */
11062
 
 
11063
 
  memset(table, 0, sizeof(*table));
11064
 
  memset(reg_field, 0, sizeof(Field*)*(field_count+1));
11065
 
  memset(default_field, 0, sizeof(Field*) * (field_count));
11066
 
  memset(from_field, 0, sizeof(Field*)*field_count);
11067
 
 
11068
 
  table->mem_root= own_root;
11069
 
  mem_root_save= thd->mem_root;
11070
 
  thd->mem_root= &table->mem_root;
11071
 
 
11072
 
  table->field=reg_field;
11073
 
  table->alias= table_alias;
11074
 
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
11075
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11076
 
  table->map=1;
11077
 
  table->temp_pool_slot = temp_pool_slot;
11078
 
  table->copy_blobs= 1;
11079
 
  table->in_use= thd;
11080
 
  table->quick_keys.init();
11081
 
  table->covering_keys.init();
11082
 
  table->keys_in_use_for_query.init();
11083
 
 
11084
 
  table->s= share;
11085
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11086
 
  share->blob_field= blob_field;
11087
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11088
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11089
 
  share->table_charset= param->table_charset;
11090
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
11091
 
  share->keys_for_keyread.init();
11092
 
  share->keys_in_use.init();
11093
 
 
11094
 
  /* Calculate which type of fields we will store in the temporary table */
11095
 
 
11096
 
  reclength= string_total_length= 0;
11097
 
  blob_count= string_count= null_count= hidden_null_count= group_null_items= 0;
11098
 
  param->using_indirect_summary_function=0;
11099
 
 
11100
 
  List_iterator_fast<Item> li(fields);
11101
 
  Item *item;
11102
 
  Field **tmp_from_field=from_field;
11103
 
  while ((item=li++))
11104
 
  {
11105
 
    Item::Type type=item->type();
11106
 
    if (not_all_columns)
11107
 
    {
11108
 
      if (item->with_sum_func && type != Item::SUM_FUNC_ITEM)
11109
 
      {
11110
 
        if (item->used_tables() & OUTER_REF_TABLE_BIT)
11111
 
          item->update_used_tables();
11112
 
        if (type == Item::SUBSELECT_ITEM ||
11113
 
            (item->used_tables() & ~OUTER_REF_TABLE_BIT))
11114
 
        {
11115
 
          /*
11116
 
            Mark that the we have ignored an item that refers to a summary
11117
 
            function. We need to know this if someone is going to use
11118
 
            DISTINCT on the result.
11119
 
          */
11120
 
          param->using_indirect_summary_function=1;
11121
 
          continue;
11122
 
        }
11123
 
      }
11124
 
      if (item->const_item() && (int) hidden_field_count <= 0)
11125
 
        continue; // We don't have to store this
11126
 
    }
11127
 
    if (type == Item::SUM_FUNC_ITEM && !group && !save_sum_fields)
11128
 
    {                                           /* Can't calc group yet */
11129
 
      ((Item_sum*) item)->result_field=0;
11130
 
      for (i=0 ; i < ((Item_sum*) item)->arg_count ; i++)
11131
 
      {
11132
 
        Item **argp= ((Item_sum*) item)->args + i;
11133
 
        Item *arg= *argp;
11134
 
        if (!arg->const_item())
11135
 
        {
11136
 
          Field *new_field=
11137
 
            create_tmp_field(thd, table, arg, arg->type(), &copy_func,
11138
 
                             tmp_from_field, &default_field[fieldnr],
11139
 
                             group != 0,not_all_columns,
11140
 
                             distinct, 0,
11141
 
                             param->convert_blob_length);
11142
 
          if (!new_field)
11143
 
            goto err;                                   // Should be OOM
11144
 
          tmp_from_field++;
11145
 
          reclength+=new_field->pack_length();
11146
 
          if (new_field->flags & BLOB_FLAG)
11147
 
          {
11148
 
            *blob_field++= fieldnr;
11149
 
            blob_count++;
11150
 
          }
11151
 
          *(reg_field++)= new_field;
11152
 
          if (new_field->real_type() == DRIZZLE_TYPE_VARCHAR)
11153
 
          {
11154
 
            string_count++;
11155
 
            string_total_length+= new_field->pack_length();
11156
 
          }
11157
 
          thd->mem_root= mem_root_save;
11158
 
          thd->change_item_tree(argp, new Item_field(new_field));
11159
 
          thd->mem_root= &table->mem_root;
11160
 
          if (!(new_field->flags & NOT_NULL_FLAG))
11161
 
          {
11162
 
            null_count++;
11163
 
            /*
11164
 
              new_field->maybe_null() is still false, it will be
11165
 
              changed below. But we have to setup Item_field correctly
11166
 
            */
11167
 
            (*argp)->maybe_null=1;
11168
 
          }
11169
 
          new_field->field_index= fieldnr++;
11170
 
        }
11171
 
      }
11172
 
    }
11173
 
    else
11174
 
    {
11175
 
      /*
11176
 
        The last parameter to create_tmp_field() is a bit tricky:
11177
 
 
11178
 
        We need to set it to 0 in union, to get fill_record() to modify the
11179
 
        temporary table.
11180
 
        We need to set it to 1 on multi-table-update and in select to
11181
 
        write rows to the temporary table.
11182
 
        We here distinguish between UNION and multi-table-updates by the fact
11183
 
        that in the later case group is set to the row pointer.
11184
 
      */
11185
 
      Field *new_field= (param->schema_table) ?
11186
 
        create_tmp_field_for_schema(thd, item, table) :
11187
 
        create_tmp_field(thd, table, item, type, &copy_func,
11188
 
                         tmp_from_field, &default_field[fieldnr],
11189
 
                         group != 0,
11190
 
                         !force_copy_fields &&
11191
 
                           (not_all_columns || group !=0),
11192
 
                         /*
11193
 
                           If item->marker == 4 then we force create_tmp_field
11194
 
                           to create a 64-bit longs for BIT fields because HEAP
11195
 
                           tables can't index BIT fields directly. We do the same
11196
 
                           for distinct, as we want the distinct index to be
11197
 
                           usable in this case too.
11198
 
                         */
11199
 
                         item->marker == 4 || param->bit_fields_as_long,
11200
 
                         force_copy_fields,
11201
 
                         param->convert_blob_length);
11202
 
 
11203
 
      if (!new_field)
11204
 
      {
11205
 
        if (thd->is_fatal_error)
11206
 
          goto err;                             // Got OOM
11207
 
        continue;                               // Some kindf of const item
11208
 
      }
11209
 
      if (type == Item::SUM_FUNC_ITEM)
11210
 
        ((Item_sum *) item)->result_field= new_field;
11211
 
      tmp_from_field++;
11212
 
      reclength+=new_field->pack_length();
11213
 
      if (!(new_field->flags & NOT_NULL_FLAG))
11214
 
        null_count++;
11215
 
      if (new_field->flags & BLOB_FLAG)
11216
 
      {
11217
 
        *blob_field++= fieldnr;
11218
 
        blob_count++;
11219
 
      }
11220
 
      if (item->marker == 4 && item->maybe_null)
11221
 
      {
11222
 
        group_null_items++;
11223
 
        new_field->flags|= GROUP_FLAG;
11224
 
      }
11225
 
      new_field->field_index= fieldnr++;
11226
 
      *(reg_field++)= new_field;
11227
 
    }
11228
 
    if (!--hidden_field_count)
11229
 
    {
11230
 
      /*
11231
 
        This was the last hidden field; Remember how many hidden fields could
11232
 
        have null
11233
 
      */
11234
 
      hidden_null_count=null_count;
11235
 
      /*
11236
 
        We need to update hidden_field_count as we may have stored group
11237
 
        functions with constant arguments
11238
 
      */
11239
 
      param->hidden_field_count= fieldnr;
11240
 
      null_count= 0;
11241
 
    }
11242
 
  }
11243
 
  assert(fieldnr == (uint) (reg_field - table->field));
11244
 
  assert(field_count >= (uint) (reg_field - table->field));
11245
 
  field_count= fieldnr;
11246
 
  *reg_field= 0;
11247
 
  *blob_field= 0;                               // End marker
11248
 
  share->fields= field_count;
11249
 
 
11250
 
  /* If result table is small; use a heap */
11251
 
  /* future: storage engine selection can be made dynamic? */
11252
 
  if (blob_count || using_unique_constraint ||
11253
 
      (select_options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
11254
 
      OPTION_BIG_TABLES || (select_options & TMP_TABLE_FORCE_MYISAM))
11255
 
  {
11256
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11257
 
    table->file= get_new_handler(share, &table->mem_root,
11258
 
                                 share->db_type());
11259
 
    if (group &&
11260
 
        (param->group_parts > table->file->max_key_parts() ||
11261
 
         param->group_length > table->file->max_key_length()))
11262
 
      using_unique_constraint=1;
11263
 
  }
11264
 
  else
11265
 
  {
11266
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
11267
 
    table->file= get_new_handler(share, &table->mem_root,
11268
 
                                 share->db_type());
11269
 
  }
11270
 
  if (!table->file)
11271
 
    goto err;
11272
 
 
11273
 
 
11274
 
  if (!using_unique_constraint)
11275
 
    reclength+= group_null_items;       // null flag is stored separately
11276
 
 
11277
 
  share->blob_fields= blob_count;
11278
 
  if (blob_count == 0)
11279
 
  {
11280
 
    /* We need to ensure that first byte is not 0 for the delete link */
11281
 
    if (param->hidden_field_count)
11282
 
      hidden_null_count++;
11283
 
    else
11284
 
      null_count++;
11285
 
  }
11286
 
  hidden_null_pack_length=(hidden_null_count+7)/8;
11287
 
  null_pack_length= (hidden_null_pack_length +
11288
 
                     (null_count + total_uneven_bit_length + 7) / 8);
11289
 
  reclength+=null_pack_length;
11290
 
  if (!reclength)
11291
 
    reclength=1;                                // Dummy select
11292
 
  /* Use packed rows if there is blobs or a lot of space to gain */
11293
 
  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)))
11294
 
    use_packed_rows= 1;
11295
 
 
11296
 
  share->reclength= reclength;
11297
 
  {
11298
 
    uint alloc_length=ALIGN_SIZE(reclength+MI_UNIQUE_HASH_LENGTH+1);
11299
 
    share->rec_buff_length= alloc_length;
11300
 
    if (!(table->record[0]= (uchar*)
11301
 
                            alloc_root(&table->mem_root, alloc_length*3)))
11302
 
      goto err;
11303
 
    table->record[1]= table->record[0]+alloc_length;
11304
 
    share->default_values= table->record[1]+alloc_length;
11305
 
  }
11306
 
  copy_func[0]=0;                               // End marker
11307
 
  param->func_count= copy_func - param->items_to_copy; 
11308
 
 
11309
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11310
 
 
11311
 
  recinfo=param->start_recinfo;
11312
 
  null_flags=(uchar*) table->record[0];
11313
 
  pos=table->record[0]+ null_pack_length;
11314
 
  if (null_pack_length)
11315
 
  {
11316
 
    memset(recinfo, 0, sizeof(*recinfo));
11317
 
    recinfo->type=FIELD_NORMAL;
11318
 
    recinfo->length=null_pack_length;
11319
 
    recinfo++;
11320
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
11321
 
 
11322
 
    table->null_flags= (uchar*) table->record[0];
11323
 
    share->null_fields= null_count+ hidden_null_count;
11324
 
    share->null_bytes= null_pack_length;
11325
 
  }
11326
 
  null_count= (blob_count == 0) ? 1 : 0;
11327
 
  hidden_field_count=param->hidden_field_count;
11328
 
  for (i=0,reg_field=table->field; i < field_count; i++,reg_field++,recinfo++)
11329
 
  {
11330
 
    Field *field= *reg_field;
11331
 
    uint length;
11332
 
    memset(recinfo, 0, sizeof(*recinfo));
11333
 
 
11334
 
    if (!(field->flags & NOT_NULL_FLAG))
11335
 
    {
11336
 
      if (field->flags & GROUP_FLAG && !using_unique_constraint)
11337
 
      {
11338
 
        /*
11339
 
          We have to reserve one byte here for NULL bits,
11340
 
          as this is updated by 'end_update()'
11341
 
        */
11342
 
        *pos++=0;                               // Null is stored here
11343
 
        recinfo->length=1;
11344
 
        recinfo->type=FIELD_NORMAL;
11345
 
        recinfo++;
11346
 
        memset(recinfo, 0, sizeof(*recinfo));
11347
 
      }
11348
 
      else
11349
 
      {
11350
 
        recinfo->null_bit= 1 << (null_count & 7);
11351
 
        recinfo->null_pos= null_count/8;
11352
 
      }
11353
 
      field->move_field(pos,null_flags+null_count/8,
11354
 
                        1 << (null_count & 7));
11355
 
      null_count++;
11356
 
    }
11357
 
    else
11358
 
      field->move_field(pos,(uchar*) 0,0);
11359
 
    field->reset();
11360
 
 
11361
 
    /*
11362
 
      Test if there is a default field value. The test for ->ptr is to skip
11363
 
      'offset' fields generated by initalize_tables
11364
 
    */
11365
 
    if (default_field[i] && default_field[i]->ptr)
11366
 
    {
11367
 
      /* 
11368
 
         default_field[i] is set only in the cases  when 'field' can
11369
 
         inherit the default value that is defined for the field referred
11370
 
         by the Item_field object from which 'field' has been created.
11371
 
      */
11372
 
      my_ptrdiff_t diff;
11373
 
      Field *orig_field= default_field[i];
11374
 
      /* Get the value from default_values */
11375
 
      diff= (my_ptrdiff_t) (orig_field->table->s->default_values-
11376
 
                            orig_field->table->record[0]);
11377
 
      orig_field->move_field_offset(diff);      // Points now at default_values
11378
 
      if (orig_field->is_real_null())
11379
 
        field->set_null();
11380
 
      else
11381
 
      {
11382
 
        field->set_notnull();
11383
 
        memcpy(field->ptr, orig_field->ptr, field->pack_length());
11384
 
      }
11385
 
      orig_field->move_field_offset(-diff);     // Back to record[0]
11386
 
    } 
11387
 
 
11388
 
    if (from_field[i])
11389
 
    {                                           /* Not a table Item */
11390
 
      copy->set(field,from_field[i],save_sum_fields);
11391
 
      copy++;
11392
 
    }
11393
 
    length=field->pack_length();
11394
 
    pos+= length;
11395
 
 
11396
 
    /* Make entry for create table */
11397
 
    recinfo->length=length;
11398
 
    if (field->flags & BLOB_FLAG)
11399
 
      recinfo->type= (int) FIELD_BLOB;
11400
 
    else
11401
 
      recinfo->type=FIELD_NORMAL;
11402
 
    if (!--hidden_field_count)
11403
 
      null_count=(null_count+7) & ~7;           // move to next byte
11404
 
 
11405
 
    // fix table name in field entry
11406
 
    field->table_name= &table->alias;
11407
 
  }
11408
 
 
11409
 
  param->copy_field_end=copy;
11410
 
  param->recinfo=recinfo;
11411
 
  store_record(table,s->default_values);        // Make empty default record
11412
 
 
11413
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
11414
 
    share->max_rows= ~(ha_rows) 0;
11415
 
  else
11416
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11417
 
                                 min(thd->variables.tmp_table_size,
11418
 
                                     thd->variables.max_heap_table_size) :
11419
 
                                 thd->variables.tmp_table_size) /
11420
 
                                 share->reclength);
11421
 
  set_if_bigger(share->max_rows,1);             // For dummy start options
11422
 
  /*
11423
 
    Push the LIMIT clause to the temporary table creation, so that we
11424
 
    materialize only up to 'rows_limit' records instead of all result records.
11425
 
  */
11426
 
  set_if_smaller(share->max_rows, rows_limit);
11427
 
  param->end_write_records= rows_limit;
11428
 
 
11429
 
  keyinfo= param->keyinfo;
11430
 
 
11431
 
  if (group)
11432
 
  {
11433
 
    table->group=group;                         /* Table is grouped by key */
11434
 
    param->group_buff=group_buff;
11435
 
    share->keys=1;
11436
 
    share->uniques= test(using_unique_constraint);
11437
 
    table->key_info=keyinfo;
11438
 
    keyinfo->key_part=key_part_info;
11439
 
    keyinfo->flags=HA_NOSAME;
11440
 
    keyinfo->usable_key_parts=keyinfo->key_parts= param->group_parts;
11441
 
    keyinfo->key_length=0;
11442
 
    keyinfo->rec_per_key=0;
11443
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11444
 
    keyinfo->name= (char*) "group_key";
11445
 
    ORDER *cur_group= group;
11446
 
    for (; cur_group ; cur_group= cur_group->next, key_part_info++)
11447
 
    {
11448
 
      Field *field=(*cur_group->item)->get_tmp_table_field();
11449
 
      bool maybe_null=(*cur_group->item)->maybe_null;
11450
 
      key_part_info->null_bit=0;
11451
 
      key_part_info->field=  field;
11452
 
      key_part_info->offset= field->offset(table->record[0]);
11453
 
      key_part_info->length= (uint16_t) field->key_length();
11454
 
      key_part_info->type=   (uint8_t) field->key_type();
11455
 
      key_part_info->key_type =
11456
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11457
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11458
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11459
 
        0 : FIELDFLAG_BINARY;
11460
 
      if (!using_unique_constraint)
11461
 
      {
11462
 
        cur_group->buff=(char*) group_buff;
11463
 
        if (!(cur_group->field= field->new_key_field(thd->mem_root,table,
11464
 
                                                     group_buff +
11465
 
                                                     test(maybe_null),
11466
 
                                                     field->null_ptr,
11467
 
                                                     field->null_bit)))
11468
 
          goto err; /* purecov: inspected */
11469
 
        if (maybe_null)
11470
 
        {
11471
 
          /*
11472
 
            To be able to group on NULL, we reserved place in group_buff
11473
 
            for the NULL flag just before the column. (see above).
11474
 
            The field data is after this flag.
11475
 
            The NULL flag is updated in 'end_update()' and 'end_write()'
11476
 
          */
11477
 
          keyinfo->flags|= HA_NULL_ARE_EQUAL;   // def. that NULL == NULL
11478
 
          key_part_info->null_bit=field->null_bit;
11479
 
          key_part_info->null_offset= (uint) (field->null_ptr -
11480
 
                                              (uchar*) table->record[0]);
11481
 
          cur_group->buff++;                        // Pointer to field data
11482
 
          group_buff++;                         // Skipp null flag
11483
 
        }
11484
 
        /* In GROUP BY 'a' and 'a ' are equal for VARCHAR fields */
11485
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL;
11486
 
        group_buff+= cur_group->field->pack_length();
11487
 
      }
11488
 
      keyinfo->key_length+=  key_part_info->length;
11489
 
    }
11490
 
  }
11491
 
 
11492
 
  if (distinct && field_count != param->hidden_field_count)
11493
 
  {
11494
 
    /*
11495
 
      Create an unique key or an unique constraint over all columns
11496
 
      that should be in the result.  In the temporary table, there are
11497
 
      'param->hidden_field_count' extra columns, whose null bits are stored
11498
 
      in the first 'hidden_null_pack_length' bytes of the row.
11499
 
    */
11500
 
    if (blob_count)
11501
 
    {
11502
 
      /*
11503
 
        Special mode for index creation in MyISAM used to support unique
11504
 
        indexes on blobs with arbitrary length. Such indexes cannot be
11505
 
        used for lookups.
11506
 
      */
11507
 
      share->uniques= 1;
11508
 
    }
11509
 
    null_pack_length-=hidden_null_pack_length;
11510
 
    keyinfo->key_parts= ((field_count-param->hidden_field_count)+
11511
 
                         (share->uniques ? test(null_pack_length) : 0));
11512
 
    table->distinct= 1;
11513
 
    share->keys= 1;
11514
 
    if (!(key_part_info= (KEY_PART_INFO*)
11515
 
          alloc_root(&table->mem_root,
11516
 
                     keyinfo->key_parts * sizeof(KEY_PART_INFO))))
11517
 
      goto err;
11518
 
    memset(key_part_info, 0, keyinfo->key_parts * sizeof(KEY_PART_INFO));
11519
 
    table->key_info=keyinfo;
11520
 
    keyinfo->key_part=key_part_info;
11521
 
    keyinfo->flags=HA_NOSAME | HA_NULL_ARE_EQUAL;
11522
 
    keyinfo->key_length=(uint16_t) reclength;
11523
 
    keyinfo->name= (char*) "distinct_key";
11524
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11525
 
    keyinfo->rec_per_key=0;
11526
 
 
11527
 
    /*
11528
 
      Create an extra field to hold NULL bits so that unique indexes on
11529
 
      blobs can distinguish NULL from 0. This extra field is not needed
11530
 
      when we do not use UNIQUE indexes for blobs.
11531
 
    */
11532
 
    if (null_pack_length && share->uniques)
11533
 
    {
11534
 
      key_part_info->null_bit=0;
11535
 
      key_part_info->offset=hidden_null_pack_length;
11536
 
      key_part_info->length=null_pack_length;
11537
 
      key_part_info->field= new Field_varstring(table->record[0],
11538
 
                                                (uint32_t) key_part_info->length,
11539
 
                                                0,
11540
 
                                                (uchar*) 0,
11541
 
                                                (uint) 0,
11542
 
                                                Field::NONE,
11543
 
                                                NullS, 
11544
 
                                                table->s,
11545
 
                                                &my_charset_bin);
11546
 
      if (!key_part_info->field)
11547
 
        goto err;
11548
 
      key_part_info->field->init(table);
11549
 
      key_part_info->key_type=FIELDFLAG_BINARY;
11550
 
      key_part_info->type=    HA_KEYTYPE_BINARY;
11551
 
      key_part_info++;
11552
 
    }
11553
 
    /* Create a distinct key over the columns we are going to return */
11554
 
    for (i=param->hidden_field_count, reg_field=table->field + i ;
11555
 
         i < field_count;
11556
 
         i++, reg_field++, key_part_info++)
11557
 
    {
11558
 
      key_part_info->null_bit=0;
11559
 
      key_part_info->field=    *reg_field;
11560
 
      key_part_info->offset=   (*reg_field)->offset(table->record[0]);
11561
 
      key_part_info->length=   (uint16_t) (*reg_field)->pack_length();
11562
 
      /* TODO:
11563
 
        The below method of computing the key format length of the
11564
 
        key part is a copy/paste from opt_range.cc, and table.cc.
11565
 
        This should be factored out, e.g. as a method of Field.
11566
 
        In addition it is not clear if any of the Field::*_length
11567
 
        methods is supposed to compute the same length. If so, it
11568
 
        might be reused.
11569
 
      */
11570
 
      key_part_info->store_length= key_part_info->length;
11571
 
 
11572
 
      if ((*reg_field)->real_maybe_null())
11573
 
        key_part_info->store_length+= HA_KEY_NULL_LENGTH;
11574
 
      if ((*reg_field)->type() == DRIZZLE_TYPE_BLOB || 
11575
 
          (*reg_field)->real_type() == DRIZZLE_TYPE_VARCHAR)
11576
 
        key_part_info->store_length+= HA_KEY_BLOB_LENGTH;
11577
 
 
11578
 
      key_part_info->type=     (uint8_t) (*reg_field)->key_type();
11579
 
      key_part_info->key_type =
11580
 
        ((ha_base_keytype) key_part_info->type == HA_KEYTYPE_TEXT ||
11581
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT1 ||
11582
 
         (ha_base_keytype) key_part_info->type == HA_KEYTYPE_VARTEXT2) ?
11583
 
        0 : FIELDFLAG_BINARY;
11584
 
    }
11585
 
  }
11586
 
 
11587
 
  if (thd->is_fatal_error)                              // If end of memory
11588
 
    goto err;                                    /* purecov: inspected */
11589
 
  share->db_record_offset= 1;
11590
 
  if (share->db_type() == myisam_hton)
11591
 
  {
11592
 
    if (create_myisam_tmp_table(table, param->keyinfo, param->start_recinfo,
11593
 
                                &param->recinfo, select_options))
11594
 
      goto err;
11595
 
  }
11596
 
  if (open_tmp_table(table))
11597
 
    goto err;
11598
 
 
11599
 
  thd->mem_root= mem_root_save;
11600
 
 
11601
 
  return(table);
11602
 
 
11603
 
err:
11604
 
  thd->mem_root= mem_root_save;
11605
 
  free_tmp_table(thd,table);                    /* purecov: inspected */
11606
 
  if (temp_pool_slot != MY_BIT_NONE)
11607
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11608
 
  return(NULL);                         /* purecov: inspected */
11609
 
}
11610
 
 
11611
 
 
11612
 
 
11613
 
 
11614
 
/*
11615
 
  Create a temporary table to weed out duplicate rowid combinations
11616
 
 
11617
 
  SYNOPSIS
11618
 
 
11619
 
    create_duplicate_weedout_tmp_table()
11620
 
      thd
11621
 
      uniq_tuple_length_arg
11622
 
      SJ_TMP_TABLE 
11623
 
 
11624
 
  DESCRIPTION
11625
 
    Create a temporary table to weed out duplicate rowid combinations. The
11626
 
    table has a single column that is a concatenation of all rowids in the
11627
 
    combination. 
11628
 
 
11629
 
    Depending on the needed length, there are two cases:
11630
 
 
11631
 
    1. When the length of the column < max_key_length:
11632
 
 
11633
 
      CREATE TABLE tmp (col VARBINARY(n) NOT NULL, UNIQUE KEY(col));
11634
 
 
11635
 
    2. Otherwise (not a valid SQL syntax but internally supported):
11636
 
 
11637
 
      CREATE TABLE tmp (col VARBINARY NOT NULL, UNIQUE CONSTRAINT(col));
11638
 
 
11639
 
    The code in this function was produced by extraction of relevant parts
11640
 
    from create_tmp_table().
11641
 
 
11642
 
  RETURN
11643
 
    created table
11644
 
    NULL on error
11645
 
*/
11646
 
 
11647
 
TABLE *create_duplicate_weedout_tmp_table(THD *thd, 
11648
 
                                          uint uniq_tuple_length_arg,
11649
 
                                          SJ_TMP_TABLE *sjtbl)
11650
 
{
11651
 
  MEM_ROOT *mem_root_save, own_root;
11652
 
  TABLE *table;
11653
 
  TABLE_SHARE *share;
11654
 
  uint  temp_pool_slot=MY_BIT_NONE;
11655
 
  char  *tmpname,path[FN_REFLEN];
11656
 
  Field **reg_field;
11657
 
  KEY_PART_INFO *key_part_info;
11658
 
  KEY *keyinfo;
11659
 
  uchar *group_buff;
11660
 
  uchar *bitmaps;
11661
 
  uint *blob_field;
11662
 
  MI_COLUMNDEF *recinfo, *start_recinfo;
11663
 
  bool using_unique_constraint=false;
11664
 
  Field *field, *key_field;
11665
 
  uint blob_count, null_pack_length, null_count;
11666
 
  uchar *null_flags;
11667
 
  uchar *pos;
11668
 
  
11669
 
  /*
11670
 
    STEP 1: Get temporary table name
11671
 
  */
11672
 
  statistic_increment(thd->status_var.created_tmp_tables, &LOCK_status);
11673
 
  if (use_temp_pool && !(test_flags & TEST_KEEP_TMP_TABLES))
11674
 
    temp_pool_slot = bitmap_lock_set_next(&temp_pool);
11675
 
 
11676
 
  if (temp_pool_slot != MY_BIT_NONE) // we got a slot
11677
 
    sprintf(path, "%s_%lx_%i", tmp_file_prefix,
11678
 
            current_pid, temp_pool_slot);
11679
 
  else
11680
 
  {
11681
 
    /* if we run out of slots or we are not using tempool */
11682
 
    sprintf(path,"%s%lx_%lx_%x", tmp_file_prefix,current_pid,
11683
 
            thd->thread_id, thd->tmp_table++);
11684
 
  }
11685
 
  fn_format(path, path, mysql_tmpdir, "", MY_REPLACE_EXT|MY_UNPACK_FILENAME);
11686
 
 
11687
 
  /* STEP 2: Figure if we'll be using a key or blob+constraint */
11688
 
  if (uniq_tuple_length_arg >= CONVERT_IF_BIGGER_TO_BLOB)
11689
 
    using_unique_constraint= true;
11690
 
 
11691
 
  /* STEP 3: Allocate memory for temptable description */
11692
 
  init_sql_alloc(&own_root, TABLE_ALLOC_BLOCK_SIZE, 0);
11693
 
  if (!multi_alloc_root(&own_root,
11694
 
                        &table, sizeof(*table),
11695
 
                        &share, sizeof(*share),
11696
 
                        &reg_field, sizeof(Field*) * (1+1),
11697
 
                        &blob_field, sizeof(uint)*2,
11698
 
                        &keyinfo, sizeof(*keyinfo),
11699
 
                        &key_part_info, sizeof(*key_part_info) * 2,
11700
 
                        &start_recinfo,
11701
 
                        sizeof(*recinfo)*(1*2+4),
11702
 
                        &tmpname, (uint) strlen(path)+1,
11703
 
                        &group_buff, (!using_unique_constraint ?
11704
 
                                      uniq_tuple_length_arg : 0),
11705
 
                        &bitmaps, bitmap_buffer_size(1)*2,
11706
 
                        NullS))
11707
 
  {
11708
 
    if (temp_pool_slot != MY_BIT_NONE)
11709
 
      bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11710
 
    return(NULL);
11711
 
  }
11712
 
  stpcpy(tmpname,path);
11713
 
  
11714
 
 
11715
 
  /* STEP 4: Create TABLE description */
11716
 
  memset(table, 0, sizeof(*table));
11717
 
  memset(reg_field, 0, sizeof(Field*)*2);
11718
 
 
11719
 
  table->mem_root= own_root;
11720
 
  mem_root_save= thd->mem_root;
11721
 
  thd->mem_root= &table->mem_root;
11722
 
 
11723
 
  table->field=reg_field;
11724
 
  table->alias= "weedout-tmp";
11725
 
  table->reginfo.lock_type=TL_WRITE;    /* Will be updated */
11726
 
  table->db_stat=HA_OPEN_KEYFILE+HA_OPEN_RNDFILE;
11727
 
  table->map=1;
11728
 
  table->temp_pool_slot = temp_pool_slot;
11729
 
  table->copy_blobs= 1;
11730
 
  table->in_use= thd;
11731
 
  table->quick_keys.init();
11732
 
  table->covering_keys.init();
11733
 
  table->keys_in_use_for_query.init();
11734
 
 
11735
 
  table->s= share;
11736
 
  init_tmp_table_share(thd, share, "", 0, tmpname, tmpname);
11737
 
  share->blob_field= blob_field;
11738
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11739
 
  share->db_low_byte_first=1;                // True for HEAP and MyISAM
11740
 
  share->table_charset= NULL;
11741
 
  share->primary_key= MAX_KEY;               // Indicate no primary key
11742
 
  share->keys_for_keyread.init();
11743
 
  share->keys_in_use.init();
11744
 
 
11745
 
  blob_count= 0;
11746
 
 
11747
 
  /* Create the field */
11748
 
  {
11749
 
    /*
11750
 
      For the sake of uniformity, always use Field_varstring.
11751
 
    */
11752
 
    field= new Field_varstring(uniq_tuple_length_arg, false, "rowids", share,
11753
 
                               &my_charset_bin);
11754
 
    if (!field)
11755
 
      return(0);
11756
 
    field->table= table;
11757
 
    field->key_start.init(0);
11758
 
    field->part_of_key.init(0);
11759
 
    field->part_of_sortkey.init(0);
11760
 
    field->unireg_check= Field::NONE;
11761
 
    field->flags= (NOT_NULL_FLAG | BINARY_FLAG | NO_DEFAULT_VALUE_FLAG);
11762
 
    field->reset_fields();
11763
 
    field->init(table);
11764
 
    field->orig_table= NULL;
11765
 
     
11766
 
    field->field_index= 0;
11767
 
    
11768
 
    *(reg_field++)= field;
11769
 
    *blob_field= 0;
11770
 
    *reg_field= 0;
11771
 
 
11772
 
    share->fields= 1;
11773
 
    share->blob_fields= 0;
11774
 
  }
11775
 
 
11776
 
  uint reclength= field->pack_length();
11777
 
  if (using_unique_constraint)
11778
 
  { 
11779
 
    share->db_plugin= ha_lock_engine(0, myisam_hton);
11780
 
    table->file= get_new_handler(share, &table->mem_root,
11781
 
                                 share->db_type());
11782
 
    assert(uniq_tuple_length_arg <= table->file->max_key_length());
11783
 
  }
11784
 
  else
11785
 
  {
11786
 
    share->db_plugin= ha_lock_engine(0, heap_hton);
11787
 
    table->file= get_new_handler(share, &table->mem_root,
11788
 
                                 share->db_type());
11789
 
  }
11790
 
  if (!table->file)
11791
 
    goto err;
11792
 
 
11793
 
  null_count=1;
11794
 
  
11795
 
  null_pack_length= 1;
11796
 
  reclength += null_pack_length;
11797
 
 
11798
 
  share->reclength= reclength;
11799
 
  {
11800
 
    uint alloc_length=ALIGN_SIZE(share->reclength + MI_UNIQUE_HASH_LENGTH+1);
11801
 
    share->rec_buff_length= alloc_length;
11802
 
    if (!(table->record[0]= (uchar*)
11803
 
                            alloc_root(&table->mem_root, alloc_length*3)))
11804
 
      goto err;
11805
 
    table->record[1]= table->record[0]+alloc_length;
11806
 
    share->default_values= table->record[1]+alloc_length;
11807
 
  }
11808
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11809
 
 
11810
 
  recinfo= start_recinfo;
11811
 
  null_flags=(uchar*) table->record[0];
11812
 
  pos=table->record[0]+ null_pack_length;
11813
 
  if (null_pack_length)
11814
 
  {
11815
 
    memset(recinfo, 0, sizeof(*recinfo));
11816
 
    recinfo->type=FIELD_NORMAL;
11817
 
    recinfo->length=null_pack_length;
11818
 
    recinfo++;
11819
 
    memset(null_flags, 255, null_pack_length);  // Set null fields
11820
 
 
11821
 
    table->null_flags= (uchar*) table->record[0];
11822
 
    share->null_fields= null_count;
11823
 
    share->null_bytes= null_pack_length;
11824
 
  }
11825
 
  null_count=1;
11826
 
 
11827
 
  {
11828
 
    //Field *field= *reg_field;
11829
 
    uint length;
11830
 
    memset(recinfo, 0, sizeof(*recinfo));
11831
 
    field->move_field(pos,(uchar*) 0,0);
11832
 
 
11833
 
    field->reset();
11834
 
    /*
11835
 
      Test if there is a default field value. The test for ->ptr is to skip
11836
 
      'offset' fields generated by initalize_tables
11837
 
    */
11838
 
    // Initialize the table field:
11839
 
    memset(field->ptr, 0, field->pack_length());
11840
 
 
11841
 
    length=field->pack_length();
11842
 
    pos+= length;
11843
 
 
11844
 
    /* Make entry for create table */
11845
 
    recinfo->length=length;
11846
 
    if (field->flags & BLOB_FLAG)
11847
 
      recinfo->type= (int) FIELD_BLOB;
11848
 
    else
11849
 
      recinfo->type=FIELD_NORMAL;
11850
 
 
11851
 
    field->table_name= &table->alias;
11852
 
  }
11853
 
 
11854
 
  //param->recinfo=recinfo;
11855
 
  //store_record(table,s->default_values);        // Make empty default record
11856
 
 
11857
 
  if (thd->variables.tmp_table_size == ~ (uint64_t) 0)          // No limit
11858
 
    share->max_rows= ~(ha_rows) 0;
11859
 
  else
11860
 
    share->max_rows= (ha_rows) (((share->db_type() == heap_hton) ?
11861
 
                                 min(thd->variables.tmp_table_size,
11862
 
                                     thd->variables.max_heap_table_size) :
11863
 
                                 thd->variables.tmp_table_size) /
11864
 
                                 share->reclength);
11865
 
  set_if_bigger(share->max_rows,1);             // For dummy start options
11866
 
 
11867
 
 
11868
 
  //// keyinfo= param->keyinfo;
11869
 
  if (true)
11870
 
  {
11871
 
    share->keys=1;
11872
 
    share->uniques= test(using_unique_constraint);
11873
 
    table->key_info=keyinfo;
11874
 
    keyinfo->key_part=key_part_info;
11875
 
    keyinfo->flags=HA_NOSAME;
11876
 
    keyinfo->usable_key_parts= keyinfo->key_parts= 1;
11877
 
    keyinfo->key_length=0;
11878
 
    keyinfo->rec_per_key=0;
11879
 
    keyinfo->algorithm= HA_KEY_ALG_UNDEF;
11880
 
    keyinfo->name= (char*) "weedout_key";
11881
 
    {
11882
 
      key_part_info->null_bit=0;
11883
 
      key_part_info->field=  field;
11884
 
      key_part_info->offset= field->offset(table->record[0]);
11885
 
      key_part_info->length= (uint16_t) field->key_length();
11886
 
      key_part_info->type=   (uint8_t) field->key_type();
11887
 
      key_part_info->key_type = FIELDFLAG_BINARY;
11888
 
      if (!using_unique_constraint)
11889
 
      {
11890
 
        if (!(key_field= field->new_key_field(thd->mem_root, table,
11891
 
                                              group_buff,
11892
 
                                              field->null_ptr,
11893
 
                                              field->null_bit)))
11894
 
          goto err;
11895
 
        key_part_info->key_part_flag|= HA_END_SPACE_ARE_EQUAL; //todo need this?
11896
 
      }
11897
 
      keyinfo->key_length+=  key_part_info->length;
11898
 
    }
11899
 
  }
11900
 
 
11901
 
  if (thd->is_fatal_error)                              // If end of memory
11902
 
    goto err;
11903
 
  share->db_record_offset= 1;
11904
 
  if (share->db_type() == myisam_hton)
11905
 
  {
11906
 
    recinfo++;
11907
 
    if (create_myisam_tmp_table(table, keyinfo, start_recinfo, &recinfo, 0))
11908
 
      goto err;
11909
 
  }
11910
 
  sjtbl->start_recinfo= start_recinfo;
11911
 
  sjtbl->recinfo=       recinfo;
11912
 
  if (open_tmp_table(table))
11913
 
    goto err;
11914
 
 
11915
 
  thd->mem_root= mem_root_save;
11916
 
  return(table);
11917
 
 
11918
 
err:
11919
 
  thd->mem_root= mem_root_save;
11920
 
  free_tmp_table(thd,table);                    /* purecov: inspected */
11921
 
  if (temp_pool_slot != MY_BIT_NONE)
11922
 
    bitmap_lock_clear_bit(&temp_pool, temp_pool_slot);
11923
 
  return(NULL);                         /* purecov: inspected */
11924
 
}
11925
 
 
11926
 
/****************************************************************************/
11927
 
 
11928
 
/**
11929
 
  Create a reduced TABLE object with properly set up Field list from a
11930
 
  list of field definitions.
11931
 
 
11932
 
    The created table doesn't have a table handler associated with
11933
 
    it, has no keys, no group/distinct, no copy_funcs array.
11934
 
    The sole purpose of this TABLE object is to use the power of Field
11935
 
    class to read/write data to/from table->record[0]. Then one can store
11936
 
    the record in any container (RB tree, hash, etc).
11937
 
    The table is created in THD mem_root, so are the table's fields.
11938
 
    Consequently, if you don't BLOB fields, you don't need to free it.
11939
 
 
11940
 
  @param thd         connection handle
11941
 
  @param field_list  list of column definitions
11942
 
 
11943
 
  @return
11944
 
    0 if out of memory, TABLE object in case of success
11945
 
*/
11946
 
 
11947
 
TABLE *create_virtual_tmp_table(THD *thd, List<Create_field> &field_list)
11948
 
{
11949
 
  uint field_count= field_list.elements;
11950
 
  uint blob_count= 0;
11951
 
  Field **field;
11952
 
  Create_field *cdef;                           /* column definition */
11953
 
  uint record_length= 0;
11954
 
  uint null_count= 0;                 /* number of columns which may be null */
11955
 
  uint null_pack_length;              /* NULL representation array length */
11956
 
  uint *blob_field;
11957
 
  uchar *bitmaps;
11958
 
  TABLE *table;
11959
 
  TABLE_SHARE *share;
11960
 
 
11961
 
  if (!multi_alloc_root(thd->mem_root,
11962
 
                        &table, sizeof(*table),
11963
 
                        &share, sizeof(*share),
11964
 
                        &field, (field_count + 1) * sizeof(Field*),
11965
 
                        &blob_field, (field_count+1) *sizeof(uint),
11966
 
                        &bitmaps, bitmap_buffer_size(field_count)*2,
11967
 
                        NullS))
11968
 
    return 0;
11969
 
 
11970
 
  memset(table, 0, sizeof(*table));
11971
 
  memset(share, 0, sizeof(*share));
11972
 
  table->field= field;
11973
 
  table->s= share;
11974
 
  share->blob_field= blob_field;
11975
 
  share->fields= field_count;
11976
 
  share->blob_ptr_size= portable_sizeof_char_ptr;
11977
 
  setup_tmp_table_column_bitmaps(table, bitmaps);
11978
 
 
11979
 
  /* Create all fields and calculate the total length of record */
11980
 
  List_iterator_fast<Create_field> it(field_list);
11981
 
  while ((cdef= it++))
11982
 
  {
11983
 
    *field= make_field(share, 0, cdef->length,
11984
 
                       (uchar*) (f_maybe_null(cdef->pack_flag) ? "" : 0),
11985
 
                       f_maybe_null(cdef->pack_flag) ? 1 : 0,
11986
 
                       cdef->pack_flag, cdef->sql_type, cdef->charset,
11987
 
                       cdef->unireg_check,
11988
 
                       cdef->interval, cdef->field_name);
11989
 
    if (!*field)
11990
 
      goto error;
11991
 
    (*field)->init(table);
11992
 
    record_length+= (*field)->pack_length();
11993
 
    if (! ((*field)->flags & NOT_NULL_FLAG))
11994
 
      null_count++;
11995
 
 
11996
 
    if ((*field)->flags & BLOB_FLAG)
11997
 
      share->blob_field[blob_count++]= (uint) (field - table->field);
11998
 
 
11999
 
    field++;
12000
 
  }
12001
 
  *field= NULL;                             /* mark the end of the list */
12002
 
  share->blob_field[blob_count]= 0;            /* mark the end of the list */
12003
 
  share->blob_fields= blob_count;
12004
 
 
12005
 
  null_pack_length= (null_count + 7)/8;
12006
 
  share->reclength= record_length + null_pack_length;
12007
 
  share->rec_buff_length= ALIGN_SIZE(share->reclength + 1);
12008
 
  table->record[0]= (uchar*) thd->alloc(share->rec_buff_length);
12009
 
  if (!table->record[0])
12010
 
    goto error;
12011
 
 
12012
 
  if (null_pack_length)
12013
 
  {
12014
 
    table->null_flags= (uchar*) table->record[0];
12015
 
    share->null_fields= null_count;
12016
 
    share->null_bytes= null_pack_length;
12017
 
  }
12018
 
 
12019
 
  table->in_use= thd;           /* field->reset() may access table->in_use */
12020
 
  {
12021
 
    /* Set up field pointers */
12022
 
    uchar *null_pos= table->record[0];
12023
 
    uchar *field_pos= null_pos + share->null_bytes;
12024
 
    uint null_bit= 1;
12025
 
 
12026
 
    for (field= table->field; *field; ++field)
12027
 
    {
12028
 
      Field *cur_field= *field;
12029
 
      if ((cur_field->flags & NOT_NULL_FLAG))
12030
 
        cur_field->move_field(field_pos);
12031
 
      else
12032
 
      {
12033
 
        cur_field->move_field(field_pos, (uchar*) null_pos, null_bit);
12034
 
        null_bit<<= 1;
12035
 
        if (null_bit == (1 << 8))
12036
 
        {
12037
 
          ++null_pos;
12038
 
          null_bit= 1;
12039
 
        }
12040
 
      }
12041
 
      cur_field->reset();
12042
 
 
12043
 
      field_pos+= cur_field->pack_length();
12044
 
    }
12045
 
  }
12046
 
  return table;
12047
 
error:
12048
 
  for (field= table->field; *field; ++field)
12049
 
    delete *field;                         /* just invokes field destructor */
12050
 
  return 0;
12051
 
}
12052
 
 
12053
 
 
12054
 
static bool open_tmp_table(TABLE *table)
12055
 
{
12056
 
  int error;
12057
 
  if ((error=table->file->ha_open(table, table->s->table_name.str,O_RDWR,
12058
 
                                  HA_OPEN_TMP_TABLE | HA_OPEN_INTERNAL_TABLE)))
12059
 
  {
12060
 
    table->file->print_error(error,MYF(0)); /* purecov: inspected */
12061
 
    table->db_stat=0;
12062
 
    return(1);
12063
 
  }
12064
 
  (void) table->file->extra(HA_EXTRA_QUICK);            /* Faster */
12065
 
  return(0);
12066
 
}
12067
 
 
12068
 
 
12069
 
/*
12070
 
  Create MyISAM temporary table
12071
 
 
12072
 
  SYNOPSIS
12073
 
    create_myisam_tmp_table()
12074
 
      table           Table object that descrimes the table to be created
12075
 
      keyinfo         Description of the index (there is always one index)
12076
 
      start_recinfo   MyISAM's column descriptions
12077
 
      recinfo INOUT   End of MyISAM's column descriptions
12078
 
      options         Option bits
12079
 
   
12080
 
  DESCRIPTION
12081
 
    Create a MyISAM temporary table according to passed description. The is
12082
 
    assumed to have one unique index or constraint.
12083
 
 
12084
 
    The passed array or MI_COLUMNDEF structures must have this form:
12085
 
 
12086
 
      1. 1-byte column (afaiu for 'deleted' flag) (note maybe not 1-byte
12087
 
         when there are many nullable columns)
12088
 
      2. Table columns
12089
 
      3. One free MI_COLUMNDEF element (*recinfo points here)
12090
 
   
12091
 
    This function may use the free element to create hash column for unique
12092
 
    constraint.
12093
 
 
12094
 
   RETURN
12095
 
     false - OK
12096
 
     true  - Error
12097
 
*/
12098
 
 
12099
 
static bool create_myisam_tmp_table(TABLE *table, KEY *keyinfo, 
12100
 
                                    MI_COLUMNDEF *start_recinfo,
12101
 
                                    MI_COLUMNDEF **recinfo, 
12102
 
                                    uint64_t options)
12103
 
{
12104
 
  int error;
12105
 
  MI_KEYDEF keydef;
12106
 
  MI_UNIQUEDEF uniquedef;
12107
 
  TABLE_SHARE *share= table->s;
12108
 
 
12109
 
  if (share->keys)
12110
 
  {                                             // Get keys for ni_create
12111
 
    bool using_unique_constraint=0;
12112
 
    HA_KEYSEG *seg= (HA_KEYSEG*) alloc_root(&table->mem_root,
12113
 
                                            sizeof(*seg) * keyinfo->key_parts);
12114
 
    if (!seg)
12115
 
      goto err;
12116
 
 
12117
 
    memset(seg, 0, sizeof(*seg) * keyinfo->key_parts);
12118
 
    if (keyinfo->key_length >= table->file->max_key_length() ||
12119
 
        keyinfo->key_parts > table->file->max_key_parts() ||
12120
 
        share->uniques)
12121
 
    {
12122
 
      /* Can't create a key; Make a unique constraint instead of a key */
12123
 
      share->keys=    0;
12124
 
      share->uniques= 1;
12125
 
      using_unique_constraint=1;
12126
 
      memset(&uniquedef, 0, sizeof(uniquedef));
12127
 
      uniquedef.keysegs=keyinfo->key_parts;
12128
 
      uniquedef.seg=seg;
12129
 
      uniquedef.null_are_equal=1;
12130
 
 
12131
 
      /* Create extra column for hash value */
12132
 
      memset(*recinfo, 0, sizeof(**recinfo));
12133
 
      (*recinfo)->type= FIELD_CHECK;
12134
 
      (*recinfo)->length=MI_UNIQUE_HASH_LENGTH;
12135
 
      (*recinfo)++;
12136
 
      share->reclength+=MI_UNIQUE_HASH_LENGTH;
12137
 
    }
12138
 
    else
12139
 
    {
12140
 
      /* Create an unique key */
12141
 
      memset(&keydef, 0, sizeof(keydef));
12142
 
      keydef.flag=HA_NOSAME | HA_BINARY_PACK_KEY | HA_PACK_KEY;
12143
 
      keydef.keysegs=  keyinfo->key_parts;
12144
 
      keydef.seg= seg;
12145
 
    }
12146
 
    for (uint i=0; i < keyinfo->key_parts ; i++,seg++)
12147
 
    {
12148
 
      Field *field=keyinfo->key_part[i].field;
12149
 
      seg->flag=     0;
12150
 
      seg->language= field->charset()->number;
12151
 
      seg->length=   keyinfo->key_part[i].length;
12152
 
      seg->start=    keyinfo->key_part[i].offset;
12153
 
      if (field->flags & BLOB_FLAG)
12154
 
      {
12155
 
        seg->type=
12156
 
        ((keyinfo->key_part[i].key_type & FIELDFLAG_BINARY) ?
12157
 
         HA_KEYTYPE_VARBINARY2 : HA_KEYTYPE_VARTEXT2);
12158
 
        seg->bit_start= (uint8_t)(field->pack_length() - share->blob_ptr_size);
12159
 
        seg->flag= HA_BLOB_PART;
12160
 
        seg->length=0;                  // Whole blob in unique constraint
12161
 
      }
12162
 
      else
12163
 
      {
12164
 
        seg->type= keyinfo->key_part[i].type;
12165
 
      }
12166
 
      if (!(field->flags & NOT_NULL_FLAG))
12167
 
      {
12168
 
        seg->null_bit= field->null_bit;
12169
 
        seg->null_pos= (uint) (field->null_ptr - (uchar*) table->record[0]);
12170
 
        /*
12171
 
          We are using a GROUP BY on something that contains NULL
12172
 
          In this case we have to tell MyISAM that two NULL should
12173
 
          on INSERT be regarded at the same value
12174
 
        */
12175
 
        if (!using_unique_constraint)
12176
 
          keydef.flag|= HA_NULL_ARE_EQUAL;
12177
 
      }
12178
 
    }
12179
 
  }
12180
 
  MI_CREATE_INFO create_info;
12181
 
  memset(&create_info, 0, sizeof(create_info));
12182
 
 
12183
 
  if ((options & (OPTION_BIG_TABLES | SELECT_SMALL_RESULT)) ==
12184
 
      OPTION_BIG_TABLES)
12185
 
    create_info.data_file_length= ~(uint64_t) 0;
12186
 
 
12187
 
  if ((error=mi_create(share->table_name.str, share->keys, &keydef,
12188
 
                       (uint) (*recinfo-start_recinfo),
12189
 
                       start_recinfo,
12190
 
                       share->uniques, &uniquedef,
12191
 
                       &create_info,
12192
 
                       HA_CREATE_TMP_TABLE)))
12193
 
  {
12194
 
    table->file->print_error(error,MYF(0));     /* purecov: inspected */
12195
 
    table->db_stat=0;
12196
 
    goto err;
12197
 
  }
12198
 
  status_var_increment(table->in_use->status_var.created_tmp_disk_tables);
12199
 
  share->db_record_offset= 1;
12200
 
  return(0);
12201
 
 err:
12202
 
  return(1);
12203
 
}
12204
 
 
12205
 
 
12206
 
void
12207
 
free_tmp_table(THD *thd, TABLE *entry)
12208
 
{
12209
 
  MEM_ROOT own_root= entry->mem_root;
12210
 
  const char *save_proc_info;
12211
 
 
12212
 
  save_proc_info=thd->proc_info;
12213
 
  thd_proc_info(thd, "removing tmp table");
12214
 
 
12215
 
  if (entry->file)
12216
 
  {
12217
 
    if (entry->db_stat)
12218
 
      entry->file->ha_drop_table(entry->s->table_name.str);
12219
 
    else
12220
 
      entry->file->ha_delete_table(entry->s->table_name.str);
12221
 
    delete entry->file;
12222
 
  }
12223
 
 
12224
 
  /* free blobs */
12225
 
  for (Field **ptr=entry->field ; *ptr ; ptr++)
12226
 
    (*ptr)->free();
12227
 
  free_io_cache(entry);
12228
 
 
12229
 
  if (entry->temp_pool_slot != MY_BIT_NONE)
12230
 
    bitmap_lock_clear_bit(&temp_pool, entry->temp_pool_slot);
12231
 
 
12232
 
  plugin_unlock(0, entry->s->db_plugin);
12233
 
 
12234
 
  free_root(&own_root, MYF(0)); /* the table is allocated in its own root */
12235
 
  thd_proc_info(thd, save_proc_info);
12236
 
 
12237
 
  return;
12238
 
}
12239
 
 
12240
 
/**
12241
 
  If a HEAP table gets full, create a MyISAM table and copy all rows
12242
 
  to this.
12243
 
*/
12244
 
 
12245
 
bool create_myisam_from_heap(THD *thd, TABLE *table,
12246
 
                             MI_COLUMNDEF *start_recinfo,
12247
 
                             MI_COLUMNDEF **recinfo, 
12248
 
                             int error, bool ignore_last_dupp_key_error)
12249
 
{
12250
 
  TABLE new_table;
12251
 
  TABLE_SHARE share;
12252
 
  const char *save_proc_info;
12253
 
  int write_err;
12254
 
 
12255
 
  if (table->s->db_type() != heap_hton || 
12256
 
      error != HA_ERR_RECORD_FILE_FULL)
12257
 
  {
12258
 
    table->file->print_error(error,MYF(0));
12259
 
    return(1);
12260
 
  }
12261
 
  new_table= *table;
12262
 
  share= *table->s;
12263
 
  new_table.s= &share;
12264
 
  new_table.s->db_plugin= ha_lock_engine(thd, myisam_hton);
12265
 
  if (!(new_table.file= get_new_handler(&share, &new_table.mem_root,
12266
 
                                        new_table.s->db_type())))
12267
 
    return(1);                          // End of memory
12268
 
 
12269
 
  save_proc_info=thd->proc_info;
12270
 
  thd_proc_info(thd, "converting HEAP to MyISAM");
12271
 
 
12272
 
  if (create_myisam_tmp_table(&new_table, table->key_info, start_recinfo,
12273
 
                              recinfo, thd->lex->select_lex.options | 
12274
 
                                               thd->options))
12275
 
    goto err2;
12276
 
  if (open_tmp_table(&new_table))
12277
 
    goto err1;
12278
 
  if (table->file->indexes_are_disabled())
12279
 
    new_table.file->ha_disable_indexes(HA_KEY_SWITCH_ALL);
12280
 
  table->file->ha_index_or_rnd_end();
12281
 
  table->file->ha_rnd_init(1);
12282
 
  if (table->no_rows)
12283
 
  {
12284
 
    new_table.file->extra(HA_EXTRA_NO_ROWS);
12285
 
    new_table.no_rows=1;
12286
 
  }
12287
 
 
12288
 
#ifdef TO_BE_DONE_LATER_IN_4_1
12289
 
  /*
12290
 
    To use start_bulk_insert() (which is new in 4.1) we need to find
12291
 
    all places where a corresponding end_bulk_insert() should be put.
12292
 
  */
12293
 
  table->file->info(HA_STATUS_VARIABLE); /* update table->file->stats.records */
12294
 
  new_table.file->ha_start_bulk_insert(table->file->stats.records);
12295
 
#else
12296
 
  /* HA_EXTRA_WRITE_CACHE can stay until close, no need to disable it */
12297
 
  new_table.file->extra(HA_EXTRA_WRITE_CACHE);
12298
 
#endif
12299
 
 
12300
 
  /*
12301
 
    copy all old rows from heap table to MyISAM table
12302
 
    This is the only code that uses record[1] to read/write but this
12303
 
    is safe as this is a temporary MyISAM table without timestamp/autoincrement.
12304
 
  */
12305
 
  while (!table->file->rnd_next(new_table.record[1]))
12306
 
  {
12307
 
    write_err= new_table.file->ha_write_row(new_table.record[1]);
12308
 
    if (write_err)
12309
 
      goto err;
12310
 
  }
12311
 
  /* copy row that filled HEAP table */
12312
 
  if ((write_err=new_table.file->ha_write_row(table->record[0])))
12313
 
  {
12314
 
    if (new_table.file->is_fatal_error(write_err, HA_CHECK_DUP) ||
12315
 
        !ignore_last_dupp_key_error)
12316
 
      goto err;
12317
 
  }
12318
 
 
12319
 
  /* remove heap table and change to use myisam table */
12320
 
  (void) table->file->ha_rnd_end();
12321
 
  (void) table->file->close();                  // This deletes the table !
12322
 
  delete table->file;
12323
 
  table->file=0;
12324
 
  plugin_unlock(0, table->s->db_plugin);
12325
 
  share.db_plugin= my_plugin_lock(0, &share.db_plugin);
12326
 
  new_table.s= table->s;                       // Keep old share
12327
 
  *table= new_table;
12328
 
  *table->s= share;
12329
 
  
12330
 
  table->file->change_table_ptr(table, table->s);
12331
 
  table->use_all_columns();
12332
 
  if (save_proc_info)
12333
 
  {
12334
 
    const char *new_proc_info=
12335
 
      (!strcmp(save_proc_info,"Copying to tmp table") ?
12336
 
      "Copying to tmp table on disk" : save_proc_info);
12337
 
    thd_proc_info(thd, new_proc_info);
12338
 
  }
12339
 
  return(0);
12340
 
 
12341
 
 err:
12342
 
  table->file->print_error(write_err, MYF(0));
12343
 
  (void) table->file->ha_rnd_end();
12344
 
  (void) new_table.file->close();
12345
 
 err1:
12346
 
  new_table.file->ha_delete_table(new_table.s->table_name.str);
12347
 
 err2:
12348
 
  delete new_table.file;
12349
 
  thd_proc_info(thd, save_proc_info);
12350
 
  table->mem_root= new_table.mem_root;
12351
 
  return(1);
12352
 
}
12353
 
 
 
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
}
12354
3199
 
12355
3200
/**
12356
3201
  @details
12363
3208
  @return
12364
3209
    end_select function to use. This function can't fail.
12365
3210
*/
12366
 
 
12367
3211
Next_select_func setup_end_select_func(JOIN *join)
12368
3212
{
12369
 
  TABLE *table= join->tmp_table;
12370
 
  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;
12371
3215
  Next_select_func end_select;
12372
3216
 
12373
3217
  /* Set up select_end */
12374
3218
  if (table)
12375
3219
  {
12376
 
    if (table->group && tmp_tbl->sum_func_count && 
 
3220
    if (table->group && tmp_tbl->sum_func_count &&
12377
3221
        !tmp_tbl->precomputed_group_by)
12378
3222
    {
12379
3223
      if (table->s->keys)
12380
3224
      {
12381
 
        end_select=end_update;
 
3225
        end_select= end_update;
12382
3226
      }
12383
3227
      else
12384
3228
      {
12385
 
        end_select=end_unique_update;
 
3229
        end_select= end_unique_update;
12386
3230
      }
12387
3231
    }
12388
3232
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
12397
3241
        /*
12398
3242
          A preceding call to create_tmp_table in the case when loose
12399
3243
          index scan is used guarantees that
12400
 
          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
12401
3245
          by functions. It is OK here to use memcpy since we copy
12402
3246
          Item_sum pointers into an array of Item pointers.
12403
3247
        */
12419
3263
  return end_select;
12420
3264
}
12421
3265
 
12422
 
 
12423
3266
/**
12424
3267
  Make a join of all tables and write it on socket or to table.
12425
3268
 
12430
3273
  @retval
12431
3274
    -1  if error should be sent
12432
3275
*/
12433
 
 
12434
 
static int
12435
 
do_select(JOIN *join,List<Item> *fields,TABLE *table)
 
3276
int do_select(JOIN *join, List<Item> *fields, Table *table)
12436
3277
{
12437
3278
  int rc= 0;
12438
3279
  enum_nested_loop_state error= NESTED_LOOP_OK;
12439
 
  JOIN_TAB *join_tab= NULL;
12440
 
  
 
3280
  JoinTable *join_tab= NULL;
 
3281
 
12441
3282
  join->tmp_table= table;                       /* Save for easy recursion */
12442
3283
  join->fields= fields;
12443
3284
 
12444
3285
  if (table)
12445
3286
  {
12446
 
    VOID(table->file->extra(HA_EXTRA_WRITE_CACHE));
12447
 
    empty_record(table);
 
3287
    table->cursor->extra(HA_EXTRA_WRITE_CACHE);
 
3288
    table->emptyRecord();
12448
3289
    if (table->group && join->tmp_table_param.sum_func_count &&
12449
 
        table->s->keys && !table->file->inited)
12450
 
      table->file->ha_index_init(0, 0);
 
3290
        table->s->keys && !table->cursor->inited)
 
3291
      table->cursor->ha_index_init(0, 0);
12451
3292
  }
12452
3293
  /* Set up select_end */
12453
3294
  Next_select_func end_select= setup_end_select_func(join);
12468
3309
    {
12469
3310
      error= (*end_select)(join, 0, 0);
12470
3311
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
12471
 
        error= (*end_select)(join, 0, 1);
 
3312
              error= (*end_select)(join, 0, 1);
12472
3313
 
12473
3314
      /*
12474
3315
        If we don't go through evaluate_join_record(), do the counting
12476
3317
        so we don't touch it here.
12477
3318
      */
12478
3319
      join->examined_rows++;
12479
 
      join->thd->row_count++;
 
3320
      join->session->row_count++;
12480
3321
      assert(join->examined_rows <= 1);
12481
3322
    }
12482
3323
    else if (join->send_row_on_empty_set())
12506
3347
    if (!table)                                 // If sending data to client
12507
3348
    {
12508
3349
      /*
12509
 
        The following will unlock all cursors if the command wasn't an
12510
 
        update command
 
3350
        The following will unlock all cursors if the command wasn't an
 
3351
        update command
12511
3352
      */
12512
3353
      join->join_free();                        // Unlock all cursors
12513
3354
      if (join->result->send_eof())
12514
 
        rc= 1;                                  // Don't send error
 
3355
        rc= 1;                                  // Don't send error
12515
3356
    }
12516
3357
  }
12517
3358
  else
12519
3360
  if (table)
12520
3361
  {
12521
3362
    int tmp, new_errno= 0;
12522
 
    if ((tmp=table->file->extra(HA_EXTRA_NO_CACHE)))
 
3363
    if ((tmp=table->cursor->extra(HA_EXTRA_NO_CACHE)))
12523
3364
    {
12524
3365
      new_errno= tmp;
12525
3366
    }
12526
 
    if ((tmp=table->file->ha_index_or_rnd_end()))
 
3367
    if ((tmp=table->cursor->ha_index_or_rnd_end()))
12527
3368
    {
12528
3369
      new_errno= tmp;
12529
3370
    }
12530
3371
    if (new_errno)
12531
 
      table->file->print_error(new_errno,MYF(0));
 
3372
      table->print_error(new_errno,MYF(0));
12532
3373
  }
12533
 
  return(join->thd->is_error() ? -1 : rc);
 
3374
  return(join->session->is_error() ? -1 : rc);
12534
3375
}
12535
3376
 
12536
 
 
12537
 
enum_nested_loop_state
12538
 
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)
12539
3378
{
12540
3379
  enum_nested_loop_state rc;
12541
3380
 
12546
3385
      rc= sub_select(join,join_tab,end_of_records);
12547
3386
    return rc;
12548
3387
  }
12549
 
  if (join->thd->killed)                // If aborted by user
 
3388
  if (join->session->killed)            // If aborted by user
12550
3389
  {
12551
 
    join->thd->send_kill_message();
12552
 
    return NESTED_LOOP_KILLED;                   /* purecov: inspected */
 
3390
    join->session->send_kill_message();
 
3391
    return NESTED_LOOP_KILLED;
12553
3392
  }
12554
3393
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
12555
3394
  {
12556
 
    if (!store_record_in_cache(&join_tab->cache))
 
3395
    if (! store_record_in_cache(&join_tab->cache))
12557
3396
      return NESTED_LOOP_OK;                     // There is more room in cache
12558
3397
    return flush_cached_records(join,join_tab,false);
12559
3398
  }
12566
3405
/**
12567
3406
  Retrieve records ends with a given beginning from the result of a join.
12568
3407
 
12569
 
    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
12570
3409
    preceding the table join_tab in the execution plan, the function
12571
3410
    retrieves all matching full records from the result set and
12572
 
    send them to the result set stream. 
 
3411
    send them to the result set stream.
12573
3412
 
12574
3413
  @note
12575
3414
    The function effectively implements the  final (n-k) nested loops
12609
3448
    first row with t3.a=t1.a has been encountered.
12610
3449
    Thus, the second predicate P2 is supplied with a guarded value that are
12611
3450
    stored in the field 'found' of the first inner table for the outer join
12612
 
    (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
12613
3452
    of table t1  appears, the value becomes true. For now on the predicate
12614
3453
    is evaluated immediately after the row of table t2 has been read.
12615
3454
    When the first row with t3.a=t1.a has been encountered all
12617
3456
    Only when all of them are true the row is sent to the output stream.
12618
3457
    If not, the function returns to the lowest nest level that has a false
12619
3458
    attached condition.
12620
 
    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
12621
3460
    the above example the on expression were (t3.a=t1.a AND t2.a=t1.a),
12622
3461
    then t1.a=t2.a would be pushed down to table t2, and without any
12623
3462
    guard.
12627
3466
    is complemented by nulls  for t2 and t3. Then the pushed down predicates
12628
3467
    are checked for the composed row almost in the same way as it had
12629
3468
    been done for the first row with a match. The only difference is
12630
 
    the predicates from on expressions are not checked. 
 
3469
    the predicates from on expressions are not checked.
12631
3470
 
12632
3471
  @par
12633
3472
  @b IMPLEMENTATION
12643
3482
    and a pointer to a guarding boolean variable.
12644
3483
    When the value of the guard variable is true the value of the object
12645
3484
    is the same as the value of the predicate, otherwise it's just returns
12646
 
    true. 
12647
 
    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
12648
3487
    to t is remembered in the field 'return_tab' of the join structure.
12649
3488
    Consider the following query:
12650
3489
    @code
12661
3500
    t5.a=t3.a is found, the pushed down predicate t4.b=2 OR t4.b IS NULL
12662
3501
    becomes 'activated', as well the predicate t4.a=t2.a. But
12663
3502
    the predicate (t2.b=5 OR t2.b IS NULL) can not be checked until
12664
 
    t4.a=t2.a becomes true. 
 
3503
    t4.a=t2.a becomes true.
12665
3504
    In order not to re-evaluate the predicates that were already evaluated
12666
3505
    as attached pushed down predicates, a pointer to the the first
12667
3506
    most inner unmatched table is maintained in join_tab->first_unmatched.
12668
3507
    Thus, when the first row from t5 with t5.a=t3.a is found
12669
 
    this pointer for t5 is changed from t4 to t2.             
 
3508
    this pointer for t5 is changed from t4 to t2.
12670
3509
 
12671
3510
    @par
12672
3511
    @b STRUCTURE @b NOTES
12677
3516
  @param join      pointer to the structure providing all context info for
12678
3517
                   the query
12679
3518
  @param join_tab  the first next table of the execution plan to be retrieved
12680
 
  @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
12681
3520
 
12682
3521
  @return
12683
3522
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
12684
3523
*/
12685
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
12686
 
 
12687
 
enum_nested_loop_state
12688
 
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)
12689
3525
{
12690
3526
  join_tab->table->null_row=0;
12691
3527
  if (end_of_records)
12695
3531
  enum_nested_loop_state rc;
12696
3532
  READ_RECORD *info= &join_tab->read_record;
12697
3533
 
12698
 
  if (join_tab->flush_weedout_table)
12699
 
  {
12700
 
    do_sj_reset(join_tab->flush_weedout_table);
12701
 
  }
12702
 
 
12703
3534
  if (join->resume_nested_loop)
12704
3535
  {
12705
3536
    /* If not the last table, plunge down the nested loop */
12726
3557
      /* Set first_unmatched for the last inner table of this group */
12727
3558
      join_tab->last_inner->first_unmatched= join_tab;
12728
3559
    }
12729
 
    join->thd->row_count= 0;
 
3560
    join->session->row_count= 0;
12730
3561
 
12731
3562
    error= (*join_tab->read_first_record)(join_tab);
12732
3563
    rc= evaluate_join_record(join, join_tab, error);
12733
3564
  }
12734
 
  
12735
 
  /* 
12736
 
    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
12737
3568
    change should probably be made in 5.1, too.
12738
3569
  */
12739
3570
  while (rc == NESTED_LOOP_OK && join->return_tab >= join_tab)
12751
3582
  return rc;
12752
3583
}
12753
3584
 
12754
 
 
12755
 
 
12756
 
 
12757
 
/*
12758
 
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
12759
 
 
12760
 
  SYNPOSIS
12761
 
    do_sj_dups_weedout()
12762
 
      
12763
 
  RETURN
12764
 
    -1  Error
12765
 
    1   The row combination is a duplicate (discard it)
12766
 
    0   The row combination is not a duplicate (continue)
12767
 
*/
12768
 
 
12769
 
int do_sj_dups_weedout(THD *thd, SJ_TMP_TABLE *sjtbl) 
12770
 
{
12771
 
  int error;
12772
 
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
12773
 
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
12774
 
  uchar *ptr= sjtbl->tmp_table->record[0] + 1;
12775
 
  uchar *nulls_ptr= ptr;
12776
 
  
12777
 
  /* Put the the rowids tuple into table->record[0]: */
12778
 
 
12779
 
  // 1. Store the length 
12780
 
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
12781
 
  {
12782
 
    *ptr= (uchar)(sjtbl->rowid_len + sjtbl->null_bytes);
12783
 
    ptr++;
12784
 
  }
12785
 
  else
12786
 
  {
12787
 
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
12788
 
    ptr += 2;
12789
 
  }
12790
 
 
12791
 
  // 2. Zero the null bytes 
12792
 
  if (sjtbl->null_bytes)
12793
 
  {
12794
 
    memset(ptr, 0, sjtbl->null_bytes);
12795
 
    ptr += sjtbl->null_bytes; 
12796
 
  }
12797
 
 
12798
 
  // 3. Put the rowids
12799
 
  for (uint i=0; tab != tab_end; tab++, i++)
12800
 
  {
12801
 
    handler *h= tab->join_tab->table->file;
12802
 
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
12803
 
    {
12804
 
      /* It's a NULL-complemented row */
12805
 
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
12806
 
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
12807
 
    }
12808
 
    else
12809
 
    {
12810
 
      /* Copy the rowid value */
12811
 
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
12812
 
        h->position(tab->join_tab->table->record[0]);
12813
 
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
12814
 
    }
12815
 
  }
12816
 
 
12817
 
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
12818
 
  if (error)
12819
 
  {
12820
 
    /* create_myisam_from_heap will generate error if needed */
12821
 
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
12822
 
        create_myisam_from_heap(thd, sjtbl->tmp_table, sjtbl->start_recinfo, 
12823
 
                                &sjtbl->recinfo, error, 1))
12824
 
      return -1;
12825
 
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
12826
 
    return 1;
12827
 
  }
12828
 
  return 0;
12829
 
}
12830
 
 
12831
 
 
12832
 
/*
12833
 
  SemiJoinDuplicateElimination: Reset the temporary table
12834
 
*/
12835
 
 
12836
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
12837
 
{
12838
 
  if (sj_tbl->tmp_table)
12839
 
    return sj_tbl->tmp_table->file->ha_delete_all_rows();
12840
 
  return 0;
12841
 
}
12842
 
 
12843
 
/*
12844
 
  Process one record of the nested loop join.
12845
 
 
12846
 
    This function will evaluate parts of WHERE/ON clauses that are
12847
 
    applicable to the partial record on hand and in case of success
12848
 
    submit this record to the next level of the nested loop.
12849
 
*/
12850
 
 
12851
 
static enum_nested_loop_state
12852
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
12853
 
                     int error)
12854
 
{
12855
 
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
12856
 
  ha_rows found_records=join->found_records;
12857
 
  COND *select_cond= join_tab->select_cond;
12858
 
 
12859
 
  if (error > 0 || (join->thd->is_error()))     // Fatal error
12860
 
    return NESTED_LOOP_ERROR;
12861
 
  if (error < 0)
12862
 
    return NESTED_LOOP_NO_MORE_ROWS;
12863
 
  if (join->thd->killed)                        // Aborted by user
12864
 
  {
12865
 
    join->thd->send_kill_message();
12866
 
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
12867
 
  }
12868
 
  if (!select_cond || select_cond->val_int())
12869
 
  {
12870
 
    /*
12871
 
      There is no select condition or the attached pushed down
12872
 
      condition is true => a match is found.
12873
 
    */
12874
 
    bool found= 1;
12875
 
    while (join_tab->first_unmatched && found)
12876
 
    {
12877
 
      /*
12878
 
        The while condition is always false if join_tab is not
12879
 
        the last inner join table of an outer join operation.
12880
 
      */
12881
 
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
12882
 
      /*
12883
 
        Mark that a match for current outer table is found.
12884
 
        This activates push down conditional predicates attached
12885
 
        to the all inner tables of the outer join.
12886
 
      */
12887
 
      first_unmatched->found= 1;
12888
 
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
12889
 
      {
12890
 
        if (tab->table->reginfo.not_exists_optimize)
12891
 
          return NESTED_LOOP_NO_MORE_ROWS;
12892
 
        /* Check all predicates that has just been activated. */
12893
 
        /*
12894
 
          Actually all predicates non-guarded by first_unmatched->found
12895
 
          will be re-evaluated again. It could be fixed, but, probably,
12896
 
          it's not worth doing now.
12897
 
        */
12898
 
        if (tab->select_cond && !tab->select_cond->val_int())
12899
 
        {
12900
 
          /* The condition attached to table tab is false */
12901
 
          if (tab == join_tab)
12902
 
            found= 0;
12903
 
          else
12904
 
          {
12905
 
            /*
12906
 
              Set a return point if rejected predicate is attached
12907
 
              not to the last table of the current nest level.
12908
 
            */
12909
 
            join->return_tab= tab;
12910
 
            return NESTED_LOOP_OK;
12911
 
          }
12912
 
        }
12913
 
      }
12914
 
      /*
12915
 
        Check whether join_tab is not the last inner table
12916
 
        for another embedding outer join.
12917
 
      */
12918
 
      if ((first_unmatched= first_unmatched->first_upper) &&
12919
 
          first_unmatched->last_inner != join_tab)
12920
 
        first_unmatched= 0;
12921
 
      join_tab->first_unmatched= first_unmatched;
12922
 
    }
12923
 
 
12924
 
    JOIN_TAB *return_tab= join->return_tab;
12925
 
    join_tab->found_match= true;
12926
 
    if (join_tab->check_weed_out_table)
12927
 
    {
12928
 
      int res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table);
12929
 
      if (res == -1)
12930
 
        return NESTED_LOOP_ERROR;
12931
 
      if (res == 1)
12932
 
        return NESTED_LOOP_OK;
12933
 
    }
12934
 
    else if (join_tab->do_firstmatch)
12935
 
    {
12936
 
      /* 
12937
 
        We should return to the join_tab->do_firstmatch after we have 
12938
 
        enumerated all the suffixes for current prefix row combination
12939
 
      */
12940
 
      return_tab= join_tab->do_firstmatch;
12941
 
    }
12942
 
 
12943
 
    /*
12944
 
      It was not just a return to lower loop level when one
12945
 
      of the newly activated predicates is evaluated as false
12946
 
      (See above join->return_tab= tab).
12947
 
    */
12948
 
    join->examined_rows++;
12949
 
    join->thd->row_count++;
12950
 
 
12951
 
    if (found)
12952
 
    {
12953
 
      enum enum_nested_loop_state rc;
12954
 
      /* A match from join_tab is found for the current partial join. */
12955
 
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
12956
 
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
12957
 
        return rc;
12958
 
      if (return_tab < join->return_tab)
12959
 
        join->return_tab= return_tab;
12960
 
 
12961
 
      if (join->return_tab < join_tab)
12962
 
        return NESTED_LOOP_OK;
12963
 
      /*
12964
 
        Test if this was a SELECT DISTINCT query on a table that
12965
 
        was not in the field list;  In this case we can abort if
12966
 
        we found a row, as no new rows can be added to the result.
12967
 
      */
12968
 
      if (not_used_in_distinct && found_records != join->found_records)
12969
 
        return NESTED_LOOP_NO_MORE_ROWS;
12970
 
    }
12971
 
    else
12972
 
      join_tab->read_record.file->unlock_row();
12973
 
  }
12974
 
  else
12975
 
  {
12976
 
    /*
12977
 
      The condition pushed down to the table join_tab rejects all rows
12978
 
      with the beginning coinciding with the current partial join.
12979
 
    */
12980
 
    join->examined_rows++;
12981
 
    join->thd->row_count++;
12982
 
    join_tab->read_record.file->unlock_row();
12983
 
  }
12984
 
  return NESTED_LOOP_OK;
12985
 
}
12986
 
 
12987
 
 
12988
 
/**
12989
 
 
12990
 
  @details
12991
 
    Construct a NULL complimented partial join record and feed it to the next
12992
 
    level of the nested loop. This function is used in case we have
12993
 
    an OUTER join and no matching record was found.
12994
 
*/
12995
 
 
12996
 
static enum_nested_loop_state
12997
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
12998
 
{
12999
 
  /*
13000
 
    The table join_tab is the first inner table of a outer join operation
13001
 
    and no matches has been found for the current outer row.
13002
 
  */
13003
 
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
13004
 
  /* Cache variables for faster loop */
13005
 
  COND *select_cond;
13006
 
  for ( ; join_tab <= last_inner_tab ; join_tab++)
13007
 
  {
13008
 
    /* Change the the values of guard predicate variables. */
13009
 
    join_tab->found= 1;
13010
 
    join_tab->not_null_compl= 0;
13011
 
    /* The outer row is complemented by nulls for each inner tables */
13012
 
    restore_record(join_tab->table,s->default_values);  // Make empty record
13013
 
    mark_as_null_row(join_tab->table);       // For group by without error
13014
 
    select_cond= join_tab->select_cond;
13015
 
    /* Check all attached conditions for inner table rows. */
13016
 
    if (select_cond && !select_cond->val_int())
13017
 
      return NESTED_LOOP_OK;
13018
 
  }
13019
 
  join_tab--;
13020
 
  /*
13021
 
    The row complemented by nulls might be the first row
13022
 
    of embedding outer joins.
13023
 
    If so, perform the same actions as in the code
13024
 
    for the first regular outer join row above.
13025
 
  */
13026
 
  for ( ; ; )
13027
 
  {
13028
 
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
13029
 
    if ((first_unmatched= first_unmatched->first_upper) &&
13030
 
        first_unmatched->last_inner != join_tab)
13031
 
      first_unmatched= 0;
13032
 
    join_tab->first_unmatched= first_unmatched;
13033
 
    if (!first_unmatched)
13034
 
      break;
13035
 
    first_unmatched->found= 1;
13036
 
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
13037
 
    {
13038
 
      if (tab->select_cond && !tab->select_cond->val_int())
13039
 
      {
13040
 
        join->return_tab= tab;
13041
 
        return NESTED_LOOP_OK;
13042
 
      }
13043
 
    }
13044
 
  }
13045
 
  /*
13046
 
    The row complemented by nulls satisfies all conditions
13047
 
    attached to inner tables.
13048
 
    Send the row complemented by nulls to be joined with the
13049
 
    remaining tables.
13050
 
  */
13051
 
  return (*join_tab->next_select)(join, join_tab+1, 0);
13052
 
}
13053
 
 
13054
 
 
13055
 
static enum_nested_loop_state
13056
 
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
13057
 
{
13058
 
  enum_nested_loop_state rc= NESTED_LOOP_OK;
13059
 
  int error;
13060
 
  READ_RECORD *info;
13061
 
 
13062
 
  join_tab->table->null_row= 0;
13063
 
  if (!join_tab->cache.records)
13064
 
    return NESTED_LOOP_OK;                      /* Nothing to do */
13065
 
  if (skip_last)
13066
 
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
13067
 
  if (join_tab->use_quick == 2)
13068
 
  {
13069
 
    if (join_tab->select->quick)
13070
 
    {                                   /* Used quick select last. reset it */
13071
 
      delete join_tab->select->quick;
13072
 
      join_tab->select->quick=0;
13073
 
    }
13074
 
  }
13075
 
 /* read through all records */
13076
 
  if ((error=join_init_read_record(join_tab)))
13077
 
  {
13078
 
    reset_cache_write(&join_tab->cache);
13079
 
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
13080
 
  }
13081
 
 
13082
 
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
13083
 
  {
13084
 
    tmp->status=tmp->table->status;
13085
 
    tmp->table->status=0;
13086
 
  }
13087
 
 
13088
 
  info= &join_tab->read_record;
13089
 
  do
13090
 
  {
13091
 
    if (join->thd->killed)
13092
 
    {
13093
 
      join->thd->send_kill_message();
13094
 
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
13095
 
    }
13096
 
    SQL_SELECT *select=join_tab->select;
13097
 
    if (rc == NESTED_LOOP_OK &&
13098
 
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
13099
 
    {
13100
 
      uint i;
13101
 
      reset_cache_read(&join_tab->cache);
13102
 
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
13103
 
      {
13104
 
        read_cached_record(join_tab);
13105
 
        if (!select || !select->skip_record())
13106
 
        {
13107
 
          int res= 0;
13108
 
          if (!join_tab->check_weed_out_table || 
13109
 
              !(res= do_sj_dups_weedout(join->thd, join_tab->check_weed_out_table)))
13110
 
          {
13111
 
            rc= (join_tab->next_select)(join,join_tab+1,0);
13112
 
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
13113
 
            {
13114
 
              reset_cache_write(&join_tab->cache);
13115
 
              return rc;
13116
 
            }
13117
 
          }
13118
 
          if (res == -1)
13119
 
            return NESTED_LOOP_ERROR;
13120
 
        }
13121
 
      }
13122
 
    }
13123
 
  } while (!(error=info->read_record(info)));
13124
 
 
13125
 
  if (skip_last)
13126
 
    read_cached_record(join_tab);               // Restore current record
13127
 
  reset_cache_write(&join_tab->cache);
13128
 
  if (error > 0)                                // Fatal error
13129
 
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
13130
 
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
13131
 
    tmp2->table->status=tmp2->status;
13132
 
  return NESTED_LOOP_OK;
13133
 
}
13134
 
 
13135
 
 
13136
 
/*****************************************************************************
13137
 
  The different ways to read a record
13138
 
  Returns -1 if row was not found, 0 if row was found and 1 on errors
13139
 
*****************************************************************************/
13140
 
 
13141
 
/** Help function when we get some an error from the table handler. */
13142
 
 
13143
 
int report_error(TABLE *table, int error)
13144
 
{
13145
 
  if (error == HA_ERR_END_OF_FILE || error == HA_ERR_KEY_NOT_FOUND)
13146
 
  {
13147
 
    table->status= STATUS_GARBAGE;
13148
 
    return -1;                                  // key not found; ok
13149
 
  }
13150
 
  /*
13151
 
    Locking reads can legally return also these errors, do not
13152
 
    print them to the .err log
13153
 
  */
13154
 
  if (error != HA_ERR_LOCK_DEADLOCK && error != HA_ERR_LOCK_WAIT_TIMEOUT)
13155
 
    sql_print_error("Got error %d when reading table '%s'",
13156
 
                    error, table->s->path.str);
13157
 
  table->file->print_error(error,MYF(0));
13158
 
  return 1;
13159
 
}
13160
 
 
13161
 
 
13162
 
int safe_index_read(JOIN_TAB *tab)
13163
 
{
13164
 
  int error;
13165
 
  TABLE *table= tab->table;
13166
 
  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],
13167
3590
                                         tab->ref.key_buff,
13168
3591
                                         make_prev_keypart_map(tab->ref.key_parts),
13169
3592
                                         HA_READ_KEY_EXACT)))
13170
 
    return report_error(table, error);
 
3593
    return table->report_error(error);
13171
3594
  return 0;
13172
3595
}
13173
3596
 
13174
 
 
13175
 
static int
13176
 
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
3597
int join_read_const_table(JoinTable *tab, optimizer::Position *pos)
13177
3598
{
13178
3599
  int error;
13179
 
  TABLE *table=tab->table;
 
3600
  Table *table=tab->table;
13180
3601
  table->const_table=1;
13181
3602
  table->null_row=0;
13182
3603
  table->status=STATUS_NO_RECORD;
13183
 
  
13184
 
  if (tab->type == JT_SYSTEM)
 
3604
 
 
3605
  if (tab->type == AM_SYSTEM)
13185
3606
  {
13186
3607
    if ((error=join_read_system(tab)))
13187
3608
    {                                           // Info for DESCRIBE
13188
3609
      tab->info="const row not found";
13189
3610
      /* Mark for EXPLAIN that the row was not found */
13190
 
      pos->records_read=0.0;
13191
 
      pos->ref_depend_map= 0;
13192
 
      if (!table->maybe_null || error > 0)
13193
 
        return(error);
 
3611
      pos->setFanout(0.0);
 
3612
      pos->clearRefDependMap();
 
3613
      if (! table->maybe_null || error > 0)
 
3614
        return(error);
13194
3615
    }
13195
3616
  }
13196
3617
  else
13197
3618
  {
13198
 
    if (!table->key_read && table->covering_keys.is_set(tab->ref.key) &&
13199
 
        !table->no_keyread &&
13200
 
        (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)
13201
3623
    {
13202
3624
      table->key_read=1;
13203
 
      table->file->extra(HA_EXTRA_KEYREAD);
 
3625
      table->cursor->extra(HA_EXTRA_KEYREAD);
13204
3626
      tab->index= tab->ref.key;
13205
3627
    }
13206
3628
    error=join_read_const(tab);
13207
3629
    if (table->key_read)
13208
3630
    {
13209
3631
      table->key_read=0;
13210
 
      table->file->extra(HA_EXTRA_NO_KEYREAD);
 
3632
      table->cursor->extra(HA_EXTRA_NO_KEYREAD);
13211
3633
    }
13212
3634
    if (error)
13213
3635
    {
13214
3636
      tab->info="unique row not found";
13215
3637
      /* Mark for EXPLAIN that the row was not found */
13216
 
      pos->records_read=0.0;
13217
 
      pos->ref_depend_map= 0;
 
3638
      pos->setFanout(0.0);
 
3639
      pos->clearRefDependMap();
13218
3640
      if (!table->maybe_null || error > 0)
13219
 
        return(error);
 
3641
        return(error);
13220
3642
    }
13221
3643
  }
13222
3644
  if (*tab->on_expr_ref && !table->null_row)
13223
3645
  {
13224
3646
    if ((table->null_row= test((*tab->on_expr_ref)->val_int() == 0)))
13225
 
      mark_as_null_row(table);  
 
3647
      table->mark_as_null_row();
13226
3648
  }
13227
3649
  if (!table->null_row)
13228
3650
    table->maybe_null=0;
13231
3653
  JOIN *join= tab->join;
13232
3654
  if (join->conds)
13233
3655
    update_const_equal_items(join->conds, tab);
13234
 
  TABLE_LIST *tbl;
 
3656
  TableList *tbl;
13235
3657
  for (tbl= join->select_lex->leaf_tables; tbl; tbl= tbl->next_leaf)
13236
3658
  {
13237
 
    TABLE_LIST *embedded;
13238
 
    TABLE_LIST *embedding= tbl;
 
3659
    TableList *embedded;
 
3660
    TableList *embedding= tbl;
13239
3661
    do
13240
3662
    {
13241
3663
      embedded= embedding;
13250
3672
  return(0);
13251
3673
}
13252
3674
 
13253
 
 
13254
 
static int
13255
 
join_read_system(JOIN_TAB *tab)
 
3675
int join_read_system(JoinTable *tab)
13256
3676
{
13257
 
  TABLE *table= tab->table;
 
3677
  Table *table= tab->table;
13258
3678
  int error;
13259
3679
  if (table->status & STATUS_GARBAGE)           // If first read
13260
3680
  {
13261
 
    if ((error=table->file->read_first_row(table->record[0],
 
3681
    if ((error=table->cursor->read_first_row(table->record[0],
13262
3682
                                           table->s->primary_key)))
13263
3683
    {
13264
3684
      if (error != HA_ERR_END_OF_FILE)
13265
 
        return report_error(table, error);
13266
 
      mark_as_null_row(tab->table);
13267
 
      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
13268
3688
      return -1;
13269
3689
    }
13270
 
    store_record(table,record[1]);
 
3690
    table->storeRecord();
13271
3691
  }
13272
3692
  else if (!table->status)                      // Only happens with left join
13273
 
    restore_record(table,record[1]);                    // restore old record
 
3693
    table->restoreRecord();                     // restore old record
13274
3694
  table->null_row=0;
13275
3695
  return table->status ? -1 : 0;
13276
3696
}
13277
3697
 
13278
 
 
13279
3698
/**
13280
3699
  Read a (constant) table when there is at most one matching row.
13281
3700
 
13288
3707
  @retval
13289
3708
    1   Got an error (other than row not found) during read
13290
3709
*/
13291
 
 
13292
 
static int
13293
 
join_read_const(JOIN_TAB *tab)
 
3710
int join_read_const(JoinTable *tab)
13294
3711
{
13295
3712
  int error;
13296
 
  TABLE *table= tab->table;
 
3713
  Table *table= tab->table;
13297
3714
  if (table->status & STATUS_GARBAGE)           // If first read
13298
3715
  {
13299
3716
    table->status= 0;
13300
 
    if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
13301
 
      error=HA_ERR_KEY_NOT_FOUND;
 
3717
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
 
3718
      error= HA_ERR_KEY_NOT_FOUND;
13302
3719
    else
13303
3720
    {
13304
 
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
13305
 
                                            (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,
13306
3723
                                            make_prev_keypart_map(tab->ref.key_parts),
13307
3724
                                            HA_READ_KEY_EXACT);
13308
3725
    }
13309
3726
    if (error)
13310
3727
    {
13311
3728
      table->status= STATUS_NOT_FOUND;
13312
 
      mark_as_null_row(tab->table);
13313
 
      empty_record(table);
 
3729
      tab->table->mark_as_null_row();
 
3730
      table->emptyRecord();
13314
3731
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13315
 
        return report_error(table, error);
 
3732
        return table->report_error(error);
13316
3733
      return -1;
13317
3734
    }
13318
 
    store_record(table,record[1]);
 
3735
    table->storeRecord();
13319
3736
  }
13320
3737
  else if (!(table->status & ~STATUS_NULL_ROW)) // Only happens with left join
13321
3738
  {
13322
3739
    table->status=0;
13323
 
    restore_record(table,record[1]);                    // restore old record
 
3740
    table->restoreRecord();                     // restore old record
13324
3741
  }
13325
3742
  table->null_row=0;
13326
3743
  return table->status ? -1 : 0;
13327
3744
}
13328
3745
 
13329
 
 
13330
3746
/*
13331
3747
  eq_ref access method implementation: "read_first" function
13332
3748
 
13333
3749
  SYNOPSIS
13334
3750
    join_read_key()
13335
 
      tab  JOIN_TAB of the accessed table
 
3751
      tab  JoinTable of the accessed table
13336
3752
 
13337
3753
  DESCRIPTION
13338
3754
    This is "read_fist" function for the "ref" access method. The difference
13340
3756
 
13341
3757
  RETURN
13342
3758
    0  - Ok
13343
 
   -1  - Row not found 
 
3759
   -1  - Row not found
13344
3760
    1  - Error
13345
3761
*/
13346
 
 
13347
 
static int
13348
 
join_read_key(JOIN_TAB *tab)
 
3762
int join_read_key(JoinTable *tab)
13349
3763
{
13350
3764
  int error;
13351
 
  TABLE *table= tab->table;
 
3765
  Table *table= tab->table;
13352
3766
 
13353
 
  if (!table->file->inited)
 
3767
  if (!table->cursor->inited)
13354
3768
  {
13355
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
 
3769
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
13356
3770
  }
13357
3771
 
13358
3772
  /* TODO: Why don't we do "Late NULLs Filtering" here? */
13364
3778
      table->status=STATUS_NOT_FOUND;
13365
3779
      return -1;
13366
3780
    }
13367
 
    error=table->file->index_read_map(table->record[0],
 
3781
    error=table->cursor->index_read_map(table->record[0],
13368
3782
                                      tab->ref.key_buff,
13369
3783
                                      make_prev_keypart_map(tab->ref.key_parts),
13370
3784
                                      HA_READ_KEY_EXACT);
13371
3785
    if (error && error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13372
 
      return report_error(table, error);
 
3786
      return table->report_error(error);
13373
3787
  }
13374
3788
  table->null_row=0;
13375
3789
  return table->status ? -1 : 0;
13376
3790
}
13377
3791
 
13378
 
 
13379
3792
/*
13380
3793
  ref access method implementation: "read_first" function
13381
3794
 
13382
3795
  SYNOPSIS
13383
3796
    join_read_always_key()
13384
 
      tab  JOIN_TAB of the accessed table
 
3797
      tab  JoinTable of the accessed table
13385
3798
 
13386
3799
  DESCRIPTION
13387
 
    This is "read_fist" function for the "ref" access method.
13388
 
   
 
3800
    This is "read_first" function for the "ref" access method.
 
3801
 
13389
3802
    The functon must leave the index initialized when it returns.
13390
3803
    ref_or_null access implementation depends on that.
13391
3804
 
13392
3805
  RETURN
13393
3806
    0  - Ok
13394
 
   -1  - Row not found 
 
3807
   -1  - Row not found
13395
3808
    1  - Error
13396
3809
*/
13397
 
 
13398
 
static int
13399
 
join_read_always_key(JOIN_TAB *tab)
 
3810
int join_read_always_key(JoinTable *tab)
13400
3811
{
13401
3812
  int error;
13402
 
  TABLE *table= tab->table;
 
3813
  Table *table= tab->table;
13403
3814
 
13404
3815
  /* Initialize the index first */
13405
 
  if (!table->file->inited)
13406
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13407
 
 
 
3816
  if (!table->cursor->inited)
 
3817
    table->cursor->ha_index_init(tab->ref.key, tab->sorted);
 
3818
 
13408
3819
  /* Perform "Late NULLs Filtering" (see internals manual for explanations) */
13409
 
  for (uint i= 0 ; i < tab->ref.key_parts ; i++)
 
3820
  for (uint32_t i= 0 ; i < tab->ref.key_parts ; i++)
13410
3821
  {
13411
3822
    if ((tab->ref.null_rejecting & 1 << i) && tab->ref.items[i]->is_null())
13412
3823
        return -1;
13413
3824
  }
13414
3825
 
13415
 
  if (cp_buffer_from_ref(tab->join->thd, table, &tab->ref))
 
3826
  if (cp_buffer_from_ref(tab->join->session, &tab->ref))
13416
3827
    return -1;
13417
 
  if ((error=table->file->index_read_map(table->record[0],
 
3828
  if ((error=table->cursor->index_read_map(table->record[0],
13418
3829
                                         tab->ref.key_buff,
13419
3830
                                         make_prev_keypart_map(tab->ref.key_parts),
13420
3831
                                         HA_READ_KEY_EXACT)))
13421
3832
  {
13422
3833
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13423
 
      return report_error(table, error);
13424
 
    return -1; /* purecov: inspected */
 
3834
      return table->report_error(error);
 
3835
    return -1;
13425
3836
  }
 
3837
 
13426
3838
  return 0;
13427
3839
}
13428
3840
 
13429
 
 
13430
3841
/**
13431
 
  This function is used when optimizing away ORDER BY in 
13432
 
  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.
13433
3844
*/
13434
 
  
13435
 
static int
13436
 
join_read_last_key(JOIN_TAB *tab)
 
3845
int join_read_last_key(JoinTable *tab)
13437
3846
{
13438
3847
  int error;
13439
 
  TABLE *table= tab->table;
 
3848
  Table *table= tab->table;
13440
3849
 
13441
 
  if (!table->file->inited)
13442
 
    table->file->ha_index_init(tab->ref.key, tab->sorted);
13443
 
  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))
13444
3853
    return -1;
13445
 
  if ((error=table->file->index_read_last_map(table->record[0],
 
3854
  if ((error=table->cursor->index_read_last_map(table->record[0],
13446
3855
                                              tab->ref.key_buff,
13447
3856
                                              make_prev_keypart_map(tab->ref.key_parts))))
13448
3857
  {
13449
3858
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13450
 
      return report_error(table, error);
13451
 
    return -1; /* purecov: inspected */
 
3859
      return table->report_error(error);
 
3860
    return -1;
13452
3861
  }
13453
3862
  return 0;
13454
3863
}
13455
3864
 
13456
 
 
13457
 
        /* ARGSUSED */
13458
 
static int
13459
 
join_no_more_records(READ_RECORD *info __attribute__((unused)))
 
3865
int join_no_more_records(READ_RECORD *)
13460
3866
{
13461
3867
  return -1;
13462
3868
}
13463
3869
 
13464
 
static int
13465
 
join_read_next_same_diff(READ_RECORD *info)
 
3870
int join_read_next_same_diff(READ_RECORD *info)
13466
3871
{
13467
 
  TABLE *table= info->table;
13468
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3872
  Table *table= info->table;
 
3873
  JoinTable *tab=table->reginfo.join_tab;
13469
3874
  if (tab->insideout_match_tab->found_match)
13470
3875
  {
13471
3876
    KEY *key= tab->table->key_info + tab->index;
13472
 
    do 
 
3877
    do
13473
3878
    {
13474
3879
      int error;
13475
3880
      /* Save index tuple from record to the buffer */
13476
3881
      key_copy(tab->insideout_buf, info->record, key, 0);
13477
3882
 
13478
 
      if ((error=table->file->index_next_same(table->record[0],
 
3883
      if ((error=table->cursor->index_next_same(table->record[0],
13479
3884
                                              tab->ref.key_buff,
13480
3885
                                              tab->ref.key_length)))
13481
3886
      {
13482
3887
        if (error != HA_ERR_END_OF_FILE)
13483
 
          return report_error(table, error);
 
3888
          return table->report_error(error);
13484
3889
        table->status= STATUS_GARBAGE;
13485
3890
        return -1;
13486
3891
      }
13487
 
    } while (!key_cmp(tab->table->key_info[tab->index].key_part, 
 
3892
    } while (!key_cmp(tab->table->key_info[tab->index].key_part,
13488
3893
                      tab->insideout_buf, key->key_length));
13489
3894
    tab->insideout_match_tab->found_match= 0;
13490
3895
    return 0;
13493
3898
    return join_read_next_same(info);
13494
3899
}
13495
3900
 
13496
 
static int
13497
 
join_read_next_same(READ_RECORD *info)
 
3901
int join_read_next_same(READ_RECORD *info)
13498
3902
{
13499
3903
  int error;
13500
 
  TABLE *table= info->table;
13501
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3904
  Table *table= info->table;
 
3905
  JoinTable *tab=table->reginfo.join_tab;
13502
3906
 
13503
 
  if ((error=table->file->index_next_same(table->record[0],
 
3907
  if ((error=table->cursor->index_next_same(table->record[0],
13504
3908
                                          tab->ref.key_buff,
13505
3909
                                          tab->ref.key_length)))
13506
3910
  {
13507
3911
    if (error != HA_ERR_END_OF_FILE)
13508
 
      return report_error(table, error);
 
3912
      return table->report_error(error);
13509
3913
    table->status= STATUS_GARBAGE;
13510
3914
    return -1;
13511
3915
  }
 
3916
 
13512
3917
  return 0;
13513
3918
}
13514
3919
 
13515
 
 
13516
 
static int
13517
 
join_read_prev_same(READ_RECORD *info)
 
3920
int join_read_prev_same(READ_RECORD *info)
13518
3921
{
13519
3922
  int error;
13520
 
  TABLE *table= info->table;
13521
 
  JOIN_TAB *tab=table->reginfo.join_tab;
 
3923
  Table *table= info->table;
 
3924
  JoinTable *tab=table->reginfo.join_tab;
13522
3925
 
13523
 
  if ((error=table->file->index_prev(table->record[0])))
13524
 
    return report_error(table, error);
 
3926
  if ((error=table->cursor->index_prev(table->record[0])))
 
3927
    return table->report_error(error);
13525
3928
  if (key_cmp_if_same(table, tab->ref.key_buff, tab->ref.key,
13526
3929
                      tab->ref.key_length))
13527
3930
  {
13531
3934
  return error;
13532
3935
}
13533
3936
 
13534
 
 
13535
 
static int
13536
 
join_init_quick_read_record(JOIN_TAB *tab)
 
3937
int join_init_quick_read_record(JoinTable *tab)
13537
3938
{
13538
3939
  if (test_if_quick_select(tab) == -1)
13539
3940
    return -1;                                  /* No possible records */
13540
3941
  return join_init_read_record(tab);
13541
3942
}
13542
3943
 
13543
 
 
13544
3944
int rr_sequential(READ_RECORD *info);
13545
 
int init_read_record_seq(JOIN_TAB *tab)
 
3945
int init_read_record_seq(JoinTable *tab)
13546
3946
{
13547
3947
  tab->read_record.read_record= rr_sequential;
13548
 
  if (tab->read_record.file->ha_rnd_init(1))
 
3948
  if (tab->read_record.cursor->ha_rnd_init(1))
13549
3949
    return 1;
13550
3950
  return (*tab->read_record.read_record)(&tab->read_record);
13551
3951
}
13552
3952
 
13553
 
static int
13554
 
test_if_quick_select(JOIN_TAB *tab)
 
3953
int test_if_quick_select(JoinTable *tab)
13555
3954
{
13556
3955
  delete tab->select->quick;
13557
 
  tab->select->quick=0;
13558
 
  return tab->select->test_quick_select(tab->join->thd, tab->keys,
13559
 
                                        (table_map) 0, HA_POS_ERROR, 0,
13560
 
                                        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);
13561
3959
}
13562
3960
 
13563
 
 
13564
 
static int
13565
 
join_init_read_record(JOIN_TAB *tab)
 
3961
int join_init_read_record(JoinTable *tab)
13566
3962
{
13567
3963
  if (tab->select && tab->select->quick && tab->select->quick->reset())
13568
3964
    return 1;
13569
 
  init_read_record(&tab->read_record, tab->join->thd, tab->table,
 
3965
  init_read_record(&tab->read_record, tab->join->session, tab->table,
13570
3966
                   tab->select,1,1);
13571
3967
  return (*tab->read_record.read_record)(&tab->read_record);
13572
3968
}
13573
3969
 
13574
 
 
13575
 
static int
13576
 
join_read_first(JOIN_TAB *tab)
 
3970
int join_read_first(JoinTable *tab)
13577
3971
{
13578
3972
  int error;
13579
 
  TABLE *table=tab->table;
13580
 
  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) &&
13581
3975
      !table->no_keyread)
13582
3976
  {
13583
3977
    table->key_read=1;
13584
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
3978
    table->cursor->extra(HA_EXTRA_KEYREAD);
13585
3979
  }
13586
3980
  tab->table->status=0;
13587
3981
  tab->read_record.table=table;
13588
 
  tab->read_record.file=table->file;
 
3982
  tab->read_record.cursor=table->cursor;
13589
3983
  tab->read_record.index=tab->index;
13590
3984
  tab->read_record.record=table->record[0];
13591
3985
  if (tab->insideout_match_tab)
13600
3994
    tab->read_record.do_insideout_scan= 0;
13601
3995
  }
13602
3996
 
13603
 
  if (!table->file->inited)
13604
 
    table->file->ha_index_init(tab->index, tab->sorted);
13605
 
  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])))
13606
4000
  {
13607
4001
    if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
13608
 
      report_error(table, error);
 
4002
      table->report_error(error);
13609
4003
    return -1;
13610
4004
  }
 
4005
 
13611
4006
  return 0;
13612
4007
}
13613
4008
 
13614
 
 
13615
 
static int
13616
 
join_read_next_different(READ_RECORD *info)
 
4009
int join_read_next_different(READ_RECORD *info)
13617
4010
{
13618
 
  JOIN_TAB *tab= info->do_insideout_scan;
 
4011
  JoinTable *tab= info->do_insideout_scan;
13619
4012
  if (tab->insideout_match_tab->found_match)
13620
4013
  {
13621
4014
    KEY *key= tab->table->key_info + tab->index;
13622
 
    do 
 
4015
    do
13623
4016
    {
13624
4017
      int error;
13625
4018
      /* Save index tuple from record to the buffer */
13626
4019
      key_copy(tab->insideout_buf, info->record, key, 0);
13627
4020
 
13628
 
      if ((error=info->file->index_next(info->record)))
13629
 
        return report_error(info->table, error);
13630
 
      
13631
 
    } 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,
13632
4024
                      tab->insideout_buf, key->key_length));
13633
4025
    tab->insideout_match_tab->found_match= 0;
13634
4026
    return 0;
13637
4029
    return join_read_next(info);
13638
4030
}
13639
4031
 
13640
 
 
13641
 
static int
13642
 
join_read_next(READ_RECORD *info)
 
4032
int join_read_next(READ_RECORD *info)
13643
4033
{
13644
4034
  int error;
13645
 
  if ((error=info->file->index_next(info->record)))
13646
 
    return report_error(info->table, error);
 
4035
  if ((error=info->cursor->index_next(info->record)))
 
4036
    return info->table->report_error(error);
13647
4037
  return 0;
13648
4038
}
13649
4039
 
13650
 
 
13651
 
static int
13652
 
join_read_last(JOIN_TAB *tab)
 
4040
int join_read_last(JoinTable *tab)
13653
4041
{
13654
 
  TABLE *table=tab->table;
 
4042
  Table *table=tab->table;
13655
4043
  int error;
13656
 
  if (!table->key_read && table->covering_keys.is_set(tab->index) &&
 
4044
  if (!table->key_read && table->covering_keys.test(tab->index) &&
13657
4045
      !table->no_keyread)
13658
4046
  {
13659
4047
    table->key_read=1;
13660
 
    table->file->extra(HA_EXTRA_KEYREAD);
 
4048
    table->cursor->extra(HA_EXTRA_KEYREAD);
13661
4049
  }
13662
4050
  tab->table->status=0;
13663
4051
  tab->read_record.read_record=join_read_prev;
13664
4052
  tab->read_record.table=table;
13665
 
  tab->read_record.file=table->file;
 
4053
  tab->read_record.cursor=table->cursor;
13666
4054
  tab->read_record.index=tab->index;
13667
4055
  tab->read_record.record=table->record[0];
13668
 
  if (!table->file->inited)
13669
 
    table->file->ha_index_init(tab->index, 1);
13670
 
  if ((error= tab->table->file->index_last(tab->table->record[0])))
13671
 
    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
 
13672
4061
  return 0;
13673
4062
}
13674
4063
 
13675
 
 
13676
 
static int
13677
 
join_read_prev(READ_RECORD *info)
 
4064
int join_read_prev(READ_RECORD *info)
13678
4065
{
13679
4066
  int error;
13680
 
  if ((error= info->file->index_prev(info->record)))
13681
 
    return report_error(info->table, error);
 
4067
  if ((error= info->cursor->index_prev(info->record)))
 
4068
    return info->table->report_error(error);
 
4069
 
13682
4070
  return 0;
13683
4071
}
13684
4072
 
13685
4073
/**
13686
4074
  Reading of key with key reference and one part that may be NULL.
13687
4075
*/
13688
 
 
13689
 
int
13690
 
join_read_always_key_or_null(JOIN_TAB *tab)
 
4076
int join_read_always_key_or_null(JoinTable *tab)
13691
4077
{
13692
4078
  int res;
13693
4079
 
13701
4087
  return safe_index_read(tab);
13702
4088
}
13703
4089
 
13704
 
 
13705
 
int
13706
 
join_read_next_same_or_null(READ_RECORD *info)
 
4090
int join_read_next_same_or_null(READ_RECORD *info)
13707
4091
{
13708
4092
  int error;
13709
4093
  if ((error= join_read_next_same(info)) >= 0)
13710
4094
    return error;
13711
 
  JOIN_TAB *tab= info->table->reginfo.join_tab;
 
4095
  JoinTable *tab= info->table->reginfo.join_tab;
13712
4096
 
13713
4097
  /* Test if we have already done a read after null key */
13714
4098
  if (*tab->ref.null_ref_key)
13717
4101
  return safe_index_read(tab);                  // then read null keys
13718
4102
}
13719
4103
 
13720
 
 
13721
 
/*****************************************************************************
13722
 
  DESCRIPTION
13723
 
    Functions that end one nested loop iteration. Different functions
13724
 
    are used to support GROUP BY clause and to redirect records
13725
 
    to a table (e.g. in case of SELECT into a temporary table) or to the
13726
 
    network client.
13727
 
 
13728
 
  RETURN VALUES
13729
 
    NESTED_LOOP_OK           - the record has been successfully handled
13730
 
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
13731
 
                               was detected
13732
 
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
13733
 
                               the record
13734
 
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
13735
 
                               additionally, the nested loop produced the
13736
 
                               number of rows specified in the LIMIT clause
13737
 
                               for the query
13738
 
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
13739
 
                               additionally, there is a cursor and the nested
13740
 
                               loop algorithm produced the number of rows
13741
 
                               that is specified for current cursor fetch
13742
 
                               operation.
13743
 
   All return values except NESTED_LOOP_OK abort the nested loop.
13744
 
*****************************************************************************/
13745
 
 
13746
 
/* ARGSUSED */
13747
 
static enum_nested_loop_state
13748
 
end_send(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13749
 
         bool end_of_records)
13750
 
{
13751
 
  if (!end_of_records)
13752
 
  {
13753
 
    int error;
13754
 
    if (join->having && join->having->val_int() == 0)
13755
 
      return(NESTED_LOOP_OK);               // Didn't match having
13756
 
    error=0;
13757
 
    if (join->do_send_rows)
13758
 
      error=join->result->send_data(*join->fields);
13759
 
    if (error)
13760
 
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
13761
 
    if (++join->send_records >= join->unit->select_limit_cnt &&
13762
 
        join->do_send_rows)
13763
 
    {
13764
 
      if (join->select_options & OPTION_FOUND_ROWS)
13765
 
      {
13766
 
        JOIN_TAB *jt=join->join_tab;
13767
 
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
13768
 
            && !join->send_group_parts && !join->having && !jt->select_cond &&
13769
 
            !(jt->select && jt->select->quick) &&
13770
 
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
13771
 
            (jt->ref.key < 0))
13772
 
        {
13773
 
          /* Join over all rows in table;  Return number of found rows */
13774
 
          TABLE *table=jt->table;
13775
 
 
13776
 
          join->select_options ^= OPTION_FOUND_ROWS;
13777
 
          if (table->sort.record_pointers ||
13778
 
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
13779
 
          {
13780
 
            /* Using filesort */
13781
 
            join->send_records= table->sort.found_records;
13782
 
          }
13783
 
          else
13784
 
          {
13785
 
            table->file->info(HA_STATUS_VARIABLE);
13786
 
            join->send_records= table->file->stats.records;
13787
 
          }
13788
 
        }
13789
 
        else 
13790
 
        {
13791
 
          join->do_send_rows= 0;
13792
 
          if (join->unit->fake_select_lex)
13793
 
            join->unit->fake_select_lex->select_limit= 0;
13794
 
          return(NESTED_LOOP_OK);
13795
 
        }
13796
 
      }
13797
 
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
13798
 
    }
13799
 
    else if (join->send_records >= join->fetch_limit)
13800
 
    {
13801
 
      /*
13802
 
        There is a server side cursor and all rows for
13803
 
        this fetch request are sent.
13804
 
      */
13805
 
      return(NESTED_LOOP_CURSOR_LIMIT);
13806
 
    }
13807
 
  }
13808
 
 
13809
 
  return(NESTED_LOOP_OK);
13810
 
}
13811
 
 
13812
 
 
13813
 
        /* ARGSUSED */
13814
 
enum_nested_loop_state
13815
 
end_send_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13816
 
               bool end_of_records)
 
4104
enum_nested_loop_state end_send_group(JOIN *join, JoinTable *, bool end_of_records)
13817
4105
{
13818
4106
  int idx= -1;
13819
4107
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
13821
4109
  if (!join->first_record || end_of_records ||
13822
4110
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
13823
4111
  {
13824
 
    if (join->first_record || 
 
4112
    if (join->first_record ||
13825
4113
        (end_of_records && !join->group && !join->group_optimized_away))
13826
4114
    {
13827
4115
      if (idx < (int) join->send_group_parts)
13828
4116
      {
13829
 
        int error=0;
13830
 
        {
13831
 
          if (!join->first_record)
13832
 
          {
13833
 
            List_iterator_fast<Item> it(*join->fields);
13834
 
            Item *item;
13835
 
            /* No matching rows for group function */
13836
 
            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();
13837
4125
 
13838
4126
            while ((item= it++))
13839
4127
              item->no_rows_in_result();
13840
 
          }
13841
 
          if (join->having && join->having->val_int() == 0)
13842
 
            error= -1;                          // Didn't satisfy having
13843
 
          else
13844
 
          {
13845
 
            if (join->do_send_rows)
13846
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
13847
 
            join->send_records++;
13848
 
          }
13849
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
13850
 
          {
13851
 
            if (join->rollup_send_data((uint) (idx+1)))
13852
 
              error= 1;
13853
 
          }
13854
 
        }
13855
 
        if (error > 0)
13856
 
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
13857
 
        if (end_of_records)
13858
 
          return(NESTED_LOOP_OK);
13859
 
        if (join->send_records >= join->unit->select_limit_cnt &&
13860
 
            join->do_send_rows)
13861
 
        {
13862
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
13863
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
13864
 
          join->do_send_rows=0;
13865
 
          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;
13866
4154
        }
13867
4155
        else if (join->send_records >= join->fetch_limit)
13868
4156
        {
13881
4169
    else
13882
4170
    {
13883
4171
      if (end_of_records)
13884
 
        return(NESTED_LOOP_OK);
 
4172
        return(NESTED_LOOP_OK);
13885
4173
      join->first_record=1;
13886
 
      VOID(test_if_item_cache_changed(join->group_fields));
 
4174
      test_if_item_cache_changed(join->group_fields);
13887
4175
    }
13888
4176
    if (idx < (int) join->send_group_parts)
13889
4177
    {
13893
4181
      */
13894
4182
      copy_fields(&join->tmp_table_param);
13895
4183
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
13896
 
        return(NESTED_LOOP_ERROR);
 
4184
        return(NESTED_LOOP_ERROR);
13897
4185
      return(ok_code);
13898
4186
    }
13899
4187
  }
13902
4190
  return(NESTED_LOOP_OK);
13903
4191
}
13904
4192
 
13905
 
 
13906
 
        /* ARGSUSED */
13907
 
enum_nested_loop_state
13908
 
end_write(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13909
 
          bool end_of_records)
13910
 
{
13911
 
  TABLE *table=join->tmp_table;
13912
 
 
13913
 
  if (join->thd->killed)                        // Aborted by user
13914
 
  {
13915
 
    join->thd->send_kill_message();
13916
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
13917
 
  }
13918
 
  if (!end_of_records)
13919
 
  {
13920
 
    copy_fields(&join->tmp_table_param);
13921
 
    copy_funcs(join->tmp_table_param.items_to_copy);
13922
 
#ifdef TO_BE_DELETED
13923
 
    if (!table->uniques)                        // If not unique handling
13924
 
    {
13925
 
      /* Copy null values from group to row */
13926
 
      ORDER   *group;
13927
 
      for (group=table->group ; group ; group=group->next)
13928
 
      {
13929
 
        Item *item= *group->item;
13930
 
        if (item->maybe_null)
13931
 
        {
13932
 
          Field *field=item->get_tmp_table_field();
13933
 
          field->ptr[-1]= (uchar) (field->is_null() ? 1 : 0);
13934
 
        }
13935
 
      }
13936
 
    }
13937
 
#endif
13938
 
    if (!join->having || join->having->val_int())
13939
 
    {
13940
 
      int error;
13941
 
      join->found_records++;
13942
 
      if ((error=table->file->ha_write_row(table->record[0])))
13943
 
      {
13944
 
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
13945
 
          goto end;
13946
 
        if (create_myisam_from_heap(join->thd, table,
13947
 
                                    join->tmp_table_param.start_recinfo,
13948
 
                                    &join->tmp_table_param.recinfo,
13949
 
                                    error, 1))
13950
 
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
13951
 
        table->s->uniques=0;                    // To ensure rows are the same
13952
 
      }
13953
 
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
13954
 
          join->do_send_rows)
13955
 
      {
13956
 
        if (!(join->select_options & OPTION_FOUND_ROWS))
13957
 
          return(NESTED_LOOP_QUERY_LIMIT);
13958
 
        join->do_send_rows=0;
13959
 
        join->unit->select_limit_cnt = HA_POS_ERROR;
13960
 
        return(NESTED_LOOP_OK);
13961
 
      }
13962
 
    }
13963
 
  }
13964
 
end:
13965
 
  return(NESTED_LOOP_OK);
13966
 
}
13967
 
 
13968
 
/* ARGSUSED */
13969
 
/** Group by searching after group record and updating it if possible. */
13970
 
 
13971
 
static enum_nested_loop_state
13972
 
end_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
13973
 
           bool end_of_records)
13974
 
{
13975
 
  TABLE *table=join->tmp_table;
13976
 
  ORDER   *group;
13977
 
  int     error;
13978
 
 
13979
 
  if (end_of_records)
13980
 
    return(NESTED_LOOP_OK);
13981
 
  if (join->thd->killed)                        // Aborted by user
13982
 
  {
13983
 
    join->thd->send_kill_message();
13984
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
13985
 
  }
13986
 
 
13987
 
  join->found_records++;
13988
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
13989
 
  /* Make a key of group index */
13990
 
  for (group=table->group ; group ; group=group->next)
13991
 
  {
13992
 
    Item *item= *group->item;
13993
 
    item->save_org_in_field(group->field);
13994
 
    /* Store in the used key if the field was 0 */
13995
 
    if (item->maybe_null)
13996
 
      group->buff[-1]= (char) group->field->is_null();
13997
 
  }
13998
 
  if (!table->file->index_read_map(table->record[1],
13999
 
                                   join->tmp_table_param.group_buff,
14000
 
                                   HA_WHOLE_KEY,
14001
 
                                   HA_READ_KEY_EXACT))
14002
 
  {                                             /* Update old record */
14003
 
    restore_record(table,record[1]);
14004
 
    update_tmptable_sum_func(join->sum_funcs,table);
14005
 
    if ((error=table->file->ha_update_row(table->record[1],
14006
 
                                          table->record[0])))
14007
 
    {
14008
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14009
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14010
 
    }
14011
 
    return(NESTED_LOOP_OK);
14012
 
  }
14013
 
 
14014
 
  /*
14015
 
    Copy null bits from group key to table
14016
 
    We can't copy all data as the key may have different format
14017
 
    as the row data (for example as with VARCHAR keys)
14018
 
  */
14019
 
  KEY_PART_INFO *key_part;
14020
 
  for (group=table->group,key_part=table->key_info[0].key_part;
14021
 
       group ;
14022
 
       group=group->next,key_part++)
14023
 
  {
14024
 
    if (key_part->null_bit)
14025
 
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
14026
 
  }
14027
 
  init_tmptable_sum_functions(join->sum_funcs);
14028
 
  copy_funcs(join->tmp_table_param.items_to_copy);
14029
 
  if ((error=table->file->ha_write_row(table->record[0])))
14030
 
  {
14031
 
    if (create_myisam_from_heap(join->thd, table,
14032
 
                                join->tmp_table_param.start_recinfo,
14033
 
                                &join->tmp_table_param.recinfo,
14034
 
                                error, 0))
14035
 
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
14036
 
    /* Change method to update rows */
14037
 
    table->file->ha_index_init(0, 0);
14038
 
    join->join_tab[join->tables-1].next_select=end_unique_update;
14039
 
  }
14040
 
  join->send_records++;
14041
 
  return(NESTED_LOOP_OK);
14042
 
}
14043
 
 
14044
 
 
14045
 
/** Like end_update, but this is done with unique constraints instead of keys.  */
14046
 
 
14047
 
static enum_nested_loop_state
14048
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14049
 
                  bool end_of_records)
14050
 
{
14051
 
  TABLE *table=join->tmp_table;
14052
 
  int     error;
14053
 
 
14054
 
  if (end_of_records)
14055
 
    return(NESTED_LOOP_OK);
14056
 
  if (join->thd->killed)                        // Aborted by user
14057
 
  {
14058
 
    join->thd->send_kill_message();
14059
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
14060
 
  }
14061
 
 
14062
 
  init_tmptable_sum_functions(join->sum_funcs);
14063
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
14064
 
  copy_funcs(join->tmp_table_param.items_to_copy);
14065
 
 
14066
 
  if (!(error=table->file->ha_write_row(table->record[0])))
14067
 
    join->send_records++;                       // New group
14068
 
  else
14069
 
  {
14070
 
    if ((int) table->file->get_dup_key(error) < 0)
14071
 
    {
14072
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14073
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14074
 
    }
14075
 
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
14076
 
    {
14077
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14078
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14079
 
    }
14080
 
    restore_record(table,record[1]);
14081
 
    update_tmptable_sum_func(join->sum_funcs,table);
14082
 
    if ((error=table->file->ha_update_row(table->record[1],
14083
 
                                          table->record[0])))
14084
 
    {
14085
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
14086
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
14087
 
    }
14088
 
  }
14089
 
  return(NESTED_LOOP_OK);
14090
 
}
14091
 
 
14092
 
 
14093
 
        /* ARGSUSED */
14094
 
enum_nested_loop_state
14095
 
end_write_group(JOIN *join, JOIN_TAB *join_tab __attribute__((unused)),
14096
 
                bool end_of_records)
14097
 
{
14098
 
  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;
14099
4196
  int     idx= -1;
14100
4197
 
14101
 
  if (join->thd->killed)
 
4198
  if (join->session->killed)
14102
4199
  {                                             // Aborted by user
14103
 
    join->thd->send_kill_message();
14104
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
4200
    join->session->send_kill_message();
 
4201
    return NESTED_LOOP_KILLED;
14105
4202
  }
14106
4203
  if (!join->first_record || end_of_records ||
14107
4204
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
14111
4208
      int send_group_parts= join->send_group_parts;
14112
4209
      if (idx < send_group_parts)
14113
4210
      {
14114
 
        if (!join->first_record)
14115
 
        {
14116
 
          /* No matching rows for group function */
14117
 
          join->clear();
14118
 
        }
14119
 
        copy_sum_funcs(join->sum_funcs,
14120
 
                       join->sum_funcs_end[send_group_parts]);
14121
 
        if (!join->having || join->having->val_int())
14122
 
        {
14123
 
          int error= table->file->ha_write_row(table->record[0]);
14124
 
          if (error && create_myisam_from_heap(join->thd, table,
14125
 
                                               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,
14126
4222
                                                &join->tmp_table_param.recinfo,
14127
 
                                               error, 0))
14128
 
            return(NESTED_LOOP_ERROR);
 
4223
                                              error, 0))
 
4224
          return NESTED_LOOP_ERROR;
14129
4225
        }
14130
4226
        if (join->rollup.state != ROLLUP::STATE_NONE)
14131
 
        {
14132
 
          if (join->rollup_write_data((uint) (idx+1), table))
14133
 
            return(NESTED_LOOP_ERROR);
14134
 
        }
14135
 
        if (end_of_records)
14136
 
          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;
14137
4233
      }
14138
4234
    }
14139
4235
    else
14140
4236
    {
14141
4237
      if (end_of_records)
14142
 
        return(NESTED_LOOP_OK);
 
4238
        return NESTED_LOOP_OK;
14143
4239
      join->first_record=1;
14144
 
      VOID(test_if_item_cache_changed(join->group_fields));
 
4240
      test_if_item_cache_changed(join->group_fields);
14145
4241
    }
14146
4242
    if (idx < (int) join->send_group_parts)
14147
4243
    {
14148
4244
      copy_fields(&join->tmp_table_param);
14149
4245
      copy_funcs(join->tmp_table_param.items_to_copy);
14150
4246
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
14151
 
        return(NESTED_LOOP_ERROR);
14152
 
      return(NESTED_LOOP_OK);
 
4247
        return NESTED_LOOP_ERROR;
 
4248
      return NESTED_LOOP_OK;
14153
4249
    }
14154
4250
  }
14155
4251
  if (update_sum_func(join->sum_funcs))
14156
 
    return(NESTED_LOOP_ERROR);
14157
 
  return(NESTED_LOOP_OK);
 
4252
    return NESTED_LOOP_ERROR;
 
4253
  return NESTED_LOOP_OK;
14158
4254
}
14159
4255
 
14160
 
 
14161
4256
/*****************************************************************************
14162
4257
  Remove calculation with tables that aren't yet read. Remove also tests
14163
4258
  against fields that are read through key where the table is not a
14170
4265
  @return
14171
4266
    1 if right_item is used removable reference key on left_item
14172
4267
*/
14173
 
 
14174
 
static bool test_if_ref(Item_field *left_item,Item *right_item)
 
4268
bool test_if_ref(Item_field *left_item,Item *right_item)
14175
4269
{
14176
4270
  Field *field=left_item->field;
14177
4271
  // No need to change const test. We also have to keep tests on LEFT JOIN
14182
4276
    {
14183
4277
      right_item= right_item->real_item();
14184
4278
      if (right_item->type() == Item::FIELD_ITEM)
14185
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
4279
        return (field->eq_def(((Item_field *) right_item)->field));
14186
4280
      /* remove equalities injected by IN->EXISTS transformation */
14187
4281
      else if (right_item->type() == Item::CACHE_ITEM)
14188
4282
        return ((Item_cache *)right_item)->eq_def (field);
14189
4283
      if (right_item->const_item() && !(right_item->is_null()))
14190
4284
      {
14191
 
        /*
14192
 
          We can remove binary fields and numerical fields except float,
14193
 
          as float comparison isn't 100 % secure
14194
 
          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
14195
4289
 
14196
 
          sergefp: the above seems to be too restrictive. Counterexample:
14197
 
            create table t100 (v varchar(10), key(v)) default charset=latin1;
14198
 
            insert into t100 values ('a'),('a ');
14199
 
            explain select * from t100 where v='a';
14200
 
          The EXPLAIN shows 'using Where'. Running the query returns both
14201
 
          rows, so it seems there are no problems with endspace in the most
14202
 
          frequent case?
14203
 
        */
14204
 
        if (field->binary() &&
14205
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
14206
 
            field->decimals() == 0)
14207
 
        {
14208
 
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
14209
 
        }
 
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
        }
14210
4304
      }
14211
4305
    }
14212
4306
  }
14213
 
  return 0;                                     // keep test
14214
 
}
14215
 
 
14216
 
/**
14217
 
   @brief Replaces an expression destructively inside the expression tree of
14218
 
   the WHERE clase.
14219
 
 
14220
 
   @note Because of current requirements for semijoin flattening, we do not
14221
 
   need to recurse here, hence this function will only examine the top-level
14222
 
   AND conditions. (see JOIN::prepare, comment above the line 
14223
 
   'if (do_materialize)'
14224
 
   
14225
 
   @param join The top-level query.
14226
 
   @param old_cond The expression to be replaced.
14227
 
   @param new_cond The expression to be substituted.
14228
 
   @param do_fix_fields If true, Item::fix_fields(THD*, Item**) is called for
14229
 
   the new expression.
14230
 
   @return <code>true</code> if there was an error, <code>false</code> if
14231
 
   successful.
14232
 
*/
14233
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond, 
14234
 
                                       Item *new_cond, bool do_fix_fields)
14235
 
{
14236
 
  if (join->conds == old_cond) {
14237
 
    join->conds= new_cond;
14238
 
    if (do_fix_fields)
14239
 
      new_cond->fix_fields(join->thd, &join->conds);
14240
 
    return false;
14241
 
  }
14242
 
  
14243
 
  if (join->conds->type() == Item::COND_ITEM) {
14244
 
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
14245
 
    Item *item;
14246
 
    while ((item= li++))
14247
 
      if (item == old_cond) 
14248
 
      {
14249
 
        li.replace(new_cond);
14250
 
        if (do_fix_fields)
14251
 
          new_cond->fix_fields(join->thd, li.ref());
14252
 
        return false;
14253
 
      }
14254
 
  }
14255
 
 
14256
 
  return true;
 
4307
  return 0;
14257
4308
}
14258
4309
 
14259
4310
/*
14260
4311
  Extract a condition that can be checked after reading given table
14261
 
  
 
4312
 
14262
4313
  SYNOPSIS
14263
4314
    make_cond_for_table()
14264
4315
      cond         Condition to analyze
14265
4316
      tables       Tables for which "current field values" are available
14266
 
      used_table   Table that we're extracting the condition for (may 
 
4317
      used_table   Table that we're extracting the condition for (may
14267
4318
                   also include PSEUDO_TABLE_BITS
14268
4319
 
14269
4320
  DESCRIPTION
14273
4324
 
14274
4325
    The function assumes that
14275
4326
      - Constant parts of the condition has already been checked.
14276
 
      - Condition that could be checked for tables in 'tables' has already 
 
4327
      - Condition that could be checked for tables in 'tables' has already
14277
4328
        been checked.
14278
 
        
 
4329
 
14279
4330
    The function takes into account that some parts of the condition are
14280
4331
    guaranteed to be true by employed 'ref' access methods (the code that
14281
4332
    does this is located at the end, search down for "EQ_FUNC").
14282
4333
 
14283
4334
 
14284
 
  SEE ALSO 
 
4335
  SEE ALSO
14285
4336
    make_cond_for_info_schema uses similar algorithm
14286
4337
 
14287
4338
  RETURN
14288
4339
    Extracted condition
14289
4340
*/
14290
 
 
14291
 
static COND *
14292
 
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
14293
 
                    bool exclude_expensive_cond)
 
4341
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
14294
4342
{
14295
4343
  if (used_table && !(cond->used_tables() & used_table) &&
14296
 
      /*
14297
 
        Exclude constant conditions not checked at optimization time if
14298
 
        the table we are pushing conditions to is the first one.
14299
 
        As a result, such conditions are not considered as already checked
14300
 
        and will be checked at execution time, attached to the first table.
14301
 
      */
14302
 
      !((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()))
14303
4351
    return (COND*) 0;                           // Already checked
14304
4352
  if (cond->type() == Item::COND_ITEM)
14305
4353
  {
14308
4356
      /* Create new top level AND item */
14309
4357
      Item_cond_and *new_cond=new Item_cond_and;
14310
4358
      if (!new_cond)
14311
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
4359
        return (COND*) 0;
14312
4360
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14313
4361
      Item *item;
14314
4362
      while ((item=li++))
14315
4363
      {
14316
 
        Item *fix=make_cond_for_table(item,tables,used_table,
14317
 
                                      exclude_expensive_cond);
14318
 
        if (fix)
14319
 
          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);
14320
4368
      }
14321
 
      switch (new_cond->argument_list()->elements) {
14322
 
      case 0:
14323
 
        return (COND*) 0;                       // Always true
14324
 
      case 1:
14325
 
        return new_cond->argument_list()->head();
14326
 
      default:
14327
 
        /*
14328
 
          Item_cond_and do not need fix_fields for execution, its parameters
14329
 
          are fixed or do not need fix_fields, too
14330
 
        */
14331
 
        new_cond->quick_fix_field();
14332
 
        new_cond->used_tables_cache=
14333
 
          ((Item_cond_and*) cond)->used_tables_cache &
14334
 
          tables;
14335
 
        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;
14336
4383
      }
14337
4384
    }
14338
4385
    else
14339
4386
    {                                           // Or list
14340
4387
      Item_cond_or *new_cond=new Item_cond_or;
14341
4388
      if (!new_cond)
14342
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
4389
        return (COND*) 0;
14343
4390
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
14344
4391
      Item *item;
14345
4392
      while ((item=li++))
14346
4393
      {
14347
 
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
14348
 
        if (!fix)
14349
 
          return (COND*) 0;                     // Always true
14350
 
        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);
14351
4398
      }
14352
4399
      /*
14353
 
        Item_cond_and do not need fix_fields for execution, its parameters
14354
 
        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
14355
4402
      */
14356
4403
      new_cond->quick_fix_field();
14357
4404
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
14376
4423
  if (cond->marker == 2 || cond->eq_cmp_result() == Item::COND_OK)
14377
4424
    return cond;                                // Not boolean op
14378
4425
 
14379
 
  /* 
 
4426
  /*
14380
4427
    Remove equalities that are guaranteed to be true by use of 'ref' access
14381
4428
    method
14382
4429
  */
14384
4431
  {
14385
4432
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
14386
4433
    Item *right_item= ((Item_func*) cond)->arguments()[1];
14387
 
    if (left_item->type() == Item::FIELD_ITEM &&
14388
 
        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))
14389
4435
    {
14390
4436
      cond->marker=3;                   // Checked when read
14391
4437
      return (COND*) 0;
14392
4438
    }
14393
 
    if (right_item->type() == Item::FIELD_ITEM &&
14394
 
        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))
14395
4440
    {
14396
4441
      cond->marker=3;                   // Checked when read
14397
4442
      return (COND*) 0;
14401
4446
  return cond;
14402
4447
}
14403
4448
 
14404
 
 
14405
 
static Item *
14406
 
part_of_refkey(TABLE *table,Field *field)
 
4449
static Item *part_of_refkey(Table *table,Field *field)
14407
4450
{
14408
4451
  if (!table->reginfo.join_tab)
14409
4452
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
14410
4453
 
14411
 
  uint ref_parts=table->reginfo.join_tab->ref.key_parts;
 
4454
  uint32_t ref_parts=table->reginfo.join_tab->ref.key_parts;
14412
4455
  if (ref_parts)
14413
4456
  {
14414
4457
    KEY_PART_INFO *key_part=
14415
4458
      table->key_info[table->reginfo.join_tab->ref.key].key_part;
14416
 
    uint part;
 
4459
    uint32_t part;
14417
4460
 
14418
4461
    for (part=0 ; part < ref_parts ; part++)
14419
4462
    {
14429
4472
  return (Item*) 0;
14430
4473
}
14431
4474
 
14432
 
 
14433
4475
/**
14434
 
  Test if one can use the key to resolve ORDER BY.
 
4476
  Test if one can use the key to resolve order_st BY.
14435
4477
 
14436
4478
  @param order                 Sort order
14437
4479
  @param table                 Table to sort
14450
4492
  @retval
14451
4493
    -1   Reverse key can be used
14452
4494
*/
14453
 
 
14454
 
static int test_if_order_by_key(ORDER *order, TABLE *table, uint idx,
14455
 
                                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)
14456
4496
{
14457
 
  KEY_PART_INFO *key_part,*key_part_end;
14458
 
  key_part=table->key_info[idx].key_part;
14459
 
  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;
14460
4501
  key_part_map const_key_parts=table->const_key_parts[idx];
14461
 
  int reverse=0;
 
4502
  int reverse= 0;
14462
4503
  bool on_primary_key= false;
14463
4504
 
14464
4505
  for (; order ; order=order->next, const_key_parts>>=1)
14468
4509
 
14469
4510
    /*
14470
4511
      Skip key parts that are constants in the WHERE clause.
14471
 
      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()
14472
4513
    */
14473
4514
    for (; const_key_parts & 1 ; const_key_parts>>= 1)
14474
 
      key_part++; 
 
4515
      key_part++;
14475
4516
 
14476
4517
    if (key_part == key_part_end)
14477
4518
    {
14478
 
      /* 
 
4519
      /*
14479
4520
        We are at the end of the key. Check if the engine has the primary
14480
4521
        key as a suffix to the secondary keys. If it has continue to check
14481
4522
        the primary key as a suffix.
14482
4523
      */
14483
4524
      if (!on_primary_key &&
14484
 
          (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX) &&
 
4525
          (table->cursor->getEngine()->check_flag(HTON_BIT_PRIMARY_KEY_IN_READ_INDEX)) &&
14485
4526
          table->s->primary_key != MAX_KEY)
14486
4527
      {
14487
4528
        on_primary_key= true;
14490
4531
        const_key_parts=table->const_key_parts[table->s->primary_key];
14491
4532
 
14492
4533
        for (; const_key_parts & 1 ; const_key_parts>>= 1)
14493
 
          key_part++; 
 
4534
          key_part++;
14494
4535
        /*
14495
4536
         The primary and secondary key parts were all const (i.e. there's
14496
4537
         one row).  The sorting doesn't matter.
14514
4555
    key_part++;
14515
4556
  }
14516
4557
  *used_key_parts= on_primary_key ? table->key_info[idx].key_parts :
14517
 
    (uint) (key_part - table->key_info[idx].key_part);
14518
 
  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) &
14519
4560
                         HA_READ_PREV))
14520
4561
    reverse= 0;                                 // Index can't be used
14521
4562
  return(reverse);
14522
4563
}
14523
4564
 
14524
 
 
14525
 
uint find_shortest_key(TABLE *table, const key_map *usable_keys)
14526
 
{
14527
 
  uint min_length= (uint) ~0;
14528
 
  uint best= MAX_KEY;
14529
 
  if (!usable_keys->is_clear_all())
14530
 
  {
14531
 
    for (uint nr=0; nr < table->s->keys ; nr++)
14532
 
    {
14533
 
      if (usable_keys->is_set(nr))
14534
 
      {
14535
 
        if (table->key_info[nr].key_length < min_length)
14536
 
        {
14537
 
          min_length=table->key_info[nr].key_length;
14538
 
          best=nr;
14539
 
        }
14540
 
      }
14541
 
    }
14542
 
  }
14543
 
  return best;
14544
 
}
14545
 
 
14546
4565
/**
14547
4566
  Test if a second key is the subkey of the first one.
14548
4567
 
14558
4577
  @retval
14559
4578
    0   no sub key
14560
4579
*/
14561
 
 
14562
 
inline bool 
14563
 
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
14564
 
          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)
14565
4583
{
14566
4584
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
14567
 
    if (!key_part->field->eq(ref_key_part->field))
 
4585
    if (! key_part->field->eq(ref_key_part->field))
14568
4586
      return 0;
14569
4587
  return 1;
14570
4588
}
14580
4598
    - MAX_KEY                   If we can't use other key
14581
4599
    - the number of found key   Otherwise
14582
4600
*/
14583
 
 
14584
 
static uint
14585
 
test_if_subkey(ORDER *order, TABLE *table, uint ref, uint ref_key_parts,
14586
 
               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)
14587
4606
{
14588
 
  uint nr;
14589
 
  uint min_length= (uint) ~0;
14590
 
  uint best= MAX_KEY;
14591
 
  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;
14592
4611
  KEY_PART_INFO *ref_key_part= table->key_info[ref].key_part;
14593
4612
  KEY_PART_INFO *ref_key_part_end= ref_key_part + ref_key_parts;
14594
4613
 
14595
4614
  for (nr= 0 ; nr < table->s->keys ; nr++)
14596
4615
  {
14597
 
    if (usable_keys->is_set(nr) &&
 
4616
    if (usable_keys->test(nr) &&
14598
4617
        table->key_info[nr].key_length < min_length &&
14599
4618
        table->key_info[nr].key_parts >= ref_key_parts &&
14600
4619
        is_subkey(table->key_info[nr].key_part, ref_key_part,
14608
4627
  return best;
14609
4628
}
14610
4629
 
14611
 
 
14612
4630
/**
14613
4631
  Check if GROUP BY/DISTINCT can be optimized away because the set is
14614
4632
  already known to be distinct.
14627
4645
    of the table are referenced by a list : either the select list
14628
4646
    through find_field_in_item_list or GROUP BY list through
14629
4647
    find_field_in_order_list.
14630
 
    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
14631
4649
    can safely remove the GROUP BY/DISTINCT,
14632
4650
    as no result set can be more distinct than an unique key.
14633
4651
 
14640
4658
  @retval
14641
4659
    0                    not found.
14642
4660
*/
14643
 
 
14644
 
static bool
14645
 
list_contains_unique_index(TABLE *table,
14646
 
                          bool (*find_func) (Field *, void *), void *data)
 
4661
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
14647
4662
{
14648
 
  for (uint keynr= 0; keynr < table->s->keys; keynr++)
 
4663
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
14649
4664
  {
14650
4665
    if (keynr == table->s->primary_key ||
14651
4666
         (table->key_info[keynr].flags & HA_NOSAME))
14652
4667
    {
14653
4668
      KEY *keyinfo= table->key_info + keynr;
14654
 
      KEY_PART_INFO *key_part, *key_part_end;
 
4669
      KEY_PART_INFO *key_part= NULL;
 
4670
      KEY_PART_INFO *key_part_end= NULL;
14655
4671
 
14656
4672
      for (key_part=keyinfo->key_part,
14657
4673
           key_part_end=key_part+ keyinfo->key_parts;
14658
4674
           key_part < key_part_end;
14659
4675
           key_part++)
14660
4676
      {
14661
 
        if (key_part->field->maybe_null() || 
14662
 
            !find_func(key_part->field, data))
 
4677
        if (key_part->field->maybe_null() ||
 
4678
            ! find_func(key_part->field, data))
14663
4679
          break;
14664
4680
      }
14665
4681
      if (key_part == key_part_end)
14669
4685
  return 0;
14670
4686
}
14671
4687
 
14672
 
 
14673
4688
/**
14674
4689
  Helper function for list_contains_unique_index.
14675
 
  Find a field reference in a list of ORDER structures.
 
4690
  Find a field reference in a list of order_st structures.
14676
4691
  Finds a direct reference of the Field in the list.
14677
4692
 
14678
4693
  @param field                The field to search for.
14679
 
  @param data                 ORDER *.The list to search in
 
4694
  @param data                 order_st *.The list to search in
14680
4695
 
14681
4696
  @retval
14682
4697
    1                    found
14683
4698
  @retval
14684
4699
    0                    not found.
14685
4700
*/
14686
 
 
14687
 
static bool
14688
 
find_field_in_order_list (Field *field, void *data)
 
4701
bool find_field_in_order_list (Field *field, void *data)
14689
4702
{
14690
 
  ORDER *group= (ORDER *) data;
 
4703
  order_st *group= (order_st *) data;
14691
4704
  bool part_found= 0;
14692
 
  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)
14693
4706
  {
14694
4707
    Item *item= (*tmp_group->item)->real_item();
14695
4708
    if (item->type() == Item::FIELD_ITEM &&
14702
4715
  return part_found;
14703
4716
}
14704
4717
 
14705
 
 
14706
4718
/**
14707
4719
  Helper function for list_contains_unique_index.
14708
4720
  Find a field reference in a dynamic list of Items.
14716
4728
  @retval
14717
4729
    0                    not found.
14718
4730
*/
14719
 
 
14720
 
static bool
14721
 
find_field_in_item_list (Field *field, void *data)
 
4731
bool find_field_in_item_list (Field *field, void *data)
14722
4732
{
14723
4733
  List<Item> *fields= (List<Item> *) data;
14724
4734
  bool part_found= 0;
14737
4747
  return part_found;
14738
4748
}
14739
4749
 
14740
 
 
14741
4750
/**
14742
 
  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.
14743
4752
 
14744
4753
  SYNOPSIS
14745
4754
    test_if_skip_sort_order()
14749
4758
      no_changes
14750
4759
      map
14751
4760
 
14752
 
  If we can use an index, the JOIN_TAB / tab->select struct
 
4761
  If we can use an index, the JoinTable / tab->select struct
14753
4762
  is changed to use the index.
14754
4763
 
14755
4764
  The index must cover all fields in <order>, or it will not be considered.
14756
4765
 
14757
4766
  @todo
14758
 
    - sergeyp: Results of all index merge selects actually are ordered 
 
4767
    - sergeyp: Results of all index merge selects actually are ordered
14759
4768
    by clustered PK values.
14760
4769
 
14761
4770
  @retval
14763
4772
  @retval
14764
4773
    1    We can use an index.
14765
4774
*/
14766
 
 
14767
 
static bool
14768
 
test_if_skip_sort_order(JOIN_TAB *tab,ORDER *order,ha_rows select_limit,
14769
 
                        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)
14770
4776
{
14771
 
  int ref_key;
14772
 
  uint ref_key_parts;
 
4777
  int32_t ref_key;
 
4778
  uint32_t ref_key_parts;
14773
4779
  int order_direction;
14774
 
  uint used_key_parts;
14775
 
  TABLE *table=tab->table;
14776
 
  SQL_SELECT *select=tab->select;
 
4780
  uint32_t used_key_parts;
 
4781
  Table *table=tab->table;
 
4782
  optimizer::SqlSelect *select= tab->select;
14777
4783
  key_map usable_keys;
14778
 
  QUICK_SELECT_I *save_quick= 0;
 
4784
  optimizer::QuickSelectInterface *save_quick= NULL;
14779
4785
 
14780
4786
  /*
14781
 
    Keys disabled by ALTER TABLE ... DISABLE KEYS should have already
 
4787
    Keys disabled by ALTER Table ... DISABLE KEYS should have already
14782
4788
    been taken into account.
14783
4789
  */
14784
4790
  usable_keys= *map;
14785
4791
 
14786
 
  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)
14787
4793
  {
14788
4794
    Item *item= (*tmp_order->item)->real_item();
14789
4795
    if (item->type() != Item::FIELD_ITEM)
14790
4796
    {
14791
 
      usable_keys.clear_all();
 
4797
      usable_keys.reset();
14792
4798
      return(0);
14793
4799
    }
14794
 
    usable_keys.intersect(((Item_field*) item)->field->part_of_sortkey);
14795
 
    if (usable_keys.is_clear_all())
 
4800
    usable_keys&= ((Item_field*) item)->field->part_of_sortkey;
 
4801
    if (usable_keys.none())
14796
4802
      return(0);                                        // No usable keys
14797
4803
  }
14798
4804
 
14802
4808
  {
14803
4809
    ref_key=       tab->ref.key;
14804
4810
    ref_key_parts= tab->ref.key_parts;
14805
 
    if (tab->type == JT_REF_OR_NULL)
 
4811
    if (tab->type == AM_REF_OR_NULL)
14806
4812
      return(0);
14807
4813
  }
14808
 
  else if (select && select->quick)             // Range found by opt_range
 
4814
  else if (select && select->quick)             // Range found by optimizer/range
14809
4815
  {
14810
4816
    int quick_type= select->quick->get_type();
14811
4817
    save_quick= select->quick;
14812
 
    /* 
14813
 
      assume results are not ordered when index merge is used 
14814
 
      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
14815
4821
      by clustered PK values.
14816
4822
    */
14817
 
  
14818
 
    if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE || 
14819
 
        quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
14820
 
        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)
14821
4827
      return(0);
14822
4828
    ref_key=       select->quick->index;
14823
4829
    ref_key_parts= select->quick->used_key_parts;
14828
4834
    /*
14829
4835
      We come here when there is a REF key.
14830
4836
    */
14831
 
    if (!usable_keys.is_set(ref_key))
 
4837
    if (! usable_keys.test(ref_key))
14832
4838
    {
14833
4839
      /*
14834
 
        We come here when ref_key is not among usable_keys
 
4840
        We come here when ref_key is not among usable_keys
14835
4841
      */
14836
 
      uint new_ref_key;
 
4842
      uint32_t new_ref_key;
14837
4843
      /*
14838
 
        If using index only read, only consider other possible index only
14839
 
        keys
 
4844
        If using index only read, only consider other possible index only
 
4845
        keys
14840
4846
      */
14841
 
      if (table->covering_keys.is_set(ref_key))
14842
 
        usable_keys.intersect(table->covering_keys);
 
4847
      if (table->covering_keys.test(ref_key))
 
4848
        usable_keys&= table->covering_keys;
14843
4849
      if (tab->pre_idx_push_select_cond)
14844
4850
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
14845
4851
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
14846
4852
                                       &usable_keys)) < MAX_KEY)
14847
4853
      {
14848
 
        /* Found key that can be used to retrieve data in sorted order */
14849
 
        if (tab->ref.key >= 0)
14850
 
        {
 
4854
        /* Found key that can be used to retrieve data in sorted order */
 
4855
        if (tab->ref.key >= 0)
 
4856
        {
14851
4857
          /*
14852
 
            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
14853
4859
            the index search tuple for new_ref_key will be different (e.g.
14854
4860
            when one index is defined as (part1, part2, ...) and another as
14855
 
            (part1, part2(N), ...) and the WHERE clause contains 
14856
 
            "part1 = const1 AND part2=const2". 
 
4861
            (part1, part2(N), ...) and the WHERE clause contains
 
4862
            "part1 = const1 AND part2=const2".
14857
4863
            So we build tab->ref from scratch here.
14858
4864
          */
14859
 
          KEYUSE *keyuse= tab->keyuse;
14860
 
          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)
14861
4867
            keyuse++;
14862
4868
 
14863
 
          if (create_ref_for_key(tab->join, tab, keyuse, 
 
4869
          if (create_ref_for_key(tab->join, tab, keyuse,
14864
4870
                                 tab->join->const_table_map))
14865
4871
            return(0);
14866
 
        }
14867
 
        else
14868
 
        {
 
4872
        }
 
4873
        else
 
4874
        {
14869
4875
          /*
14870
 
            The range optimizer constructed QUICK_RANGE for ref_key, and
 
4876
            The range optimizer constructed QuickRange for ref_key, and
14871
4877
            we want to use instead new_ref_key as the index. We can't
14872
4878
            just change the index of the quick select, because this may
14873
4879
            result in an incosistent QUICK_SELECT object. Below we
14875
4881
            parameres are set correctly by the range optimizer.
14876
4882
           */
14877
4883
          key_map new_ref_key_map;
14878
 
          new_ref_key_map.clear_all();  // Force the creation of quick select
14879
 
          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.
14880
4886
 
14881
 
          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,
14882
4888
                                        (tab->join->select_options &
14883
4889
                                         OPTION_FOUND_ROWS) ?
14884
4890
                                        HA_POS_ERROR :
14886
4892
                                        true) <=
14887
4893
              0)
14888
4894
            return(0);
14889
 
        }
 
4895
        }
14890
4896
        ref_key= new_ref_key;
14891
4897
      }
14892
4898
    }
14893
4899
    /* Check if we get the rows in requested sorted order by using the key */
14894
 
    if (usable_keys.is_set(ref_key) &&
 
4900
    if (usable_keys.test(ref_key) &&
14895
4901
        (order_direction= test_if_order_by_key(order,table,ref_key,
14896
4902
                                               &used_key_parts)))
14897
4903
      goto check_reverse_order;
14901
4907
      Check whether there is an index compatible with the given order
14902
4908
      usage of which is cheaper than usage of the ref_key index (ref_key>=0)
14903
4909
      or a table scan.
14904
 
      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.
14905
4911
    */
14906
 
    uint nr;
 
4912
    uint32_t nr;
14907
4913
    key_map keys;
14908
 
    uint best_key_parts= 0;
 
4914
    uint32_t best_key_parts= 0;
14909
4915
    int best_key_direction= 0;
14910
4916
    ha_rows best_records= 0;
14911
4917
    double read_time;
14913
4919
    bool is_best_covering= false;
14914
4920
    double fanout= 1;
14915
4921
    JOIN *join= tab->join;
14916
 
    uint tablenr= tab - join->join_tab;
14917
 
    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;
14918
4924
    bool group= join->group && order == join->group_list;
 
4925
    optimizer::Position cur_pos;
14919
4926
 
14920
4927
    /*
14921
4928
      If not used with LIMIT, only use keys if the whole query can be
14924
4931
    */
14925
4932
    if (select_limit >= table_records)
14926
4933
    {
14927
 
      /* 
14928
 
        filesort() and join cache are usually faster than reading in 
 
4934
      /*
 
4935
        filesort() and join cache are usually faster than reading in
14929
4936
        index order and not using join cache
14930
4937
        */
14931
 
      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)
14932
4939
        return(0);
14933
 
      keys= *table->file->keys_to_use_for_scanning();
14934
 
      keys.merge(table->covering_keys);
 
4940
      keys= *table->cursor->keys_to_use_for_scanning();
 
4941
      keys|= table->covering_keys;
14935
4942
 
14936
4943
      /*
14937
 
        We are adding here also the index specified in FORCE INDEX clause, 
14938
 
        if any.
14939
 
        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.
14940
4947
      */
14941
 
      if (table->force_index) 
14942
 
        keys.merge(group ? table->keys_in_use_for_group_by :
14943
 
                           table->keys_in_use_for_order_by);
14944
 
      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;
14945
4952
    }
14946
4953
    else
14947
4954
      keys= usable_keys;
14948
4955
 
14949
 
    read_time= join->best_positions[tablenr].read_time;
14950
 
    for (uint i= tablenr+1; i < join->tables; i++)
14951
 
      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
    }
14952
4963
 
14953
4964
    for (nr=0; nr < table->s->keys ; nr++)
14954
4965
    {
14955
4966
      int direction;
14956
 
      if (keys.is_set(nr) &&
 
4967
      if (keys.test(nr) &&
14957
4968
          (direction= test_if_order_by_key(order, table, nr, &used_key_parts)))
14958
4969
      {
14959
 
        bool is_covering= table->covering_keys.is_set(nr) || (nr == table->s->primary_key && table->file->primary_key_is_clustered());
14960
 
        
14961
 
        /* 
14962
 
          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.
14963
4974
          For GROUP BY without limit always use index scan
14964
 
          if there is a suitable index. 
 
4975
          if there is a suitable index.
14965
4976
          Why we hold to this asymmetry hardly can be explained
14966
4977
          rationally. It's easy to demonstrate that using
14967
4978
          temporary table + filesort could be cheaper for grouping
14968
4979
          queries too.
14969
 
        */ 
 
4980
        */
14970
4981
        if (is_covering ||
14971
 
            select_limit != HA_POS_ERROR || 
 
4982
            select_limit != HA_POS_ERROR ||
14972
4983
            (ref_key < 0 && (group || table->force_index)))
14973
 
        { 
 
4984
        {
14974
4985
          double rec_per_key;
14975
4986
          double index_scan_time;
14976
4987
          KEY *keyinfo= tab->table->key_info+nr;
14979
4990
          if (group)
14980
4991
          {
14981
4992
            rec_per_key= keyinfo->rec_per_key[used_key_parts-1];
14982
 
            set_if_bigger(rec_per_key, 1);
 
4993
            set_if_bigger(rec_per_key, 1.0);
14983
4994
            /*
14984
4995
              With a grouping query each group containing on average
14985
4996
              rec_per_key records produces only one row that will
14986
4997
              be included into the result set.
14987
 
            */  
 
4998
            */
14988
4999
            if (select_limit > table_records/rec_per_key)
14989
5000
                select_limit= table_records;
14990
5001
            else
14991
5002
              select_limit= (ha_rows) (select_limit*rec_per_key);
14992
5003
          }
14993
 
          /* 
 
5004
          /*
14994
5005
            If tab=tk is not the last joined table tn then to get first
14995
5006
            L records from the result set we can expect to retrieve
14996
5007
            only L/fanout(tk,tn) where fanout(tk,tn) says how many
14999
5010
            So the estimate for L/fanout(tk,tn) will be too optimistic
15000
5011
            and as result we'll choose an index scan when using ref/range
15001
5012
            access + filesort will be cheaper.
15002
 
          */
 
5013
          */
15003
5014
          select_limit= (ha_rows) (select_limit < fanout ?
15004
5015
                                   1 : select_limit/fanout);
15005
5016
          /*
15006
5017
            We assume that each of the tested indexes is not correlated
15007
5018
            with ref_key. Thus, to select first N records we have to scan
15008
 
            N/selectivity(ref_key) index entries. 
 
5019
            N/selectivity(ref_key) index entries.
15009
5020
            selectivity(ref_key) = #scanned_records/#table_records =
15010
5021
            table->quick_condition_rows/table_records.
15011
5022
            In any case we can't select more than #table_records.
15012
 
            N/(table->quick_condition_rows/table_records) > table_records 
 
5023
            N/(table->quick_condition_rows/table_records) > table_records
15013
5024
            <=> N > table->quick_condition_rows.
15014
 
          */ 
 
5025
          */
15015
5026
          if (select_limit > table->quick_condition_rows)
15016
5027
            select_limit= table_records;
15017
5028
          else
15019
5030
                                     (double) table_records /
15020
5031
                                      table->quick_condition_rows);
15021
5032
          rec_per_key= keyinfo->rec_per_key[keyinfo->key_parts-1];
15022
 
          set_if_bigger(rec_per_key, 1);
 
5033
          set_if_bigger(rec_per_key, 1.0);
15023
5034
          /*
15024
5035
            Here we take into account the fact that rows are
15025
5036
            accessed in sequences rec_per_key records in each.
15026
5037
            Rows in such a sequence are supposed to be ordered
15027
5038
            by rowid/primary key. When reading the data
15028
5039
            in a sequence we'll touch not more pages than the
15029
 
            table file contains.
 
5040
            table cursor contains.
15030
5041
            TODO. Use the formula for a disk sweep sequential access
15031
 
            to calculate the cost of accessing data rows for one 
 
5042
            to calculate the cost of accessing data rows for one
15032
5043
            index entry.
15033
 
          */
 
5044
          */
15034
5045
          index_scan_time= select_limit/rec_per_key *
15035
 
                           min(rec_per_key, table->file->scan_time());
 
5046
                           min(rec_per_key, table->cursor->scan_time());
15036
5047
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
15037
5048
              index_scan_time < read_time)
15038
5049
          {
15039
5050
            ha_rows quick_records= table_records;
15040
5051
            if (is_best_covering && !is_covering)
15041
5052
              continue;
15042
 
            if (table->quick_keys.is_set(nr))
 
5053
            if (table->quick_keys.test(nr))
15043
5054
              quick_records= table->quick_rows[nr];
15044
5055
            if (best_key < 0 ||
15045
5056
                (select_limit <= min(quick_records,best_records) ?
15050
5061
              best_key_parts= keyinfo->key_parts;
15051
5062
              best_records= quick_records;
15052
5063
              is_best_covering= is_covering;
15053
 
              best_key_direction= direction; 
 
5064
              best_key_direction= direction;
15054
5065
            }
15055
 
          }   
15056
 
        }      
 
5066
          }
 
5067
        }
15057
5068
      }
15058
5069
    }
15059
5070
    if (best_key >= 0)
15060
5071
    {
15061
5072
      bool quick_created= false;
15062
 
      if (table->quick_keys.is_set(best_key) && best_key != ref_key)
 
5073
      if (table->quick_keys.test(best_key) && best_key != ref_key)
15063
5074
      {
15064
 
        key_map map;
15065
 
        map.clear_all();       // Force the creation of quick select
15066
 
        map.set_bit(best_key); // only best_key.
15067
 
        quick_created=         
15068
 
          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,
15069
5080
                                    join->select_options & OPTION_FOUND_ROWS ?
15070
5081
                                    HA_POS_ERROR :
15071
5082
                                    join->unit->select_limit_cnt,
15074
5085
      if (!no_changes)
15075
5086
      {
15076
5087
        if (!quick_created)
15077
 
        {
 
5088
        {
15078
5089
          tab->index= best_key;
15079
5090
          tab->read_first_record= best_key_direction > 0 ?
15080
5091
                                  join_read_first:join_read_last;
15081
 
          tab->type=JT_NEXT;           // Read with index_first(), index_next()
 
5092
          tab->type= AM_NEXT;           // Read with index_first(), index_next()
15082
5093
          if (select && select->quick)
15083
5094
          {
15084
5095
            delete select->quick;
15085
5096
            select->quick= 0;
15086
5097
          }
15087
 
          if (table->covering_keys.is_set(best_key))
 
5098
          if (table->covering_keys.test(best_key))
15088
5099
          {
15089
5100
            table->key_read=1;
15090
 
            table->file->extra(HA_EXTRA_KEYREAD);
 
5101
            table->cursor->extra(HA_EXTRA_KEYREAD);
15091
5102
          }
15092
 
          table->file->ha_index_or_rnd_end();
 
5103
          table->cursor->ha_index_or_rnd_end();
15093
5104
          if (join->select_options & SELECT_DESCRIBE)
15094
5105
          {
15095
5106
            tab->ref.key= -1;
15096
5107
            tab->ref.key_parts= 0;
15097
 
            if (select_limit < table_records) 
 
5108
            if (select_limit < table_records)
15098
5109
              tab->limit= select_limit;
15099
5110
          }
15100
5111
        }
15101
 
        else if (tab->type != JT_ALL)
 
5112
        else if (tab->type != AM_ALL)
15102
5113
        {
15103
5114
          /*
15104
5115
            We're about to use a quick access to the table.
15106
5117
            method is actually used.
15107
5118
          */
15108
5119
          assert(tab->select->quick);
15109
 
          tab->type=JT_ALL;
 
5120
          tab->type= AM_ALL;
15110
5121
          tab->use_quick=1;
15111
5122
          tab->ref.key= -1;
15112
5123
          tab->ref.key_parts=0;         // Don't use ref key.
15113
5124
          tab->read_first_record= join_init_read_record;
15114
 
          /*
15115
 
            TODO: update the number of records in join->best_positions[tablenr]
15116
 
          */
15117
5125
        }
15118
5126
      }
15119
5127
      used_key_parts= best_key_parts;
15120
5128
      order_direction= best_key_direction;
15121
5129
    }
15122
5130
    else
15123
 
      return(0); 
15124
 
  } 
 
5131
      return(0);
 
5132
  }
15125
5133
 
15126
 
check_reverse_order:                  
15127
 
  if (order_direction == -1)            // If ORDER BY ... DESC
 
5134
check_reverse_order:
 
5135
  if (order_direction == -1)            // If order_st BY ... DESC
15128
5136
  {
15129
5137
    if (select && select->quick)
15130
5138
    {
15131
5139
      /*
15132
 
        Don't reverse the sort order, if it's already done.
 
5140
        Don't reverse the sort order, if it's already done.
15133
5141
        (In some cases test_if_order_by_key() can be called multiple times
15134
5142
      */
15135
 
      if (!select->quick->reverse_sorted())
 
5143
      if (! select->quick->reverse_sorted())
15136
5144
      {
15137
 
        QUICK_SELECT_DESC *tmp;
 
5145
        optimizer::QuickSelectDescending *tmp= NULL;
15138
5146
        bool error= false;
15139
5147
        int quick_type= select->quick->get_type();
15140
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE ||
15141
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
15142
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION ||
15143
 
            quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
15144
 
        {
15145
 
          tab->limit= 0;
15146
 
          select->quick= save_quick;
15147
 
          return(0);                   // Use filesort
15148
 
        }
15149
 
            
15150
 
        /* ORDER BY range_key DESC */
15151
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
15152
 
                                    used_key_parts, &error);
15153
 
        if (!tmp || error)
15154
 
        {
15155
 
          delete tmp;
15156
 
          select->quick= save_quick;
15157
 
          tab->limit= 0;
15158
 
          return(0);            // Reverse sort not supported
15159
 
        }
15160
 
        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;
15161
5170
      }
15162
5171
    }
15163
 
    else if (tab->type != JT_NEXT && 
 
5172
    else if (tab->type != AM_NEXT &&
15164
5173
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
15165
5174
    {
15166
5175
      /*
15167
 
        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
15168
5177
 
15169
 
        Use a traversal function that starts by reading the last row
15170
 
        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.
15171
5180
      */
15172
5181
      tab->read_first_record= join_read_last_key;
15173
5182
      tab->read_record.read_record= join_read_prev_same;
15175
5184
  }
15176
5185
  else if (select && select->quick)
15177
5186
    select->quick->sorted= 1;
15178
 
  return(1);
 
5187
  return 1;
15179
5188
}
15180
5189
 
15181
 
 
15182
5190
/*
15183
5191
  If not selecting by given key, create an index how records should be read
15184
5192
 
15185
5193
  SYNOPSIS
15186
5194
   create_sort_index()
15187
 
     thd                Thread handler
 
5195
     session            Thread Cursor
15188
5196
     tab                Table to sort (in join structure)
15189
5197
     order              How table should be sorted
15190
5198
     filesort_limit     Max number of rows that needs to be sorted
15191
5199
     select_limit       Max number of rows in final output
15192
5200
                        Used to decide if we should use index or not
15193
 
     is_order_by        true if we are sorting on ORDER BY, false if GROUP BY
15194
 
                        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
15195
5203
 
15196
5204
 
15197
5205
  IMPLEMENTATION
15198
5206
   - If there is an index that can be used, 'tab' is modified to use
15199
5207
     this index.
15200
 
   - 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
15201
5209
     retrieve rows in order (should be done with 'read_record').
15202
5210
     The sorted data is stored in tab->table and will be freed when calling
15203
 
     free_io_cache(tab->table).
 
5211
     tab->table->free_io_cache().
15204
5212
 
15205
5213
  RETURN VALUES
15206
5214
    0           ok
15207
5215
    -1          Some fatal error
15208
5216
    1           No records
15209
5217
*/
15210
 
 
15211
 
static int
15212
 
create_sort_index(THD *thd, JOIN *join, ORDER *order,
15213
 
                  ha_rows filesort_limit, ha_rows select_limit,
15214
 
                  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)
15215
5219
{
15216
 
  uint length= 0;
 
5220
  uint32_t length= 0;
15217
5221
  ha_rows examined_rows;
15218
 
  TABLE *table;
15219
 
  SQL_SELECT *select;
15220
 
  JOIN_TAB *tab;
 
5222
  Table *table;
 
5223
  optimizer::SqlSelect *select= NULL;
 
5224
  JoinTable *tab;
15221
5225
 
15222
5226
  if (join->tables == join->const_tables)
15223
5227
    return(0);                          // One row, no need to sort
15231
5235
    is going to be used as it is applied now only for one table queries
15232
5236
    with covering indexes.
15233
5237
  */
15234
 
  if ((order != join->group_list || 
 
5238
  if ((order != join->group_list ||
15235
5239
       !(join->select_options & SELECT_BIG_RESULT) ||
15236
 
       (select && select->quick && (select->quick->get_type() == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))) &&
15237
 
      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,
15238
5242
                              is_order_by ?  &table->keys_in_use_for_order_by :
15239
5243
                              &table->keys_in_use_for_group_by))
15240
5244
    return(0);
15241
 
  for (ORDER *ord= join->order; ord; ord= ord->next)
 
5245
  for (order_st *ord= join->order; ord; ord= ord->next)
15242
5246
    length++;
15243
 
  if (!(join->sortorder= 
 
5247
  if (!(join->sortorder=
15244
5248
        make_unireg_sortorder(order, &length, join->sortorder)))
15245
 
    goto err;                           /* purecov: inspected */
 
5249
    goto err;
15246
5250
 
15247
 
  table->sort.io_cache=(IO_CACHE*) my_malloc(sizeof(IO_CACHE),
15248
 
                                             MYF(MY_WME | MY_ZEROFILL));
 
5251
  table->sort.io_cache= new IO_CACHE;
 
5252
  memset(table->sort.io_cache, 0, sizeof(IO_CACHE));
15249
5253
  table->status=0;                              // May be wrong if quick_select
15250
5254
 
15251
5255
  // If table has a range, move it to select
15255
5259
    {
15256
5260
      select->quick=tab->quick;
15257
5261
      tab->quick=0;
15258
 
      /* 
 
5262
      /*
15259
5263
        We can only use 'Only index' if quick key is same as ref_key
15260
5264
        and in index_merge 'Only index' cannot be used
15261
5265
      */
15262
 
      if (table->key_read && ((uint) tab->ref.key != select->quick->index))
 
5266
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
15263
5267
      {
15264
 
        table->key_read=0;
15265
 
        table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5268
        table->key_read=0;
 
5269
        table->cursor->extra(HA_EXTRA_NO_KEYREAD);
15266
5270
      }
15267
5271
    }
15268
5272
    else
15269
5273
    {
15270
5274
      /*
15271
 
        We have a ref on a const;  Change this to a range that filesort
15272
 
        can use.
15273
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
15274
 
        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.
15275
5279
      */
15276
 
      if (!(select->quick= (get_quick_select_for_ref(thd, table, &tab->ref, 
15277
 
                                                     tab->found_records))))
15278
 
        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
      }
15279
5287
    }
15280
5288
  }
15281
5289
 
15282
 
  /* Fill schema tables with data before filesort if it's necessary */
15283
 
  if ((join->select_lex->options & OPTION_SCHEMA_TABLE) &&
15284
 
      get_schema_tables_result(join, PROCESSED_BY_CREATE_SORT_INDEX))
15285
 
    goto err;
15286
 
 
15287
5290
  if (table->s->tmp_table)
15288
 
    table->file->info(HA_STATUS_VARIABLE);      // Get record count
15289
 
  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,
15290
5293
                                     select, filesort_limit, 0,
15291
5294
                                     &examined_rows);
15292
5295
  tab->records= table->sort.found_records;      // For SQL_CALC_ROWS
15298
5301
  tab->select_cond=0;
15299
5302
  tab->last_inner= 0;
15300
5303
  tab->first_unmatched= 0;
15301
 
  tab->type=JT_ALL;                             // Read with normal read_record
 
5304
  tab->type= AM_ALL;                            // Read with normal read_record
15302
5305
  tab->read_first_record= join_init_read_record;
15303
5306
  tab->join->examined_rows+=examined_rows;
15304
5307
  if (table->key_read)                          // Restore if we used indexes
15305
5308
  {
15306
5309
    table->key_read=0;
15307
 
    table->file->extra(HA_EXTRA_NO_KEYREAD);
 
5310
    table->cursor->extra(HA_EXTRA_NO_KEYREAD);
15308
5311
  }
15309
5312
  return(table->sort.found_records == HA_POS_ERROR);
15310
5313
err:
15311
5314
  return(-1);
15312
5315
}
15313
5316
 
15314
 
/*****************************************************************************
15315
 
  Remove duplicates from tmp table
15316
 
  This should be recoded to add a unique index to the table and remove
15317
 
  duplicates
15318
 
  Table is a locked single thread table
15319
 
  fields is the number of fields to check (from the end)
15320
 
*****************************************************************************/
15321
 
 
15322
 
static bool compare_record(TABLE *table, Field **ptr)
15323
 
{
15324
 
  for (; *ptr ; ptr++)
15325
 
  {
15326
 
    if ((*ptr)->cmp_offset(table->s->rec_buff_length))
15327
 
      return 1;
15328
 
  }
15329
 
  return 0;
15330
 
}
15331
 
 
15332
 
static bool copy_blobs(Field **ptr)
15333
 
{
15334
 
  for (; *ptr ; ptr++)
15335
 
  {
15336
 
    if ((*ptr)->flags & BLOB_FLAG)
15337
 
      if (((Field_blob *) (*ptr))->copy())
15338
 
        return 1;                               // Error
15339
 
  }
15340
 
  return 0;
15341
 
}
15342
 
 
15343
 
static void free_blobs(Field **ptr)
15344
 
{
15345
 
  for (; *ptr ; ptr++)
15346
 
  {
15347
 
    if ((*ptr)->flags & BLOB_FLAG)
15348
 
      ((Field_blob *) (*ptr))->free();
15349
 
  }
15350
 
}
15351
 
 
15352
 
 
15353
 
static int
15354
 
remove_duplicates(JOIN *join, TABLE *entry,List<Item> &fields, Item *having)
15355
 
{
15356
 
  int error;
15357
 
  ulong reclength,offset;
15358
 
  uint field_count;
15359
 
  THD *thd= join->thd;
15360
 
 
15361
 
  entry->reginfo.lock_type=TL_WRITE;
15362
 
 
15363
 
  /* Calculate how many saved fields there is in list */
15364
 
  field_count=0;
15365
 
  List_iterator<Item> it(fields);
15366
 
  Item *item;
15367
 
  while ((item=it++))
15368
 
  {
15369
 
    if (item->get_tmp_table_field() && ! item->const_item())
15370
 
      field_count++;
15371
 
  }
15372
 
 
15373
 
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having) 
15374
 
  {                    // only const items with no OPTION_FOUND_ROWS
15375
 
    join->unit->select_limit_cnt= 1;            // Only send first row
15376
 
    return(0);
15377
 
  }
15378
 
  Field **first_field=entry->field+entry->s->fields - field_count;
15379
 
  offset= (field_count ? 
15380
 
           entry->field[entry->s->fields - field_count]->
15381
 
           offset(entry->record[0]) : 0);
15382
 
  reclength=entry->s->reclength-offset;
15383
 
 
15384
 
  free_io_cache(entry);                         // Safety
15385
 
  entry->file->info(HA_STATUS_VARIABLE);
15386
 
  if (entry->s->db_type() == heap_hton ||
15387
 
      (!entry->s->blob_fields &&
15388
 
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
15389
 
        thd->variables.sortbuff_size)))
15390
 
    error=remove_dup_with_hash_index(join->thd, entry,
15391
 
                                     field_count, first_field,
15392
 
                                     reclength, having);
15393
 
  else
15394
 
    error=remove_dup_with_compare(join->thd, entry, first_field, offset,
15395
 
                                  having);
15396
 
 
15397
 
  free_blobs(first_field);
15398
 
  return(error);
15399
 
}
15400
 
 
15401
 
 
15402
 
static int remove_dup_with_compare(THD *thd, TABLE *table, Field **first_field,
15403
 
                                   ulong offset, Item *having)
15404
 
{
15405
 
  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;
15406
5320
  char *org_record,*new_record;
15407
 
  uchar *record;
 
5321
  unsigned char *record;
15408
5322
  int error;
15409
 
  ulong reclength= table->s->reclength-offset;
 
5323
  uint32_t reclength= table->s->reclength-offset;
15410
5324
 
15411
5325
  org_record=(char*) (record=table->record[0])+offset;
15412
5326
  new_record=(char*) table->record[1]+offset;
15413
5327
 
15414
 
  file->ha_rnd_init(1);
15415
 
  error=file->rnd_next(record);
 
5328
  cursor->ha_rnd_init(1);
 
5329
  error=cursor->rnd_next(record);
15416
5330
  for (;;)
15417
5331
  {
15418
 
    if (thd->killed)
 
5332
    if (session->killed)
15419
5333
    {
15420
 
      thd->send_kill_message();
 
5334
      session->send_kill_message();
15421
5335
      error=0;
15422
5336
      goto err;
15423
5337
    }
15424
5338
    if (error)
15425
5339
    {
15426
5340
      if (error == HA_ERR_RECORD_DELETED)
15427
 
        continue;
 
5341
        continue;
15428
5342
      if (error == HA_ERR_END_OF_FILE)
15429
 
        break;
 
5343
        break;
15430
5344
      goto err;
15431
5345
    }
15432
5346
    if (having && !having->val_int())
15433
5347
    {
15434
 
      if ((error=file->ha_delete_row(record)))
15435
 
        goto err;
15436
 
      error=file->rnd_next(record);
 
5348
      if ((error=cursor->ha_delete_row(record)))
 
5349
        goto err;
 
5350
      error=cursor->rnd_next(record);
15437
5351
      continue;
15438
5352
    }
15439
5353
    if (copy_blobs(first_field))
15444
5358
    }
15445
5359
    memcpy(new_record,org_record,reclength);
15446
5360
 
15447
 
    /* Read through rest of file and mark duplicated rows deleted */
 
5361
    /* Read through rest of cursor and mark duplicated rows deleted */
15448
5362
    bool found=0;
15449
5363
    for (;;)
15450
5364
    {
15451
 
      if ((error=file->rnd_next(record)))
 
5365
      if ((error=cursor->rnd_next(record)))
15452
5366
      {
15453
 
        if (error == HA_ERR_RECORD_DELETED)
15454
 
          continue;
15455
 
        if (error == HA_ERR_END_OF_FILE)
15456
 
          break;
15457
 
        goto err;
 
5367
        if (error == HA_ERR_RECORD_DELETED)
 
5368
          continue;
 
5369
        if (error == HA_ERR_END_OF_FILE)
 
5370
          break;
 
5371
        goto err;
15458
5372
      }
15459
 
      if (compare_record(table, first_field) == 0)
 
5373
      if (table->compare_record(first_field) == 0)
15460
5374
      {
15461
 
        if ((error=file->ha_delete_row(record)))
15462
 
          goto err;
 
5375
        if ((error=cursor->ha_delete_row(record)))
 
5376
          goto err;
15463
5377
      }
15464
5378
      else if (!found)
15465
5379
      {
15466
 
        found=1;
15467
 
        file->position(record); // Remember position
 
5380
        found= 1;
 
5381
        cursor->position(record);       // Remember position
15468
5382
      }
15469
5383
    }
15470
5384
    if (!found)
15471
 
      break;                                    // End of file
 
5385
      break;                                    // End of cursor
15472
5386
    /* Restart search on next row */
15473
 
    error=file->restart_rnd_next(record,file->ref);
 
5387
    error=cursor->restart_rnd_next(record,cursor->ref);
15474
5388
  }
15475
5389
 
15476
 
  file->extra(HA_EXTRA_NO_CACHE);
 
5390
  cursor->extra(HA_EXTRA_NO_CACHE);
15477
5391
  return(0);
15478
5392
err:
15479
 
  file->extra(HA_EXTRA_NO_CACHE);
 
5393
  cursor->extra(HA_EXTRA_NO_CACHE);
15480
5394
  if (error)
15481
 
    file->print_error(error,MYF(0));
 
5395
    table->print_error(error,MYF(0));
15482
5396
  return(1);
15483
5397
}
15484
5398
 
15485
 
 
15486
5399
/**
15487
5400
  Generate a hash index for each row to quickly find duplicate rows.
15488
5401
 
15489
5402
  @note
15490
5403
    Note that this will not work on tables with blobs!
15491
5404
*/
15492
 
 
15493
 
static int remove_dup_with_hash_index(THD *thd, TABLE *table,
15494
 
                                      uint field_count,
15495
 
                                      Field **first_field,
15496
 
                                      ulong key_length,
15497
 
                                      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)
15498
5411
{
15499
 
  uchar *key_buffer, *key_pos, *record=table->record[0];
 
5412
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
15500
5413
  int error;
15501
 
  handler *file= table->file;
15502
 
  ulong extra_length= ALIGN_SIZE(key_length)-key_length;
15503
 
  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;
15504
5417
  HASH hash;
15505
5418
 
15506
 
  if (!my_multi_malloc(MYF(MY_WME),
 
5419
  if (! memory::multi_malloc(false,
15507
5420
                       &key_buffer,
15508
 
                       (uint) ((key_length + extra_length) *
15509
 
                               (long) file->stats.records),
 
5421
                       (uint32_t) ((key_length + extra_length) *
 
5422
                               (long) cursor->stats.records),
15510
5423
                       &field_lengths,
15511
 
                       (uint) (field_count*sizeof(*field_lengths)),
15512
 
                       NullS))
 
5424
                       (uint32_t) (field_count*sizeof(*field_lengths)),
 
5425
                       NULL))
15513
5426
    return(1);
15514
5427
 
15515
5428
  {
15516
5429
    Field **ptr;
15517
 
    ulong total_length= 0;
 
5430
    uint32_t total_length= 0;
15518
5431
    for (ptr= first_field, field_length=field_lengths ; *ptr ; ptr++)
15519
5432
    {
15520
 
      uint length= (*ptr)->sort_length();
 
5433
      uint32_t length= (*ptr)->sort_length();
15521
5434
      (*field_length++)= length;
15522
5435
      total_length+= length;
15523
5436
    }
15526
5439
    extra_length= ALIGN_SIZE(key_length)-key_length;
15527
5440
  }
15528
5441
 
15529
 
  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,
15530
5443
                key_length, (hash_get_key) 0, 0, 0))
15531
5444
  {
15532
 
    my_free((char*) key_buffer,MYF(0));
 
5445
    free((char*) key_buffer);
15533
5446
    return(1);
15534
5447
  }
15535
5448
 
15536
 
  file->ha_rnd_init(1);
 
5449
  cursor->ha_rnd_init(1);
15537
5450
  key_pos=key_buffer;
15538
5451
  for (;;)
15539
5452
  {
15540
 
    uchar *org_key_pos;
15541
 
    if (thd->killed)
 
5453
    unsigned char *org_key_pos;
 
5454
    if (session->killed)
15542
5455
    {
15543
 
      thd->send_kill_message();
 
5456
      session->send_kill_message();
15544
5457
      error=0;
15545
5458
      goto err;
15546
5459
    }
15547
 
    if ((error=file->rnd_next(record)))
 
5460
    if ((error=cursor->rnd_next(record)))
15548
5461
    {
15549
5462
      if (error == HA_ERR_RECORD_DELETED)
15550
 
        continue;
 
5463
        continue;
15551
5464
      if (error == HA_ERR_END_OF_FILE)
15552
 
        break;
 
5465
        break;
15553
5466
      goto err;
15554
5467
    }
15555
5468
    if (having && !having->val_int())
15556
5469
    {
15557
 
      if ((error=file->ha_delete_row(record)))
15558
 
        goto err;
 
5470
      if ((error=cursor->ha_delete_row(record)))
 
5471
        goto err;
15559
5472
      continue;
15560
5473
    }
15561
5474
 
15571
5484
    if (hash_search(&hash, org_key_pos, key_length))
15572
5485
    {
15573
5486
      /* Duplicated found ; Remove the row */
15574
 
      if ((error=file->ha_delete_row(record)))
15575
 
        goto err;
 
5487
      if ((error=cursor->ha_delete_row(record)))
 
5488
        goto err;
15576
5489
    }
15577
5490
    else
15578
5491
      (void) my_hash_insert(&hash, org_key_pos);
15579
5492
    key_pos+=extra_length;
15580
5493
  }
15581
 
  my_free((char*) key_buffer,MYF(0));
 
5494
  free((char*) key_buffer);
15582
5495
  hash_free(&hash);
15583
 
  file->extra(HA_EXTRA_NO_CACHE);
15584
 
  (void) file->ha_rnd_end();
 
5496
  cursor->extra(HA_EXTRA_NO_CACHE);
 
5497
  (void) cursor->ha_rnd_end();
15585
5498
  return(0);
15586
5499
 
15587
5500
err:
15588
 
  my_free((char*) key_buffer,MYF(0));
 
5501
  free((char*) key_buffer);
15589
5502
  hash_free(&hash);
15590
 
  file->extra(HA_EXTRA_NO_CACHE);
15591
 
  (void) file->ha_rnd_end();
 
5503
  cursor->extra(HA_EXTRA_NO_CACHE);
 
5504
  (void) cursor->ha_rnd_end();
15592
5505
  if (error)
15593
 
    file->print_error(error,MYF(0));
 
5506
    table->print_error(error,MYF(0));
15594
5507
  return(1);
15595
5508
}
15596
5509
 
15597
 
 
15598
 
SORT_FIELD *make_unireg_sortorder(ORDER *order, uint *length,
15599
 
                                  SORT_FIELD *sortorder)
 
5510
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
15600
5511
{
15601
 
  uint count;
 
5512
  uint32_t count;
15602
5513
  SORT_FIELD *sort,*pos;
15603
5514
 
15604
5515
  count=0;
15605
 
  for (ORDER *tmp = order; tmp; tmp=tmp->next)
 
5516
  for (order_st *tmp = order; tmp; tmp=tmp->next)
15606
5517
    count++;
15607
5518
  if (!sortorder)
15608
 
    sortorder= (SORT_FIELD*) sql_alloc(sizeof(SORT_FIELD) *
 
5519
    sortorder= (SORT_FIELD*) memory::sql_alloc(sizeof(SORT_FIELD) *
15609
5520
                                       (max(count, *length) + 1));
15610
5521
  pos= sort= sortorder;
15611
5522
 
15632
5543
  return(sort);
15633
5544
}
15634
5545
 
15635
 
 
15636
 
/*****************************************************************************
15637
 
  Fill join cache with packed records
15638
 
  Records are stored in tab->cache.buffer and last record in
15639
 
  last record is stored with pointers to blobs to support very big
15640
 
  records
15641
 
******************************************************************************/
15642
 
 
15643
 
static int
15644
 
join_init_cache(THD *thd,JOIN_TAB *tables,uint table_count)
15645
 
{
15646
 
  register unsigned int i;
15647
 
  unsigned int length, blobs;
15648
 
  size_t size;
15649
 
  CACHE_FIELD *copy,**blob_ptr;
15650
 
  JOIN_CACHE  *cache;
15651
 
  JOIN_TAB *join_tab;
15652
 
 
15653
 
  cache= &tables[table_count].cache;
15654
 
  cache->fields=blobs=0;
15655
 
 
15656
 
  join_tab=tables;
15657
 
  for (i=0 ; i < table_count ; i++,join_tab++)
15658
 
  {
15659
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
15660
 
      calc_used_field_length(thd, join_tab);
15661
 
    cache->fields+=join_tab->used_fields;
15662
 
    blobs+=join_tab->used_blobs;
15663
 
 
15664
 
    /* SemiJoinDuplicateElimination: reserve space for rowid */
15665
 
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15666
 
    {
15667
 
      cache->fields++;
15668
 
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
15669
 
    }
15670
 
  }
15671
 
  if (!(cache->field=(CACHE_FIELD*)
15672
 
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
15673
 
 
15674
 
                  sizeof(CACHE_FIELD*))))
15675
 
  {
15676
 
    my_free((uchar*) cache->buff,MYF(0));               /* purecov: inspected */
15677
 
    cache->buff=0;                              /* purecov: inspected */
15678
 
    return(1);                          /* purecov: inspected */
15679
 
  }
15680
 
  copy=cache->field;
15681
 
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
15682
 
    (cache->field+cache->fields+table_count*2);
15683
 
 
15684
 
  length=0;
15685
 
  for (i=0 ; i < table_count ; i++)
15686
 
  {
15687
 
    uint null_fields=0,used_fields;
15688
 
    Field **f_ptr,*field;
15689
 
    MY_BITMAP *read_set= tables[i].table->read_set;
15690
 
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
15691
 
         used_fields ;
15692
 
         f_ptr++)
15693
 
    {
15694
 
      field= *f_ptr;
15695
 
      if (bitmap_is_set(read_set, field->field_index))
15696
 
      {
15697
 
        used_fields--;
15698
 
        length+=field->fill_cache_field(copy);
15699
 
        if (copy->blob_field)
15700
 
          (*blob_ptr++)=copy;
15701
 
        if (field->maybe_null())
15702
 
          null_fields++;
15703
 
        copy->get_rowid= NULL;
15704
 
        copy++;
15705
 
      }
15706
 
    }
15707
 
    /* Copy null bits from table */
15708
 
    if (null_fields && tables[i].table->s->null_fields)
15709
 
    {                                           /* must copy null bits */
15710
 
      copy->str= tables[i].table->null_flags;
15711
 
      copy->length= tables[i].table->s->null_bytes;
15712
 
      copy->strip=0;
15713
 
      copy->blob_field=0;
15714
 
      copy->get_rowid= NULL;
15715
 
      length+=copy->length;
15716
 
      copy++;
15717
 
      cache->fields++;
15718
 
    }
15719
 
    /* If outer join table, copy null_row flag */
15720
 
    if (tables[i].table->maybe_null)
15721
 
    {
15722
 
      copy->str= (uchar*) &tables[i].table->null_row;
15723
 
      copy->length=sizeof(tables[i].table->null_row);
15724
 
      copy->strip=0;
15725
 
      copy->blob_field=0;
15726
 
      copy->get_rowid= NULL;
15727
 
      length+=copy->length;
15728
 
      copy++;
15729
 
      cache->fields++;
15730
 
    }
15731
 
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
15732
 
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
15733
 
    {
15734
 
      copy->str= tables[i].table->file->ref;
15735
 
      copy->length= tables[i].table->file->ref_length;
15736
 
      copy->strip=0;
15737
 
      copy->blob_field=0;
15738
 
      copy->get_rowid= NULL;
15739
 
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
15740
 
      {
15741
 
        /* We will need to call h->position(): */
15742
 
        copy->get_rowid= tables[i].table;
15743
 
        /* And those after us won't have to: */
15744
 
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
15745
 
      }
15746
 
      copy++;
15747
 
    }
15748
 
  }
15749
 
 
15750
 
  cache->length=length+blobs*sizeof(char*);
15751
 
  cache->blobs=blobs;
15752
 
  *blob_ptr=0;                                  /* End sequentel */
15753
 
  size=max(thd->variables.join_buff_size, (ulong)cache->length);
15754
 
  if (!(cache->buff=(uchar*) my_malloc(size,MYF(0))))
15755
 
    return(1);                          /* Don't use cache */ /* purecov: inspected */
15756
 
  cache->end=cache->buff+size;
15757
 
  reset_cache_write(cache);
15758
 
  return(0);
15759
 
}
15760
 
 
15761
 
 
15762
 
static ulong
15763
 
used_blob_length(CACHE_FIELD **ptr)
15764
 
{
15765
 
  uint length,blob_length;
15766
 
  for (length=0 ; *ptr ; ptr++)
15767
 
  {
15768
 
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
15769
 
    length+=blob_length;
15770
 
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
15771
 
  }
15772
 
  return length;
15773
 
}
15774
 
 
15775
 
 
15776
 
static bool
15777
 
store_record_in_cache(JOIN_CACHE *cache)
15778
 
{
15779
 
  uint length;
15780
 
  uchar *pos;
15781
 
  CACHE_FIELD *copy,*end_field;
15782
 
  bool last_record;
15783
 
 
15784
 
  pos=cache->pos;
15785
 
  end_field=cache->field+cache->fields;
15786
 
 
15787
 
  length=cache->length;
15788
 
  if (cache->blobs)
15789
 
    length+=used_blob_length(cache->blob_ptr);
15790
 
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
15791
 
    cache->ptr_record=cache->records;
15792
 
  /*
15793
 
    There is room in cache. Put record there
15794
 
  */
15795
 
  cache->records++;
15796
 
  for (copy=cache->field ; copy < end_field; copy++)
15797
 
  {
15798
 
    if (copy->blob_field)
15799
 
    {
15800
 
      if (last_record)
15801
 
      {
15802
 
        copy->blob_field->get_image(pos, copy->length+sizeof(char*), 
15803
 
                                    copy->blob_field->charset());
15804
 
        pos+=copy->length+sizeof(char*);
15805
 
      }
15806
 
      else
15807
 
      {
15808
 
        copy->blob_field->get_image(pos, copy->length, // blob length
15809
 
                                    copy->blob_field->charset());
15810
 
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
15811
 
        pos+=copy->length+copy->blob_length;
15812
 
      }
15813
 
    }
15814
 
    else
15815
 
    {
15816
 
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
15817
 
      if (copy->get_rowid)
15818
 
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
15819
 
 
15820
 
      if (copy->strip)
15821
 
      {
15822
 
        uchar *str,*end;
15823
 
        for (str=copy->str,end= str+copy->length;
15824
 
             end > str && end[-1] == ' ' ;
15825
 
             end--) ;
15826
 
        length=(uint) (end-str);
15827
 
        memcpy(pos+2, str, length);
15828
 
        int2store(pos, length);
15829
 
        pos+= length+2;
15830
 
      }
15831
 
      else
15832
 
      {
15833
 
        memcpy(pos,copy->str,copy->length);
15834
 
        pos+=copy->length;
15835
 
      }
15836
 
    }
15837
 
  }
15838
 
  cache->pos=pos;
15839
 
  return last_record || (size_t) (cache->end - pos) < cache->length;
15840
 
}
15841
 
 
15842
 
 
15843
 
static void
15844
 
reset_cache_read(JOIN_CACHE *cache)
15845
 
{
15846
 
  cache->record_nr=0;
15847
 
  cache->pos=cache->buff;
15848
 
}
15849
 
 
15850
 
 
15851
 
static void reset_cache_write(JOIN_CACHE *cache)
15852
 
{
15853
 
  reset_cache_read(cache);
15854
 
  cache->records= 0;
15855
 
  cache->ptr_record= (uint) ~0;
15856
 
}
15857
 
 
15858
 
 
15859
 
static void
15860
 
read_cached_record(JOIN_TAB *tab)
15861
 
{
15862
 
  uchar *pos;
15863
 
  uint length;
15864
 
  bool last_record;
15865
 
  CACHE_FIELD *copy,*end_field;
15866
 
 
15867
 
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
15868
 
  pos=tab->cache.pos;
15869
 
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
15870
 
       copy < end_field;
15871
 
       copy++)
15872
 
  {
15873
 
    if (copy->blob_field)
15874
 
    {
15875
 
      if (last_record)
15876
 
      {
15877
 
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
15878
 
                                    copy->blob_field->charset());
15879
 
        pos+=copy->length+sizeof(char*);
15880
 
      }
15881
 
      else
15882
 
      {
15883
 
        copy->blob_field->set_ptr(pos, pos+copy->length);
15884
 
        pos+=copy->length+copy->blob_field->get_length();
15885
 
      }
15886
 
    }
15887
 
    else
15888
 
    {
15889
 
      if (copy->strip)
15890
 
      {
15891
 
        length= uint2korr(pos);
15892
 
        memcpy(copy->str, pos+2, length);
15893
 
        memset(copy->str+length, ' ', copy->length-length);
15894
 
        pos+= 2 + length;
15895
 
      }
15896
 
      else
15897
 
      {
15898
 
        memcpy(copy->str,pos,copy->length);
15899
 
        pos+=copy->length;
15900
 
      }
15901
 
    }
15902
 
  }
15903
 
  tab->cache.pos=pos;
15904
 
  return;
15905
 
}
15906
 
 
15907
 
 
15908
5546
/*
15909
5547
  eq_ref: Create the lookup key and check if it is the same as saved key
15910
5548
 
15911
5549
  SYNOPSIS
15912
5550
    cmp_buffer_with_ref()
15913
5551
      tab  Join tab of the accessed table
15914
 
 
15915
 
  DESCRIPTION 
15916
 
    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
15917
5555
    we've used this key at previous lookup (If yes, we don't need to repeat
15918
5556
    the lookup - the record has been already fetched)
15919
5557
 
15920
 
  RETURN 
 
5558
  RETURN
15921
5559
    true   No cached record for the key, or failed to create the key (due to
15922
5560
           out-of-domain error)
15923
 
    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
15924
5562
           is already in table->record)
15925
5563
*/
15926
 
 
15927
 
static bool
15928
 
cmp_buffer_with_ref(JOIN_TAB *tab)
 
5564
static bool cmp_buffer_with_ref(JoinTable *tab)
15929
5565
{
15930
5566
  bool no_prev_key;
15931
5567
  if (!tab->ref.disable_cache)
15936
5572
      memcpy(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length);
15937
5573
    }
15938
5574
  }
15939
 
  else 
 
5575
  else
15940
5576
    no_prev_key= true;
15941
 
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->thd, tab->table,
15942
 
                                            &tab->ref)) ||
 
5577
  if ((tab->ref.key_err= cp_buffer_from_ref(tab->join->session, &tab->ref)) ||
15943
5578
      no_prev_key)
15944
5579
    return 1;
15945
5580
  return memcmp(tab->ref.key_buff2, tab->ref.key_buff, tab->ref.key_length)
15946
5581
    != 0;
15947
5582
}
15948
5583
 
15949
 
 
15950
 
bool
15951
 
cp_buffer_from_ref(THD *thd, TABLE *table, TABLE_REF *ref)
 
5584
bool cp_buffer_from_ref(Session *session, table_reference_st *ref)
15952
5585
{
15953
 
  enum enum_check_fields save_count_cuted_fields= thd->count_cuted_fields;
15954
 
  thd->count_cuted_fields= CHECK_FIELD_IGNORE;
15955
 
  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;
15956
5588
  bool result= 0;
15957
5589
 
15958
 
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
 
5590
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
15959
5591
  {
15960
5592
    if ((*copy)->copy() & 1)
15961
5593
    {
15963
5595
      break;
15964
5596
    }
15965
5597
  }
15966
 
  thd->count_cuted_fields= save_count_cuted_fields;
15967
 
  dbug_tmp_restore_column_map(table->write_set, old_map);
 
5598
  session->count_cuted_fields= save_count_cuted_fields;
15968
5599
  return result;
15969
5600
}
15970
5601
 
15971
 
 
15972
5602
/*****************************************************************************
15973
5603
  Group and order functions
15974
5604
*****************************************************************************/
15975
5605
 
15976
5606
/**
15977
 
  Resolve an ORDER BY or GROUP BY column reference.
 
5607
  Resolve an order_st BY or GROUP BY column reference.
15978
5608
 
15979
 
  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
15980
5610
  BY clause, find the actual column it represents. If the column being
15981
5611
  resolved is from the GROUP BY clause, the procedure searches the SELECT
15982
5612
  list 'fields' and the columns in the FROM list 'tables'. If 'order' is from
15983
 
  the ORDER BY clause, only the SELECT list is being searched.
 
5613
  the order_st BY clause, only the SELECT list is being searched.
15984
5614
 
15985
5615
  If 'order' is resolved to an Item, then order->item is set to the found
15986
5616
  Item. If there is no item for the found column (that is, it was resolved
15989
5619
 
15990
5620
  ref_pointer_array and all_fields are updated.
15991
5621
 
15992
 
  @param[in] thd                     Pointer to current thread structure
 
5622
  @param[in] session                 Pointer to current thread structure
15993
5623
  @param[in,out] ref_pointer_array  All select, group and order by fields
15994
5624
  @param[in] tables                 List of tables to search in (usually
15995
5625
    FROM clause)
15998
5628
    SELECT list)
15999
5629
  @param[in,out] all_fields         All select, group and order by fields
16000
5630
  @param[in] is_group_field         True if order is a GROUP field, false if
16001
 
    ORDER by field
 
5631
    order_st by field
16002
5632
 
16003
5633
  @retval
16004
5634
    false if OK
16005
5635
  @retval
16006
5636
    true  if error occurred
16007
5637
*/
16008
 
 
16009
 
static bool
16010
 
find_order_in_list(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16011
 
                   ORDER *order, List<Item> &fields, List<Item> &all_fields,
16012
 
                   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)
16013
5645
{
16014
 
  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. */
16015
5647
  Item::Type order_item_type;
16016
5648
  Item **select_item; /* The corresponding item from the SELECT clause. */
16017
5649
  Field *from_field;  /* The corresponding field from the FROM clause. */
16018
 
  uint counter;
 
5650
  uint32_t counter;
16019
5651
  enum_resolution_type resolution;
16020
5652
 
16021
5653
  /*
16024
5656
  */
16025
5657
  if (order_item->type() == Item::INT_ITEM && order_item->basic_const_item())
16026
5658
  {                                             /* Order by position */
16027
 
    uint count= (uint) order_item->val_int();
 
5659
    uint32_t count= (uint32_t) order_item->val_int();
16028
5660
    if (!count || count > fields.elements)
16029
5661
    {
16030
5662
      my_error(ER_BAD_FIELD_ERROR, MYF(0),
16031
 
               order_item->full_name(), thd->where);
 
5663
               order_item->full_name(), session->where);
16032
5664
      return true;
16033
5665
    }
16034
5666
    order->item= ref_pointer_array + count - 1;
16037
5669
    order->counter_used= 1;
16038
5670
    return false;
16039
5671
  }
16040
 
  /* Lookup the current GROUP/ORDER field in the SELECT clause. */
 
5672
  /* Lookup the current GROUP/order_st field in the SELECT clause. */
16041
5673
  select_item= find_item_in_list(order_item, fields, &counter,
16042
5674
                                 REPORT_EXCEPT_NOT_FOUND, &resolution);
16043
5675
  if (!select_item)
16054
5686
      for this name (in case if we would perform lookup in all tables).
16055
5687
    */
16056
5688
    if (resolution == RESOLVED_BEHIND_ALIAS && !order_item->fixed &&
16057
 
        order_item->fix_fields(thd, order->item))
 
5689
        order_item->fix_fields(session, order->item))
16058
5690
      return true;
16059
5691
 
16060
5692
    /* Lookup the current GROUP field in the FROM clause. */
16063
5695
    if ((is_group_field && order_item_type == Item::FIELD_ITEM) ||
16064
5696
        order_item_type == Item::REF_ITEM)
16065
5697
    {
16066
 
      from_field= find_field_in_tables(thd, (Item_ident*) order_item, tables,
16067
 
                                       NULL, &view_ref, IGNORE_ERRORS, true,
16068
 
                                       false);
 
5698
      from_field= find_field_in_tables(session, (Item_ident*) order_item, tables,
 
5699
                                       NULL, &view_ref, IGNORE_ERRORS, false);
16069
5700
      if (!from_field)
16070
5701
        from_field= (Field*) not_found_field;
16071
5702
    }
16103
5734
        warning so the user knows that the field from the FROM clause
16104
5735
        overshadows the column reference from the SELECT list.
16105
5736
      */
16106
 
      push_warning_printf(thd, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
 
5737
      push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_WARN, ER_NON_UNIQ_ERROR,
16107
5738
                          ER(ER_NON_UNIQ_ERROR),
16108
5739
                          ((Item_ident*) order_item)->field_name,
16109
 
                          current_thd->where);
 
5740
                          current_session->where);
16110
5741
    }
16111
5742
  }
16112
5743
 
16123
5754
    arguments for which fix_fields already was called.
16124
5755
  */
16125
5756
  if (!order_item->fixed &&
16126
 
      (order_item->fix_fields(thd, order->item) ||
 
5757
      (order_item->fix_fields(session, order->item) ||
16127
5758
       (order_item= *order->item)->check_cols(1) ||
16128
 
       thd->is_fatal_error))
 
5759
       session->is_fatal_error))
16129
5760
    return true; /* Wrong field. */
16130
5761
 
16131
 
  uint el= all_fields.elements;
 
5762
  uint32_t el= all_fields.elements;
16132
5763
  all_fields.push_front(order_item); /* Add new field to field list. */
16133
5764
  ref_pointer_array[el]= order_item;
16134
5765
  order->item= ref_pointer_array + el;
16135
5766
  return false;
16136
5767
}
16137
5768
 
16138
 
 
16139
5769
/**
16140
5770
  Change order to point at item in select list.
16141
5771
 
16142
5772
  If item isn't a number and doesn't exits in the select list, add it the
16143
5773
  the field list.
16144
5774
*/
16145
 
 
16146
 
int setup_order(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16147
 
                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)
16148
5781
{
16149
 
  thd->where="order clause";
 
5782
  session->where="order clause";
16150
5783
  for (; order; order=order->next)
16151
5784
  {
16152
 
    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,
16153
5786
                           all_fields, false))
16154
5787
      return 1;
16155
5788
  }
16156
5789
  return 0;
16157
5790
}
16158
5791
 
16159
 
 
16160
5792
/**
16161
5793
  Intitialize the GROUP BY list.
16162
5794
 
16163
 
  @param thd                    Thread handler
 
5795
  @param session                        Thread Cursor
16164
5796
  @param ref_pointer_array      We store references to all fields that was
16165
5797
                               not in 'fields' here.
16166
5798
  @param fields         All fields in the select part. Any item in
16182
5814
  @retval
16183
5815
    1  error (probably out of memory)
16184
5816
*/
16185
 
 
16186
 
int
16187
 
setup_group(THD *thd, Item **ref_pointer_array, TABLE_LIST *tables,
16188
 
            List<Item> &fields, List<Item> &all_fields, ORDER *order,
16189
 
            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)
16190
5824
{
16191
5825
  *hidden_group_fields=0;
16192
 
  ORDER *ord;
 
5826
  order_st *ord;
16193
5827
 
16194
5828
  if (!order)
16195
5829
    return 0;                           /* Everything is ok */
16196
5830
 
16197
 
  uint org_fields=all_fields.elements;
 
5831
  uint32_t org_fields=all_fields.elements;
16198
5832
 
16199
 
  thd->where="group statement";
 
5833
  session->where="group statement";
16200
5834
  for (ord= order; ord; ord= ord->next)
16201
5835
  {
16202
 
    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,
16203
5837
                           all_fields, true))
16204
5838
      return 1;
16205
5839
    (*ord->item)->marker= UNDEF_POS;            /* Mark found */
16230
5864
    Item_field *field;
16231
5865
    int cur_pos_in_select_list= 0;
16232
5866
    List_iterator<Item> li(fields);
16233
 
    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);
16234
5868
 
16235
5869
    field= naf_it++;
16236
5870
    while (field && (item=li++))
16279
5913
  Try to use the fields in the order given by 'order' to allow one to
16280
5914
  optimize away 'order by'.
16281
5915
*/
16282
 
 
16283
 
static ORDER *
16284
 
create_distinct_group(THD *thd, Item **ref_pointer_array,
16285
 
                      ORDER *order_list, List<Item> &fields,
16286
 
                      List<Item> &all_fields __attribute__((unused)),
16287
 
                      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)
16288
5922
{
16289
5923
  List_iterator<Item> li(fields);
16290
5924
  Item *item;
16291
 
  ORDER *order,*group,**prev;
 
5925
  order_st *order,*group,**prev;
16292
5926
 
16293
5927
  *all_order_by_fields_used= 1;
16294
5928
  while ((item=li++))
16299
5933
  {
16300
5934
    if (order->in_field_list)
16301
5935
    {
16302
 
      ORDER *ord=(ORDER*) thd->memdup((char*) order,sizeof(ORDER));
 
5936
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
16303
5937
      if (!ord)
16304
 
        return 0;
 
5938
        return 0;
16305
5939
      *prev=ord;
16306
5940
      prev= &ord->next;
16307
5941
      (*ord->item)->marker=1;
16315
5949
  {
16316
5950
    if (!item->const_item() && !item->with_sum_func && !item->marker)
16317
5951
    {
16318
 
      /* 
16319
 
        Don't put duplicate columns from the SELECT list into the 
 
5952
      /*
 
5953
        Don't put duplicate columns from the SELECT list into the
16320
5954
        GROUP BY list.
16321
5955
      */
16322
 
      ORDER *ord_iter;
 
5956
      order_st *ord_iter;
16323
5957
      for (ord_iter= group; ord_iter; ord_iter= ord_iter->next)
16324
5958
        if ((*ord_iter->item)->eq(item, 1))
16325
5959
          goto next_item;
16326
 
      
16327
 
      ORDER *ord=(ORDER*) thd->calloc(sizeof(ORDER));
 
5960
 
 
5961
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
16328
5962
      if (!ord)
16329
 
        return 0;
 
5963
        return 0;
16330
5964
 
16331
5965
      /*
16332
5966
        We have here only field_list (not all_field_list), so we can use
16345
5979
  return group;
16346
5980
}
16347
5981
 
16348
 
 
16349
5982
/**
16350
5983
  Update join with count of the different type of fields.
16351
5984
*/
16352
 
 
16353
 
void
16354
 
count_field_types(SELECT_LEX *select_lex, TMP_TABLE_PARAM *param, 
16355
 
                  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)
16356
5986
{
16357
5987
  List_iterator<Item> li(fields);
16358
5988
  Item *field;
16369
5999
    {
16370
6000
      if (! field->const_item())
16371
6001
      {
16372
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
6002
        Item_sum *sum_item=(Item_sum*) field->real_item();
16373
6003
        if (!sum_item->depended_from() ||
16374
6004
            sum_item->depended_from() == select_lex)
16375
6005
        {
16377
6007
            param->quick_group=0;                       // UDF SUM function
16378
6008
          param->sum_func_count++;
16379
6009
 
16380
 
          for (uint i=0 ; i < sum_item->arg_count ; i++)
 
6010
          for (uint32_t i=0 ; i < sum_item->arg_count ; i++)
16381
6011
          {
16382
6012
            if (sum_item->args[0]->real_item()->type() == Item::FIELD_ITEM)
16383
6013
              param->field_count++;
16392
6022
    {
16393
6023
      param->func_count++;
16394
6024
      if (reset_with_sum_func)
16395
 
        field->with_sum_func=0;
16396
 
    }
16397
 
  }
16398
 
}
16399
 
 
16400
 
 
16401
 
/**
16402
 
  Return 1 if second is a subpart of first argument.
16403
 
 
16404
 
  If first parts has different direction, change it to second part
16405
 
  (group is sorted like order)
16406
 
*/
16407
 
 
16408
 
static bool
16409
 
test_if_subpart(ORDER *a,ORDER *b)
16410
 
{
16411
 
  for (; a && b; a=a->next,b=b->next)
16412
 
  {
16413
 
    if ((*a->item)->eq(*b->item,1))
16414
 
      a->asc=b->asc;
16415
 
    else
16416
 
      return 0;
16417
 
  }
16418
 
  return test(!b);
16419
 
}
16420
 
 
16421
 
/**
16422
 
  Return table number if there is only one table in sort order
16423
 
  and group and order is compatible, else return 0.
16424
 
*/
16425
 
 
16426
 
static TABLE *
16427
 
get_sort_by_table(ORDER *a,ORDER *b,TABLE_LIST *tables)
16428
 
{
16429
 
  table_map map= (table_map) 0;
16430
 
 
16431
 
  if (!a)
16432
 
    a=b;                                        // Only one need to be given
16433
 
  else if (!b)
16434
 
    b=a;
16435
 
 
16436
 
  for (; a && b; a=a->next,b=b->next)
16437
 
  {
16438
 
    if (!(*a->item)->eq(*b->item,1))
16439
 
      return(0);
16440
 
    map|=a->item[0]->used_tables();
16441
 
  }
16442
 
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
16443
 
    return(0);
16444
 
 
16445
 
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
16446
 
  if (map != tables->table->map)
16447
 
    return(0);                          // More than one table
16448
 
  return(tables->table);
16449
 
}
16450
 
 
16451
 
 
16452
 
/**
16453
 
  calc how big buffer we need for comparing group entries.
16454
 
*/
16455
 
 
16456
 
static void
16457
 
calc_group_buffer(JOIN *join,ORDER *group)
16458
 
{
16459
 
  uint key_length=0, parts=0, null_parts=0;
16460
 
 
16461
 
  if (group)
16462
 
    join->group= 1;
16463
 
  for (; group ; group=group->next)
16464
 
  {
16465
 
    Item *group_item= *group->item;
16466
 
    Field *field= group_item->get_tmp_table_field();
16467
 
    if (field)
16468
 
    {
16469
 
      enum_field_types type;
16470
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
16471
 
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
16472
 
      else if (type == DRIZZLE_TYPE_VARCHAR)
16473
 
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
16474
 
      else
16475
 
        key_length+= field->pack_length();
16476
 
    }
16477
 
    else
16478
 
    { 
16479
 
      switch (group_item->result_type()) {
16480
 
      case REAL_RESULT:
16481
 
        key_length+= sizeof(double);
16482
 
        break;
16483
 
      case INT_RESULT:
16484
 
        key_length+= sizeof(int64_t);
16485
 
        break;
16486
 
      case DECIMAL_RESULT:
16487
 
        key_length+= my_decimal_get_binary_size(group_item->max_length - 
16488
 
                                                (group_item->decimals ? 1 : 0),
16489
 
                                                group_item->decimals);
16490
 
        break;
16491
 
      case STRING_RESULT:
16492
 
      {
16493
 
        enum enum_field_types type= group_item->field_type();
16494
 
        /*
16495
 
          As items represented as DATE/TIME fields in the group buffer
16496
 
          have STRING_RESULT result type, we increase the length 
16497
 
          by 8 as maximum pack length of such fields.
16498
 
        */
16499
 
        if (type == DRIZZLE_TYPE_TIME ||
16500
 
            type == DRIZZLE_TYPE_NEWDATE ||
16501
 
            type == DRIZZLE_TYPE_DATETIME ||
16502
 
            type == DRIZZLE_TYPE_TIMESTAMP)
16503
 
        {
16504
 
          key_length+= 8;
16505
 
        }
16506
 
        else
16507
 
        {
16508
 
          /*
16509
 
            Group strings are taken as varstrings and require an length field.
16510
 
            A field is not yet created by create_tmp_field()
16511
 
            and the sizes should match up.
16512
 
          */
16513
 
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
16514
 
        }
16515
 
        break;
16516
 
      }
16517
 
      default:
16518
 
        /* This case should never be choosen */
16519
 
        assert(0);
16520
 
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
16521
 
      }
16522
 
    }
16523
 
    parts++;
16524
 
    if (group_item->maybe_null)
16525
 
      null_parts++;
16526
 
  }
16527
 
  join->tmp_table_param.group_length=key_length+null_parts;
16528
 
  join->tmp_table_param.group_parts=parts;
16529
 
  join->tmp_table_param.group_null_parts=null_parts;
16530
 
}
16531
 
 
16532
 
 
16533
 
/**
16534
 
  allocate group fields or take prepared (cached).
16535
 
 
16536
 
  @param main_join   join of current select
16537
 
  @param curr_join   current join (join of current select or temporary copy
16538
 
                     of it)
16539
 
 
16540
 
  @retval
16541
 
    0   ok
16542
 
  @retval
16543
 
    1   failed
16544
 
*/
16545
 
 
16546
 
static bool
16547
 
make_group_fields(JOIN *main_join, JOIN *curr_join)
16548
 
{
16549
 
  if (main_join->group_fields_cache.elements)
16550
 
  {
16551
 
    curr_join->group_fields= main_join->group_fields_cache;
16552
 
    curr_join->sort_and_group= 1;
16553
 
  }
16554
 
  else
16555
 
  {
16556
 
    if (alloc_group_fields(curr_join, curr_join->group_list))
16557
 
      return (1);
16558
 
    main_join->group_fields_cache= curr_join->group_fields;
16559
 
  }
16560
 
  return (0);
16561
 
}
16562
 
 
16563
 
 
16564
 
/**
16565
 
  Get a list of buffers for saveing last group.
16566
 
 
16567
 
  Groups are saved in reverse order for easyer check loop.
16568
 
*/
16569
 
 
16570
 
static bool
16571
 
alloc_group_fields(JOIN *join,ORDER *group)
16572
 
{
16573
 
  if (group)
16574
 
  {
16575
 
    for (; group ; group=group->next)
16576
 
    {
16577
 
      Cached_item *tmp=new_Cached_item(join->thd, *group->item, false);
16578
 
      if (!tmp || join->group_fields.push_front(tmp))
16579
 
        return true;
16580
 
    }
16581
 
  }
16582
 
  join->sort_and_group=1;                       /* Mark for do_select */
16583
 
  return false;
16584
 
}
16585
 
 
 
6025
        field->with_sum_func=0;
 
6026
    }
 
6027
  }
 
6028
}
16586
6029
 
16587
6030
/*
16588
6031
  Test if a single-row cache of items changed, and update the cache.
16590
6033
  @details Test if a list of items that typically represents a result
16591
6034
  row has changed. If the value of some item changed, update the cached
16592
6035
  value for this item.
16593
 
  
 
6036
 
16594
6037
  @param list list of <item, cached_value> pairs stored as Cached_item.
16595
6038
 
16596
6039
  @return -1 if no item changed
16597
6040
  @return index of the first item that changed
16598
6041
*/
16599
 
 
16600
6042
int test_if_item_cache_changed(List<Cached_item> &list)
16601
6043
{
16602
6044
  List_iterator<Cached_item> li(list);
16611
6053
  return(idx);
16612
6054
}
16613
6055
 
16614
 
 
16615
6056
/**
16616
6057
  Setup copy_fields to save fields at start of new group.
16617
6058
 
16621
6062
  Change old item_field to use a new field with points at saved fieldvalue
16622
6063
  This function is only called before use of send_fields.
16623
6064
 
16624
 
  @param thd                   THD pointer
 
6065
  @param session                   Session pointer
16625
6066
  @param param                 temporary table parameters
16626
6067
  @param ref_pointer_array     array of pointers to top elements of filed list
16627
6068
  @param res_selected_fields   new list of items of select item list
16640
6081
  @retval
16641
6082
    !=0   error
16642
6083
*/
16643
 
 
16644
 
bool
16645
 
setup_copy_fields(THD *thd, TMP_TABLE_PARAM *param,
16646
 
                  Item **ref_pointer_array,
16647
 
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
16648
 
                  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)
16649
6091
{
16650
6092
  Item *pos;
16651
6093
  List_iterator_fast<Item> li(all_fields);
16652
 
  Copy_field *copy= NULL;
 
6094
  CopyField *copy= NULL;
16653
6095
  res_selected_fields.empty();
16654
6096
  res_all_fields.empty();
16655
6097
  List_iterator_fast<Item> itr(res_all_fields);
16656
6098
  List<Item> extra_funcs;
16657
 
  uint i, border= all_fields.elements - elements;
 
6099
  uint32_t i, border= all_fields.elements - elements;
16658
6100
 
16659
 
  if (param->field_count && 
16660
 
      !(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]))
16661
6103
    goto err2;
16662
6104
 
16663
6105
  param->copy_funcs.empty();
16664
6106
  for (i= 0; (pos= li++); i++)
16665
6107
  {
16666
6108
    Field *field;
16667
 
    uchar *tmp;
 
6109
    unsigned char *tmp;
16668
6110
    Item *real_pos= pos->real_item();
16669
6111
    if (real_pos->type() == Item::FIELD_ITEM)
16670
6112
    {
16671
6113
      Item_field *item;
16672
 
      if (!(item= new Item_field(thd, ((Item_field*) real_pos))))
16673
 
        goto err;
 
6114
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
 
6115
        goto err;
16674
6116
      if (pos->type() == Item::REF_ITEM)
16675
6117
      {
16676
6118
        /* preserve the names of the ref when dereferncing */
16682
6124
      pos= item;
16683
6125
      if (item->field->flags & BLOB_FLAG)
16684
6126
      {
16685
 
        if (!(pos= new Item_copy_string(pos)))
16686
 
          goto err;
16687
 
       /*
16688
 
         Item_copy_string::copy for function can call 
16689
 
         Item_copy_string::val_int for blob via Item_ref.
16690
 
         But if Item_copy_string::copy for blob isn't called before,
16691
 
         it's value will be wrong
16692
 
         so let's insert Item_copy_string for blobs in the beginning of 
16693
 
         copy_funcs
16694
 
         (to see full test case look at having.test, BUG #4358) 
16695
 
       */
16696
 
        if (param->copy_funcs.push_front(pos))
16697
 
          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;
16698
6140
      }
16699
6141
      else
16700
6142
      {
16701
 
        /* 
16702
 
           set up save buffer and change result_field to point at 
16703
 
           saved value
16704
 
        */
16705
 
        field= item->field;
16706
 
        item->result_field=field->new_field(thd->mem_root,field->table, 1);
16707
6143
        /*
16708
 
          We need to allocate one extra byte for null handling and
16709
 
          another extra byte to not get warnings from purify in
16710
 
          Field_varstring::val_int
 
6144
          set up save buffer and change result_field to point at
 
6145
          saved value
16711
6146
        */
16712
 
        if (!(tmp= (uchar*) sql_alloc(field->pack_length()+2)))
16713
 
          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;
16714
6156
        if (copy)
16715
6157
        {
16716
6158
          copy->set(tmp, item->result_field);
16730
6172
    {                                           // Save for send fields
16731
6173
      pos= real_pos;
16732
6174
      /* TODO:
16733
 
         In most cases this result will be sent to the user.
16734
 
         This should be changed to use copy_int or copy_real depending
16735
 
         on how the value is to be used: In some cases this may be an
16736
 
         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(*))
16737
6179
      */
16738
6180
      if (!(pos=new Item_copy_string(pos)))
16739
 
        goto err;
16740
 
      if (i < border)                           // HAVING, ORDER and GROUP BY
 
6181
        goto err;
 
6182
      if (i < border)                           // HAVING, order_st and GROUP BY
16741
6183
      {
16742
6184
        if (extra_funcs.push_back(pos))
16743
6185
          goto err;
16744
6186
      }
16745
6187
      else if (param->copy_funcs.push_back(pos))
16746
 
        goto err;
 
6188
        goto err;
16747
6189
    }
16748
6190
    res_all_fields.push_back(pos);
16749
6191
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
16755
6197
    itr++;
16756
6198
  itr.sublist(res_selected_fields, elements);
16757
6199
  /*
16758
 
    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
16759
6201
    reference used in these will resolve to a item that is already calculated
16760
6202
  */
16761
6203
  param->copy_funcs.concat(&extra_funcs);
16762
6204
 
16763
6205
  return(0);
16764
6206
 
16765
 
 err:
 
6207
err:
16766
6208
  if (copy)
16767
6209
    delete [] param->copy_field;                        // This is never 0
16768
6210
  param->copy_field=0;
16770
6212
  return(true);
16771
6213
}
16772
6214
 
16773
 
 
16774
6215
/**
16775
6216
  Make a copy of all simple SELECT'ed items.
16776
6217
 
16777
6218
  This is done at the start of a new group so that we can retrieve
16778
6219
  these later when the group changes.
16779
6220
*/
16780
 
 
16781
 
void
16782
 
copy_fields(TMP_TABLE_PARAM *param)
 
6221
void copy_fields(Tmp_Table_Param *param)
16783
6222
{
16784
 
  Copy_field *ptr=param->copy_field;
16785
 
  Copy_field *end=param->copy_field_end;
 
6223
  CopyField *ptr= param->copy_field;
 
6224
  CopyField *end= param->copy_field_end;
16786
6225
 
16787
6226
  for (; ptr != end; ptr++)
16788
6227
    (*ptr->do_copy)(ptr);
16793
6232
    item->copy();
16794
6233
}
16795
6234
 
16796
 
 
16797
 
/**
16798
 
  Make an array of pointers to sum_functions to speed up
16799
 
  sum_func calculation.
16800
 
 
16801
 
  @retval
16802
 
    0   ok
16803
 
  @retval
16804
 
    1   Error
16805
 
*/
16806
 
 
16807
 
bool JOIN::alloc_func_list()
16808
 
{
16809
 
  uint func_count, group_parts;
16810
 
 
16811
 
  func_count= tmp_table_param.sum_func_count;
16812
 
  /*
16813
 
    If we are using rollup, we need a copy of the summary functions for
16814
 
    each level
16815
 
  */
16816
 
  if (rollup.state != ROLLUP::STATE_NONE)
16817
 
    func_count*= (send_group_parts+1);
16818
 
 
16819
 
  group_parts= send_group_parts;
16820
 
  /*
16821
 
    If distinct, reserve memory for possible
16822
 
    disctinct->group_by optimization
16823
 
  */
16824
 
  if (select_distinct)
16825
 
  {
16826
 
    group_parts+= fields_list.elements;
16827
 
    /*
16828
 
      If the ORDER clause is specified then it's possible that
16829
 
      it also will be optimized, so reserve space for it too
16830
 
    */
16831
 
    if (order)
16832
 
    {
16833
 
      ORDER *ord;
16834
 
      for (ord= order; ord; ord= ord->next)
16835
 
        group_parts++;
16836
 
    }
16837
 
  }
16838
 
 
16839
 
  /* This must use calloc() as rollup_make_fields depends on this */
16840
 
  sum_funcs= (Item_sum**) thd->calloc(sizeof(Item_sum**) * (func_count+1) +
16841
 
                                      sizeof(Item_sum***) * (group_parts+1));
16842
 
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
16843
 
  return(sum_funcs == 0);
16844
 
}
16845
 
 
16846
 
 
16847
 
/**
16848
 
  Initialize 'sum_funcs' array with all Item_sum objects.
16849
 
 
16850
 
  @param field_list        All items
16851
 
  @param send_fields       Items in select list
16852
 
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
16853
 
  @param recompute         Set to true if sum_funcs must be recomputed
16854
 
 
16855
 
  @retval
16856
 
    0  ok
16857
 
  @retval
16858
 
    1  error
16859
 
*/
16860
 
 
16861
 
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
16862
 
                              bool before_group_by, bool recompute)
16863
 
{
16864
 
  List_iterator_fast<Item> it(field_list);
16865
 
  Item_sum **func;
16866
 
  Item *item;
16867
 
 
16868
 
  if (*sum_funcs && !recompute)
16869
 
    return(false); /* We have already initialized sum_funcs. */
16870
 
 
16871
 
  func= sum_funcs;
16872
 
  while ((item=it++))
16873
 
  {
16874
 
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
16875
 
        (!((Item_sum*) item)->depended_from() ||
16876
 
         ((Item_sum *)item)->depended_from() == select_lex))
16877
 
      *func++= (Item_sum*) item;
16878
 
  }
16879
 
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
16880
 
  {
16881
 
    rollup.state= ROLLUP::STATE_READY;
16882
 
    if (rollup_make_fields(field_list, send_fields, &func))
16883
 
      return(true);                     // Should never happen
16884
 
  }
16885
 
  else if (rollup.state == ROLLUP::STATE_NONE)
16886
 
  {
16887
 
    for (uint i=0 ; i <= send_group_parts ;i++)
16888
 
      sum_funcs_end[i]= func;
16889
 
  }
16890
 
  else if (rollup.state == ROLLUP::STATE_READY)
16891
 
    return(false);                         // Don't put end marker
16892
 
  *func=0;                                      // End marker
16893
 
  return(false);
16894
 
}
16895
 
 
16896
 
 
16897
6235
/**
16898
6236
  Change all funcs and sum_funcs to fields in tmp table, and create
16899
6237
  new list of all items.
16900
6238
 
16901
 
  @param thd                   THD pointer
 
6239
  @param session                   Session pointer
16902
6240
  @param ref_pointer_array     array of pointers to top elements of filed list
16903
6241
  @param res_selected_fields   new list of items of select item list
16904
6242
  @param res_all_fields        new list of all items
16910
6248
  @retval
16911
6249
    !=0   error
16912
6250
*/
16913
 
 
16914
 
static bool
16915
 
change_to_use_tmp_fields(THD *thd, Item **ref_pointer_array,
16916
 
                         List<Item> &res_selected_fields,
16917
 
                         List<Item> &res_all_fields,
16918
 
                         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)
16919
6257
{
16920
6258
  List_iterator_fast<Item> it(all_fields);
16921
6259
  Item *item_field,*item;
16923
6261
  res_selected_fields.empty();
16924
6262
  res_all_fields.empty();
16925
6263
 
16926
 
  uint i, border= all_fields.elements - elements;
 
6264
  uint32_t i, border= all_fields.elements - elements;
16927
6265
  for (i= 0; (item= it++); i++)
16928
6266
  {
16929
6267
    Field *field;
16936
6274
    {
16937
6275
      if (item->type() == Item::FIELD_ITEM)
16938
6276
      {
16939
 
        item_field= item->get_tmp_table_item(thd);
 
6277
        item_field= item->get_tmp_table_item(session);
16940
6278
      }
16941
6279
      else if ((field= item->get_tmp_table_field()))
16942
6280
      {
16943
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
16944
 
          item_field= ((Item_sum*) item)->result_item(field);
16945
 
        else
16946
 
          item_field= (Item*) new Item_field(field);
16947
 
        if (!item_field)
16948
 
          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
16949
6287
 
16950
6288
        if (item->real_item()->type() != Item::FIELD_ITEM)
16951
6289
          field->orig_table= 0;
16952
 
        item_field->name= item->name;
 
6290
        item_field->name= item->name;
16953
6291
        if (item->type() == Item::REF_ITEM)
16954
6292
        {
16955
6293
          Item_field *ifield= (Item_field *) item_field;
16959
6297
        }
16960
6298
      }
16961
6299
      else
16962
 
        item_field= item;
 
6300
        item_field= item;
16963
6301
    }
16964
6302
    res_all_fields.push_back(item_field);
16965
6303
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
16973
6311
  return(false);
16974
6312
}
16975
6313
 
16976
 
 
16977
6314
/**
16978
6315
  Change all sum_func refs to fields to point at fields in tmp table.
16979
6316
  Change all funcs to be fields in tmp table.
16980
6317
 
16981
 
  @param thd                   THD pointer
 
6318
  @param session                   Session pointer
16982
6319
  @param ref_pointer_array     array of pointers to top elements of filed list
16983
6320
  @param res_selected_fields   new list of items of select item list
16984
6321
  @param res_all_fields        new list of all items
16990
6327
  @retval
16991
6328
    1   error
16992
6329
*/
16993
 
 
16994
 
static bool
16995
 
change_refs_to_tmp_fields(THD *thd, Item **ref_pointer_array,
16996
 
                          List<Item> &res_selected_fields,
16997
 
                          List<Item> &res_all_fields, uint elements,
16998
 
                          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)
16999
6336
{
17000
6337
  List_iterator_fast<Item> it(all_fields);
17001
6338
  Item *item, *new_item;
17002
6339
  res_selected_fields.empty();
17003
6340
  res_all_fields.empty();
17004
6341
 
17005
 
  uint i, border= all_fields.elements - elements;
 
6342
  uint32_t i, border= all_fields.elements - elements;
17006
6343
  for (i= 0; (item= it++); i++)
17007
6344
  {
17008
 
    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));
17009
6346
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
17010
6347
      new_item;
17011
6348
  }
17015
6352
    itr++;
17016
6353
  itr.sublist(res_selected_fields, elements);
17017
6354
 
17018
 
  return thd->is_fatal_error;
 
6355
  return session->is_fatal_error;
17019
6356
}
17020
6357
 
17021
 
 
17022
 
 
17023
6358
/******************************************************************************
17024
6359
  Code for calculating functions
17025
6360
******************************************************************************/
17026
6361
 
17027
 
 
17028
6362
/**
17029
6363
  Call ::setup for all sum functions.
17030
6364
 
17031
 
  @param thd           thread handler
 
6365
  @param session           thread Cursor
17032
6366
  @param func_ptr      sum function list
17033
6367
 
17034
6368
  @retval
17036
6370
  @retval
17037
6371
    true   error
17038
6372
*/
17039
 
 
17040
 
static bool setup_sum_funcs(THD *thd, Item_sum **func_ptr)
 
6373
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
17041
6374
{
17042
6375
  Item_sum *func;
17043
6376
  while ((func= *(func_ptr++)))
17044
6377
  {
17045
 
    if (func->setup(thd))
 
6378
    if (func->setup(session))
17046
6379
      return(true);
17047
6380
  }
17048
6381
  return(false);
17049
6382
}
17050
6383
 
17051
 
 
17052
 
static void
17053
 
init_tmptable_sum_functions(Item_sum **func_ptr)
 
6384
void init_tmptable_sum_functions(Item_sum **func_ptr)
17054
6385
{
17055
6386
  Item_sum *func;
17056
6387
  while ((func= *(func_ptr++)))
17057
6388
    func->reset_field();
17058
6389
}
17059
6390
 
17060
 
 
17061
6391
/** Update record 0 in tmp_table from record 1. */
17062
 
 
17063
 
static void
17064
 
update_tmptable_sum_func(Item_sum **func_ptr,
17065
 
                         TABLE *tmp_table __attribute__((unused)))
 
6392
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
17066
6393
{
17067
6394
  Item_sum *func;
17068
6395
  while ((func= *(func_ptr++)))
17069
6396
    func->update_field();
17070
6397
}
17071
6398
 
17072
 
 
17073
6399
/** Copy result of sum functions to record in tmp_table. */
17074
 
 
17075
 
static void
17076
 
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
6400
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
17077
6401
{
17078
6402
  for (; func_ptr != end_ptr ; func_ptr++)
17079
6403
    (void) (*func_ptr)->save_in_result_field(1);
17080
6404
  return;
17081
6405
}
17082
6406
 
17083
 
 
17084
 
static bool
17085
 
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
6407
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
17086
6408
{
17087
6409
  for (; func_ptr != end_ptr ;func_ptr++)
17088
6410
  {
17098
6420
  return 0;
17099
6421
}
17100
6422
 
17101
 
 
17102
 
static bool
17103
 
update_sum_func(Item_sum **func_ptr)
 
6423
bool update_sum_func(Item_sum **func_ptr)
17104
6424
{
17105
6425
  Item_sum *func;
17106
6426
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
17110
6430
}
17111
6431
 
17112
6432
/** Copy result of functions to record in tmp_table. */
17113
 
 
17114
 
void
17115
 
copy_funcs(Item **func_ptr)
 
6433
void copy_funcs(Item **func_ptr)
17116
6434
{
17117
6435
  Item *func;
17118
6436
  for (; (func = *func_ptr) ; func_ptr++)
17119
6437
    func->save_in_result_field(1);
17120
6438
}
17121
6439
 
17122
 
 
17123
 
/**
17124
 
  Create a condition for a const reference and add this to the
17125
 
  currenct select for the table.
17126
 
*/
17127
 
 
17128
 
static bool add_ref_to_table_cond(THD *thd, JOIN_TAB *join_tab)
17129
 
{
17130
 
  if (!join_tab->ref.key_parts)
17131
 
    return(false);
17132
 
 
17133
 
  Item_cond_and *cond=new Item_cond_and();
17134
 
  TABLE *table=join_tab->table;
17135
 
  int error;
17136
 
  if (!cond)
17137
 
    return(true);
17138
 
 
17139
 
  for (uint i=0 ; i < join_tab->ref.key_parts ; i++)
17140
 
  {
17141
 
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
17142
 
                              fieldnr-1];
17143
 
    Item *value=join_tab->ref.items[i];
17144
 
    cond->add(new Item_func_equal(new Item_field(field), value));
17145
 
  }
17146
 
  if (thd->is_fatal_error)
17147
 
    return(true);
17148
 
 
17149
 
  if (!cond->fixed)
17150
 
    cond->fix_fields(thd, (Item**)&cond);
17151
 
  if (join_tab->select)
17152
 
  {
17153
 
    error=(int) cond->add(join_tab->select->cond);
17154
 
    join_tab->select_cond=join_tab->select->cond=cond;
17155
 
  }
17156
 
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
17157
 
                                          &error)))
17158
 
    join_tab->select_cond=cond;
17159
 
 
17160
 
  return(error ? true : false);
17161
 
}
17162
 
 
17163
 
 
17164
6440
/**
17165
6441
  Free joins of subselect of this select.
17166
6442
 
17167
 
  @param thd      THD pointer
17168
 
  @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
17169
6445
*/
17170
 
 
17171
 
void free_underlaid_joins(THD *thd __attribute__((unused)),
17172
 
                          SELECT_LEX *select)
 
6446
void free_underlaid_joins(Session *, Select_Lex *select)
17173
6447
{
17174
 
  for (SELECT_LEX_UNIT *unit= select->first_inner_unit();
 
6448
  for (Select_Lex_Unit *unit= select->first_inner_unit();
17175
6449
       unit;
17176
6450
       unit= unit->next_unit())
17177
6451
    unit->cleanup();
17193
6467
  @b EXAMPLES
17194
6468
    @code
17195
6469
      SELECT a+1 FROM t1 GROUP BY a WITH ROLLUP
17196
 
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP 
 
6470
      SELECT SUM(a)+a FROM t1 GROUP BY a WITH ROLLUP
17197
6471
  @endcode
17198
6472
 
17199
6473
  @b IMPLEMENTATION
17206
6480
    This substitution is needed GROUP BY queries with ROLLUP if
17207
6481
    SELECT list contains expressions over group by attributes.
17208
6482
 
17209
 
  @param thd                  reference to the context
 
6483
  @param session                  reference to the context
17210
6484
  @param expr                 expression to make replacement
17211
6485
  @param group_list           list of references to group by items
17212
6486
  @param changed        out:  returns 1 if item contains a replaced field item
17213
6487
 
17214
6488
  @todo
17215
6489
    - TODO: Some functions are not null-preserving. For those functions
17216
 
    updating of the maybe_null attribute is an overkill. 
 
6490
    updating of the maybe_null attribute is an overkill.
17217
6491
 
17218
6492
  @retval
17219
6493
    0   if ok
17220
6494
  @retval
17221
6495
    1   on error
17222
6496
*/
17223
 
 
17224
 
static bool change_group_ref(THD *thd, Item_func *expr, ORDER *group_list,
17225
 
                             bool *changed)
 
6497
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
17226
6498
{
17227
6499
  if (expr->arg_count)
17228
6500
  {
17229
 
    Name_resolution_context *context= &thd->lex->current_select->context;
 
6501
    Name_resolution_context *context= &session->lex->current_select->context;
17230
6502
    Item **arg,**arg_end;
17231
6503
    bool arg_changed= false;
17232
6504
    for (arg= expr->arguments(),
17236
6508
      Item *item= *arg;
17237
6509
      if (item->type() == Item::FIELD_ITEM || item->type() == Item::REF_ITEM)
17238
6510
      {
17239
 
        ORDER *group_tmp;
 
6511
        order_st *group_tmp;
17240
6512
        for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17241
6513
        {
17242
6514
          if (item->eq(*group_tmp->item,0))
17245
6517
            if (!(new_item= new Item_ref(context, group_tmp->item, 0,
17246
6518
                                        item->name)))
17247
6519
              return 1;                                 // fatal_error is set
17248
 
            thd->change_item_tree(arg, new_item);
 
6520
            session->change_item_tree(arg, new_item);
17249
6521
            arg_changed= true;
17250
6522
          }
17251
6523
        }
17252
6524
      }
17253
6525
      else if (item->type() == Item::FUNC_ITEM)
17254
6526
      {
17255
 
        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))
17256
6528
          return 1;
17257
6529
      }
17258
6530
    }
17266
6538
}
17267
6539
 
17268
6540
 
17269
 
/** Allocate memory needed for other rollup functions. */
17270
 
 
17271
 
bool JOIN::rollup_init()
17272
 
{
17273
 
  uint i,j;
17274
 
  Item **ref_array;
17275
 
 
17276
 
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
17277
 
  rollup.state= ROLLUP::STATE_INITED;
17278
 
 
17279
 
  /*
17280
 
    Create pointers to the different sum function groups
17281
 
    These are updated by rollup_make_fields()
17282
 
  */
17283
 
  tmp_table_param.group_parts= send_group_parts;
17284
 
 
17285
 
  if (!(rollup.null_items= (Item_null_result**) thd->alloc((sizeof(Item*) +
17286
 
                                                sizeof(Item**) +
17287
 
                                                sizeof(List<Item>) +
17288
 
                                                ref_pointer_array_size)
17289
 
                                                * send_group_parts )))
17290
 
    return 1;
17291
 
  
17292
 
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
17293
 
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
17294
 
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
17295
 
 
17296
 
  /*
17297
 
    Prepare space for field list for the different levels
17298
 
    These will be filled up in rollup_make_fields()
17299
 
  */
17300
 
  for (i= 0 ; i < send_group_parts ; i++)
17301
 
  {
17302
 
    rollup.null_items[i]= new (thd->mem_root) Item_null_result();
17303
 
    List<Item> *rollup_fields= &rollup.fields[i];
17304
 
    rollup_fields->empty();
17305
 
    rollup.ref_pointer_arrays[i]= ref_array;
17306
 
    ref_array+= all_fields.elements;
17307
 
  }
17308
 
  for (i= 0 ; i < send_group_parts; i++)
17309
 
  {
17310
 
    for (j=0 ; j < fields_list.elements ; j++)
17311
 
      rollup.fields[i].push_back(rollup.null_items[i]);
17312
 
  }
17313
 
  List_iterator<Item> it(all_fields);
17314
 
  Item *item;
17315
 
  while ((item= it++))
17316
 
  {
17317
 
    ORDER *group_tmp;
17318
 
    bool found_in_group= 0;
17319
 
 
17320
 
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
17321
 
    {
17322
 
      if (*group_tmp->item == item)
17323
 
      {
17324
 
        item->maybe_null= 1;
17325
 
        found_in_group= 1;
17326
 
        if (item->const_item())
17327
 
        {
17328
 
          /*
17329
 
            For ROLLUP queries each constant item referenced in GROUP BY list
17330
 
            is wrapped up into an Item_func object yielding the same value
17331
 
            as the constant item. The objects of the wrapper class are never
17332
 
            considered as constant items and besides they inherit all
17333
 
            properties of the Item_result_field class.
17334
 
            This wrapping allows us to ensure writing constant items
17335
 
            into temporary tables whenever the result of the ROLLUP
17336
 
            operation has to be written into a temporary table, e.g. when
17337
 
            ROLLUP is used together with DISTINCT in the SELECT list.
17338
 
            Usually when creating temporary tables for a intermidiate
17339
 
            result we do not include fields for constant expressions.
17340
 
          */           
17341
 
          Item* new_item= new Item_func_rollup_const(item);
17342
 
          if (!new_item)
17343
 
            return 1;
17344
 
          new_item->fix_fields(thd, (Item **) 0);
17345
 
          thd->change_item_tree(it.ref(), new_item);
17346
 
          for (ORDER *tmp= group_tmp; tmp; tmp= tmp->next)
17347
 
          { 
17348
 
            if (*tmp->item == item)
17349
 
              thd->change_item_tree(tmp->item, new_item);
17350
 
          }
17351
 
        }
17352
 
      }
17353
 
    }
17354
 
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
17355
 
    {
17356
 
      bool changed= false;
17357
 
      if (change_group_ref(thd, (Item_func *) item, group_list, &changed))
17358
 
        return 1;
17359
 
      /*
17360
 
        We have to prevent creation of a field in a temporary table for
17361
 
        an expression that contains GROUP BY attributes.
17362
 
        Marking the expression item as 'with_sum_func' will ensure this.
17363
 
      */ 
17364
 
      if (changed)
17365
 
        item->with_sum_func= 1;
17366
 
    }
17367
 
  }
17368
 
  return 0;
17369
 
}
17370
 
  
17371
 
 
17372
 
/**
17373
 
  Fill up rollup structures with pointers to fields to use.
17374
 
 
17375
 
  Creates copies of item_sum items for each sum level.
17376
 
 
17377
 
  @param fields_arg             List of all fields (hidden and real ones)
17378
 
  @param sel_fields             Pointer to selected fields
17379
 
  @param func                   Store here a pointer to all fields
17380
 
 
17381
 
  @retval
17382
 
    0   if ok;
17383
 
    In this case func is pointing to next not used element.
17384
 
  @retval
17385
 
    1    on error
17386
 
*/
17387
 
 
17388
 
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
17389
 
                              Item_sum ***func)
17390
 
{
17391
 
  List_iterator_fast<Item> it(fields_arg);
17392
 
  Item *first_field= sel_fields.head();
17393
 
  uint level;
17394
 
 
17395
 
  /*
17396
 
    Create field lists for the different levels
17397
 
 
17398
 
    The idea here is to have a separate field list for each rollup level to
17399
 
    avoid all runtime checks of which columns should be NULL.
17400
 
 
17401
 
    The list is stored in reverse order to get sum function in such an order
17402
 
    in func that it makes it easy to reset them with init_sum_functions()
17403
 
 
17404
 
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17405
 
 
17406
 
    rollup.fields[0] will contain list where a,b,c is NULL
17407
 
    rollup.fields[1] will contain list where b,c is NULL
17408
 
    ...
17409
 
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
17410
 
    ...
17411
 
    sum_funcs_end[0] points to all sum functions
17412
 
    sum_funcs_end[1] points to all sum functions, except grand totals
17413
 
    ...
17414
 
  */
17415
 
 
17416
 
  for (level=0 ; level < send_group_parts ; level++)
17417
 
  {
17418
 
    uint i;
17419
 
    uint pos= send_group_parts - level -1;
17420
 
    bool real_fields= 0;
17421
 
    Item *item;
17422
 
    List_iterator<Item> new_it(rollup.fields[pos]);
17423
 
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
17424
 
    ORDER *start_group;
17425
 
 
17426
 
    /* Point to first hidden field */
17427
 
    Item **ref_array= ref_array_start + fields_arg.elements-1;
17428
 
 
17429
 
    /* Remember where the sum functions ends for the previous level */
17430
 
    sum_funcs_end[pos+1]= *func;
17431
 
 
17432
 
    /* Find the start of the group for this level */
17433
 
    for (i= 0, start_group= group_list ;
17434
 
         i++ < pos ;
17435
 
         start_group= start_group->next)
17436
 
      ;
17437
 
 
17438
 
    it.rewind();
17439
 
    while ((item= it++))
17440
 
    {
17441
 
      if (item == first_field)
17442
 
      {
17443
 
        real_fields= 1;                         // End of hidden fields
17444
 
        ref_array= ref_array_start;
17445
 
      }
17446
 
 
17447
 
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
17448
 
          (!((Item_sum*) item)->depended_from() ||
17449
 
           ((Item_sum *)item)->depended_from() == select_lex))
17450
 
          
17451
 
      {
17452
 
        /*
17453
 
          This is a top level summary function that must be replaced with
17454
 
          a sum function that is reset for this level.
17455
 
 
17456
 
          NOTE: This code creates an object which is not that nice in a
17457
 
          sub select.  Fortunately it's not common to have rollup in
17458
 
          sub selects.
17459
 
        */
17460
 
        item= item->copy_or_same(thd);
17461
 
        ((Item_sum*) item)->make_unique();
17462
 
        *(*func)= (Item_sum*) item;
17463
 
        (*func)++;
17464
 
      }
17465
 
      else 
17466
 
      {
17467
 
        /* Check if this is something that is part of this group by */
17468
 
        ORDER *group_tmp;
17469
 
        for (group_tmp= start_group, i= pos ;
17470
 
             group_tmp ; group_tmp= group_tmp->next, i++)
17471
 
        {
17472
 
          if (*group_tmp->item == item)
17473
 
          {
17474
 
            /*
17475
 
              This is an element that is used by the GROUP BY and should be
17476
 
              set to NULL in this level
17477
 
            */
17478
 
            Item_null_result *null_item= new (thd->mem_root) Item_null_result();
17479
 
            if (!null_item)
17480
 
              return 1;
17481
 
            item->maybe_null= 1;                // Value will be null sometimes
17482
 
            null_item->result_field= item->get_tmp_table_field();
17483
 
            item= null_item;
17484
 
            break;
17485
 
          }
17486
 
        }
17487
 
      }
17488
 
      *ref_array= item;
17489
 
      if (real_fields)
17490
 
      {
17491
 
        (void) new_it++;                        // Point to next item
17492
 
        new_it.replace(item);                   // Replace previous
17493
 
        ref_array++;
17494
 
      }
17495
 
      else
17496
 
        ref_array--;
17497
 
    }
17498
 
  }
17499
 
  sum_funcs_end[0]= *func;                      // Point to last function
17500
 
  return 0;
17501
 
}
17502
 
 
17503
 
/**
17504
 
  Send all rollup levels higher than the current one to the client.
17505
 
 
17506
 
  @b SAMPLE
17507
 
    @code
17508
 
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
17509
 
  @endcode
17510
 
 
17511
 
  @param idx            Level we are on:
17512
 
                        - 0 = Total sum level
17513
 
                        - 1 = First group changed  (a)
17514
 
                        - 2 = Second group changed (a,b)
17515
 
 
17516
 
  @retval
17517
 
    0   ok
17518
 
  @retval
17519
 
    1   If send_data_failed()
17520
 
*/
17521
 
 
17522
 
int JOIN::rollup_send_data(uint idx)
17523
 
{
17524
 
  uint i;
17525
 
  for (i= send_group_parts ; i-- > idx ; )
17526
 
  {
17527
 
    /* Get reference pointers to sum functions in place */
17528
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
17529
 
           ref_pointer_array_size);
17530
 
    if ((!having || having->val_int()))
17531
 
    {
17532
 
      if (send_records < unit->select_limit_cnt && do_send_rows &&
17533
 
          result->send_data(rollup.fields[i]))
17534
 
        return 1;
17535
 
      send_records++;
17536
 
    }
17537
 
  }
17538
 
  /* Restore ref_pointer_array */
17539
 
  set_items_ref_array(current_ref_pointer_array);
17540
 
  return 0;
17541
 
}
17542
 
 
17543
 
/**
17544
 
  Write all rollup levels higher than the current one to a temp table.
17545
 
 
17546
 
  @b SAMPLE
17547
 
    @code
17548
 
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
17549
 
  @endcode
17550
 
 
17551
 
  @param idx                 Level we are on:
17552
 
                               - 0 = Total sum level
17553
 
                               - 1 = First group changed  (a)
17554
 
                               - 2 = Second group changed (a,b)
17555
 
  @param table               reference to temp table
17556
 
 
17557
 
  @retval
17558
 
    0   ok
17559
 
  @retval
17560
 
    1   if write_data_failed()
17561
 
*/
17562
 
 
17563
 
int JOIN::rollup_write_data(uint idx, TABLE *table_arg)
17564
 
{
17565
 
  uint i;
17566
 
  for (i= send_group_parts ; i-- > idx ; )
17567
 
  {
17568
 
    /* Get reference pointers to sum functions in place */
17569
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
17570
 
           ref_pointer_array_size);
17571
 
    if ((!having || having->val_int()))
17572
 
    {
17573
 
      int write_error;
17574
 
      Item *item;
17575
 
      List_iterator_fast<Item> it(rollup.fields[i]);
17576
 
      while ((item= it++))
17577
 
      {
17578
 
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
17579
 
          item->save_in_result_field(1);
17580
 
      }
17581
 
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
17582
 
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
17583
 
      {
17584
 
        if (create_myisam_from_heap(thd, table_arg, 
17585
 
                                    tmp_table_param.start_recinfo,
17586
 
                                    &tmp_table_param.recinfo,
17587
 
                                    write_error, 0))
17588
 
          return 1;                  
17589
 
      }
17590
 
    }
17591
 
  }
17592
 
  /* Restore ref_pointer_array */
17593
 
  set_items_ref_array(current_ref_pointer_array);
17594
 
  return 0;
17595
 
}
17596
 
 
17597
 
/**
17598
 
  clear results if there are not rows found for group
17599
 
  (end_send_group/end_write_group)
17600
 
*/
17601
 
 
17602
 
void JOIN::clear()
17603
 
{
17604
 
  clear_tables(this);
17605
 
  copy_fields(&tmp_table_param);
17606
 
 
17607
 
  if (sum_funcs)
17608
 
  {
17609
 
    Item_sum *func, **func_ptr= sum_funcs;
17610
 
    while ((func= *(func_ptr++)))
17611
 
      func->clear();
17612
 
  }
17613
 
}
17614
 
 
17615
 
/**
17616
 
  EXPLAIN handling.
17617
 
 
17618
 
  Send a description about what how the select will be done to stdout.
17619
 
*/
17620
 
 
17621
 
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
17622
 
                     bool distinct,const char *message)
17623
 
{
17624
 
  List<Item> field_list;
17625
 
  List<Item> item_list;
17626
 
  THD *thd=join->thd;
17627
 
  select_result *result=join->result;
17628
 
  Item *item_null= new Item_null();
17629
 
  const CHARSET_INFO * const cs= system_charset_info;
17630
 
  int quick_type;
17631
 
  /* Don't log this into the slow query log */
17632
 
  thd->server_status&= ~(SERVER_QUERY_NO_INDEX_USED | SERVER_QUERY_NO_GOOD_INDEX_USED);
17633
 
  join->unit->offset_limit_cnt= 0;
17634
 
 
17635
 
  /* 
17636
 
    NOTE: the number/types of items pushed into item_list must be in sync with
17637
 
    EXPLAIN column types as they're "defined" in THD::send_explain_fields()
17638
 
  */
17639
 
  if (message)
17640
 
  {
17641
 
    item_list.push_back(new Item_int((int32_t)
17642
 
                                     join->select_lex->select_number));
17643
 
    item_list.push_back(new Item_string(join->select_lex->type,
17644
 
                                        strlen(join->select_lex->type), cs));
17645
 
    for (uint i=0 ; i < 7; i++)
17646
 
      item_list.push_back(item_null);
17647
 
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17648
 
      item_list.push_back(item_null);
17649
 
  
17650
 
    item_list.push_back(new Item_string(message,strlen(message),cs));
17651
 
    if (result->send_data(item_list))
17652
 
      join->error= 1;
17653
 
  }
17654
 
  else if (join->select_lex == join->unit->fake_select_lex)
17655
 
  {
17656
 
    /* 
17657
 
      here we assume that the query will return at least two rows, so we
17658
 
      show "filesort" in EXPLAIN. Of course, sometimes we'll be wrong
17659
 
      and no filesort will be actually done, but executing all selects in
17660
 
      the UNION to provide precise EXPLAIN information will hardly be
17661
 
      appreciated :)
17662
 
    */
17663
 
    char table_name_buffer[NAME_LEN];
17664
 
    item_list.empty();
17665
 
    /* id */
17666
 
    item_list.push_back(new Item_null);
17667
 
    /* select_type */
17668
 
    item_list.push_back(new Item_string(join->select_lex->type,
17669
 
                                        strlen(join->select_lex->type),
17670
 
                                        cs));
17671
 
    /* table */
17672
 
    {
17673
 
      SELECT_LEX *sl= join->unit->first_select();
17674
 
      uint len= 6, lastop= 0;
17675
 
      memcpy(table_name_buffer, STRING_WITH_LEN("<union"));
17676
 
      for (; sl && len + lastop + 5 < NAME_LEN; sl= sl->next_select())
17677
 
      {
17678
 
        len+= lastop;
17679
 
        lastop= snprintf(table_name_buffer + len, NAME_LEN - len,
17680
 
                         "%u,", sl->select_number);
17681
 
      }
17682
 
      if (sl || len + lastop >= NAME_LEN)
17683
 
      {
17684
 
        memcpy(table_name_buffer + len, STRING_WITH_LEN("...>") + 1);
17685
 
        len+= 4;
17686
 
      }
17687
 
      else
17688
 
      {
17689
 
        len+= lastop;
17690
 
        table_name_buffer[len - 1]= '>';  // change ',' to '>'
17691
 
      }
17692
 
      item_list.push_back(new Item_string(table_name_buffer, len, cs));
17693
 
    }
17694
 
    /* type */
17695
 
    item_list.push_back(new Item_string(join_type_str[JT_ALL],
17696
 
                                          strlen(join_type_str[JT_ALL]),
17697
 
                                          cs));
17698
 
    /* possible_keys */
17699
 
    item_list.push_back(item_null);
17700
 
    /* key*/
17701
 
    item_list.push_back(item_null);
17702
 
    /* key_len */
17703
 
    item_list.push_back(item_null);
17704
 
    /* ref */
17705
 
    item_list.push_back(item_null);
17706
 
    /* in_rows */
17707
 
    if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17708
 
      item_list.push_back(item_null);
17709
 
    /* rows */
17710
 
    item_list.push_back(item_null);
17711
 
    /* extra */
17712
 
    if (join->unit->global_parameters->order_list.first)
17713
 
      item_list.push_back(new Item_string("Using filesort",
17714
 
                                          14, cs));
17715
 
    else
17716
 
      item_list.push_back(new Item_string("", 0, cs));
17717
 
 
17718
 
    if (result->send_data(item_list))
17719
 
      join->error= 1;
17720
 
  }
17721
 
  else
17722
 
  {
17723
 
    table_map used_tables=0;
17724
 
    for (uint i=0 ; i < join->tables ; i++)
17725
 
    {
17726
 
      JOIN_TAB *tab=join->join_tab+i;
17727
 
      TABLE *table=tab->table;
17728
 
      TABLE_LIST *table_list= tab->table->pos_in_table_list;
17729
 
      char buff[512]; 
17730
 
      char buff1[512], buff2[512], buff3[512];
17731
 
      char keylen_str_buf[64];
17732
 
      String extra(buff, sizeof(buff),cs);
17733
 
      char table_name_buffer[NAME_LEN];
17734
 
      String tmp1(buff1,sizeof(buff1),cs);
17735
 
      String tmp2(buff2,sizeof(buff2),cs);
17736
 
      String tmp3(buff3,sizeof(buff3),cs);
17737
 
      extra.length(0);
17738
 
      tmp1.length(0);
17739
 
      tmp2.length(0);
17740
 
      tmp3.length(0);
17741
 
 
17742
 
      quick_type= -1;
17743
 
      item_list.empty();
17744
 
      /* id */
17745
 
      item_list.push_back(new Item_uint((uint32_t)
17746
 
                                       join->select_lex->select_number));
17747
 
      /* select_type */
17748
 
      item_list.push_back(new Item_string(join->select_lex->type,
17749
 
                                          strlen(join->select_lex->type),
17750
 
                                          cs));
17751
 
      if (tab->type == JT_ALL && tab->select && tab->select->quick)
17752
 
      {
17753
 
        quick_type= tab->select->quick->get_type();
17754
 
        if ((quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE) ||
17755
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT) ||
17756
 
            (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION))
17757
 
          tab->type = JT_INDEX_MERGE;
17758
 
        else
17759
 
          tab->type = JT_RANGE;
17760
 
      }
17761
 
      /* table */
17762
 
      if (table->derived_select_number)
17763
 
      {
17764
 
        /* Derived table name generation */
17765
 
        int len= snprintf(table_name_buffer, sizeof(table_name_buffer)-1,
17766
 
                          "<derived%u>",
17767
 
                          table->derived_select_number);
17768
 
        item_list.push_back(new Item_string(table_name_buffer, len, cs));
17769
 
      }
17770
 
      else
17771
 
      {
17772
 
        TABLE_LIST *real_table= table->pos_in_table_list; 
17773
 
        item_list.push_back(new Item_string(real_table->alias,
17774
 
                                            strlen(real_table->alias),
17775
 
                                            cs));
17776
 
      }
17777
 
      /* "type" column */
17778
 
      item_list.push_back(new Item_string(join_type_str[tab->type],
17779
 
                                          strlen(join_type_str[tab->type]),
17780
 
                                          cs));
17781
 
      /* Build "possible_keys" value and add it to item_list */
17782
 
      if (!tab->keys.is_clear_all())
17783
 
      {
17784
 
        uint j;
17785
 
        for (j=0 ; j < table->s->keys ; j++)
17786
 
        {
17787
 
          if (tab->keys.is_set(j))
17788
 
          {
17789
 
            if (tmp1.length())
17790
 
              tmp1.append(',');
17791
 
            tmp1.append(table->key_info[j].name, 
17792
 
                        strlen(table->key_info[j].name),
17793
 
                        system_charset_info);
17794
 
          }
17795
 
        }
17796
 
      }
17797
 
      if (tmp1.length())
17798
 
        item_list.push_back(new Item_string(tmp1.ptr(),tmp1.length(),cs));
17799
 
      else
17800
 
        item_list.push_back(item_null);
17801
 
 
17802
 
      /* Build "key", "key_len", and "ref" values and add them to item_list */
17803
 
      if (tab->ref.key_parts)
17804
 
      {
17805
 
        KEY *key_info=table->key_info+ tab->ref.key;
17806
 
        register uint length;
17807
 
        item_list.push_back(new Item_string(key_info->name,
17808
 
                                            strlen(key_info->name),
17809
 
                                            system_charset_info));
17810
 
        length= int64_t2str(tab->ref.key_length, keylen_str_buf, 10) - 
17811
 
                keylen_str_buf;
17812
 
        item_list.push_back(new Item_string(keylen_str_buf, length,
17813
 
                                            system_charset_info));
17814
 
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
17815
 
        {
17816
 
          if (tmp2.length())
17817
 
            tmp2.append(',');
17818
 
          tmp2.append((*ref)->name(), strlen((*ref)->name()),
17819
 
                      system_charset_info);
17820
 
        }
17821
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17822
 
      }
17823
 
      else if (tab->type == JT_NEXT)
17824
 
      {
17825
 
        KEY *key_info=table->key_info+ tab->index;
17826
 
        register uint length;
17827
 
        item_list.push_back(new Item_string(key_info->name,
17828
 
                                            strlen(key_info->name),cs));
17829
 
        length= int64_t2str(key_info->key_length, keylen_str_buf, 10) - 
17830
 
                keylen_str_buf;
17831
 
        item_list.push_back(new Item_string(keylen_str_buf, 
17832
 
                                            length,
17833
 
                                            system_charset_info));
17834
 
        item_list.push_back(item_null);
17835
 
      }
17836
 
      else if (tab->select && tab->select->quick)
17837
 
      {
17838
 
        tab->select->quick->add_keys_and_lengths(&tmp2, &tmp3);
17839
 
        item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17840
 
        item_list.push_back(new Item_string(tmp3.ptr(),tmp3.length(),cs));
17841
 
        item_list.push_back(item_null);
17842
 
      }
17843
 
      else
17844
 
      {
17845
 
        if (table_list->schema_table &&
17846
 
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
17847
 
        {
17848
 
          const char *tmp_buff;
17849
 
          int f_idx;
17850
 
          if (table_list->has_db_lookup_value)
17851
 
          {
17852
 
            f_idx= table_list->schema_table->idx_field1;
17853
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17854
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17855
 
          }          
17856
 
          if (table_list->has_table_lookup_value)
17857
 
          {
17858
 
            if (table_list->has_db_lookup_value)
17859
 
              tmp2.append(',');
17860
 
            f_idx= table_list->schema_table->idx_field2;
17861
 
            tmp_buff= table_list->schema_table->fields_info[f_idx].field_name;
17862
 
            tmp2.append(tmp_buff, strlen(tmp_buff), cs);
17863
 
          }
17864
 
          if (tmp2.length())
17865
 
            item_list.push_back(new Item_string(tmp2.ptr(),tmp2.length(),cs));
17866
 
          else
17867
 
            item_list.push_back(item_null);
17868
 
        }
17869
 
        else
17870
 
          item_list.push_back(item_null);
17871
 
        item_list.push_back(item_null);
17872
 
        item_list.push_back(item_null);
17873
 
      }
17874
 
      
17875
 
      /* Add "rows" field to item_list. */
17876
 
      if (table_list->schema_table)
17877
 
      {
17878
 
        /* in_rows */
17879
 
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17880
 
          item_list.push_back(item_null);
17881
 
        /* rows */
17882
 
        item_list.push_back(item_null);
17883
 
      }
17884
 
      else
17885
 
      {
17886
 
        double examined_rows;
17887
 
        if (tab->select && tab->select->quick)
17888
 
          examined_rows= rows2double(tab->select->quick->records);
17889
 
        else if (tab->type == JT_NEXT || tab->type == JT_ALL)
17890
 
          examined_rows= rows2double(tab->limit ? tab->limit : 
17891
 
                                     tab->table->file->records());
17892
 
        else
17893
 
          examined_rows= join->best_positions[i].records_read; 
17894
 
 
17895
 
        item_list.push_back(new Item_int((int64_t) (uint64_t) examined_rows, 
17896
 
                                         MY_INT64_NUM_DECIMAL_DIGITS));
17897
 
 
17898
 
        /* Add "filtered" field to item_list. */
17899
 
        if (join->thd->lex->describe & DESCRIBE_EXTENDED)
17900
 
        {
17901
 
          float f= 0.0; 
17902
 
          if (examined_rows)
17903
 
            f= (float) (100.0 * join->best_positions[i].records_read /
17904
 
                        examined_rows);
17905
 
          item_list.push_back(new Item_float(f, 2));
17906
 
        }
17907
 
      }
17908
 
 
17909
 
      /* Build "Extra" field and add it to item_list. */
17910
 
      bool key_read=table->key_read;
17911
 
      if ((tab->type == JT_NEXT || tab->type == JT_CONST) &&
17912
 
          table->covering_keys.is_set(tab->index))
17913
 
        key_read=1;
17914
 
      if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT &&
17915
 
          !((QUICK_ROR_INTERSECT_SELECT*)tab->select->quick)->need_to_fetch_row)
17916
 
        key_read=1;
17917
 
        
17918
 
      if (tab->info)
17919
 
        item_list.push_back(new Item_string(tab->info,strlen(tab->info),cs));
17920
 
      else if (tab->packed_info & TAB_INFO_HAVE_VALUE)
17921
 
      {
17922
 
        if (tab->packed_info & TAB_INFO_USING_INDEX)
17923
 
          extra.append(STRING_WITH_LEN("; Using index"));
17924
 
        if (tab->packed_info & TAB_INFO_USING_WHERE)
17925
 
          extra.append(STRING_WITH_LEN("; Using where"));
17926
 
        if (tab->packed_info & TAB_INFO_FULL_SCAN_ON_NULL)
17927
 
          extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
17928
 
        /* Skip initial "; "*/
17929
 
        const char *str= extra.ptr();
17930
 
        uint32_t len= extra.length();
17931
 
        if (len)
17932
 
        {
17933
 
          str += 2;
17934
 
          len -= 2;
17935
 
        }
17936
 
        item_list.push_back(new Item_string(str, len, cs));
17937
 
      }
17938
 
      else
17939
 
      {
17940
 
        uint keyno= MAX_KEY;
17941
 
        if (tab->ref.key_parts)
17942
 
          keyno= tab->ref.key;
17943
 
        else if (tab->select && tab->select->quick)
17944
 
          keyno = tab->select->quick->index;
17945
 
 
17946
 
        if (keyno != MAX_KEY && keyno == table->file->pushed_idx_cond_keyno &&
17947
 
            table->file->pushed_idx_cond)
17948
 
          extra.append(STRING_WITH_LEN("; Using index condition"));
17949
 
 
17950
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_ROR_UNION || 
17951
 
            quick_type == QUICK_SELECT_I::QS_TYPE_ROR_INTERSECT ||
17952
 
            quick_type == QUICK_SELECT_I::QS_TYPE_INDEX_MERGE)
17953
 
        {
17954
 
          extra.append(STRING_WITH_LEN("; Using "));
17955
 
          tab->select->quick->add_info_string(&extra);
17956
 
        }
17957
 
          if (tab->select)
17958
 
        {
17959
 
          if (tab->use_quick == 2)
17960
 
          {
17961
 
            /* 4 bits per 1 hex digit + terminating '\0' */
17962
 
            char buf[MAX_KEY / 4 + 1];
17963
 
            extra.append(STRING_WITH_LEN("; Range checked for each "
17964
 
                                         "record (index map: 0x"));
17965
 
            extra.append(tab->keys.print(buf));
17966
 
            extra.append(')');
17967
 
          }
17968
 
          else if (tab->select->cond)
17969
 
          {
17970
 
            const COND *pushed_cond= tab->table->file->pushed_cond;
17971
 
 
17972
 
            if (thd->variables.engine_condition_pushdown && pushed_cond)
17973
 
            {
17974
 
              extra.append(STRING_WITH_LEN("; Using where with pushed "
17975
 
                                           "condition"));
17976
 
              if (thd->lex->describe & DESCRIBE_EXTENDED)
17977
 
              {
17978
 
                extra.append(STRING_WITH_LEN(": "));
17979
 
                ((COND *)pushed_cond)->print(&extra, QT_ORDINARY);
17980
 
              }
17981
 
            }
17982
 
            else
17983
 
              extra.append(STRING_WITH_LEN("; Using where"));
17984
 
          }
17985
 
        }
17986
 
        if (key_read)
17987
 
        {
17988
 
          if (quick_type == QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX)
17989
 
            extra.append(STRING_WITH_LEN("; Using index for group-by"));
17990
 
          else
17991
 
            extra.append(STRING_WITH_LEN("; Using index"));
17992
 
        }
17993
 
        if (table->reginfo.not_exists_optimize)
17994
 
          extra.append(STRING_WITH_LEN("; Not exists"));
17995
 
          
17996
 
        if (quick_type == QUICK_SELECT_I::QS_TYPE_RANGE &&
17997
 
            !(((QUICK_RANGE_SELECT*)(tab->select->quick))->mrr_flags &
17998
 
             HA_MRR_USE_DEFAULT_IMPL))
17999
 
        {
18000
 
          extra.append(STRING_WITH_LEN("; Using MRR"));
18001
 
        }
18002
 
 
18003
 
        if (table_list->schema_table &&
18004
 
            table_list->schema_table->i_s_requested_object & OPTIMIZE_I_S_TABLE)
18005
 
        {
18006
 
          if (!table_list->table_open_method)
18007
 
            extra.append(STRING_WITH_LEN("; Skip_open_table"));
18008
 
          else if (table_list->table_open_method == OPEN_FRM_ONLY)
18009
 
            extra.append(STRING_WITH_LEN("; Open_frm_only"));
18010
 
          else
18011
 
            extra.append(STRING_WITH_LEN("; Open_full_table"));
18012
 
          if (table_list->has_db_lookup_value &&
18013
 
              table_list->has_table_lookup_value)
18014
 
            extra.append(STRING_WITH_LEN("; Scanned 0 databases"));
18015
 
          else if (table_list->has_db_lookup_value ||
18016
 
                   table_list->has_table_lookup_value)
18017
 
            extra.append(STRING_WITH_LEN("; Scanned 1 database"));
18018
 
          else
18019
 
            extra.append(STRING_WITH_LEN("; Scanned all databases"));
18020
 
        }
18021
 
        if (need_tmp_table)
18022
 
        {
18023
 
          need_tmp_table=0;
18024
 
          extra.append(STRING_WITH_LEN("; Using temporary"));
18025
 
        }
18026
 
        if (need_order)
18027
 
        {
18028
 
          need_order=0;
18029
 
          extra.append(STRING_WITH_LEN("; Using filesort"));
18030
 
        }
18031
 
        if (distinct & test_all_bits(used_tables,thd->used_tables))
18032
 
          extra.append(STRING_WITH_LEN("; Distinct"));
18033
 
 
18034
 
        if (tab->insideout_match_tab)
18035
 
        {
18036
 
          extra.append(STRING_WITH_LEN("; LooseScan"));
18037
 
        }
18038
 
 
18039
 
        if (tab->flush_weedout_table)
18040
 
          extra.append(STRING_WITH_LEN("; Start temporary"));
18041
 
        else if (tab->check_weed_out_table)
18042
 
          extra.append(STRING_WITH_LEN("; End temporary"));
18043
 
        else if (tab->do_firstmatch)
18044
 
        {
18045
 
          extra.append(STRING_WITH_LEN("; FirstMatch("));
18046
 
          TABLE *prev_table=tab->do_firstmatch->table;
18047
 
          if (prev_table->derived_select_number)
18048
 
          {
18049
 
            char namebuf[NAME_LEN];
18050
 
            /* Derived table name generation */
18051
 
            int len= snprintf(namebuf, sizeof(namebuf)-1,
18052
 
                              "<derived%u>",
18053
 
                              prev_table->derived_select_number);
18054
 
            extra.append(namebuf, len);
18055
 
          }
18056
 
          else
18057
 
            extra.append(prev_table->pos_in_table_list->alias);
18058
 
          extra.append(STRING_WITH_LEN(")"));
18059
 
        }
18060
 
 
18061
 
        for (uint part= 0; part < tab->ref.key_parts; part++)
18062
 
        {
18063
 
          if (tab->ref.cond_guards[part])
18064
 
          {
18065
 
            extra.append(STRING_WITH_LEN("; Full scan on NULL key"));
18066
 
            break;
18067
 
          }
18068
 
        }
18069
 
 
18070
 
        if (i > 0 && tab[-1].next_select == sub_select_cache)
18071
 
          extra.append(STRING_WITH_LEN("; Using join buffer"));
18072
 
 
18073
 
        /* Skip initial "; "*/
18074
 
        const char *str= extra.ptr();
18075
 
        uint32_t len= extra.length();
18076
 
        if (len)
18077
 
        {
18078
 
          str += 2;
18079
 
          len -= 2;
18080
 
        }
18081
 
        item_list.push_back(new Item_string(str, len, cs));
18082
 
      }
18083
 
      // For next iteration
18084
 
      used_tables|=table->map;
18085
 
      if (result->send_data(item_list))
18086
 
        join->error= 1;
18087
 
    }
18088
 
  }
18089
 
  for (SELECT_LEX_UNIT *unit= join->select_lex->first_inner_unit();
18090
 
       unit;
18091
 
       unit= unit->next_unit())
18092
 
  {
18093
 
    if (mysql_explain_union(thd, unit, result))
18094
 
      return;
18095
 
  }
18096
 
  return;
18097
 
}
18098
 
 
18099
 
 
18100
 
bool mysql_explain_union(THD *thd, SELECT_LEX_UNIT *unit, select_result *result)
18101
 
{
18102
 
  bool res= 0;
18103
 
  SELECT_LEX *first= unit->first_select();
18104
 
 
18105
 
  for (SELECT_LEX *sl= first;
18106
 
       sl;
18107
 
       sl= sl->next_select())
18108
 
  {
18109
 
    // drop UNCACHEABLE_EXPLAIN, because it is for internal usage only
18110
 
    uint8_t uncacheable= (sl->uncacheable & ~UNCACHEABLE_EXPLAIN);
18111
 
    sl->type= (((&thd->lex->select_lex)==sl)?
18112
 
               (sl->first_inner_unit() || sl->next_select() ? 
18113
 
                "PRIMARY" : "SIMPLE"):
18114
 
               ((sl == first)?
18115
 
                ((sl->linkage == DERIVED_TABLE_TYPE) ?
18116
 
                 "DERIVED":
18117
 
                 ((uncacheable & UNCACHEABLE_DEPENDENT) ?
18118
 
                  "DEPENDENT SUBQUERY":
18119
 
                  (uncacheable?"UNCACHEABLE SUBQUERY":
18120
 
                   "SUBQUERY"))):
18121
 
                ((uncacheable & UNCACHEABLE_DEPENDENT) ?
18122
 
                 "DEPENDENT UNION":
18123
 
                 uncacheable?"UNCACHEABLE UNION":
18124
 
                 "UNION")));
18125
 
    sl->options|= SELECT_DESCRIBE;
18126
 
  }
18127
 
  if (unit->is_union())
18128
 
  {
18129
 
    unit->fake_select_lex->select_number= UINT_MAX; // jost for initialization
18130
 
    unit->fake_select_lex->type= "UNION RESULT";
18131
 
    unit->fake_select_lex->options|= SELECT_DESCRIBE;
18132
 
    if (!(res= unit->prepare(thd, result, SELECT_NO_UNLOCK | SELECT_DESCRIBE)))
18133
 
      res= unit->exec();
18134
 
    res|= unit->cleanup();
18135
 
  }
18136
 
  else
18137
 
  {
18138
 
    thd->lex->current_select= first;
18139
 
    unit->set_limit(unit->global_parameters);
18140
 
    res= mysql_select(thd, &first->ref_pointer_array,
18141
 
                        (TABLE_LIST*) first->table_list.first,
18142
 
                        first->with_wild, first->item_list,
18143
 
                        first->where,
18144
 
                        first->order_list.elements +
18145
 
                        first->group_list.elements,
18146
 
                        (ORDER*) first->order_list.first,
18147
 
                        (ORDER*) first->group_list.first,
18148
 
                        first->having,
18149
 
                        (ORDER*) thd->lex->proc_list.first,
18150
 
                        first->options | thd->options | SELECT_DESCRIBE,
18151
 
                        result, unit, first);
18152
 
  }
18153
 
  return(res || thd->is_error());
18154
 
}
18155
 
 
18156
 
 
18157
 
static void print_table_array(THD *thd, String *str, TABLE_LIST **table, 
18158
 
                              TABLE_LIST **end)
18159
 
{
18160
 
  (*table)->print(thd, str, QT_ORDINARY);
18161
 
 
18162
 
  for (TABLE_LIST **tbl= table + 1; tbl < end; tbl++)
18163
 
  {
18164
 
    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;
18165
6549
    if (curr->outer_join)
18166
6550
    {
18167
6551
      /* MySQL converts right to left joins */
18169
6553
    }
18170
6554
    else if (curr->straight)
18171
6555
      str->append(STRING_WITH_LEN(" straight_join "));
18172
 
    else if (curr->sj_inner_tables)
18173
 
      str->append(STRING_WITH_LEN(" semi join "));
18174
6556
    else
18175
6557
      str->append(STRING_WITH_LEN(" join "));
18176
 
    curr->print(thd, str, QT_ORDINARY);
 
6558
    curr->print(session, str, QT_ORDINARY);
18177
6559
    if (curr->on_expr)
18178
6560
    {
18179
6561
      str->append(STRING_WITH_LEN(" on("));
18183
6565
  }
18184
6566
}
18185
6567
 
18186
 
 
18187
6568
/**
18188
6569
  Print joins from the FROM clause.
18189
 
  @param thd     thread handler
 
6570
  @param session     thread Cursor
18190
6571
  @param str     string where table should be printed
18191
6572
  @param tables  list of tables in join
18192
6573
  @query_type    type of the query is being generated
18193
6574
*/
18194
 
 
18195
 
static void print_join(THD *thd,
18196
 
                       String *str,
18197
 
                       List<TABLE_LIST> *tables,
18198
 
                       enum_query_type query_type __attribute__((unused)))
 
6575
void print_join(Session *session, String *str,
 
6576
                List<TableList> *tables, enum_query_type)
18199
6577
{
18200
6578
  /* List is reversed => we should reverse it before using */
18201
 
  List_iterator_fast<TABLE_LIST> ti(*tables);
18202
 
  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*) *
18203
6581
                                                tables->elements);
18204
6582
  if (table == 0)
18205
6583
    return;  // out of memory
18206
6584
 
18207
 
  for (TABLE_LIST **t= table + (tables->elements - 1); t >= table; t--)
 
6585
  for (TableList **t= table + (tables->elements - 1); t >= table; t--)
18208
6586
    *t= ti++;
18209
 
  
18210
 
  /* 
18211
 
    If the first table is a semi-join nest, swap it with something that is
18212
 
    not a semi-join nest.
18213
 
  */
18214
 
  if ((*table)->sj_inner_tables)
18215
 
  {
18216
 
    TABLE_LIST **end= table + tables->elements;
18217
 
    for (TABLE_LIST **t2= table; t2!=end; t2++)
18218
 
    {
18219
 
      if (!(*t2)->sj_inner_tables)
18220
 
      {
18221
 
        TABLE_LIST *tmp= *t2;
18222
 
        *t2= *table;
18223
 
        *table= tmp;
18224
 
        break;
18225
 
      }
18226
 
    }
18227
 
  }
18228
6587
  assert(tables->elements >= 1);
18229
 
  print_table_array(thd, str, table, table + tables->elements);
18230
 
}
18231
 
 
18232
 
 
18233
 
/**
18234
 
  @brief Print an index hint
18235
 
 
18236
 
  @details Prints out the USE|FORCE|IGNORE index hint.
18237
 
 
18238
 
  @param      thd         the current thread
18239
 
  @param[out] str         appends the index hint here
18240
 
  @param      hint        what the hint is (as string : "USE INDEX"|
18241
 
                          "FORCE INDEX"|"IGNORE INDEX")
18242
 
  @param      hint_length the length of the string in 'hint'
18243
 
  @param      indexes     a list of index names for the hint
18244
 
*/
18245
 
 
18246
 
void 
18247
 
Index_hint::print(THD *thd, String *str)
18248
 
{
18249
 
  switch (type)
18250
 
  {
18251
 
    case INDEX_HINT_IGNORE: str->append(STRING_WITH_LEN("IGNORE INDEX")); break;
18252
 
    case INDEX_HINT_USE:    str->append(STRING_WITH_LEN("USE INDEX")); break;
18253
 
    case INDEX_HINT_FORCE:  str->append(STRING_WITH_LEN("FORCE INDEX")); break;
18254
 
  }
18255
 
  str->append (STRING_WITH_LEN(" ("));
18256
 
  if (key_name.length)
18257
 
  {
18258
 
    if (thd && !my_strnncoll(system_charset_info,
18259
 
                             (const uchar *)key_name.str, key_name.length, 
18260
 
                             (const uchar *)primary_key_name, 
18261
 
                             strlen(primary_key_name)))
18262
 
      str->append(primary_key_name);
18263
 
    else
18264
 
      append_identifier(thd, str, key_name.str, key_name.length);
18265
 
  }
18266
 
  str->append(')');
18267
 
}
18268
 
 
18269
 
 
18270
 
/**
18271
 
  Print table as it should be in join list.
18272
 
 
18273
 
  @param str   string where table should be printed
18274
 
*/
18275
 
 
18276
 
void TABLE_LIST::print(THD *thd, String *str, enum_query_type query_type)
18277
 
{
18278
 
  if (nested_join)
18279
 
  {
18280
 
    str->append('(');
18281
 
    print_join(thd, str, &nested_join->join_list, query_type);
18282
 
    str->append(')');
18283
 
  }
18284
 
  else
18285
 
  {
18286
 
    const char *cmp_name;                         // Name to compare with alias
18287
 
    if (derived)
18288
 
    {
18289
 
      // A derived table
18290
 
      str->append('(');
18291
 
      derived->print(str, query_type);
18292
 
      str->append(')');
18293
 
      cmp_name= "";                               // Force printing of alias
18294
 
    }
18295
 
    else
18296
 
    {
18297
 
      // A normal table
18298
 
      {
18299
 
        append_identifier(thd, str, db, db_length);
18300
 
        str->append('.');
18301
 
      }
18302
 
      if (schema_table)
18303
 
      {
18304
 
        append_identifier(thd, str, schema_table_name,
18305
 
                          strlen(schema_table_name));
18306
 
        cmp_name= schema_table_name;
18307
 
      }
18308
 
      else
18309
 
      {
18310
 
        append_identifier(thd, str, table_name, table_name_length);
18311
 
        cmp_name= table_name;
18312
 
      }
18313
 
    }
18314
 
    if (my_strcasecmp(table_alias_charset, cmp_name, alias))
18315
 
    {
18316
 
      char t_alias_buff[MAX_ALIAS_NAME];
18317
 
      const char *t_alias= alias;
18318
 
 
18319
 
      str->append(' ');
18320
 
      if (lower_case_table_names== 1)
18321
 
      {
18322
 
        if (alias && alias[0])
18323
 
        {
18324
 
          stpcpy(t_alias_buff, alias);
18325
 
          my_casedn_str(files_charset_info, t_alias_buff);
18326
 
          t_alias= t_alias_buff;
18327
 
        }
18328
 
      }
18329
 
 
18330
 
      append_identifier(thd, str, t_alias, strlen(t_alias));
18331
 
    }
18332
 
 
18333
 
    if (index_hints)
18334
 
    {
18335
 
      List_iterator<Index_hint> it(*index_hints);
18336
 
      Index_hint *hint;
18337
 
 
18338
 
      while ((hint= it++))
18339
 
      {
18340
 
        str->append (STRING_WITH_LEN(" "));
18341
 
        hint->print (thd, str);
18342
 
      }
18343
 
    }
18344
 
  }
18345
 
}
18346
 
 
18347
 
 
18348
 
void st_select_lex::print(THD *thd, String *str, enum_query_type query_type)
18349
 
{
18350
 
  /* QQ: thd may not be set for sub queries, but this should be fixed */
18351
 
  if (!thd)
18352
 
    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;
18353
6596
 
18354
6597
  str->append(STRING_WITH_LEN("select "));
18355
6598
 
18356
6599
  /* First add options */
18357
6600
  if (options & SELECT_STRAIGHT_JOIN)
18358
6601
    str->append(STRING_WITH_LEN("straight_join "));
18359
 
  if ((thd->lex->lock_option == TL_READ_HIGH_PRIORITY) &&
18360
 
      (this == &thd->lex->select_lex))
18361
 
    str->append(STRING_WITH_LEN("high_priority "));
18362
6602
  if (options & SELECT_DISTINCT)
18363
6603
    str->append(STRING_WITH_LEN("distinct "));
18364
6604
  if (options & SELECT_SMALL_RESULT)
18391
6631
  {
18392
6632
    str->append(STRING_WITH_LEN(" from "));
18393
6633
    /* go through join tree */
18394
 
    print_join(thd, str, &top_join_list, query_type);
 
6634
    print_join(session, str, &top_join_list, query_type);
18395
6635
  }
18396
6636
  else if (where)
18397
6637
  {
18398
6638
    /*
18399
 
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as 
 
6639
      "SELECT 1 FROM DUAL WHERE 2" should not be printed as
18400
6640
      "SELECT 1 WHERE 2": the 1st syntax is valid, but the 2nd is not.
18401
6641
    */
18402
6642
    str->append(STRING_WITH_LEN(" from DUAL "));
18419
6659
  if (group_list.elements)
18420
6660
  {
18421
6661
    str->append(STRING_WITH_LEN(" group by "));
18422
 
    print_order(str, (ORDER *) group_list.first, query_type);
 
6662
    print_order(str, (order_st *) group_list.first, query_type);
18423
6663
    switch (olap)
18424
6664
    {
18425
6665
      case CUBE_TYPE:
18450
6690
  if (order_list.elements)
18451
6691
  {
18452
6692
    str->append(STRING_WITH_LEN(" order by "));
18453
 
    print_order(str, (ORDER *) order_list.first, query_type);
 
6693
    print_order(str, (order_st *) order_list.first, query_type);
18454
6694
  }
18455
6695
 
18456
6696
  // limit
18457
 
  print_limit(thd, str, query_type);
 
6697
  print_limit(session, str, query_type);
18458
6698
 
18459
6699
  // PROCEDURE unsupported here
18460
6700
}
18461
6701
 
18462
 
 
18463
 
/**
18464
 
  change select_result object of JOIN.
18465
 
 
18466
 
  @param res            new select_result object
18467
 
 
18468
 
  @retval
18469
 
    false   OK
18470
 
  @retval
18471
 
    true    error
18472
 
*/
18473
 
 
18474
 
bool JOIN::change_result(select_result *res)
18475
 
{
18476
 
  result= res;
18477
 
  if (result->prepare(fields_list, select_lex->master_unit()) ||
18478
 
                     result->prepare2())
18479
 
  {
18480
 
    return(true);
18481
 
  }
18482
 
  return(false);
18483
 
}
18484
 
 
18485
6702
/**
18486
6703
  @} (end of group Query_Optimizer)
18487
6704
*/