~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/sql_select.cc

  • Committer: Brian Aker
  • Date: 2009-06-01 04:09:48 UTC
  • mfrom: (1039.2.9 working)
  • Revision ID: brian@gaz-20090601040948-ke10l7ndpzd6oupw
Merge Jay.

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
25
#include "drizzled/server_includes.h"
27
 
#include "drizzled/sql_select.h"
 
26
#include "drizzled/sql_select.h" /* include join.h */
28
27
#include "drizzled/sj_tmp_table.h"
29
28
#include "drizzled/table_map_iterator.h"
30
29
 
31
 
#include "mysys/my_bit.h"
32
30
#include "drizzled/error.h"
33
31
#include "drizzled/gettext.h"
34
32
#include "drizzled/util/test.h"
61
59
                              "index_merge"
62
60
};
63
61
 
64
 
struct st_sargable_param;
65
 
 
66
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array);
67
 
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds,
68
 
                                 DYNAMIC_ARRAY *keyuse);
69
 
static bool update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,
70
 
                                JOIN_TAB *join_tab,
71
 
                                uint32_t tables, COND *conds,
72
 
                                COND_EQUAL *cond_equal,
73
 
                                table_map table_map, Select_Lex *select_lex,
74
 
                                st_sargable_param **sargables);
75
62
static int sort_keyuse(KEYUSE *a,KEYUSE *b);
76
 
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
77
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
78
 
                               table_map used_tables);
79
 
static bool choose_plan(JOIN *join,table_map join_tables);
80
 
 
81
 
static void best_access_path(JOIN *join, JOIN_TAB *s, Session *session,
82
 
                             table_map remaining_tables, uint32_t idx,
83
 
                             double record_count, double read_time);
84
 
static void optimize_straight_join(JOIN *join, table_map join_tables);
85
 
static bool greedy_search(JOIN *join, table_map remaining_tables,
86
 
                             uint32_t depth, uint32_t prune_level);
87
 
static bool best_extension_by_limited_search(JOIN *join,
88
 
                                             table_map remaining_tables,
89
 
                                             uint32_t idx, double record_count,
90
 
                                             double read_time, uint32_t depth,
91
 
                                             uint32_t prune_level);
92
 
static uint32_t determine_search_depth(JOIN* join);
93
 
extern "C" int join_tab_cmp(const void* ptr1, const void* ptr2);
94
 
extern "C" int join_tab_cmp_straight(const void* ptr1, const void* ptr2);
95
 
/*
96
 
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
97
 
  tested and approved.
98
 
*/
99
 
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index,
100
 
                      double record_count,double read_time);
101
 
static uint32_t cache_record_length(JOIN *join,uint32_t index);
102
 
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
103
 
static bool get_best_combination(JOIN *join);
104
 
static store_key *get_store_key(Session *session,
105
 
                                KEYUSE *keyuse, table_map used_tables,
106
 
                                KEY_PART_INFO *key_part, unsigned char *key_buff,
107
 
                                uint32_t maybe_null);
108
 
static bool make_simple_join(JOIN *join,Table *tmp_table);
109
 
static void make_outerjoin_info(JOIN *join);
110
 
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
111
 
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
112
 
static bool only_eq_ref_tables(JOIN *join, order_st *order, table_map tables);
113
 
static void update_depend_map(JOIN *join);
114
 
static void update_depend_map(JOIN *join, order_st *order);
115
 
static order_st *remove_constants(JOIN *join,order_st *first_order,COND *cond,
116
 
                                  bool change_list, bool *simple_order);
117
 
static int return_zero_rows(JOIN *join, select_result *res,TableList *tables,
118
 
                            List<Item> &fields, bool send_row,
119
 
                            uint64_t select_options, const char *info,
120
 
                            Item *having);
121
63
static COND *build_equal_items(Session *session, COND *cond,
122
64
                               COND_EQUAL *inherited,
123
65
                               List<TableList> *join_list,
124
66
                               COND_EQUAL **cond_equal_ref);
125
 
static COND* substitute_for_best_equal_field(COND *cond,
126
 
                                             COND_EQUAL *cond_equal,
127
 
                                             void *table_join_idx);
128
 
static COND *simplify_joins(JOIN *join, List<TableList> *join_list,
129
 
                            COND *conds, bool top, bool in_sj);
130
 
static bool check_interleaving_with_nj(JOIN_TAB *last, JOIN_TAB *next);
131
 
static void restore_prev_nj_state(JOIN_TAB *last);
132
 
static void reset_nj_counters(List<TableList> *join_list);
133
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
134
 
                                          uint32_t first_unused);
135
 
 
136
 
static
137
 
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab);
138
 
static void restore_prev_sj_state(const table_map remaining_tables,
139
 
                                  const JOIN_TAB *tab);
140
 
 
141
 
static COND *optimize_cond(JOIN *join, COND *conds,
142
 
                           List<TableList> *join_list,
143
 
                           Item::cond_result *cond_value);
144
 
static bool const_expression_in_where(COND *conds,Item *item, Item **comp_item);
145
 
static int do_select(JOIN *join,List<Item> *fields,Table *tmp_table);
146
 
 
147
 
static enum_nested_loop_state
148
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
149
 
                     int error);
150
 
static enum_nested_loop_state
151
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab);
152
 
static enum_nested_loop_state
153
 
flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last);
154
 
static enum_nested_loop_state
155
 
end_send(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
156
 
static enum_nested_loop_state
157
 
end_write(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
158
 
static enum_nested_loop_state
159
 
end_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
160
 
static enum_nested_loop_state
161
 
end_unique_update(JOIN *join, JOIN_TAB *join_tab, bool end_of_records);
162
 
 
163
 
static int join_read_const_table(JOIN_TAB *tab, POSITION *pos);
164
 
static int join_read_system(JOIN_TAB *tab);
165
 
static int join_read_const(JOIN_TAB *tab);
166
 
static int join_read_key(JOIN_TAB *tab);
167
 
static int join_read_always_key(JOIN_TAB *tab);
168
 
static int join_read_last_key(JOIN_TAB *tab);
169
 
static int join_no_more_records(READ_RECORD *info);
170
 
static int join_read_next(READ_RECORD *info);
171
 
static int join_read_next_different(READ_RECORD *info);
172
 
static int join_init_quick_read_record(JOIN_TAB *tab);
173
 
static int test_if_quick_select(JOIN_TAB *tab);
174
 
static int join_init_read_record(JOIN_TAB *tab);
175
 
static int join_read_first(JOIN_TAB *tab);
176
 
static int join_read_next_same(READ_RECORD *info);
177
 
static int join_read_next_same_diff(READ_RECORD *info);
178
 
static int join_read_last(JOIN_TAB *tab);
179
 
static int join_read_prev_same(READ_RECORD *info);
180
 
static int join_read_prev(READ_RECORD *info);
181
 
int join_read_always_key_or_null(JOIN_TAB *tab);
182
 
int join_read_next_same_or_null(READ_RECORD *info);
183
 
static COND *make_cond_for_table(COND *cond,table_map table,
184
 
                                 table_map used_table,
185
 
                                 bool exclude_expensive_cond);
 
67
 
186
68
static Item* part_of_refkey(Table *form,Field *field);
187
 
static bool test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,
188
 
                                    ha_rows select_limit, bool no_changes,
189
 
                                    const key_map *map);
190
 
static bool list_contains_unique_index(Table *table,
191
 
                          bool (*find_func) (Field *, void *), void *data);
192
 
static bool find_field_in_item_list (Field *field, void *data);
193
 
static bool find_field_in_order_list (Field *field, void *data);
194
 
static int create_sort_index(Session *session, JOIN *join, order_st *order,
195
 
                             ha_rows filesort_limit, ha_rows select_limit,
196
 
                             bool is_order_by);
197
 
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields,
198
 
                             Item *having);
199
 
static int remove_dup_with_compare(Session *session, Table *entry, Field **field,
200
 
                                   uint32_t offset, Item *having);
201
 
static int remove_dup_with_hash_index(Session *session,Table *table,
202
 
                                      uint32_t field_count, Field **first_field,
203
 
                                      uint32_t key_length, Item *having);
204
 
static int join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count);
205
 
static uint32_t used_blob_length(CACHE_FIELD **ptr);
206
 
static bool store_record_in_cache(JOIN_CACHE *cache);
207
 
static void reset_cache_read(JOIN_CACHE *cache);
208
 
static void reset_cache_write(JOIN_CACHE *cache);
209
 
static void read_cached_record(JOIN_TAB *tab);
210
69
static bool cmp_buffer_with_ref(JOIN_TAB *tab);
211
 
static order_st *create_distinct_group(Session *session, Item **ref_pointer_array,
212
 
                                    order_st *order, List<Item> &fields,
213
 
                                    List<Item> &all_fields,
214
 
                                    bool *all_order_by_fields_used);
215
 
static bool test_if_subpart(order_st *a,order_st *b);
216
 
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
217
 
static void calc_group_buffer(JOIN *join,order_st *group);
218
 
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
219
 
static bool alloc_group_fields(JOIN *join,order_st *group);
220
 
// Create list for using with tempory table
221
 
static bool change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
222
 
                                     List<Item> &new_list1,
223
 
                                     List<Item> &new_list2,
224
 
                                     uint32_t elements, List<Item> &items);
225
 
// Create list for using with tempory table
226
 
static bool change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
227
 
                                      List<Item> &new_list1,
228
 
                                      List<Item> &new_list2,
229
 
                                      uint32_t elements, List<Item> &items);
230
 
static void init_tmptable_sum_functions(Item_sum **func);
231
 
static void update_tmptable_sum_func(Item_sum **func,Table *tmp_table);
232
 
static void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end);
233
 
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
234
 
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr);
235
 
static bool init_sum_functions(Item_sum **func, Item_sum **end);
236
 
static bool update_sum_func(Item_sum **func);
237
 
void select_describe(JOIN *join, bool need_tmp_table,bool need_order,
238
 
                            bool distinct, const char *message=NULL);
239
 
static Item *remove_additional_cond(Item* conds);
240
 
static void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab);
241
 
static bool test_if_ref(Item_field *left_item,Item *right_item);
242
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
243
 
                                       Item *new_cond, bool fix_fields);
244
 
 
 
70
static void change_cond_ref_to_const(Session *session,
 
71
                                     I_List<COND_CMP> *save_list,
 
72
                                     Item *and_father,
 
73
                                     Item *cond,
 
74
                                     Item *field,
 
75
                                     Item *value);
 
76
static bool copy_blobs(Field **ptr);
245
77
static bool eval_const_cond(COND *cond)
246
78
{
247
79
    return ((Item_func*) cond)->val_int() ? true : false;
248
80
}
249
81
 
250
 
 
251
82
/*
252
83
  This is used to mark equalities that were made from i-th IN-equality.
253
84
  We limit semi-join InsideOut optimization to handling max 64 inequalities,
256
87
const char *subq_sj_cond_name=
257
88
  "0123456789ABCDEF0123456789abcdef0123456789ABCDEF0123456789abcdef-sj-cond";
258
89
 
259
 
static bool bitmap_covers(const table_map x, const table_map y)
 
90
static bool copy_blobs(Field **ptr)
260
91
{
261
 
  return !test(y & ~x);
 
92
  for (; *ptr ; ptr++)
 
93
  {
 
94
    if ((*ptr)->flags & BLOB_FLAG)
 
95
      if (((Field_blob *) (*ptr))->copy())
 
96
        return 1;                               // Error
 
97
  }
 
98
  return 0;
262
99
}
263
100
 
264
101
/**
265
102
  This handles SELECT with and without UNION.
266
103
*/
267
 
 
268
104
bool handle_select(Session *session, LEX *lex, select_result *result,
269
105
                   uint64_t setup_tables_done_option)
270
106
{
271
107
  bool res;
272
 
  register Select_Lex *select_lex = &lex->select_lex;
 
108
  register Select_Lex *select_lex= &lex->select_lex;
273
109
  DRIZZLE_SELECT_START();
274
110
 
275
111
  if (select_lex->master_unit()->is_union() ||
307
143
  return(res);
308
144
}
309
145
 
310
 
 
311
146
/*
312
147
  Fix fields referenced from inner selects.
313
148
 
348
183
    true  an error occured
349
184
    false ok
350
185
*/
351
 
 
352
 
bool
353
 
fix_inner_refs(Session *session, List<Item> &all_fields, Select_Lex *select,
354
 
                 Item **ref_pointer_array)
 
186
bool fix_inner_refs(Session *session, 
 
187
                    List<Item> &all_fields, 
 
188
                    Select_Lex *select, 
 
189
                    Item **ref_pointer_array)
355
190
{
356
191
  Item_outer_ref *ref;
357
192
  bool res= false;
418
253
  return res;
419
254
}
420
255
 
421
 
/**
422
 
  Function to setup clauses without sum functions.
423
 
*/
424
 
inline int setup_without_group(Session *session, Item **ref_pointer_array,
425
 
                               TableList *tables,
426
 
                               TableList *,
427
 
                               List<Item> &fields,
428
 
                               List<Item> &all_fields,
429
 
                               COND **conds,
430
 
                               order_st *order,
431
 
                               order_st *group, bool *hidden_group_fields)
432
 
{
433
 
  int res;
434
 
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
435
 
 
436
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
437
 
  res= setup_conds(session, tables, conds);
438
 
 
439
 
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
440
 
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
441
 
                          order);
442
 
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
443
 
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
444
 
                          group, hidden_group_fields);
445
 
  session->lex->allow_sum_func= save_allow_sum_func;
446
 
  return(res);
447
 
}
448
 
 
449
256
/*****************************************************************************
450
257
  Check fields, find best join, do the select and output fields.
451
258
  mysql_select assumes that all tables are already opened
452
259
*****************************************************************************/
453
260
 
454
 
/**
455
 
  Prepare of whole select (including sub queries in future).
456
 
 
457
 
  @todo
458
 
    Add check of calculation of GROUP functions and fields:
459
 
    SELECT COUNT(*)+table.col1 from table1;
460
 
 
461
 
  @retval
462
 
    -1   on error
463
 
  @retval
464
 
    0   on success
465
 
*/
466
 
int
467
 
JOIN::prepare(Item ***rref_pointer_array,
468
 
              TableList *tables_init,
469
 
              uint32_t wild_num, COND *conds_init, uint32_t og_num,
470
 
              order_st *order_init, order_st *group_init,
471
 
              Item *having_init,
472
 
              Select_Lex *select_lex_arg,
473
 
              Select_Lex_Unit *unit_arg)
474
 
{
475
 
  // to prevent double initialization on EXPLAIN
476
 
  if (optimized)
477
 
    return(0);
478
 
 
479
 
  conds= conds_init;
480
 
  order= order_init;
481
 
  group_list= group_init;
482
 
  having= having_init;
483
 
  tables_list= tables_init;
484
 
  select_lex= select_lex_arg;
485
 
  select_lex->join= this;
486
 
  join_list= &select_lex->top_join_list;
487
 
  union_part= unit_arg->is_union();
488
 
 
489
 
  session->lex->current_select->is_item_list_lookup= 1;
490
 
  /*
491
 
    If we have already executed SELECT, then it have not sense to prevent
492
 
    its table from update (see unique_table())
493
 
  */
494
 
  if (session->derived_tables_processing)
495
 
    select_lex->exclude_from_table_unique_test= true;
496
 
 
497
 
  /* Check that all tables, fields, conds and order are ok */
498
 
 
499
 
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
500
 
      setup_tables_and_check_access(session, &select_lex->context, join_list,
501
 
                                    tables_list, &select_lex->leaf_tables,
502
 
                                    false))
503
 
      return(-1);
504
 
 
505
 
  TableList *table_ptr;
506
 
  for (table_ptr= select_lex->leaf_tables;
507
 
       table_ptr;
508
 
       table_ptr= table_ptr->next_leaf)
509
 
    tables++;
510
 
 
511
 
  if (setup_wild(session, fields_list, &all_fields, wild_num) ||
512
 
      select_lex->setup_ref_array(session, og_num) ||
513
 
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
514
 
                   &all_fields, 1) ||
515
 
      setup_without_group(session, (*rref_pointer_array), tables_list,
516
 
                          select_lex->leaf_tables, fields_list,
517
 
                          all_fields, &conds, order, group_list,
518
 
                          &hidden_group_fields))
519
 
    return(-1);                         /* purecov: inspected */
520
 
 
521
 
  ref_pointer_array= *rref_pointer_array;
522
 
 
523
 
  if (having)
524
 
  {
525
 
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
526
 
    session->where="having clause";
527
 
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
528
 
    select_lex->having_fix_field= 1;
529
 
    bool having_fix_rc= (!having->fixed &&
530
 
                         (having->fix_fields(session, &having) ||
531
 
                          having->check_cols(1)));
532
 
    select_lex->having_fix_field= 0;
533
 
    if (having_fix_rc || session->is_error())
534
 
      return(-1);                               /* purecov: inspected */
535
 
    session->lex->allow_sum_func= save_allow_sum_func;
536
 
  }
537
 
 
538
 
  {
539
 
    Item_subselect *subselect;
540
 
    Item_in_subselect *in_subs= NULL;
541
 
    /*
542
 
      Are we in a subquery predicate?
543
 
      TODO: the block below will be executed for every PS execution without need.
544
 
    */
545
 
    if ((subselect= select_lex->master_unit()->item))
546
 
    {
547
 
      bool do_semijoin= !test(session->variables.optimizer_switch &
548
 
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
549
 
      if (subselect->substype() == Item_subselect::IN_SUBS)
550
 
        in_subs= (Item_in_subselect*)subselect;
551
 
 
552
 
      /*
553
 
        Check if we're in subquery that is a candidate for flattening into a
554
 
        semi-join (which is done done in flatten_subqueries()). The
555
 
        requirements are:
556
 
          1. Subquery predicate is an IN/=ANY subq predicate
557
 
          2. Subquery is a single SELECT (not a UNION)
558
 
          3. Subquery does not have GROUP BY or order_st BY
559
 
          4. Subquery does not use aggregate functions or HAVING
560
 
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
561
 
          6. No execution method was already chosen (by a prepared statement).
562
 
 
563
 
          (*). We are not in a subquery of a single table UPDATE/DELETE that
564
 
               doesn't have a JOIN (TODO: We should handle this at some
565
 
               point by switching to multi-table UPDATE/DELETE)
566
 
 
567
 
          (**). We're not in a confluent table-less subquery, like
568
 
                "SELECT 1".
569
 
      */
570
 
      if (in_subs &&                                                    // 1
571
 
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
572
 
          !select_lex->group_list.elements && !order &&                 // 3
573
 
          !having && !select_lex->with_sum_func &&                      // 4
574
 
          session->session_marker &&                                            // 5
575
 
          select_lex->outer_select()->join &&                           // (*)
576
 
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
577
 
          do_semijoin &&
578
 
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
579
 
      {
580
 
        {
581
 
          if (!in_subs->left_expr->fixed &&
582
 
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
583
 
          {
584
 
            return(-1);
585
 
          }
586
 
          /*
587
 
            Check that the right part of the subselect contains no more than one
588
 
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
589
 
          */
590
 
          if (subselect->substype() == Item_subselect::IN_SUBS &&
591
 
             (select_lex->item_list.elements !=
592
 
              ((Item_in_subselect*)subselect)->left_expr->cols()))
593
 
          {
594
 
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
595
 
            return(-1);
596
 
          }
597
 
        }
598
 
 
599
 
        /* Register the subquery for further processing */
600
 
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
601
 
        in_subs->expr_join_nest= (TableList*)session->session_marker;
602
 
      }
603
 
      else
604
 
      {
605
 
        bool do_materialize= !test(session->variables.optimizer_switch &
606
 
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
607
 
        /*
608
 
          Check if the subquery predicate can be executed via materialization.
609
 
          The required conditions are:
610
 
          1. Subquery predicate is an IN/=ANY subq predicate
611
 
          2. Subquery is a single SELECT (not a UNION)
612
 
          3. Subquery is not a table-less query. In this case there is no
613
 
             point in materializing.
614
 
          4. Subquery predicate is a top-level predicate
615
 
             (this implies it is not negated)
616
 
             TODO: this is a limitation that should be lifeted once we
617
 
             implement correct NULL semantics (WL#3830)
618
 
          5. Subquery is non-correlated
619
 
             TODO:
620
 
             This is an overly restrictive condition. It can be extended to:
621
 
             (Subquery is non-correlated ||
622
 
              Subquery is correlated to any query outer to IN predicate ||
623
 
              (Subquery is correlated to the immediate outer query &&
624
 
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
625
 
               aggregate functions) && subquery predicate is not under "NOT IN"))
626
 
          6. No execution method was already chosen (by a prepared statement).
627
 
 
628
 
          (*) The subquery must be part of a SELECT statement. The current
629
 
               condition also excludes multi-table update statements.
630
 
 
631
 
          We have to determine whether we will perform subquery materialization
632
 
          before calling the IN=>EXISTS transformation, so that we know whether to
633
 
          perform the whole transformation or only that part of it which wraps
634
 
          Item_in_subselect in an Item_in_optimizer.
635
 
        */
636
 
        if (do_materialize &&
637
 
            in_subs  &&                                                   // 1
638
 
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
639
 
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
640
 
            session->lex->sql_command == SQLCOM_SELECT)                       // *
641
 
        {
642
 
          if (in_subs->is_top_level_item() &&                             // 4
643
 
              !in_subs->is_correlated &&                                  // 5
644
 
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
645
 
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
646
 
        }
647
 
 
648
 
        Item_subselect::trans_res trans_res;
649
 
        if ((trans_res= subselect->select_transformer(this)) !=
650
 
            Item_subselect::RES_OK)
651
 
        {
652
 
          return((trans_res == Item_subselect::RES_ERROR));
653
 
        }
654
 
      }
655
 
    }
656
 
  }
657
 
 
658
 
  if (order)
659
 
  {
660
 
    order_st *ord;
661
 
    for (ord= order; ord; ord= ord->next)
662
 
    {
663
 
      Item *item= *ord->item;
664
 
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
665
 
        item->split_sum_func(session, ref_pointer_array, all_fields);
666
 
    }
667
 
  }
668
 
 
669
 
  if (having && having->with_sum_func)
670
 
    having->split_sum_func(session, ref_pointer_array, all_fields,
671
 
                           &having, true);
672
 
  if (select_lex->inner_sum_func_list)
673
 
  {
674
 
    Item_sum *end=select_lex->inner_sum_func_list;
675
 
    Item_sum *item_sum= end;
676
 
    do
677
 
    {
678
 
      item_sum= item_sum->next;
679
 
      item_sum->split_sum_func(session, ref_pointer_array,
680
 
                               all_fields, item_sum->ref_by, false);
681
 
    } while (item_sum != end);
682
 
  }
683
 
 
684
 
  if (select_lex->inner_refs_list.elements &&
685
 
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
686
 
    return(-1);
687
 
 
688
 
  /*
689
 
    Check if there are references to un-aggregated columns when computing
690
 
    aggregate functions with implicit grouping (there is no GROUP BY).
691
 
 
692
 
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
693
 
  */
694
 
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
695
 
  {
696
 
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
697
 
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
698
 
    return(-1);
699
 
  }
700
 
  {
701
 
    /* Caclulate the number of groups */
702
 
    send_group_parts= 0;
703
 
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
704
 
      send_group_parts++;
705
 
  }
706
 
 
707
 
  if (error)
708
 
    goto err;                                   /* purecov: inspected */
709
 
 
710
 
  if (result && result->prepare(fields_list, unit_arg))
711
 
    goto err;                                   /* purecov: inspected */
712
 
 
713
 
  /* Init join struct */
714
 
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
715
 
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
716
 
  this->group= group_list != 0;
717
 
  unit= unit_arg;
718
 
 
719
 
#ifdef RESTRICTED_GROUP
720
 
  if (sum_func_count && !group_list && (func_count || field_count))
721
 
  {
722
 
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
723
 
    goto err;
724
 
  }
725
 
#endif
726
 
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
727
 
    goto err;
728
 
  if (alloc_func_list())
729
 
    goto err;
730
 
 
731
 
  return(0); // All OK
732
 
 
733
 
err:
734
 
  return(-1);                           /* purecov: inspected */
735
 
}
736
 
 
737
 
 
738
 
/*
739
 
  Remove the predicates pushed down into the subquery
740
 
 
741
 
  SYNOPSIS
742
 
    JOIN::remove_subq_pushed_predicates()
743
 
      where   IN  Must be NULL
744
 
              OUT The remaining WHERE condition, or NULL
745
 
 
746
 
  DESCRIPTION
747
 
    Given that this join will be executed using (unique|index)_subquery,
748
 
    without "checking NULL", remove the predicates that were pushed down
749
 
    into the subquery.
750
 
 
751
 
    If the subquery compares scalar values, we can remove the condition that
752
 
    was wrapped into trig_cond (it will be checked when needed by the subquery
753
 
    engine)
754
 
 
755
 
    If the subquery compares row values, we need to keep the wrapped
756
 
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
757
 
    and non-NULL values, we'll do a full table scan and will rely on the
758
 
    equalities corresponding to non-NULL parts of left tuple to filter out
759
 
    non-matching records.
760
 
 
761
 
    TODO: We can remove the equalities that will be guaranteed to be true by the
762
 
    fact that subquery engine will be using index lookup. This must be done only
763
 
    for cases where there are no conversion errors of significance, e.g. 257
764
 
    that is searched in a byte. But this requires homogenization of the return
765
 
    codes of all Field*::store() methods.
766
 
*/
767
 
 
768
 
void JOIN::remove_subq_pushed_predicates(Item **where)
769
 
{
770
 
  if (conds->type() == Item::FUNC_ITEM &&
771
 
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
772
 
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
773
 
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
774
 
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
775
 
                   ((Item_func *)conds)->arguments()[0]))
776
 
  {
777
 
    *where= 0;
778
 
    return;
779
 
  }
780
 
}
781
 
 
782
 
 
783
261
/*
784
262
  Index lookup-based subquery: save some flags for EXPLAIN output
785
263
 
798
276
      "Full scan on NULL key" (TAB_INFO_FULL_SCAN_ON_NULL)
799
277
    and set appropriate flags in join_tab->packed_info.
800
278
*/
801
 
 
802
 
static void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
 
279
void save_index_subquery_explain_info(JOIN_TAB *join_tab, Item* where)
803
280
{
804
281
  join_tab->packed_info= TAB_INFO_HAVE_VALUE;
805
282
  if (join_tab->table->covering_keys.test(join_tab->ref.key))
816
293
  }
817
294
}
818
295
 
819
 
 
820
 
 
821
 
 
822
 
/*
823
 
  Check if the table's rowid is included in the temptable
824
 
 
825
 
  SYNOPSIS
826
 
    sj_table_is_included()
827
 
      join      The join
828
 
      join_tab  The table to be checked
829
 
 
830
 
  DESCRIPTION
831
 
    SemiJoinDuplicateElimination: check the table's rowid should be included
832
 
    in the temptable. This is so if
833
 
 
834
 
    1. The table is not embedded within some semi-join nest
835
 
    2. The has been pulled out of a semi-join nest, or
836
 
 
837
 
    3. The table is functionally dependent on some previous table
838
 
 
839
 
    [4. This is also true for constant tables that can't be
840
 
        NULL-complemented but this function is not called for such tables]
841
 
 
842
 
  RETURN
843
 
    true  - Include table's rowid
844
 
    false - Don't
845
 
*/
846
 
 
847
 
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
848
 
{
849
 
  if (join_tab->emb_sj_nest)
850
 
    return false;
851
 
 
852
 
  /* Check if this table is functionally dependent on the tables that
853
 
     are within the same outer join nest
854
 
  */
855
 
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
856
 
  if (join_tab->type == JT_EQ_REF)
857
 
  {
858
 
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
859
 
    uint32_t idx;
860
 
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
861
 
    {
862
 
      JOIN_TAB *ref_tab= join->join_tab + idx;
863
 
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
864
 
        return true;
865
 
    }
866
 
    /* Ok, functionally dependent */
867
 
    return false;
868
 
  }
869
 
  /* Not functionally dependent => need to include*/
870
 
  return true;
871
 
}
872
 
 
873
 
 
874
 
/*
875
 
  Setup the strategies to eliminate semi-join duplicates.
876
 
 
877
 
  SYNOPSIS
878
 
    setup_semijoin_dups_elimination()
879
 
      join           Join to process
880
 
      options        Join options (needed to see if join buffering will be
881
 
                     used or not)
882
 
      no_jbuf_after  Another bit of information re where join buffering will
883
 
                     be used.
884
 
 
885
 
  DESCRIPTION
886
 
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
887
 
    strategies:
888
 
 
889
 
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
890
 
                         of row combinations)
891
 
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
892
 
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
893
 
                  together and picking the 1st one)
894
 
 
895
 
    The join order has "duplicate-generating ranges", and every range is
896
 
    served by one strategy or a combination of FirstMatch with with some
897
 
    other strategy.
898
 
 
899
 
    "Duplicate-generating range" is defined as a range within the join order
900
 
    that contains all of the inner tables of a semi-join. All ranges must be
901
 
    disjoint, if tables of several semi-joins are interleaved, then the ranges
902
 
    are joined together, which is equivalent to converting
903
 
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
904
 
    to
905
 
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
906
 
    .
907
 
 
908
 
    Applicability conditions are as follows:
909
 
 
910
 
    DuplicateWeedout strategy
911
 
    ~~~~~~~~~~~~~~~~~~~~~~~~~
912
 
 
913
 
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
914
 
      +------+  +=========================+  +---+
915
 
        (1)                 (2)               (3)
916
 
 
917
 
       (1) - Prefix of OuterTables (those that participate in
918
 
             IN-equality and/or are correlated with subquery) and outer
919
 
             Noncorrelated Tables.
920
 
       (2) - The handled range. The range starts with the first sj-inner
921
 
             table, and covers all sj-inner and outer tables
922
 
             Within the range,  Inner, Outer, outer Noncorrelated tables
923
 
             may follow in any order.
924
 
       (3) - The suffix of outer Noncorrelated tables.
925
 
 
926
 
    FirstMatch strategy
927
 
    ~~~~~~~~~~~~~~~~~~~
928
 
 
929
 
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
930
 
      +------+  +==================+  +---+
931
 
        (1)             (2)          (3)
932
 
 
933
 
      (1) - Prefix of outer and non-correlated tables
934
 
      (2) - The handled range, which may contain only inner and
935
 
            non-correlated tables.
936
 
      (3) - The suffix of outer Noncorrelated tables.
937
 
 
938
 
    InsideOut strategy
939
 
    ~~~~~~~~~~~~~~~~~~
940
 
 
941
 
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
942
 
     +--------+   +===========+ +=============+   +------+
943
 
        (1)           (2)          (3)              (4)
944
 
 
945
 
      (1) - Prefix that may contain any outer tables. The prefix must contain
946
 
            all the non-trivially correlated outer tables. (non-trivially means
947
 
            that the correlation is not just through the IN-equality).
948
 
 
949
 
      (2) - Inner table for which the InsideOut scan is performed.
950
 
 
951
 
      (3) - The remainder of the duplicate-generating range. It is served by
952
 
            application of FirstMatch strategy, with the exception that
953
 
            outer IN-correlated tables are considered to be non-correlated.
954
 
 
955
 
      (4) - THe suffix of outer and outer non-correlated tables.
956
 
 
957
 
    If several strategies are applicable, their relative priorities are:
958
 
      1. InsideOut
959
 
      2. FirstMatch
960
 
      3. DuplicateWeedout
961
 
 
962
 
    This function walks over the join order and sets up the strategies by
963
 
    setting appropriate members in join_tab structures.
964
 
 
965
 
  RETURN
966
 
    false  OK
967
 
    true   Out of memory error
968
 
*/
969
 
 
970
 
static
971
 
int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
972
 
{
973
 
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
974
 
  struct {
975
 
    /*
976
 
      0 - invalid (EOF marker),
977
 
      1 - InsideOut,
978
 
      2 - Temptable (maybe confluent),
979
 
      3 - Temptable with join buffering
980
 
    */
981
 
    uint32_t strategy;
982
 
    uint32_t start_idx; /* Left range bound */
983
 
    uint32_t end_idx;   /* Right range bound */
984
 
    /*
985
 
      For Temptable strategy: Bitmap of all outer and correlated tables from
986
 
      all involved join nests.
987
 
    */
988
 
    table_map outer_tables;
989
 
  } dups_ranges [MAX_TABLES];
990
 
 
991
 
  TableList *emb_insideout_nest= NULL;
992
 
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
993
 
                               tables) whose ranges we're in */
994
 
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
995
 
  table_map range_start_map= 0; /* table_map at current range start */
996
 
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
997
 
  int cur_range= 0;
998
 
  uint32_t i;
999
 
 
1000
 
  /*
1001
 
    First pass: locate the duplicate-generating ranges and pick the strategies.
1002
 
  */
1003
 
  for (i=join->const_tables ; i < join->tables ; i++)
1004
 
  {
1005
 
    JOIN_TAB *tab=join->join_tab+i;
1006
 
    Table *table=tab->table;
1007
 
    cur_map |= table->map;
1008
 
 
1009
 
    if (tab->emb_sj_nest) // Encountered an sj-inner table
1010
 
    {
1011
 
      if (!emb_sj_map)
1012
 
      {
1013
 
        dups_ranges[cur_range].start_idx= i;
1014
 
        range_start_map= cur_map & ~table->map;
1015
 
        /*
1016
 
          Remember if this is a possible start of range that is covered by
1017
 
          the InsideOut strategy (the reason that it is not covered could
1018
 
          be that it overlaps with anther semi-join's range. we don't
1019
 
          support InsideOut for joined ranges)
1020
 
        */
1021
 
        if (join->best_positions[i].use_insideout_scan)
1022
 
          emb_insideout_nest= tab->emb_sj_nest;
1023
 
      }
1024
 
 
1025
 
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
1026
 
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
1027
 
 
1028
 
      if (tab->emb_sj_nest != emb_insideout_nest)
1029
 
      {
1030
 
        /*
1031
 
          Two different semi-joins interleave. This cannot be handled by
1032
 
          InsideOut strategy.
1033
 
        */
1034
 
        emb_insideout_nest= NULL;
1035
 
      }
1036
 
    }
1037
 
 
1038
 
    if (emb_sj_map) /* We're in duplicate-generating range */
1039
 
    {
1040
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
1041
 
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
1042
 
          i <= no_jbuf_after && !dealing_with_jbuf)
1043
 
      {
1044
 
        /*
1045
 
          This table uses join buffering, which makes use of FirstMatch or
1046
 
          InsideOut strategies impossible for the current and (we assume)
1047
 
          preceding duplicate-producing ranges.
1048
 
          That is, for the join order:
1049
 
 
1050
 
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
1051
 
                  |     |     |     |          | \
1052
 
                  +-----+     +-----+          |  join buffering use
1053
 
                     r1          r2         we're here
1054
 
 
1055
 
          we'll have to remove r1 and r2 and use duplicate-elimination
1056
 
          strategy that spans all the tables, starting from the very 1st
1057
 
          one.
1058
 
        */
1059
 
        dealing_with_jbuf= true;
1060
 
        emb_insideout_nest= false;
1061
 
 
1062
 
        /*
1063
 
          Absorb all preceding duplicate-eliminating ranges. Their strategies
1064
 
          do not matter:
1065
 
        */
1066
 
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
1067
 
        {
1068
 
          dups_ranges[cur_range].outer_tables |=
1069
 
            dups_ranges[prev_range].outer_tables;
1070
 
        }
1071
 
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
1072
 
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
1073
 
        cur_range=  0;
1074
 
      }
1075
 
 
1076
 
      /*
1077
 
        Check if we are at the end of duplicate-producing range. We are if
1078
 
 
1079
 
        1. It's an InsideOut range (which presumes all correlated tables are
1080
 
           in the prefix), and all inner tables are in the join order prefix,
1081
 
           or
1082
 
        2. It's a DuplicateElimination range (possibly covering several
1083
 
           SJ-nests), and all inner, outer, and correlated tables of all
1084
 
           sj-nests are in the join order prefix.
1085
 
      */
1086
 
      bool end_of_range= false;
1087
 
      if (emb_insideout_nest &&
1088
 
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
1089
 
      {
1090
 
        /* Save that this range is handled with InsideOut: */
1091
 
        dups_ranges[cur_range].strategy= 1;
1092
 
        end_of_range= true;
1093
 
      }
1094
 
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
1095
 
      {
1096
 
        /*
1097
 
          This is a complete range to be handled with either DuplicateWeedout
1098
 
          or FirstMatch
1099
 
        */
1100
 
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
1101
 
        /*
1102
 
          This will hold tables from within the range that need to be put
1103
 
          into the join buffer before we can use the FirstMatch on its tail.
1104
 
        */
1105
 
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
1106
 
                                             ~range_start_map;
1107
 
        end_of_range= true;
1108
 
      }
1109
 
 
1110
 
      if (end_of_range)
1111
 
      {
1112
 
        dups_ranges[cur_range].end_idx= i+1;
1113
 
        emb_sj_map= emb_outer_tables= 0;
1114
 
        emb_insideout_nest= NULL;
1115
 
        dealing_with_jbuf= false;
1116
 
        dups_ranges[++cur_range].strategy= 0;
1117
 
      }
1118
 
    }
1119
 
  }
1120
 
 
1121
 
  Session *session= join->session;
1122
 
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
1123
 
  /*
1124
 
    Second pass: setup the chosen strategies
1125
 
  */
1126
 
  for (int j= 0; j < cur_range; j++)
1127
 
  {
1128
 
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
1129
 
    JOIN_TAB *jump_to;
1130
 
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
1131
 
    {
1132
 
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
1133
 
      jump_to= tab++;
1134
 
    }
1135
 
    else // DuplicateWeedout strategy
1136
 
    {
1137
 
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
1138
 
      table_map weed_cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
1139
 
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
1140
 
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
1141
 
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
1142
 
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
1143
 
      JOIN_TAB *last_outer_tab= tab - 1;
1144
 
      /*
1145
 
        Walk through the range and remember
1146
 
         - tables that need their rowids to be put into temptable
1147
 
         - the last outer table
1148
 
      */
1149
 
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1150
 
      {
1151
 
        if (sj_table_is_included(join, tab))
1152
 
        {
1153
 
          last_tab->join_tab= tab;
1154
 
          last_tab->rowid_offset= jt_rowid_offset;
1155
 
          jt_rowid_offset += tab->table->file->ref_length;
1156
 
          if (tab->table->maybe_null)
1157
 
          {
1158
 
            last_tab->null_byte= jt_null_bits / 8;
1159
 
            last_tab->null_bit= jt_null_bits++;
1160
 
          }
1161
 
          last_tab++;
1162
 
          tab->table->prepare_for_position();
1163
 
          tab->rowid_keep_flags= rowid_keep_flags;
1164
 
        }
1165
 
        weed_cur_map |= tab->table->map;
1166
 
        if (!tab->emb_sj_nest && bitmap_covers(weed_cur_map,
1167
 
                                               dups_ranges[j].outer_tables))
1168
 
          last_outer_tab= tab;
1169
 
      }
1170
 
 
1171
 
      if (jt_rowid_offset) /* Temptable has at least one rowid */
1172
 
      {
1173
 
        SJ_TMP_TABLE *sjtbl;
1174
 
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
1175
 
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
1176
 
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
1177
 
          return(true);
1178
 
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
1179
 
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
1180
 
        sjtbl->rowid_len= jt_rowid_offset;
1181
 
        sjtbl->null_bits= jt_null_bits;
1182
 
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
1183
 
 
1184
 
        *next_sjtbl_ptr= sjtbl;
1185
 
        next_sjtbl_ptr= &(sjtbl->next);
1186
 
        sjtbl->next= NULL;
1187
 
 
1188
 
        sjtbl->tmp_table=
1189
 
          create_duplicate_weedout_tmp_table(session,
1190
 
                                             sjtbl->rowid_len +
1191
 
                                             sjtbl->null_bytes,
1192
 
                                             sjtbl);
1193
 
 
1194
 
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
1195
 
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
1196
 
      }
1197
 
      tab= last_outer_tab + 1;
1198
 
      jump_to= last_outer_tab;
1199
 
    }
1200
 
 
1201
 
    /* Create the FirstMatch tail */
1202
 
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
1203
 
    {
1204
 
      if (tab->emb_sj_nest)
1205
 
        tab->do_firstmatch= jump_to;
1206
 
      else
1207
 
        jump_to= tab;
1208
 
    }
1209
 
  }
1210
 
  return(false);
1211
 
}
1212
 
 
1213
 
 
1214
 
static void cleanup_sj_tmp_tables(JOIN *join)
1215
 
{
1216
 
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
1217
 
       sj_tbl= sj_tbl->next)
1218
 
  {
1219
 
    if (sj_tbl->tmp_table)
1220
 
    {
1221
 
      sj_tbl->tmp_table->free_tmp_table(join->session);
1222
 
    }
1223
 
  }
1224
 
  join->sj_tmp_tables= NULL;
1225
 
}
1226
 
 
1227
 
uint32_t make_join_orderinfo(JOIN *join);
1228
 
 
1229
 
/**
1230
 
  global select optimisation.
1231
 
 
1232
 
  @note
1233
 
    error code saved in field 'error'
1234
 
 
1235
 
  @retval
1236
 
    0   success
1237
 
  @retval
1238
 
    1   error
1239
 
*/
1240
 
 
1241
 
int
1242
 
JOIN::optimize()
1243
 
{
1244
 
  // to prevent double initialization on EXPLAIN
1245
 
  if (optimized)
1246
 
    return(0);
1247
 
  optimized= 1;
1248
 
 
1249
 
  session->set_proc_info("optimizing");
1250
 
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
1251
 
              unit->select_limit_cnt);
1252
 
  /* select_limit is used to decide if we are likely to scan the whole table */
1253
 
  select_limit= unit->select_limit_cnt;
1254
 
  if (having || (select_options & OPTION_FOUND_ROWS))
1255
 
    select_limit= HA_POS_ERROR;
1256
 
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
1257
 
  // Ignore errors of execution if option IGNORE present
1258
 
  if (session->lex->ignore)
1259
 
    session->lex->current_select->no_error= 1;
1260
 
 
1261
 
#ifdef HAVE_REF_TO_FIELDS                       // Not done yet
1262
 
  /* Add HAVING to WHERE if possible */
1263
 
  if (having && !group_list && !sum_func_count)
1264
 
  {
1265
 
    if (!conds)
1266
 
    {
1267
 
      conds= having;
1268
 
      having= 0;
1269
 
    }
1270
 
    else if ((conds=new Item_cond_and(conds,having)))
1271
 
    {
1272
 
      /*
1273
 
        Item_cond_and can't be fixed after creation, so we do not check
1274
 
        conds->fixed
1275
 
      */
1276
 
      conds->fix_fields(session, &conds);
1277
 
      conds->change_ref_to_fields(session, tables_list);
1278
 
      conds->top_level_item();
1279
 
      having= 0;
1280
 
    }
1281
 
  }
1282
 
#endif
1283
 
 
1284
 
  /* Convert all outer joins to inner joins if possible */
1285
 
  conds= simplify_joins(this, join_list, conds, true, false);
1286
 
  build_bitmap_for_nested_joins(join_list, 0);
1287
 
 
1288
 
  conds= optimize_cond(this, conds, join_list, &cond_value);
1289
 
  if (session->is_error())
1290
 
  {
1291
 
    error= 1;
1292
 
    return(1);
1293
 
  }
1294
 
 
1295
 
  {
1296
 
    having= optimize_cond(this, having, join_list, &having_value);
1297
 
    if (session->is_error())
1298
 
    {
1299
 
      error= 1;
1300
 
      return(1);
1301
 
    }
1302
 
    if (select_lex->where)
1303
 
      select_lex->cond_value= cond_value;
1304
 
    if (select_lex->having)
1305
 
      select_lex->having_value= having_value;
1306
 
 
1307
 
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
1308
 
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
1309
 
    {                                           /* Impossible cond */
1310
 
      zero_result_cause=  having_value == Item::COND_FALSE ?
1311
 
                           "Impossible HAVING" : "Impossible WHERE";
1312
 
      error= 0;
1313
 
      return(0);
1314
 
    }
1315
 
  }
1316
 
 
1317
 
  /* Optimize count(*), cmin() and cmax() */
1318
 
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
1319
 
  {
1320
 
    int res;
1321
 
    /*
1322
 
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
1323
 
      to the WHERE conditions,
1324
 
      or 1 if all items were resolved,
1325
 
      or 0, or an error number HA_ERR_...
1326
 
    */
1327
 
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
1328
 
    {
1329
 
      if (res == HA_ERR_KEY_NOT_FOUND)
1330
 
      {
1331
 
        zero_result_cause= "No matching min/max row";
1332
 
        error=0;
1333
 
        return(0);
1334
 
      }
1335
 
      if (res > 1)
1336
 
      {
1337
 
        error= res;
1338
 
        return(1);
1339
 
      }
1340
 
      if (res < 0)
1341
 
      {
1342
 
        zero_result_cause= "No matching min/max row";
1343
 
        error=0;
1344
 
        return(0);
1345
 
      }
1346
 
      zero_result_cause= "Select tables optimized away";
1347
 
      tables_list= 0;                           // All tables resolved
1348
 
      /*
1349
 
        Extract all table-independent conditions and replace the WHERE
1350
 
        clause with them. All other conditions were computed by opt_sum_query
1351
 
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
1352
 
        so there is no need to compute the whole WHERE clause again.
1353
 
        Notice that make_cond_for_table() will always succeed to remove all
1354
 
        computed conditions, because opt_sum_query() is applicable only to
1355
 
        conjunctions.
1356
 
        Preserve conditions for EXPLAIN.
1357
 
      */
1358
 
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
1359
 
      {
1360
 
        COND *table_independent_conds=
1361
 
          make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
1362
 
        conds= table_independent_conds;
1363
 
      }
1364
 
    }
1365
 
  }
1366
 
  if (!tables_list)
1367
 
  {
1368
 
    error= 0;
1369
 
    return(0);
1370
 
  }
1371
 
  error= -1;                                    // Error is sent to client
1372
 
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
1373
 
 
1374
 
  /* Calculate how to do the join */
1375
 
  session->set_proc_info("statistics");
1376
 
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
1377
 
      session->is_fatal_error)
1378
 
  {
1379
 
    return(1);
1380
 
  }
1381
 
 
1382
 
  /* Remove distinct if only const tables */
1383
 
  select_distinct= select_distinct && (const_tables != tables);
1384
 
  session->set_proc_info("preparing");
1385
 
  if (result->initialize_tables(this))
1386
 
  {
1387
 
    return(1);                          // error == -1
1388
 
  }
1389
 
  if (const_table_map != found_const_table_map &&
1390
 
      !(select_options & SELECT_DESCRIBE) &&
1391
 
      (!conds ||
1392
 
       !(conds->used_tables() & RAND_TABLE_BIT) ||
1393
 
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
1394
 
  {
1395
 
    zero_result_cause= "no matching row in const table";
1396
 
    error= 0;
1397
 
    return(0);
1398
 
  }
1399
 
  if (!(session->options & OPTION_BIG_SELECTS) &&
1400
 
      best_read > (double) session->variables.max_join_size &&
1401
 
      !(select_options & SELECT_DESCRIBE))
1402
 
  {                                             /* purecov: inspected */
1403
 
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
1404
 
    error= -1;
1405
 
    return(1);
1406
 
  }
1407
 
  if (const_tables && !session->locked_tables &&
1408
 
      !(select_options & SELECT_NO_UNLOCK))
1409
 
    mysql_unlock_some_tables(session, table, const_tables);
1410
 
  if (!conds && outer_join)
1411
 
  {
1412
 
    /* Handle the case where we have an OUTER JOIN without a WHERE */
1413
 
    conds=new Item_int((int64_t) 1,1);  // Always true
1414
 
  }
1415
 
  select= make_select(*table, const_table_map,
1416
 
                      const_table_map, conds, 1, &error);
1417
 
  if (error)
1418
 
  {                                             /* purecov: inspected */
1419
 
    error= -1;                                  /* purecov: inspected */
1420
 
    return(1);
1421
 
  }
1422
 
 
1423
 
  reset_nj_counters(join_list);
1424
 
  make_outerjoin_info(this);
1425
 
 
1426
 
  /*
1427
 
    Among the equal fields belonging to the same multiple equality
1428
 
    choose the one that is to be retrieved first and substitute
1429
 
    all references to these in where condition for a reference for
1430
 
    the selected field.
1431
 
  */
1432
 
  if (conds)
1433
 
  {
1434
 
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
1435
 
    conds->update_used_tables();
1436
 
  }
1437
 
 
1438
 
  /*
1439
 
    Permorm the the optimization on fields evaluation mentioned above
1440
 
    for all on expressions.
1441
 
  */
1442
 
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
1443
 
  {
1444
 
    if (*tab->on_expr_ref)
1445
 
    {
1446
 
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
1447
 
                                                         tab->cond_equal,
1448
 
                                                         map2table);
1449
 
      (*tab->on_expr_ref)->update_used_tables();
1450
 
    }
1451
 
  }
1452
 
 
1453
 
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
1454
 
      (select_options & SELECT_DESCRIBE) &&
1455
 
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
1456
 
  {
1457
 
    conds=new Item_int((int64_t) 0,1);  // Always false
1458
 
  }
1459
 
  if (make_join_select(this, select, conds))
1460
 
  {
1461
 
    zero_result_cause=
1462
 
      "Impossible WHERE noticed after reading const tables";
1463
 
    return(0);                          // error == 0
1464
 
  }
1465
 
 
1466
 
  error= -1;                                    /* if goto err */
1467
 
 
1468
 
  /* Optimize distinct away if possible */
1469
 
  {
1470
 
    order_st *org_order= order;
1471
 
    order=remove_constants(this, order,conds,1, &simple_order);
1472
 
    if (session->is_error())
1473
 
    {
1474
 
      error= 1;
1475
 
      return(1);
1476
 
    }
1477
 
 
1478
 
    /*
1479
 
      If we are using order_st BY NULL or order_st BY const_expression,
1480
 
      return result in any order (even if we are using a GROUP BY)
1481
 
    */
1482
 
    if (!order && org_order)
1483
 
      skip_sort_order= 1;
1484
 
  }
1485
 
  /*
1486
 
     Check if we can optimize away GROUP BY/DISTINCT.
1487
 
     We can do that if there are no aggregate functions, the
1488
 
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
1489
 
     (if present) contain direct references to all key parts of
1490
 
     an unique index (in whatever order) and if the key parts of the
1491
 
     unique index cannot contain NULLs.
1492
 
     Note that the unique keys for DISTINCT and GROUP BY should not
1493
 
     be the same (as long as they are unique).
1494
 
 
1495
 
     The FROM clause must contain a single non-constant table.
1496
 
  */
1497
 
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
1498
 
      !tmp_table_param.sum_func_count &&
1499
 
      (!join_tab[const_tables].select ||
1500
 
       !join_tab[const_tables].select->quick ||
1501
 
       join_tab[const_tables].select->quick->get_type() !=
1502
 
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
1503
 
  {
1504
 
    if (group_list &&
1505
 
       list_contains_unique_index(join_tab[const_tables].table,
1506
 
                                 find_field_in_order_list,
1507
 
                                 (void *) group_list))
1508
 
    {
1509
 
      /*
1510
 
        We have found that grouping can be removed since groups correspond to
1511
 
        only one row anyway, but we still have to guarantee correct result
1512
 
        order. The line below effectively rewrites the query from GROUP BY
1513
 
        <fields> to order_st BY <fields>. There are two exceptions:
1514
 
        - if skip_sort_order is set (see above), then we can simply skip
1515
 
          GROUP BY;
1516
 
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
1517
 
          with the GROUP BY ones, i.e. either one is a prefix of another.
1518
 
          We only check if the order_st BY is a prefix of GROUP BY. In this case
1519
 
          test_if_subpart() copies the ASC/DESC attributes from the original
1520
 
          order_st BY fields.
1521
 
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
1522
 
          'order' as is.
1523
 
       */
1524
 
      if (!order || test_if_subpart(group_list, order))
1525
 
          order= skip_sort_order ? 0 : group_list;
1526
 
      /*
1527
 
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
1528
 
        rewritten to IGNORE INDEX FOR order_st BY(fields).
1529
 
      */
1530
 
      join_tab->table->keys_in_use_for_order_by=
1531
 
        join_tab->table->keys_in_use_for_group_by;
1532
 
      group_list= 0;
1533
 
      group= 0;
1534
 
    }
1535
 
    if (select_distinct &&
1536
 
       list_contains_unique_index(join_tab[const_tables].table,
1537
 
                                 find_field_in_item_list,
1538
 
                                 (void *) &fields_list))
1539
 
    {
1540
 
      select_distinct= 0;
1541
 
    }
1542
 
  }
1543
 
  if (group_list || tmp_table_param.sum_func_count)
1544
 
  {
1545
 
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
1546
 
      select_distinct=0;
1547
 
  }
1548
 
  else if (select_distinct && tables - const_tables == 1)
1549
 
  {
1550
 
    /*
1551
 
      We are only using one table. In this case we change DISTINCT to a
1552
 
      GROUP BY query if:
1553
 
      - The GROUP BY can be done through indexes (no sort) and the order_st
1554
 
        BY only uses selected fields.
1555
 
        (In this case we can later optimize away GROUP BY and order_st BY)
1556
 
      - We are scanning the whole table without LIMIT
1557
 
        This can happen if:
1558
 
        - We are using CALC_FOUND_ROWS
1559
 
        - We are using an order_st BY that can't be optimized away.
1560
 
 
1561
 
      We don't want to use this optimization when we are using LIMIT
1562
 
      because in this case we can just create a temporary table that
1563
 
      holds LIMIT rows and stop when this table is full.
1564
 
    */
1565
 
    JOIN_TAB *tab= &join_tab[const_tables];
1566
 
    bool all_order_fields_used;
1567
 
    if (order)
1568
 
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
1569
 
        &tab->table->keys_in_use_for_order_by);
1570
 
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
1571
 
                                          order, fields_list, all_fields,
1572
 
                                          &all_order_fields_used)))
1573
 
    {
1574
 
      bool skip_group= (skip_sort_order &&
1575
 
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
1576
 
                                &tab->table->keys_in_use_for_group_by) != 0);
1577
 
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
1578
 
      if ((skip_group && all_order_fields_used) ||
1579
 
          select_limit == HA_POS_ERROR ||
1580
 
          (order && !skip_sort_order))
1581
 
      {
1582
 
        /*  Change DISTINCT to GROUP BY */
1583
 
        select_distinct= 0;
1584
 
        no_order= !order;
1585
 
        if (all_order_fields_used)
1586
 
        {
1587
 
          if (order && skip_sort_order)
1588
 
          {
1589
 
            /*
1590
 
              Force MySQL to read the table in sorted order to get result in
1591
 
              order_st BY order.
1592
 
            */
1593
 
            tmp_table_param.quick_group=0;
1594
 
          }
1595
 
          order=0;
1596
 
        }
1597
 
        group=1;                                // For end_write_group
1598
 
      }
1599
 
      else
1600
 
        group_list= 0;
1601
 
    }
1602
 
    else if (session->is_fatal_error)                   // End of memory
1603
 
      return(1);
1604
 
  }
1605
 
  simple_group= 0;
1606
 
  {
1607
 
    order_st *old_group_list;
1608
 
    group_list= remove_constants(this, (old_group_list= group_list), conds,
1609
 
                                 rollup.state == ROLLUP::STATE_NONE,
1610
 
                                 &simple_group);
1611
 
    if (session->is_error())
1612
 
    {
1613
 
      error= 1;
1614
 
      return(1);
1615
 
    }
1616
 
    if (old_group_list && !group_list)
1617
 
      select_distinct= 0;
1618
 
  }
1619
 
  if (!group_list && group)
1620
 
  {
1621
 
    order=0;                                    // The output has only one row
1622
 
    simple_order=1;
1623
 
    select_distinct= 0;                       // No need in distinct for 1 row
1624
 
    group_optimized_away= 1;
1625
 
  }
1626
 
 
1627
 
  calc_group_buffer(this, group_list);
1628
 
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
1629
 
 
1630
 
  if (test_if_subpart(group_list, order) ||
1631
 
      (!group_list && tmp_table_param.sum_func_count))
1632
 
    order=0;
1633
 
 
1634
 
  // Can't use sort on head table if using row cache
1635
 
  if (full_join)
1636
 
  {
1637
 
    if (group_list)
1638
 
      simple_group=0;
1639
 
    if (order)
1640
 
      simple_order=0;
1641
 
  }
1642
 
 
1643
 
  /*
1644
 
    Check if we need to create a temporary table.
1645
 
    This has to be done if all tables are not already read (const tables)
1646
 
    and one of the following conditions holds:
1647
 
    - We are using DISTINCT (simple distinct's are already optimized away)
1648
 
    - We are using an order_st BY or GROUP BY on fields not in the first table
1649
 
    - We are using different order_st BY and GROUP BY orders
1650
 
    - The user wants us to buffer the result.
1651
 
  */
1652
 
  need_tmp= (const_tables != tables &&
1653
 
             ((select_distinct || !simple_order || !simple_group) ||
1654
 
              (group_list && order) ||
1655
 
              test(select_options & OPTION_BUFFER_RESULT)));
1656
 
 
1657
 
  uint32_t no_jbuf_after= make_join_orderinfo(this);
1658
 
  uint64_t select_opts_for_readinfo=
1659
 
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
1660
 
 
1661
 
  sj_tmp_tables= NULL;
1662
 
  if (!select_lex->sj_nests.is_empty())
1663
 
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo,
1664
 
                                    no_jbuf_after);
1665
 
 
1666
 
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
1667
 
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
1668
 
    return(1);
1669
 
 
1670
 
  /* Create all structures needed for materialized subquery execution. */
1671
 
  if (setup_subquery_materialization())
1672
 
    return(1);
1673
 
 
1674
 
  /*
1675
 
    is this simple IN subquery?
1676
 
  */
1677
 
  if (!group_list && !order &&
1678
 
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
1679
 
      tables == 1 && conds &&
1680
 
      !unit->is_union())
1681
 
  {
1682
 
    if (!having)
1683
 
    {
1684
 
      Item *where= conds;
1685
 
      if (join_tab[0].type == JT_EQ_REF &&
1686
 
          join_tab[0].ref.items[0]->name == in_left_expr_name)
1687
 
      {
1688
 
        remove_subq_pushed_predicates(&where);
1689
 
        save_index_subquery_explain_info(join_tab, where);
1690
 
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
1691
 
        error= 0;
1692
 
        return(unit->item->
1693
 
                    change_engine(new
1694
 
                                  subselect_uniquesubquery_engine(session,
1695
 
                                                                  join_tab,
1696
 
                                                                  unit->item,
1697
 
                                                                  where)));
1698
 
      }
1699
 
      else if (join_tab[0].type == JT_REF &&
1700
 
               join_tab[0].ref.items[0]->name == in_left_expr_name)
1701
 
      {
1702
 
        remove_subq_pushed_predicates(&where);
1703
 
        save_index_subquery_explain_info(join_tab, where);
1704
 
        join_tab[0].type= JT_INDEX_SUBQUERY;
1705
 
        error= 0;
1706
 
        return(unit->item->
1707
 
                    change_engine(new
1708
 
                                  subselect_indexsubquery_engine(session,
1709
 
                                                                 join_tab,
1710
 
                                                                 unit->item,
1711
 
                                                                 where,
1712
 
                                                                 NULL,
1713
 
                                                                 0)));
1714
 
      }
1715
 
    } else if (join_tab[0].type == JT_REF_OR_NULL &&
1716
 
               join_tab[0].ref.items[0]->name == in_left_expr_name &&
1717
 
               having->name == in_having_cond)
1718
 
    {
1719
 
      join_tab[0].type= JT_INDEX_SUBQUERY;
1720
 
      error= 0;
1721
 
      conds= remove_additional_cond(conds);
1722
 
      save_index_subquery_explain_info(join_tab, conds);
1723
 
      return(unit->item->
1724
 
                  change_engine(new subselect_indexsubquery_engine(session,
1725
 
                                                                   join_tab,
1726
 
                                                                   unit->item,
1727
 
                                                                   conds,
1728
 
                                                                   having,
1729
 
                                                                   1)));
1730
 
    }
1731
 
 
1732
 
  }
1733
 
  /*
1734
 
    Need to tell handlers that to play it safe, it should fetch all
1735
 
    columns of the primary key of the tables: this is because MySQL may
1736
 
    build row pointers for the rows, and for all columns of the primary key
1737
 
    the read set has not necessarily been set by the server code.
1738
 
  */
1739
 
  if (need_tmp || select_distinct || group_list || order)
1740
 
  {
1741
 
    for (uint32_t i = const_tables; i < tables; i++)
1742
 
      join_tab[i].table->prepare_for_position();
1743
 
  }
1744
 
 
1745
 
  if (const_tables != tables)
1746
 
  {
1747
 
    /*
1748
 
      Because filesort always does a full table scan or a quick range scan
1749
 
      we must add the removed reference to the select for the table.
1750
 
      We only need to do this when we have a simple_order or simple_group
1751
 
      as in other cases the join is done before the sort.
1752
 
    */
1753
 
    if ((order || group_list) &&
1754
 
        (join_tab[const_tables].type != JT_ALL) &&
1755
 
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
1756
 
        ((order && simple_order) || (group_list && simple_group)))
1757
 
    {
1758
 
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
1759
 
        return(1);
1760
 
      }
1761
 
    }
1762
 
 
1763
 
    if (!(select_options & SELECT_BIG_RESULT) &&
1764
 
        ((group_list &&
1765
 
          (!simple_group ||
1766
 
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
1767
 
                                    unit->select_limit_cnt, 0,
1768
 
                                    &join_tab[const_tables].table->
1769
 
                                    keys_in_use_for_group_by))) ||
1770
 
         select_distinct) &&
1771
 
        tmp_table_param.quick_group)
1772
 
    {
1773
 
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
1774
 
    }
1775
 
    if (order)
1776
 
    {
1777
 
      /*
1778
 
        Force using of tmp table if sorting by a SP or UDF function due to
1779
 
        their expensive and probably non-deterministic nature.
1780
 
      */
1781
 
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
1782
 
      {
1783
 
        Item *item= *tmp_order->item;
1784
 
        if (item->is_expensive())
1785
 
        {
1786
 
          /* Force tmp table without sort */
1787
 
          need_tmp=1; simple_order=simple_group=0;
1788
 
          break;
1789
 
        }
1790
 
      }
1791
 
    }
1792
 
  }
1793
 
 
1794
 
  tmp_having= having;
1795
 
  if (select_options & SELECT_DESCRIBE)
1796
 
  {
1797
 
    error= 0;
1798
 
    return(0);
1799
 
  }
1800
 
  having= 0;
1801
 
 
1802
 
  /*
1803
 
    The loose index scan access method guarantees that all grouping or
1804
 
    duplicate row elimination (for distinct) is already performed
1805
 
    during data retrieval, and that all MIN/MAX functions are already
1806
 
    computed for each group. Thus all MIN/MAX functions should be
1807
 
    treated as regular functions, and there is no need to perform
1808
 
    grouping in the main execution loop.
1809
 
    Notice that currently loose index scan is applicable only for
1810
 
    single table queries, thus it is sufficient to test only the first
1811
 
    join_tab element of the plan for its access method.
1812
 
  */
1813
 
  if (join_tab->is_using_loose_index_scan())
1814
 
    tmp_table_param.precomputed_group_by= true;
1815
 
 
1816
 
  /* Create a tmp table if distinct or if the sort is too complicated */
1817
 
  if (need_tmp)
1818
 
  {
1819
 
    session->set_proc_info("Creating tmp table");
1820
 
 
1821
 
    init_items_ref_array();
1822
 
 
1823
 
    tmp_table_param.hidden_field_count= (all_fields.elements -
1824
 
                                         fields_list.elements);
1825
 
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
1826
 
                                                             (order_st*) 0);
1827
 
    /*
1828
 
      Pushing LIMIT to the temporary table creation is not applicable
1829
 
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
1830
 
      there are aggregate functions, because in all these cases we need
1831
 
      all result rows.
1832
 
    */
1833
 
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
1834
 
                             !tmp_group &&
1835
 
                             !session->lex->current_select->with_sum_func) ?
1836
 
                            select_limit : HA_POS_ERROR;
1837
 
 
1838
 
    if (!(exec_tmp_table1=
1839
 
          create_tmp_table(session, &tmp_table_param, all_fields,
1840
 
                           tmp_group,
1841
 
                           group_list ? 0 : select_distinct,
1842
 
                           group_list && simple_group,
1843
 
                           select_options,
1844
 
                           tmp_rows_limit,
1845
 
                           (char *) "")))
1846
 
                {
1847
 
      return(1);
1848
 
    }
1849
 
 
1850
 
    /*
1851
 
      We don't have to store rows in temp table that doesn't match HAVING if:
1852
 
      - we are sorting the table and writing complete group rows to the
1853
 
        temp table.
1854
 
      - We are using DISTINCT without resolving the distinct as a GROUP BY
1855
 
        on all columns.
1856
 
 
1857
 
      If having is not handled here, it will be checked before the row
1858
 
      is sent to the client.
1859
 
    */
1860
 
    if (tmp_having &&
1861
 
        (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
1862
 
      having= tmp_having;
1863
 
 
1864
 
    /* if group or order on first table, sort first */
1865
 
    if (group_list && simple_group)
1866
 
    {
1867
 
      session->set_proc_info("Sorting for group");
1868
 
      if (create_sort_index(session, this, group_list,
1869
 
                            HA_POS_ERROR, HA_POS_ERROR, false) ||
1870
 
          alloc_group_fields(this, group_list) ||
1871
 
          make_sum_func_list(all_fields, fields_list, 1) ||
1872
 
          setup_sum_funcs(session, sum_funcs))
1873
 
      {
1874
 
        return(1);
1875
 
      }
1876
 
      group_list=0;
1877
 
    }
1878
 
    else
1879
 
    {
1880
 
      if (make_sum_func_list(all_fields, fields_list, 0) ||
1881
 
          setup_sum_funcs(session, sum_funcs))
1882
 
      {
1883
 
        return(1);
1884
 
      }
1885
 
 
1886
 
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
1887
 
      {
1888
 
        session->set_proc_info("Sorting for order");
1889
 
        if (create_sort_index(session, this, order,
1890
 
                              HA_POS_ERROR, HA_POS_ERROR, true))
1891
 
        {
1892
 
          return(1);
1893
 
        }
1894
 
        order=0;
1895
 
      }
1896
 
    }
1897
 
 
1898
 
    /*
1899
 
      Optimize distinct when used on some of the tables
1900
 
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
1901
 
      In this case we can stop scanning t2 when we have found one t1.a
1902
 
    */
1903
 
 
1904
 
    if (exec_tmp_table1->distinct)
1905
 
    {
1906
 
      table_map used_tables= session->used_tables;
1907
 
      JOIN_TAB *last_join_tab= join_tab+tables-1;
1908
 
      do
1909
 
      {
1910
 
        if (used_tables & last_join_tab->table->map)
1911
 
          break;
1912
 
        last_join_tab->not_used_in_distinct=1;
1913
 
      } while (last_join_tab-- != join_tab);
1914
 
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
1915
 
      if (order && skip_sort_order)
1916
 
      {
1917
 
        /* Should always succeed */
1918
 
        if (test_if_skip_sort_order(&join_tab[const_tables],
1919
 
                                    order, unit->select_limit_cnt, 0,
1920
 
                                    &join_tab[const_tables].table->
1921
 
                                      keys_in_use_for_order_by))
1922
 
          order=0;
1923
 
      }
1924
 
    }
1925
 
 
1926
 
    /*
1927
 
      If this join belongs to an uncacheable subquery save
1928
 
      the original join
1929
 
    */
1930
 
    if (select_lex->uncacheable && !is_top_level_join() &&
1931
 
        init_save_join_tab())
1932
 
      return(-1);                         /* purecov: inspected */
1933
 
  }
1934
 
 
1935
 
  error= 0;
1936
 
  return(0);
1937
 
}
1938
 
 
1939
 
 
1940
 
/**
1941
 
  Restore values in temporary join.
1942
 
*/
1943
 
void JOIN::restore_tmp()
1944
 
{
1945
 
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
1946
 
}
1947
 
 
1948
 
int
1949
 
JOIN::reinit()
1950
 
{
1951
 
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
1952
 
                                    select_lex->offset_limit->val_uint() :
1953
 
                                    0UL);
1954
 
 
1955
 
  first_record= 0;
1956
 
 
1957
 
  if (exec_tmp_table1)
1958
 
  {
1959
 
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
1960
 
    exec_tmp_table1->file->ha_delete_all_rows();
1961
 
    free_io_cache(exec_tmp_table1);
1962
 
    filesort_free_buffers(exec_tmp_table1,0);
1963
 
  }
1964
 
  if (exec_tmp_table2)
1965
 
  {
1966
 
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
1967
 
    exec_tmp_table2->file->ha_delete_all_rows();
1968
 
    free_io_cache(exec_tmp_table2);
1969
 
    filesort_free_buffers(exec_tmp_table2,0);
1970
 
  }
1971
 
  if (items0)
1972
 
    set_items_ref_array(items0);
1973
 
 
1974
 
  if (join_tab_save)
1975
 
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
1976
 
 
1977
 
  if (tmp_join)
1978
 
    restore_tmp();
1979
 
 
1980
 
  /* Reset of sum functions */
1981
 
  if (sum_funcs)
1982
 
  {
1983
 
    Item_sum *func, **func_ptr= sum_funcs;
1984
 
    while ((func= *(func_ptr++)))
1985
 
      func->clear();
1986
 
  }
1987
 
 
1988
 
  return(0);
1989
 
}
1990
 
 
1991
 
/**
1992
 
   @brief Save the original join layout
1993
 
 
1994
 
   @details Saves the original join layout so it can be reused in
1995
 
   re-execution and for EXPLAIN.
1996
 
 
1997
 
   @return Operation status
1998
 
   @retval 0      success.
1999
 
   @retval 1      error occurred.
2000
 
*/
2001
 
 
2002
 
bool
2003
 
JOIN::init_save_join_tab()
2004
 
{
2005
 
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
2006
 
    return 1;                                  /* purecov: inspected */
2007
 
  error= 0;                                    // Ensure that tmp_join.error= 0
2008
 
  restore_tmp();
2009
 
  return 0;
2010
 
}
2011
 
 
2012
 
 
2013
 
bool
2014
 
JOIN::save_join_tab()
2015
 
{
2016
 
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
2017
 
  {
2018
 
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
2019
 
                                                sizeof(JOIN_TAB) * tables)))
2020
 
      return 1;
2021
 
  }
2022
 
  return 0;
2023
 
}
2024
 
 
2025
 
 
2026
 
/**
2027
 
  Exec select.
2028
 
 
2029
 
  @todo
2030
 
    Note, that create_sort_index calls test_if_skip_sort_order and may
2031
 
    finally replace sorting with index scan if there is a LIMIT clause in
2032
 
    the query.  It's never shown in EXPLAIN!
2033
 
 
2034
 
  @todo
2035
 
    When can we have here session->net.report_error not zero?
2036
 
*/
2037
 
void JOIN::exec()
2038
 
{
2039
 
  List<Item> *columns_list= &fields_list;
2040
 
  int      tmp_error;
2041
 
 
2042
 
  session->set_proc_info("executing");
2043
 
  error= 0;
2044
 
 
2045
 
  if (!tables_list && (tables || !select_lex->with_sum_func))
2046
 
  {                                           
2047
 
    /* Only test of functions */
2048
 
    if (select_options & SELECT_DESCRIBE)
2049
 
      select_describe(this, false, false, false, (zero_result_cause?zero_result_cause:"No tables used"));
2050
 
    else
2051
 
    {
2052
 
      result->send_fields(*columns_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2053
 
      /*
2054
 
        We have to test for 'conds' here as the WHERE may not be constant
2055
 
        even if we don't have any tables for prepared statements or if
2056
 
        conds uses something like 'rand()'.
2057
 
      */
2058
 
      if (cond_value != Item::COND_FALSE &&
2059
 
          (!conds || conds->val_int()) &&
2060
 
          (!having || having->val_int()))
2061
 
      {
2062
 
        if (do_send_rows && result->send_data(fields_list))
2063
 
          error= 1;
2064
 
        else
2065
 
        {
2066
 
          error= (int) result->send_eof();
2067
 
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
2068
 
        }
2069
 
      }
2070
 
      else
2071
 
      {
2072
 
        error= (int) result->send_eof();
2073
 
        send_records= 0;
2074
 
      }
2075
 
    }
2076
 
    /* Single select (without union) always returns 0 or 1 row */
2077
 
    session->limit_found_rows= send_records;
2078
 
    session->examined_row_count= 0;
2079
 
    return;
2080
 
  }
2081
 
  /*
2082
 
    Don't reset the found rows count if there're no tables as
2083
 
    FOUND_ROWS() may be called. Never reset the examined row count here.
2084
 
    It must be accumulated from all join iterations of all join parts.
2085
 
  */
2086
 
  if (tables)
2087
 
    session->limit_found_rows= 0;
2088
 
 
2089
 
  if (zero_result_cause)
2090
 
  {
2091
 
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
2092
 
                            *columns_list,
2093
 
                            send_row_on_empty_set(),
2094
 
                            select_options,
2095
 
                            zero_result_cause,
2096
 
                            having);
2097
 
    return;
2098
 
  }
2099
 
 
2100
 
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
2101
 
    return;
2102
 
 
2103
 
  if (select_options & SELECT_DESCRIBE)
2104
 
  {
2105
 
    /*
2106
 
      Check if we managed to optimize order_st BY away and don't use temporary
2107
 
      table to resolve order_st BY: in that case, we only may need to do
2108
 
      filesort for GROUP BY.
2109
 
    */
2110
 
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
2111
 
    {
2112
 
      /* Reset 'order' to 'group_list' and reinit variables describing 'order' */
2113
 
      order= group_list;
2114
 
      simple_order= simple_group;
2115
 
      skip_sort_order= 0;
2116
 
    }
2117
 
    if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
2118
 
    {
2119
 
      if (const_tables == tables 
2120
 
        || ((simple_order || skip_sort_order) 
2121
 
          && test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
2122
 
      order= 0;
2123
 
    }
2124
 
    having= tmp_having;
2125
 
    select_describe(this, need_tmp, order != 0 && !skip_sort_order,  select_distinct, !tables ? "No tables used" : NULL);
2126
 
    return;
2127
 
  }
2128
 
 
2129
 
  JOIN *curr_join= this;
2130
 
  List<Item> *curr_all_fields= &all_fields;
2131
 
  List<Item> *curr_fields_list= &fields_list;
2132
 
  Table *curr_tmp_table= 0;
2133
 
  /*
2134
 
    Initialize examined rows here because the values from all join parts
2135
 
    must be accumulated in examined_row_count. Hence every join
2136
 
    iteration must count from zero.
2137
 
  */
2138
 
  curr_join->examined_rows= 0;
2139
 
 
2140
 
  /* Create a tmp table if distinct or if the sort is too complicated */
2141
 
  if (need_tmp)
2142
 
  {
2143
 
    if (tmp_join)
2144
 
    {
2145
 
      /*
2146
 
        We are in a non cacheable sub query. Get the saved join structure
2147
 
        after optimization.
2148
 
        (curr_join may have been modified during last exection and we need
2149
 
        to reset it)
2150
 
      */
2151
 
      curr_join= tmp_join;
2152
 
    }
2153
 
    curr_tmp_table= exec_tmp_table1;
2154
 
 
2155
 
    /* Copy data to the temporary table */
2156
 
    session->set_proc_info("Copying to tmp table");
2157
 
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
2158
 
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
2159
 
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2160
 
    {
2161
 
      error= tmp_error;
2162
 
      return;
2163
 
    }
2164
 
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
2165
 
 
2166
 
    if (curr_join->having)
2167
 
      curr_join->having= curr_join->tmp_having= 0; // Allready done
2168
 
 
2169
 
    /* Change sum_fields reference to calculated fields in tmp_table */
2170
 
    curr_join->all_fields= *curr_all_fields;
2171
 
    if (!items1)
2172
 
    {
2173
 
      items1= items0 + all_fields.elements;
2174
 
      if (sort_and_group || curr_tmp_table->group)
2175
 
      {
2176
 
        if (change_to_use_tmp_fields(session, items1,
2177
 
                  tmp_fields_list1, tmp_all_fields1,
2178
 
                  fields_list.elements, all_fields))
2179
 
          return;
2180
 
      }
2181
 
      else
2182
 
      {
2183
 
        if (change_refs_to_tmp_fields(session, items1,
2184
 
                    tmp_fields_list1, tmp_all_fields1,
2185
 
                    fields_list.elements, all_fields))
2186
 
          return;
2187
 
      }
2188
 
      curr_join->tmp_all_fields1= tmp_all_fields1;
2189
 
      curr_join->tmp_fields_list1= tmp_fields_list1;
2190
 
      curr_join->items1= items1;
2191
 
    }
2192
 
    curr_all_fields= &tmp_all_fields1;
2193
 
    curr_fields_list= &tmp_fields_list1;
2194
 
    curr_join->set_items_ref_array(items1);
2195
 
 
2196
 
    if (sort_and_group || curr_tmp_table->group)
2197
 
    {
2198
 
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
2199
 
                                             + curr_join->tmp_table_param.func_count;
2200
 
      curr_join->tmp_table_param.sum_func_count= 0;
2201
 
      curr_join->tmp_table_param.func_count= 0;
2202
 
    }
2203
 
    else
2204
 
    {
2205
 
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
2206
 
      curr_join->tmp_table_param.func_count= 0;
2207
 
    }
2208
 
 
2209
 
    if (curr_tmp_table->group)
2210
 
    {                                           // Already grouped
2211
 
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
2212
 
        curr_join->order= curr_join->group_list;  /* order by group */
2213
 
      curr_join->group_list= 0;
2214
 
    }
2215
 
 
2216
 
    /*
2217
 
      If we have different sort & group then we must sort the data by group
2218
 
      and copy it to another tmp table
2219
 
      This code is also used if we are using distinct something
2220
 
      we haven't been able to store in the temporary table yet
2221
 
      like SEC_TO_TIME(SUM(...)).
2222
 
    */
2223
 
 
2224
 
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) 
2225
 
        || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
2226
 
    {                                   /* Must copy to another table */
2227
 
      /* Free first data from old join */
2228
 
      curr_join->join_free();
2229
 
      if (make_simple_join(curr_join, curr_tmp_table))
2230
 
        return;
2231
 
      calc_group_buffer(curr_join, group_list);
2232
 
      count_field_types(select_lex, &curr_join->tmp_table_param,
2233
 
                        curr_join->tmp_all_fields1,
2234
 
                        curr_join->select_distinct && !curr_join->group_list);
2235
 
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
2236
 
                                                   - curr_join->tmp_fields_list1.elements;
2237
 
 
2238
 
      if (exec_tmp_table2)
2239
 
        curr_tmp_table= exec_tmp_table2;
2240
 
      else
2241
 
      {
2242
 
        /* group data to new table */
2243
 
 
2244
 
        /*
2245
 
          If the access method is loose index scan then all MIN/MAX
2246
 
          functions are precomputed, and should be treated as regular
2247
 
          functions. See extended comment in JOIN::exec.
2248
 
        */
2249
 
        if (curr_join->join_tab->is_using_loose_index_scan())
2250
 
          curr_join->tmp_table_param.precomputed_group_by= true;
2251
 
 
2252
 
        if (!(curr_tmp_table=
2253
 
              exec_tmp_table2= create_tmp_table(session,
2254
 
                                                &curr_join->tmp_table_param,
2255
 
                                                *curr_all_fields,
2256
 
                                                (order_st*) 0,
2257
 
                                                curr_join->select_distinct &&
2258
 
                                                !curr_join->group_list,
2259
 
                                                1, curr_join->select_options,
2260
 
                                                HA_POS_ERROR,
2261
 
                                                (char *) "")))
2262
 
          return;
2263
 
        curr_join->exec_tmp_table2= exec_tmp_table2;
2264
 
      }
2265
 
      if (curr_join->group_list)
2266
 
      {
2267
 
        session->set_proc_info("Creating sort index");
2268
 
        if (curr_join->join_tab == join_tab && save_join_tab())
2269
 
        {
2270
 
          return;
2271
 
        }
2272
 
        if (create_sort_index(session, curr_join, curr_join->group_list,
2273
 
                  HA_POS_ERROR, HA_POS_ERROR, false) ||
2274
 
            make_group_fields(this, curr_join))
2275
 
        {
2276
 
          return;
2277
 
        }
2278
 
        sortorder= curr_join->sortorder;
2279
 
      }
2280
 
 
2281
 
      session->set_proc_info("Copying to group table");
2282
 
      tmp_error= -1;
2283
 
      if (curr_join != this)
2284
 
      {
2285
 
        if (sum_funcs2)
2286
 
        {
2287
 
          curr_join->sum_funcs= sum_funcs2;
2288
 
          curr_join->sum_funcs_end= sum_funcs_end2;
2289
 
        }
2290
 
        else
2291
 
        {
2292
 
          curr_join->alloc_func_list();
2293
 
          sum_funcs2= curr_join->sum_funcs;
2294
 
          sum_funcs_end2= curr_join->sum_funcs_end;
2295
 
        }
2296
 
      }
2297
 
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
2298
 
        return;
2299
 
      curr_join->group_list= 0;
2300
 
 
2301
 
      if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
2302
 
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
2303
 
      
2304
 
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
2305
 
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
2306
 
      {
2307
 
        error= tmp_error;
2308
 
        return;
2309
 
      }
2310
 
      end_read_record(&curr_join->join_tab->read_record);
2311
 
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
2312
 
      curr_join->join_tab[0].table= 0;           // Table is freed
2313
 
 
2314
 
      // No sum funcs anymore
2315
 
      if (!items2)
2316
 
      {
2317
 
        items2= items1 + all_fields.elements;
2318
 
        if (change_to_use_tmp_fields(session, items2,
2319
 
                  tmp_fields_list2, tmp_all_fields2,
2320
 
                  fields_list.elements, tmp_all_fields1))
2321
 
          return;
2322
 
        curr_join->tmp_fields_list2= tmp_fields_list2;
2323
 
        curr_join->tmp_all_fields2= tmp_all_fields2;
2324
 
      }
2325
 
      curr_fields_list= &curr_join->tmp_fields_list2;
2326
 
      curr_all_fields= &curr_join->tmp_all_fields2;
2327
 
      curr_join->set_items_ref_array(items2);
2328
 
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
2329
 
      curr_join->tmp_table_param.sum_func_count= 0;
2330
 
    }
2331
 
    if (curr_tmp_table->distinct)
2332
 
      curr_join->select_distinct=0;             /* Each row is unique */
2333
 
 
2334
 
    curr_join->join_free();                     /* Free quick selects */
2335
 
    if (curr_join->select_distinct && ! curr_join->group_list)
2336
 
    {
2337
 
      session->set_proc_info("Removing duplicates");
2338
 
      if (curr_join->tmp_having)
2339
 
        curr_join->tmp_having->update_used_tables();
2340
 
 
2341
 
      if (remove_duplicates(curr_join, curr_tmp_table,
2342
 
                            *curr_fields_list, curr_join->tmp_having))
2343
 
        return;
2344
 
      
2345
 
      curr_join->tmp_having=0;
2346
 
      curr_join->select_distinct=0;
2347
 
    }
2348
 
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
2349
 
    if (make_simple_join(curr_join, curr_tmp_table))
2350
 
      return;
2351
 
    calc_group_buffer(curr_join, curr_join->group_list);
2352
 
    count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
2353
 
 
2354
 
  }
2355
 
 
2356
 
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
2357
 
  {
2358
 
    if (make_group_fields(this, curr_join))
2359
 
      return;
2360
 
 
2361
 
    if (! items3)
2362
 
    {
2363
 
      if (! items0)
2364
 
        init_items_ref_array();
2365
 
      items3= ref_pointer_array + (all_fields.elements*4);
2366
 
      setup_copy_fields(session, &curr_join->tmp_table_param,
2367
 
                        items3, tmp_fields_list3, tmp_all_fields3,
2368
 
                        curr_fields_list->elements, *curr_all_fields);
2369
 
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
2370
 
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
2371
 
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
2372
 
      curr_join->tmp_all_fields3= tmp_all_fields3;
2373
 
      curr_join->tmp_fields_list3= tmp_fields_list3;
2374
 
    }
2375
 
    else
2376
 
    {
2377
 
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
2378
 
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
2379
 
      curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
2380
 
    }
2381
 
    curr_fields_list= &tmp_fields_list3;
2382
 
    curr_all_fields= &tmp_all_fields3;
2383
 
    curr_join->set_items_ref_array(items3);
2384
 
 
2385
 
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
2386
 
                                      1, true) ||
2387
 
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
2388
 
        session->is_fatal_error)
2389
 
      return;
2390
 
  }
2391
 
  if (curr_join->group_list || curr_join->order)
2392
 
  {
2393
 
    session->set_proc_info("Sorting result");
2394
 
    /* If we have already done the group, add HAVING to sorted table */
2395
 
    if (curr_join->tmp_having && ! curr_join->group_list &&     ! curr_join->sort_and_group)
2396
 
    {
2397
 
      // Some tables may have been const
2398
 
      curr_join->tmp_having->update_used_tables();
2399
 
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
2400
 
      table_map used_tables= (curr_join->const_table_map |
2401
 
                              curr_table->table->map);
2402
 
 
2403
 
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
2404
 
      if (sort_table_cond)
2405
 
      {
2406
 
        if (!curr_table->select)
2407
 
          if (!(curr_table->select= new SQL_SELECT))
2408
 
            return;
2409
 
        if (!curr_table->select->cond)
2410
 
          curr_table->select->cond= sort_table_cond;
2411
 
        else                                    // This should never happen
2412
 
        {
2413
 
          if (!(curr_table->select->cond=
2414
 
          new Item_cond_and(curr_table->select->cond,
2415
 
                sort_table_cond)))
2416
 
            return;
2417
 
          /*
2418
 
            Item_cond_and do not need fix_fields for execution, its parameters
2419
 
            are fixed or do not need fix_fields, too
2420
 
          */
2421
 
          curr_table->select->cond->quick_fix_field();
2422
 
        }
2423
 
        curr_table->select_cond= curr_table->select->cond;
2424
 
        curr_table->select_cond->top_level_item();
2425
 
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
2426
 
                    ~ (table_map) 0,
2427
 
                    ~used_tables, 0);
2428
 
      }
2429
 
    }
2430
 
    {
2431
 
      if (group)
2432
 
        curr_join->select_limit= HA_POS_ERROR;
2433
 
      else
2434
 
      {
2435
 
        /*
2436
 
          We can abort sorting after session->select_limit rows if we there is no
2437
 
          WHERE clause for any tables after the sorted one.
2438
 
        */
2439
 
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
2440
 
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
2441
 
        for (; curr_table < end_table ; curr_table++)
2442
 
        {
2443
 
          /*
2444
 
            table->keyuse is set in the case there was an original WHERE clause
2445
 
            on the table that was optimized away.
2446
 
          */
2447
 
          if (curr_table->select_cond ||
2448
 
              (curr_table->keyuse && !curr_table->first_inner))
2449
 
          {
2450
 
            /* We have to sort all rows */
2451
 
            curr_join->select_limit= HA_POS_ERROR;
2452
 
            break;
2453
 
          }
2454
 
        }
2455
 
      }
2456
 
      if (curr_join->join_tab == join_tab && save_join_tab())
2457
 
        return;
2458
 
      /*
2459
 
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
2460
 
        chose FILESORT to be faster than INDEX SCAN or there is no
2461
 
        suitable index present.
2462
 
        Note, that create_sort_index calls test_if_skip_sort_order and may
2463
 
        finally replace sorting with index scan if there is a LIMIT clause in
2464
 
        the query. XXX: it's never shown in EXPLAIN!
2465
 
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
2466
 
      */
2467
 
      if (create_sort_index(session, curr_join,
2468
 
                            curr_join->group_list ?
2469
 
                            curr_join->group_list : curr_join->order,
2470
 
                            curr_join->select_limit,
2471
 
                            (select_options & OPTION_FOUND_ROWS ?
2472
 
                             HA_POS_ERROR : unit->select_limit_cnt),
2473
 
                            curr_join->group_list ? true : false))
2474
 
        return;
2475
 
 
2476
 
      sortorder= curr_join->sortorder;
2477
 
      if (curr_join->const_tables != curr_join->tables &&
2478
 
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
2479
 
      {
2480
 
        /*
2481
 
          If no IO cache exists for the first table then we are using an
2482
 
          INDEX SCAN and no filesort. Thus we should not remove the sorted
2483
 
          attribute on the INDEX SCAN.
2484
 
        */
2485
 
        skip_sort_order= 1;
2486
 
      }
2487
 
    }
2488
 
  }
2489
 
  /* XXX: When can we have here session->is_error() not zero? */
2490
 
  if (session->is_error())
2491
 
  {
2492
 
    error= session->is_error();
2493
 
    return;
2494
 
  }
2495
 
  curr_join->having= curr_join->tmp_having;
2496
 
  curr_join->fields= curr_fields_list;
2497
 
 
2498
 
  session->set_proc_info("Sending data");
2499
 
  result->send_fields(*curr_fields_list,
2500
 
                      Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
2501
 
  error= do_select(curr_join, curr_fields_list, NULL);
2502
 
  session->limit_found_rows= curr_join->send_records;
2503
 
 
2504
 
  /* Accumulate the counts from all join iterations of all join parts. */
2505
 
  session->examined_row_count+= curr_join->examined_rows;
2506
 
 
2507
 
  /*
2508
 
    With EXPLAIN EXTENDED we have to restore original ref_array
2509
 
    for a derived table which is always materialized.
2510
 
    Otherwise we would not be able to print the query  correctly.
2511
 
  */
2512
 
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
2513
 
    set_items_ref_array(items0);
2514
 
 
2515
 
  return;
2516
 
}
2517
 
 
2518
 
/**
2519
 
  Clean up join.
2520
 
 
2521
 
  @return
2522
 
    Return error that hold JOIN.
2523
 
*/
2524
 
int JOIN::destroy()
2525
 
{
2526
 
  select_lex->join= 0;
2527
 
 
2528
 
  if (tmp_join)
2529
 
  {
2530
 
    if (join_tab != tmp_join->join_tab)
2531
 
    {
2532
 
      JOIN_TAB *tab, *end;
2533
 
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
2534
 
        tab->cleanup();
2535
 
    }
2536
 
    tmp_join->tmp_join= 0;
2537
 
    tmp_table_param.copy_field=0;
2538
 
    return(tmp_join->destroy());
2539
 
  }
2540
 
  cond_equal= 0;
2541
 
 
2542
 
  cleanup(1);
2543
 
  if (exec_tmp_table1)
2544
 
    exec_tmp_table1->free_tmp_table(session);
2545
 
  if (exec_tmp_table2)
2546
 
    exec_tmp_table2->free_tmp_table(session);
2547
 
  delete select;
2548
 
  delete_dynamic(&keyuse);
2549
 
  return(error);
2550
 
}
2551
 
 
2552
296
/**
2553
297
  An entry point to single-unit select (a select without UNION).
2554
298
 
2591
335
  @retval
2592
336
    true   an error
2593
337
*/
2594
 
 
2595
 
bool
2596
 
mysql_select(Session *session, Item ***rref_pointer_array,
2597
 
             TableList *tables, uint32_t wild_num, List<Item> &fields,
2598
 
             COND *conds, uint32_t og_num,  order_st *order, order_st *group,
2599
 
             Item *having, uint64_t select_options,
2600
 
             select_result *result, Select_Lex_Unit *unit,
2601
 
             Select_Lex *select_lex)
 
338
bool mysql_select(Session *session,
 
339
                  Item ***rref_pointer_array,
 
340
                        TableList *tables, 
 
341
                  uint32_t wild_num, 
 
342
                  List<Item> &fields,
 
343
                        COND *conds, 
 
344
                  uint32_t og_num,  
 
345
                  order_st *order, 
 
346
                  order_st *group,
 
347
                        Item *having, 
 
348
                  uint64_t select_options,
 
349
                        select_result *result, 
 
350
                  Select_Lex_Unit *unit,
 
351
                        Select_Lex *select_lex)
2602
352
{
2603
353
  bool err;
2604
354
  bool free_join= 1;
2613
363
      creation
2614
364
    */
2615
365
    if (select_lex->linkage != DERIVED_TABLE_TYPE ||
2616
 
        (select_options & SELECT_DESCRIBE))
 
366
        (select_options & SELECT_DESCRIBE))
2617
367
    {
2618
368
      if (select_lex->linkage != GLOBAL_OPTIONS_TYPE)
2619
369
      {
2620
 
        //here is EXPLAIN of subselect or derived table
2621
 
        if (join->change_result(result))
2622
 
        {
2623
 
          return(true);
2624
 
        }
 
370
        //here is EXPLAIN of subselect or derived table
 
371
        if (join->change_result(result))
 
372
        {
 
373
          return(true);
 
374
        }
2625
375
      }
2626
376
      else
2627
377
      {
2628
378
        if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2629
379
                               conds, og_num, order, group, having, select_lex, unit)))
2630
 
        {
2631
 
          goto err;
2632
 
        }
 
380
        {
 
381
          goto err;
 
382
        }
2633
383
      }
2634
384
    }
2635
385
    free_join= 0;
2638
388
  else
2639
389
  {
2640
390
    if (!(join= new JOIN(session, fields, select_options, result)))
2641
 
        return(true);
 
391
      return(true);
2642
392
    session->set_proc_info("init");
2643
393
    session->used_tables=0;                         // Updated by setup_fields
2644
394
    if ((err= join->prepare(rref_pointer_array, tables, wild_num,
2687
437
  return(join->error);
2688
438
}
2689
439
 
2690
 
 
2691
440
int subq_sj_candidate_cmp(Item_in_subselect* const *el1,
2692
441
                          Item_in_subselect* const *el2)
2693
442
{
2695
444
         ( ((*el1)->sj_convert_priority == (*el2)->sj_convert_priority)? 0 : -1);
2696
445
}
2697
446
 
2698
 
 
2699
 
inline Item * and_items(Item* cond, Item *item)
 
447
inline Item *and_items(Item* cond, Item *item)
2700
448
{
2701
449
  return (cond? (new Item_cond_and(cond, item)) : item);
2702
450
}
2703
451
 
2704
 
 
2705
452
static TableList *alloc_join_nest(Session *session)
2706
453
{
2707
454
  TableList *tbl;
2713
460
  return tbl;
2714
461
}
2715
462
 
2716
 
 
2717
463
void fix_list_after_tbl_changes(Select_Lex *new_parent, List<TableList> *tlist)
2718
464
{
2719
465
  List_iterator<TableList> it(*tlist);
2727
473
  }
2728
474
}
2729
475
 
2730
 
 
2731
476
/*
2732
477
  Convert a subquery predicate into a TableList semi-join nest
2733
478
 
2755
500
    false  OK
2756
501
    true   Out of memory error
2757
502
*/
2758
 
 
2759
503
bool convert_subq_to_sj(JOIN *parent_join, Item_in_subselect *subq_pred)
2760
504
{
2761
505
  Select_Lex *parent_lex= parent_join->select_lex;
3025
769
  return(false);
3026
770
}
3027
771
 
3028
 
 
3029
 
/*
3030
 
  Convert candidate subquery predicates to semi-joins
3031
 
 
3032
 
  SYNOPSIS
3033
 
    JOIN::flatten_subqueries()
3034
 
 
3035
 
  DESCRIPTION
3036
 
    Convert candidate subquery predicates to semi-joins.
3037
 
 
3038
 
  RETURN
3039
 
    false  OK
3040
 
    true   Error
3041
 
*/
3042
 
 
3043
 
bool JOIN::flatten_subqueries()
3044
 
{
3045
 
  Item_in_subselect **in_subq;
3046
 
  Item_in_subselect **in_subq_end;
3047
 
 
3048
 
  if (sj_subselects.elements() == 0)
3049
 
    return(false);
3050
 
 
3051
 
  /* 1. Fix children subqueries */
3052
 
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
3053
 
       in_subq != in_subq_end; in_subq++)
3054
 
  {
3055
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3056
 
    child_join->outer_tables = child_join->tables;
3057
 
    if (child_join->flatten_subqueries())
3058
 
      return(true);
3059
 
    (*in_subq)->sj_convert_priority=
3060
 
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
3061
 
  }
3062
 
  
3063
 
  bool outer_join_disable_semi_join= false;
3064
 
  /*
3065
 
   * Temporary measure: disable semi-joins when they are together with outer
3066
 
   * joins.
3067
 
   *
3068
 
   * @see LP Bug #314911
3069
 
   */
3070
 
  for (TableList *tbl= select_lex->leaf_tables; tbl; tbl=tbl->next_leaf)
3071
 
  {
3072
 
    TableList *embedding= tbl->embedding;
3073
 
    if (tbl->on_expr || (tbl->embedding && !(embedding->sj_on_expr && 
3074
 
                                            !embedding->embedding)))
3075
 
    {
3076
 
      in_subq= sj_subselects.front();
3077
 
      outer_join_disable_semi_join= true;
3078
 
    }
3079
 
  }
3080
 
 
3081
 
  if (! outer_join_disable_semi_join)
3082
 
  {
3083
 
    /*
3084
 
      2. Pick which subqueries to convert:
3085
 
        sort the subquery array
3086
 
        - prefer correlated subqueries over uncorrelated;
3087
 
        - prefer subqueries that have greater number of outer tables;
3088
 
    */
3089
 
    sj_subselects.sort(subq_sj_candidate_cmp);
3090
 
    // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
3091
 
    /* Replace all subqueries to be flattened with Item_int(1) */
3092
 
    for (in_subq= sj_subselects.front();
3093
 
        in_subq != in_subq_end &&
3094
 
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3095
 
        in_subq++)
3096
 
    {
3097
 
      if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
3098
 
        return(true);
3099
 
    }
3100
 
 
3101
 
    for (in_subq= sj_subselects.front();
3102
 
        in_subq != in_subq_end &&
3103
 
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
3104
 
        in_subq++)
3105
 
    {
3106
 
      if (convert_subq_to_sj(this, *in_subq))
3107
 
        return(true);
3108
 
    }
3109
 
  }
3110
 
 
3111
 
  /* 3. Finalize those we didn't convert */
3112
 
  for (; in_subq!= in_subq_end; in_subq++)
3113
 
  {
3114
 
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
3115
 
    Item_subselect::trans_res res;
3116
 
    (*in_subq)->changed= 0;
3117
 
    (*in_subq)->fixed= 0;
3118
 
    res= (*in_subq)->select_transformer(child_join);
3119
 
    if (res == Item_subselect::RES_ERROR)
3120
 
      return(true);
3121
 
 
3122
 
    (*in_subq)->changed= 1;
3123
 
    (*in_subq)->fixed= 1;
3124
 
 
3125
 
    Item *substitute= (*in_subq)->substitution;
3126
 
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
3127
 
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
3128
 
      return(true);
3129
 
 
3130
 
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
3131
 
    //  return(true);
3132
 
  }
3133
 
  sj_subselects.clear();
3134
 
  return(false);
3135
 
}
3136
 
 
3137
 
/**
3138
 
  Setup for execution all subqueries of a query, for which the optimizer
3139
 
  chose hash semi-join.
3140
 
 
3141
 
  @details Iterate over all subqueries of the query, and if they are under an
3142
 
  IN predicate, and the optimizer chose to compute it via hash semi-join:
3143
 
  - try to initialize all data structures needed for the materialized execution
3144
 
    of the IN predicate,
3145
 
  - if this fails, then perform the IN=>EXISTS transformation which was
3146
 
    previously blocked during JOIN::prepare.
3147
 
 
3148
 
  This method is part of the "code generation" query processing phase.
3149
 
 
3150
 
  This phase must be called after substitute_for_best_equal_field() because
3151
 
  that function may replace items with other items from a multiple equality,
3152
 
  and we need to reference the correct items in the index access method of the
3153
 
  IN predicate.
3154
 
 
3155
 
  @return Operation status
3156
 
  @retval false     success.
3157
 
  @retval true      error occurred.
3158
 
*/
3159
 
bool JOIN::setup_subquery_materialization()
3160
 
{
3161
 
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
3162
 
       un= un->next_unit())
3163
 
  {
3164
 
    for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
3165
 
    {
3166
 
      Item_subselect *subquery_predicate= sl->master_unit()->item;
3167
 
      if (subquery_predicate &&
3168
 
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
3169
 
      {
3170
 
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
3171
 
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
3172
 
            in_subs->setup_engine())
3173
 
          return true;
3174
 
      }
3175
 
    }
3176
 
  }
3177
 
  return false;
3178
 
}
3179
 
 
3180
 
 
3181
772
/*
3182
773
  Check if table's KEYUSE elements have an eq_ref(outer_tables) candidate
3183
774
 
3198
789
    true  - There exists an eq_ref(outer-tables) candidate
3199
790
    false - Otherwise
3200
791
*/
3201
 
 
3202
792
bool find_eq_ref_candidate(Table *table, table_map sj_inner_tables)
3203
793
{
3204
794
  KEYUSE *keyuse= table->reginfo.join_tab->keyuse;
3244
834
  return false;
3245
835
}
3246
836
 
3247
 
 
3248
 
/*
3249
 
  Pull tables out of semi-join nests, if possible
3250
 
 
3251
 
  SYNOPSIS
3252
 
    pull_out_semijoin_tables()
3253
 
      join  The join where to do the semi-join flattening
3254
 
 
3255
 
  DESCRIPTION
3256
 
    Try to pull tables out of semi-join nests.
3257
 
 
3258
 
    PRECONDITIONS
3259
 
    When this function is called, the join may have several semi-join nests
3260
 
    (possibly within different semi-join nests), but it is guaranteed that
3261
 
    one semi-join nest does not contain another.
3262
 
 
3263
 
    ACTION
3264
 
    A table can be pulled out of the semi-join nest if
3265
 
     - It is a constant table
3266
 
     - It is accessed
3267
 
 
3268
 
    POSTCONDITIONS
3269
 
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
3270
 
       tables)
3271
 
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
3272
 
     * Semi-join nests TableList::sj_inner_tables
3273
 
 
3274
 
    This operation is (and should be) performed at each PS execution since
3275
 
    tables may become/cease to be constant across PS reexecutions.
3276
 
 
3277
 
  RETURN
3278
 
    0 - OK
3279
 
    1 - Out of memory error
3280
 
*/
3281
 
 
3282
 
int pull_out_semijoin_tables(JOIN *join)
3283
 
{
3284
 
  TableList *sj_nest;
3285
 
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
3286
 
 
3287
 
  /* Try pulling out of the each of the semi-joins */
3288
 
  while ((sj_nest= sj_list_it++))
3289
 
  {
3290
 
    /* Action #1: Mark the constant tables to be pulled out */
3291
 
    table_map pulled_tables= 0;
3292
 
 
3293
 
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
3294
 
    TableList *tbl;
3295
 
    while ((tbl= child_li++))
3296
 
    {
3297
 
      if (tbl->table)
3298
 
      {
3299
 
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
3300
 
        if (tbl->table->map & join->const_table_map)
3301
 
        {
3302
 
          pulled_tables |= tbl->table->map;
3303
 
        }
3304
 
      }
3305
 
    }
3306
 
 
3307
 
    /*
3308
 
      Action #2: Find which tables we can pull out based on
3309
 
      update_ref_and_keys() data. Note that pulling one table out can allow
3310
 
      us to pull out some other tables too.
3311
 
    */
3312
 
    bool pulled_a_table;
3313
 
    do
3314
 
    {
3315
 
      pulled_a_table= false;
3316
 
      child_li.rewind();
3317
 
      while ((tbl= child_li++))
3318
 
      {
3319
 
        if (tbl->table && !(pulled_tables & tbl->table->map))
3320
 
        {
3321
 
          if (find_eq_ref_candidate(tbl->table,
3322
 
                                    sj_nest->nested_join->used_tables &
3323
 
                                    ~pulled_tables))
3324
 
          {
3325
 
            pulled_a_table= true;
3326
 
            pulled_tables |= tbl->table->map;
3327
 
          }
3328
 
        }
3329
 
      }
3330
 
    } while (pulled_a_table);
3331
 
 
3332
 
    child_li.rewind();
3333
 
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
3334
 
    {
3335
 
      (sj_nest)->sj_inner_tables= 0;
3336
 
      while ((tbl= child_li++))
3337
 
      {
3338
 
        if (tbl->table)
3339
 
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3340
 
      }
3341
 
    }
3342
 
    else
3343
 
    {
3344
 
      /* Record the bitmap of inner tables, mark the inner tables */
3345
 
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
3346
 
                             ~pulled_tables;
3347
 
      (sj_nest)->sj_inner_tables= inner_tables;
3348
 
      while ((tbl= child_li++))
3349
 
      {
3350
 
        if (tbl->table)
3351
 
        {
3352
 
          if (inner_tables & tbl->table->map)
3353
 
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
3354
 
          else
3355
 
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
3356
 
        }
3357
 
      }
3358
 
    }
3359
 
  }
3360
 
  return(0);
3361
 
}
3362
 
 
3363
837
/*****************************************************************************
3364
838
  Create JOIN_TABS, make a guess about the table types,
3365
839
  Approximate how many records will be used in each table
3366
840
*****************************************************************************/
3367
 
 
3368
 
 
3369
 
static ha_rows get_quick_record_count(Session *session, SQL_SELECT *select,
3370
 
                                      Table *table,
3371
 
                                      const key_map *keys,ha_rows limit)
 
841
ha_rows get_quick_record_count(Session *session, SQL_SELECT *select, Table *table, const key_map *keys,ha_rows limit)
3372
842
{
3373
843
  int error;
3374
844
  if (check_stack_overrun(session, STACK_MIN_SIZE, NULL))
3389
859
  return(HA_POS_ERROR);                 /* This shouldn't happend */
3390
860
}
3391
861
 
3392
 
/*
3393
 
   This structure is used to collect info on potentially sargable
3394
 
   predicates in order to check whether they become sargable after
3395
 
   reading const tables.
3396
 
   We form a bitmap of indexes that can be used for sargable predicates.
3397
 
   Only such indexes are involved in range analysis.
3398
 
*/
3399
 
typedef struct st_sargable_param
3400
 
{
3401
 
  Field *field;              /* field against which to check sargability */
3402
 
  Item **arg_value;          /* values of potential keys for lookups     */
3403
 
  uint32_t num_values;           /* number of values in the above array      */
3404
 
} SARGABLE_PARAM;
3405
 
 
3406
 
/**
3407
 
  Calculate the best possible join and initialize the join structure.
3408
 
 
3409
 
  @retval
3410
 
    0   ok
3411
 
  @retval
3412
 
    1   Fatal error
3413
 
*/
3414
 
 
3415
 
static bool
3416
 
make_join_statistics(JOIN *join, TableList *tables, COND *conds,
3417
 
                     DYNAMIC_ARRAY *keyuse_array)
3418
 
{
3419
 
  int error;
3420
 
  Table *table;
3421
 
  uint32_t i,table_count,const_count,key;
3422
 
  table_map found_const_table_map, all_table_map, found_ref, refs;
3423
 
  key_map const_ref, eq_part;
3424
 
  Table **table_vector;
3425
 
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
3426
 
  KEYUSE *keyuse,*start_keyuse;
3427
 
  table_map outer_join=0;
3428
 
  SARGABLE_PARAM *sargables= 0;
3429
 
  JOIN_TAB *stat_vector[MAX_TABLES+1];
3430
 
 
3431
 
  table_count=join->tables;
3432
 
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
3433
 
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
3434
 
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
3435
 
  if (!stat || !stat_ref || !table_vector)
3436
 
    return(1);                          // Eom /* purecov: inspected */
3437
 
 
3438
 
  join->best_ref=stat_vector;
3439
 
 
3440
 
  stat_end=stat+table_count;
3441
 
  found_const_table_map= all_table_map=0;
3442
 
  const_count=0;
3443
 
 
3444
 
  for (s= stat, i= 0;
3445
 
       tables;
3446
 
       s++, tables= tables->next_leaf, i++)
3447
 
  {
3448
 
    TableList *embedding= tables->embedding;
3449
 
    stat_vector[i]=s;
3450
 
    s->keys.reset();
3451
 
    s->const_keys.reset();
3452
 
    s->checked_keys.reset();
3453
 
    s->needed_reg.reset();
3454
 
    table_vector[i]=s->table=table=tables->table;
3455
 
    table->pos_in_table_list= tables;
3456
 
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
3457
 
    if(error)
3458
 
    {
3459
 
        table->file->print_error(error, MYF(0));
3460
 
        return(1);
3461
 
    }
3462
 
    table->quick_keys.reset();
3463
 
    table->reginfo.join_tab=s;
3464
 
    table->reginfo.not_exists_optimize=0;
3465
 
    memset(table->const_key_parts, 0,
3466
 
           sizeof(key_part_map)*table->s->keys);
3467
 
    all_table_map|= table->map;
3468
 
    s->join=join;
3469
 
    s->info=0;                                  // For describe
3470
 
 
3471
 
    s->dependent= tables->dep_tables;
3472
 
    s->key_dependent= 0;
3473
 
    if (tables->schema_table)
3474
 
      table->file->stats.records= 2;
3475
 
    table->quick_condition_rows= table->file->stats.records;
3476
 
 
3477
 
    s->on_expr_ref= &tables->on_expr;
3478
 
    if (*s->on_expr_ref)
3479
 
    {
3480
 
      /* s is the only inner table of an outer join */
3481
 
      if (!table->file->stats.records && !embedding)
3482
 
      {                                         // Empty table
3483
 
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
3484
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3485
 
        continue;
3486
 
      }
3487
 
      outer_join|= table->map;
3488
 
      s->embedding_map= 0;
3489
 
      for (;embedding; embedding= embedding->embedding)
3490
 
        s->embedding_map|= embedding->nested_join->nj_map;
3491
 
      continue;
3492
 
    }
3493
 
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
3494
 
    {
3495
 
      /* s belongs to a nested join, maybe to several embedded joins */
3496
 
      s->embedding_map= 0;
3497
 
      do
3498
 
      {
3499
 
        nested_join_st *nested_join= embedding->nested_join;
3500
 
        s->embedding_map|=nested_join->nj_map;
3501
 
        s->dependent|= embedding->dep_tables;
3502
 
        embedding= embedding->embedding;
3503
 
        outer_join|= nested_join->used_tables;
3504
 
      }
3505
 
      while (embedding);
3506
 
      continue;
3507
 
    }
3508
 
    if ((table->file->stats.records <= 1) &&
3509
 
        !s->dependent &&
3510
 
        (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && !join->no_const_tables)
3511
 
    {
3512
 
      set_position(join,const_count++,s,(KEYUSE*) 0);
3513
 
    }
3514
 
  }
3515
 
  stat_vector[i]=0;
3516
 
  join->outer_join=outer_join;
3517
 
 
3518
 
  if (join->outer_join)
3519
 
  {
3520
 
    /*
3521
 
       Build transitive closure for relation 'to be dependent on'.
3522
 
       This will speed up the plan search for many cases with outer joins,
3523
 
       as well as allow us to catch illegal cross references/
3524
 
       Warshall's algorithm is used to build the transitive closure.
3525
 
       As we use bitmaps to represent the relation the complexity
3526
 
       of the algorithm is O((number of tables)^2).
3527
 
    */
3528
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3529
 
    {
3530
 
      for (uint32_t j= 0 ; j < table_count ; j++)
3531
 
      {
3532
 
        table= stat[j].table;
3533
 
        if (s->dependent & table->map)
3534
 
          s->dependent |= table->reginfo.join_tab->dependent;
3535
 
      }
3536
 
      if (s->dependent)
3537
 
        s->table->maybe_null= 1;
3538
 
    }
3539
 
    /* Catch illegal cross references for outer joins */
3540
 
    for (i= 0, s= stat ; i < table_count ; i++, s++)
3541
 
    {
3542
 
      if (s->dependent & s->table->map)
3543
 
      {
3544
 
        join->tables=0;                 // Don't use join->table
3545
 
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
3546
 
        return(1);
3547
 
      }
3548
 
      s->key_dependent= s->dependent;
3549
 
    }
3550
 
  }
3551
 
 
3552
 
  if (conds || outer_join)
3553
 
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
3554
 
                            conds, join->cond_equal,
3555
 
                            ~outer_join, join->select_lex, &sargables))
3556
 
      return(1);
3557
 
 
3558
 
  /* Read tables with 0 or 1 rows (system tables) */
3559
 
  join->const_table_map= 0;
3560
 
 
3561
 
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
3562
 
       p_pos < p_end ;
3563
 
       p_pos++)
3564
 
  {
3565
 
    int tmp;
3566
 
    s= p_pos->table;
3567
 
    s->type=JT_SYSTEM;
3568
 
    join->const_table_map|=s->table->map;
3569
 
    if ((tmp=join_read_const_table(s, p_pos)))
3570
 
    {
3571
 
      if (tmp > 0)
3572
 
        return(1);                      // Fatal error
3573
 
    }
3574
 
    else
3575
 
      found_const_table_map|= s->table->map;
3576
 
  }
3577
 
 
3578
 
  /* loop until no more const tables are found */
3579
 
  int ref_changed;
3580
 
  do
3581
 
  {
3582
 
  more_const_tables_found:
3583
 
    ref_changed = 0;
3584
 
    found_ref=0;
3585
 
 
3586
 
    /*
3587
 
      We only have to loop from stat_vector + const_count as
3588
 
      set_position() will move all const_tables first in stat_vector
3589
 
    */
3590
 
 
3591
 
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
3592
 
    {
3593
 
      table=s->table;
3594
 
 
3595
 
      /*
3596
 
        If equi-join condition by a key is null rejecting and after a
3597
 
        substitution of a const table the key value happens to be null
3598
 
        then we can state that there are no matches for this equi-join.
3599
 
      */
3600
 
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
3601
 
      {
3602
 
        /*
3603
 
          When performing an outer join operation if there are no matching rows
3604
 
          for the single row of the outer table all the inner tables are to be
3605
 
          null complemented and thus considered as constant tables.
3606
 
          Here we apply this consideration to the case of outer join operations
3607
 
          with a single inner table only because the case with nested tables
3608
 
          would require a more thorough analysis.
3609
 
          TODO. Apply single row substitution to null complemented inner tables
3610
 
          for nested outer join operations.
3611
 
        */
3612
 
        while (keyuse->table == table)
3613
 
        {
3614
 
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
3615
 
              keyuse->val->is_null() && keyuse->null_rejecting)
3616
 
          {
3617
 
            s->type= JT_CONST;
3618
 
            table->mark_as_null_row();
3619
 
            found_const_table_map|= table->map;
3620
 
            join->const_table_map|= table->map;
3621
 
            set_position(join,const_count++,s,(KEYUSE*) 0);
3622
 
            goto more_const_tables_found;
3623
 
           }
3624
 
          keyuse++;
3625
 
        }
3626
 
      }
3627
 
 
3628
 
      if (s->dependent)                         // If dependent on some table
3629
 
      {
3630
 
        // All dep. must be constants
3631
 
        if (s->dependent & ~(found_const_table_map))
3632
 
          continue;
3633
 
        if (table->file->stats.records <= 1L &&
3634
 
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
3635
 
            !table->pos_in_table_list->embedding)
3636
 
        {                                       // system table
3637
 
          int tmp= 0;
3638
 
          s->type=JT_SYSTEM;
3639
 
          join->const_table_map|=table->map;
3640
 
          set_position(join,const_count++,s,(KEYUSE*) 0);
3641
 
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
3642
 
          {
3643
 
            if (tmp > 0)
3644
 
              return(1);                        // Fatal error
3645
 
          }
3646
 
          else
3647
 
            found_const_table_map|= table->map;
3648
 
          continue;
3649
 
        }
3650
 
      }
3651
 
      /* check if table can be read by key or table only uses const refs */
3652
 
      if ((keyuse=s->keyuse))
3653
 
      {
3654
 
        s->type= JT_REF;
3655
 
        while (keyuse->table == table)
3656
 
        {
3657
 
          start_keyuse=keyuse;
3658
 
          key=keyuse->key;
3659
 
          s->keys.set(key);               // QQ: remove this ?
3660
 
 
3661
 
          refs=0;
3662
 
          const_ref.reset();
3663
 
          eq_part.reset();
3664
 
          do
3665
 
          {
3666
 
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
3667
 
            {
3668
 
              if (!((~found_const_table_map) & keyuse->used_tables))
3669
 
                const_ref.set(keyuse->keypart);
3670
 
              else
3671
 
                refs|=keyuse->used_tables;
3672
 
              eq_part.set(keyuse->keypart);
3673
 
            }
3674
 
            keyuse++;
3675
 
          } while (keyuse->table == table && keyuse->key == key);
3676
 
 
3677
 
          if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
3678
 
              !table->pos_in_table_list->embedding)
3679
 
          {
3680
 
            if ((table->key_info[key].flags & (HA_NOSAME))
3681
 
                 == HA_NOSAME)
3682
 
            {
3683
 
              if (const_ref == eq_part)
3684
 
              {                                 // Found everything for ref.
3685
 
                int tmp;
3686
 
                ref_changed = 1;
3687
 
                s->type= JT_CONST;
3688
 
                join->const_table_map|= table->map;
3689
 
                set_position(join,const_count++,s,start_keyuse);
3690
 
                if (create_ref_for_key(join, s, start_keyuse,
3691
 
                                       found_const_table_map))
3692
 
                  return(1);
3693
 
                if ((tmp=join_read_const_table(s,
3694
 
                                               join->positions+const_count-1)))
3695
 
                {
3696
 
                  if (tmp > 0)
3697
 
                    return(1);                  // Fatal error
3698
 
                }
3699
 
                else
3700
 
                  found_const_table_map|= table->map;
3701
 
                break;
3702
 
              }
3703
 
              else
3704
 
                found_ref|= refs;      // Table is const if all refs are const
3705
 
            }
3706
 
            else if (const_ref == eq_part)
3707
 
              s->const_keys.set(key);
3708
 
          }
3709
 
        }
3710
 
      }
3711
 
    }
3712
 
  } while (join->const_table_map & found_ref && ref_changed);
3713
 
 
3714
 
  /*
3715
 
    Update info on indexes that can be used for search lookups as
3716
 
    reading const tables may has added new sargable predicates.
3717
 
  */
3718
 
  if (const_count && sargables)
3719
 
  {
3720
 
    for( ; sargables->field ; sargables++)
3721
 
    {
3722
 
      Field *field= sargables->field;
3723
 
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
3724
 
      key_map possible_keys= field->key_start;
3725
 
      possible_keys&= field->table->keys_in_use_for_query;
3726
 
      bool is_const= 1;
3727
 
      for (uint32_t j=0; j < sargables->num_values; j++)
3728
 
        is_const&= sargables->arg_value[j]->const_item();
3729
 
      if (is_const)
3730
 
        join_tab[0].const_keys|= possible_keys;
3731
 
    }
3732
 
  }
3733
 
 
3734
 
  if (pull_out_semijoin_tables(join))
3735
 
    return(true);
3736
 
 
3737
 
  /* Calc how many (possible) matched records in each table */
3738
 
 
3739
 
  for (s=stat ; s < stat_end ; s++)
3740
 
  {
3741
 
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
3742
 
    {
3743
 
      /* Only one matching row */
3744
 
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
3745
 
      continue;
3746
 
    }
3747
 
    /* Approximate found rows and time to read them */
3748
 
    s->found_records=s->records=s->table->file->stats.records;
3749
 
    s->read_time=(ha_rows) s->table->file->scan_time();
3750
 
 
3751
 
    /*
3752
 
      Set a max range of how many seeks we can expect when using keys
3753
 
      This is can't be to high as otherwise we are likely to use
3754
 
      table scan.
3755
 
    */
3756
 
    s->worst_seeks= cmin((double) s->found_records / 10,
3757
 
                        (double) s->read_time*3);
3758
 
    if (s->worst_seeks < 2.0)                   // Fix for small tables
3759
 
      s->worst_seeks=2.0;
3760
 
 
3761
 
    /*
3762
 
      Add to stat->const_keys those indexes for which all group fields or
3763
 
      all select distinct fields participate in one index.
3764
 
    */
3765
 
    add_group_and_distinct_keys(join, s);
3766
 
 
3767
 
    if (s->const_keys.any() &&
3768
 
        !s->table->pos_in_table_list->embedding)
3769
 
    {
3770
 
      ha_rows records;
3771
 
      SQL_SELECT *select;
3772
 
      select= make_select(s->table, found_const_table_map,
3773
 
                          found_const_table_map,
3774
 
                          *s->on_expr_ref ? *s->on_expr_ref : conds,
3775
 
                          1, &error);
3776
 
      if (!select)
3777
 
        return(1);
3778
 
      records= get_quick_record_count(join->session, select, s->table,
3779
 
                                      &s->const_keys, join->row_limit);
3780
 
      s->quick=select->quick;
3781
 
      s->needed_reg=select->needed_reg;
3782
 
      select->quick=0;
3783
 
      if (records == 0 && s->table->reginfo.impossible_range)
3784
 
      {
3785
 
        /*
3786
 
          Impossible WHERE or ON expression
3787
 
          In case of ON, we mark that the we match one empty NULL row.
3788
 
          In case of WHERE, don't set found_const_table_map to get the
3789
 
          caller to abort with a zero row result.
3790
 
        */
3791
 
        join->const_table_map|= s->table->map;
3792
 
        set_position(join,const_count++,s,(KEYUSE*) 0);
3793
 
        s->type= JT_CONST;
3794
 
        if (*s->on_expr_ref)
3795
 
        {
3796
 
          /* Generate empty row */
3797
 
          s->info= "Impossible ON condition";
3798
 
          found_const_table_map|= s->table->map;
3799
 
          s->type= JT_CONST;
3800
 
          s->table->mark_as_null_row();         // All fields are NULL
3801
 
        }
3802
 
      }
3803
 
      if (records != HA_POS_ERROR)
3804
 
      {
3805
 
        s->found_records=records;
3806
 
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
3807
 
      }
3808
 
      delete select;
3809
 
    }
3810
 
  }
3811
 
 
3812
 
  join->join_tab=stat;
3813
 
  join->map2table=stat_ref;
3814
 
  join->table= join->all_tables=table_vector;
3815
 
  join->const_tables=const_count;
3816
 
  join->found_const_table_map=found_const_table_map;
3817
 
 
3818
 
  /* Find an optimal join order of the non-constant tables. */
3819
 
  if (join->const_tables != join->tables)
3820
 
  {
3821
 
    optimize_keyuse(join, keyuse_array);
3822
 
    if (choose_plan(join, all_table_map & ~join->const_table_map))
3823
 
      return(true);
3824
 
  }
3825
 
  else
3826
 
  {
3827
 
    memcpy(join->best_positions, join->positions,
3828
 
           sizeof(POSITION)*join->const_tables);
3829
 
    join->best_read=1.0;
3830
 
  }
3831
 
  /* Generate an execution plan from the found optimal join order. */
3832
 
  return(join->session->killed || get_best_combination(join));
3833
 
}
3834
 
 
3835
 
 
3836
862
/*****************************************************************************
3837
863
  Check with keys are used and with tables references with tables
3838
864
  Updates in stat:
3841
867
          keyuse     Pointer to possible keys
3842
868
*****************************************************************************/
3843
869
 
3844
 
/// Used when finding key fields
3845
 
typedef struct key_field_t {
3846
 
  Field         *field;
3847
 
  Item          *val;                   ///< May be empty if diff constant
3848
 
  uint          level;
3849
 
  uint          optimize; // KEY_OPTIMIZE_*
3850
 
  bool          eq_func;
3851
 
  /**
3852
 
    If true, the condition this struct represents will not be satisfied
3853
 
    when val IS NULL.
3854
 
  */
3855
 
  bool          null_rejecting;
3856
 
  bool          *cond_guard; /* See KEYUSE::cond_guard */
3857
 
  uint32_t          sj_pred_no; /* See KEYUSE::sj_pred_no */
3858
 
} KEY_FIELD;
3859
 
 
3860
870
/**
3861
871
  Merge new key definitions to old ones, remove those not used in both.
3862
872
 
3880
890
    The result of this is that we're missing some 'ref' accesses.
3881
891
    OptimizerTeam: Fix this
3882
892
*/
3883
 
 
3884
 
static KEY_FIELD *
3885
 
merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end,
3886
 
                 uint32_t and_level)
 
893
static KEY_FIELD *merge_key_fields(KEY_FIELD *start,KEY_FIELD *new_fields,KEY_FIELD *end, uint32_t and_level)
3887
894
{
3888
895
  if (start == new_fields)
3889
896
    return start;                               // Impossible or
3911
918
          The result of this is that we're missing some 'ref' accesses.
3912
919
          TODO: OptimizerTeam: Fix this
3913
920
        */
3914
 
        if (!new_fields->val->const_item())
3915
 
        {
3916
 
          /*
3917
 
            If the value matches, we can use the key reference.
3918
 
            If not, we keep it until we have examined all new values
3919
 
          */
3920
 
          if (old->val->eq(new_fields->val, old->field->binary()))
3921
 
          {
3922
 
            old->level= and_level;
3923
 
            old->optimize= ((old->optimize & new_fields->optimize &
3924
 
                             KEY_OPTIMIZE_EXISTS) |
3925
 
                            ((old->optimize | new_fields->optimize) &
3926
 
                             KEY_OPTIMIZE_REF_OR_NULL));
3927
 
            old->null_rejecting= (old->null_rejecting &&
3928
 
                                  new_fields->null_rejecting);
3929
 
          }
3930
 
        }
3931
 
        else if (old->eq_func && new_fields->eq_func &&
3932
 
                 old->val->eq_by_collation(new_fields->val,
3933
 
                                           old->field->binary(),
3934
 
                                           old->field->charset()))
 
921
        if (!new_fields->val->const_item())
 
922
        {
 
923
          /*
 
924
            If the value matches, we can use the key reference.
 
925
            If not, we keep it until we have examined all new values
 
926
          */
 
927
          if (old->val->eq(new_fields->val, old->field->binary()))
 
928
          {
 
929
            old->level= and_level;
 
930
            old->optimize= ((old->optimize & new_fields->optimize &
 
931
                KEY_OPTIMIZE_EXISTS) |
 
932
                ((old->optimize | new_fields->optimize) &
 
933
                KEY_OPTIMIZE_REF_OR_NULL));
 
934
                  old->null_rejecting= (old->null_rejecting &&
 
935
                                        new_fields->null_rejecting);
 
936
          }
 
937
        }
 
938
        else if (old->eq_func && new_fields->eq_func &&
 
939
                      old->val->eq_by_collation(new_fields->val,
 
940
                                                old->field->binary(),
 
941
                                                old->field->charset()))
3935
942
 
3936
 
        {
3937
 
          old->level= and_level;
3938
 
          old->optimize= ((old->optimize & new_fields->optimize &
3939
 
                           KEY_OPTIMIZE_EXISTS) |
3940
 
                          ((old->optimize | new_fields->optimize) &
3941
 
                           KEY_OPTIMIZE_REF_OR_NULL));
3942
 
          old->null_rejecting= (old->null_rejecting &&
3943
 
                                new_fields->null_rejecting);
3944
 
        }
3945
 
        else if (old->eq_func && new_fields->eq_func &&
3946
 
                 ((old->val->const_item() && old->val->is_null()) ||
3947
 
                  new_fields->val->is_null()))
3948
 
        {
3949
 
          /* field = expression OR field IS NULL */
3950
 
          old->level= and_level;
3951
 
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
3952
 
          /*
3953
 
            Remember the NOT NULL value unless the value does not depend
3954
 
            on other tables.
 
943
        {
 
944
          old->level= and_level;
 
945
          old->optimize= ((old->optimize & new_fields->optimize &
 
946
              KEY_OPTIMIZE_EXISTS) |
 
947
              ((old->optimize | new_fields->optimize) &
 
948
              KEY_OPTIMIZE_REF_OR_NULL));
 
949
                old->null_rejecting= (old->null_rejecting &&
 
950
                                      new_fields->null_rejecting);
 
951
        }
 
952
        else if (old->eq_func && new_fields->eq_func &&
 
953
          ((old->val->const_item() && old->val->is_null()) ||
 
954
                        new_fields->val->is_null()))
 
955
        {
 
956
          /* field = expression OR field IS NULL */
 
957
          old->level= and_level;
 
958
          old->optimize= KEY_OPTIMIZE_REF_OR_NULL;
 
959
          /*
 
960
                  Remember the NOT NULL value unless the value does not depend
 
961
                  on other tables.
 
962
                */
 
963
          if (!old->val->used_tables() && old->val->is_null())
 
964
            old->val= new_fields->val;
 
965
                /* The referred expression can be NULL: */
 
966
                old->null_rejecting= 0;
 
967
        }
 
968
        else
 
969
        {
 
970
          /*
 
971
            We are comparing two different const.  In this case we can't
 
972
            use a key-lookup on this so it's better to remove the value
 
973
            and let the range optimzier handle it
3955
974
          */
3956
 
          if (!old->val->used_tables() && old->val->is_null())
3957
 
            old->val= new_fields->val;
3958
 
          /* The referred expression can be NULL: */
3959
 
          old->null_rejecting= 0;
3960
 
        }
3961
 
        else
3962
 
        {
3963
 
          /*
3964
 
            We are comparing two different const.  In this case we can't
3965
 
            use a key-lookup on this so it's better to remove the value
3966
 
            and let the range optimzier handle it
3967
 
          */
3968
 
          if (old == --first_free)              // If last item
3969
 
            break;
3970
 
          *old= *first_free;                    // Remove old value
3971
 
          old--;                                // Retry this value
3972
 
        }
 
975
          if (old == --first_free)              // If last item
 
976
            break;
 
977
          *old= *first_free;                    // Remove old value
 
978
          old--;                                // Retry this value
 
979
        }
3973
980
      }
3974
981
    }
3975
982
  }
3988
995
  return first_free;
3989
996
}
3990
997
 
3991
 
 
3992
998
/**
3993
999
  Add a possible key to array of possible keys if it's usable as a key
3994
1000
 
4008
1014
  @returns
4009
1015
    *key_fields is incremented if we stored a key in the array
4010
1016
*/
4011
 
 
4012
 
static void
4013
 
add_key_field(KEY_FIELD **key_fields,uint32_t and_level, Item_func *cond,
4014
 
              Field *field, bool eq_func, Item **value, uint32_t num_values,
4015
 
              table_map usable_tables, SARGABLE_PARAM **sargables)
 
1017
static void add_key_field(KEY_FIELD **key_fields,
 
1018
                          uint32_t and_level,
 
1019
                          Item_func *cond,
 
1020
                          Field *field,
 
1021
                          bool eq_func,
 
1022
                          Item **value,
 
1023
                          uint32_t num_values,
 
1024
                          table_map usable_tables,
 
1025
                          SARGABLE_PARAM **sargables)
4016
1026
{
4017
1027
  uint32_t exists_optimize= 0;
4018
1028
  if (!(field->flags & PART_KEY_FLAG))
4040
1050
    {
4041
1051
      if (!eq_func || (*value)->type() != Item::NULL_ITEM ||
4042
1052
          !field->table->maybe_null || field->null_ptr)
4043
 
        return;                                 // Can't use left join optimize
 
1053
        return;                                 // Can't use left join optimize
4044
1054
      exists_optimize= KEY_OPTIMIZE_EXISTS;
4045
1055
    }
4046
1056
    else
4051
1061
      stat[0].keys|= possible_keys;             // Add possible keys
4052
1062
 
4053
1063
      /*
4054
 
        Save the following cases:
4055
 
        Field op constant
4056
 
        Field LIKE constant where constant doesn't start with a wildcard
4057
 
        Field = field2 where field2 is in a different table
4058
 
        Field op formula
4059
 
        Field IS NULL
4060
 
        Field IS NOT NULL
4061
 
         Field BETWEEN ...
4062
 
         Field IN ...
 
1064
        Save the following cases:
 
1065
        Field op constant
 
1066
        Field LIKE constant where constant doesn't start with a wildcard
 
1067
        Field = field2 where field2 is in a different table
 
1068
        Field op formula
 
1069
        Field IS NULL
 
1070
        Field IS NOT NULL
 
1071
        Field BETWEEN ...
 
1072
        Field IN ...
4063
1073
      */
4064
1074
      stat[0].key_dependent|= used_tables;
4065
1075
 
4085
1095
        (*sargables)->num_values= num_values;
4086
1096
      }
4087
1097
      /*
4088
 
        We can't always use indexes when comparing a string index to a
4089
 
        number. cmp_type() is checked to allow compare of dates to numbers.
 
1098
        We can't always use indexes when comparing a string index to a
 
1099
        number. cmp_type() is checked to allow compare of dates to numbers.
4090
1100
        eq_func is NEVER true when num_values > 1
4091
1101
       */
4092
1102
      if (!eq_func)
4180
1190
  @returns
4181
1191
    *key_fields is incremented if we stored a key in the array
4182
1192
*/
4183
 
 
4184
 
static void
4185
 
add_key_equal_fields(KEY_FIELD **key_fields, uint32_t and_level,
4186
 
                     Item_func *cond, Item_field *field_item,
4187
 
                     bool eq_func, Item **val,
4188
 
                     uint32_t num_values, table_map usable_tables,
4189
 
                     SARGABLE_PARAM **sargables)
 
1193
static void add_key_equal_fields(KEY_FIELD **key_fields,
 
1194
                                 uint32_t and_level,
 
1195
                                 Item_func *cond,
 
1196
                                 Item_field *field_item,
 
1197
                                 bool eq_func,
 
1198
                                 Item **val,
 
1199
                                 uint32_t num_values,
 
1200
                                 table_map usable_tables,
 
1201
                                 SARGABLE_PARAM **sargables)
4190
1202
{
4191
1203
  Field *field= field_item->field;
4192
1204
  add_key_field(key_fields, and_level, cond, field,
4212
1224
  }
4213
1225
}
4214
1226
 
4215
 
static void
4216
 
add_key_fields(JOIN *join, KEY_FIELD **key_fields, uint32_t *and_level,
4217
 
               COND *cond, table_map usable_tables,
4218
 
               SARGABLE_PARAM **sargables)
 
1227
static void add_key_fields(JOIN *join, 
 
1228
                           KEY_FIELD **key_fields,
 
1229
                           uint32_t *and_level,
 
1230
                           COND *cond,
 
1231
                           table_map usable_tables,
 
1232
                           SARGABLE_PARAM **sargables)
4219
1233
{
4220
1234
  if (cond->type() == Item_func::COND_ITEM)
4221
1235
  {
4229
1243
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4230
1244
                       sargables);
4231
1245
      for (; org_key_fields != *key_fields ; org_key_fields++)
4232
 
        org_key_fields->level= *and_level;
 
1246
        org_key_fields->level= *and_level;
4233
1247
    }
4234
1248
    else
4235
1249
    {
4239
1253
      Item *item;
4240
1254
      while ((item=li++))
4241
1255
      {
4242
 
        KEY_FIELD *start_key_fields= *key_fields;
4243
 
        (*and_level)++;
4244
 
        add_key_fields(join, key_fields, and_level, item, usable_tables,
4245
 
                       sargables);
4246
 
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
4247
 
                                     *key_fields,++(*and_level));
 
1256
        KEY_FIELD *start_key_fields= *key_fields;
 
1257
        (*and_level)++;
 
1258
              add_key_fields(join, key_fields, and_level, item, usable_tables,
 
1259
                            sargables);
 
1260
        *key_fields=merge_key_fields(org_key_fields,start_key_fields,
 
1261
                  *key_fields,++(*and_level));
4248
1262
      }
4249
1263
    }
4250
1264
    return;
4288
1302
    Item **values;
4289
1303
    // BETWEEN, IN, NE
4290
1304
    if (cond_func->key_item()->real_item()->type() == Item::FIELD_ITEM &&
4291
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
1305
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4292
1306
    {
4293
1307
      values= cond_func->arguments()+1;
4294
1308
      if (cond_func->functype() == Item_func::NE_FUNC &&
4328
1342
                     cond_func->functype() == Item_func::EQUAL_FUNC);
4329
1343
 
4330
1344
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4331
 
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
 
1345
        !(cond_func->arguments()[0]->used_tables() & OUTER_REF_TABLE_BIT))
4332
1346
    {
4333
1347
      add_key_equal_fields(key_fields, *and_level, cond_func,
4334
1348
                        (Item_field*) (cond_func->arguments()[0])->real_item(),
4337
1351
                           sargables);
4338
1352
    }
4339
1353
    if (cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM &&
4340
 
        cond_func->functype() != Item_func::LIKE_FUNC &&
4341
 
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
 
1354
        cond_func->functype() != Item_func::LIKE_FUNC &&
 
1355
        !(cond_func->arguments()[1]->used_tables() & OUTER_REF_TABLE_BIT))
4342
1356
    {
4343
1357
      add_key_equal_fields(key_fields, *and_level, cond_func,
4344
 
                       (Item_field*) (cond_func->arguments()[1])->real_item(),
4345
 
                           equal_func,
 
1358
                       (Item_field*) (cond_func->arguments()[1])->real_item(), equal_func,
4346
1359
                           cond_func->arguments(),1,usable_tables,
4347
1360
                           sargables);
4348
1361
    }
4351
1364
  case Item_func::OPTIMIZE_NULL:
4352
1365
    /* column_name IS [NOT] NULL */
4353
1366
    if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM &&
4354
 
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
 
1367
        !(cond_func->used_tables() & OUTER_REF_TABLE_BIT))
4355
1368
    {
4356
1369
      Item *tmp=new Item_null;
4357
1370
      if (unlikely(!tmp))                       // Should never be true
4358
 
        return;
 
1371
        return;
4359
1372
      add_key_equal_fields(key_fields, *and_level, cond_func,
4360
1373
                    (Item_field*) (cond_func->arguments()[0])->real_item(),
4361
1374
                    cond_func->functype() == Item_func::ISNULL_FUNC,
4413
1426
 
4414
1427
  If field->and_level != and_level then only mark key_part as const_part.
4415
1428
*/
4416
 
 
4417
 
static uint
4418
 
max_part_bit(key_part_map bits)
 
1429
uint32_t max_part_bit(key_part_map bits)
4419
1430
{
4420
1431
  uint32_t found;
4421
1432
  for (found=0; bits & 1 ; found++,bits>>=1) ;
4422
1433
  return found;
4423
1434
}
4424
1435
 
4425
 
static void
4426
 
add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
 
1436
static void add_key_part(DYNAMIC_ARRAY *keyuse_array,KEY_FIELD *key_field)
4427
1437
{
4428
1438
  Field *field=key_field->field;
4429
1439
  Table *form= field->table;
4434
1444
    for (uint32_t key= 0 ; key < form->sizeKeys() ; key++)
4435
1445
    {
4436
1446
      if (!(form->keys_in_use_for_query.test(key)))
4437
 
        continue;
 
1447
        continue;
4438
1448
 
4439
1449
      uint32_t key_parts= (uint32_t) form->key_info[key].key_parts;
4440
1450
      for (uint32_t part=0 ; part <  key_parts ; part++)
4441
1451
      {
4442
 
        if (field->eq(form->key_info[key].key_part[part].field))
4443
 
        {
4444
 
          keyuse.table= field->table;
4445
 
          keyuse.val =  key_field->val;
4446
 
          keyuse.key =  key;
4447
 
          keyuse.keypart=part;
4448
 
          keyuse.keypart_map= (key_part_map) 1 << part;
4449
 
          keyuse.used_tables=key_field->val->used_tables();
4450
 
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
4451
 
          keyuse.null_rejecting= key_field->null_rejecting;
4452
 
          keyuse.cond_guard= key_field->cond_guard;
4453
 
          keyuse.sj_pred_no= key_field->sj_pred_no;
4454
 
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
4455
 
        }
 
1452
        if (field->eq(form->key_info[key].key_part[part].field))
 
1453
        {
 
1454
          keyuse.table= field->table;
 
1455
          keyuse.val =  key_field->val;
 
1456
          keyuse.key =  key;
 
1457
          keyuse.keypart=part;
 
1458
          keyuse.keypart_map= (key_part_map) 1 << part;
 
1459
          keyuse.used_tables=key_field->val->used_tables();
 
1460
          keyuse.optimize= key_field->optimize & KEY_OPTIMIZE_REF_OR_NULL;
 
1461
                keyuse.null_rejecting= key_field->null_rejecting;
 
1462
                keyuse.cond_guard= key_field->cond_guard;
 
1463
                keyuse.sj_pred_no= key_field->sj_pred_no;
 
1464
          insert_dynamic(keyuse_array,(unsigned char*) &keyuse);
 
1465
        }
4456
1466
      }
4457
1467
    }
4458
1468
  }
4459
1469
}
4460
1470
 
4461
 
static int
4462
 
sort_keyuse(KEYUSE *a,KEYUSE *b)
 
1471
static int sort_keyuse(KEYUSE *a,KEYUSE *b)
4463
1472
{
4464
1473
  int res;
4465
1474
  if (a->table->tablenr != b->table->tablenr)
4477
1486
                (b->optimize & KEY_OPTIMIZE_REF_OR_NULL));
4478
1487
}
4479
1488
 
4480
 
 
4481
1489
/*
4482
1490
  Add to KEY_FIELD array all 'ref' access candidates within nested join.
4483
1491
 
4512
1520
    @endcode
4513
1521
    Here we can add 'ref' access candidates for t1 and t2, but not for t3.
4514
1522
*/
4515
 
 
4516
 
static void add_key_fields_for_nj(JOIN *join, TableList *nested_join_table,
4517
 
                                  KEY_FIELD **end, uint32_t *and_level,
 
1523
static void add_key_fields_for_nj(JOIN *join,
 
1524
                                  TableList *nested_join_table,
 
1525
                                  KEY_FIELD **end,
 
1526
                                  uint32_t *and_level,
4518
1527
                                  SARGABLE_PARAM **sargables)
4519
1528
{
4520
1529
  List_iterator<TableList> li(nested_join_table->nested_join->join_list);
4548
1557
                   sargables);
4549
1558
}
4550
1559
 
4551
 
 
4552
1560
/**
4553
1561
  Update keyuse array with all possible keys we can use to fetch rows.
4554
1562
 
4569
1577
   @retval
4570
1578
     1  Out of memory.
4571
1579
*/
4572
 
 
4573
 
static bool
4574
 
update_ref_and_keys(Session *session, DYNAMIC_ARRAY *keyuse,JOIN_TAB *join_tab,
4575
 
                    uint32_t tables, COND *cond, COND_EQUAL *,
4576
 
                    table_map normal_tables, Select_Lex *select_lex,
4577
 
                    SARGABLE_PARAM **sargables)
 
1580
bool update_ref_and_keys(Session *session,
 
1581
                         DYNAMIC_ARRAY *keyuse,
 
1582
                         JOIN_TAB *join_tab,
 
1583
                         uint32_t tables,
 
1584
                         COND *cond, 
 
1585
                         COND_EQUAL *,
 
1586
                         table_map normal_tables,
 
1587
                         Select_Lex *select_lex,
 
1588
                         SARGABLE_PARAM **sargables)
4578
1589
{
4579
1590
  uint  and_level,i,found_eq_constant;
4580
1591
  KEY_FIELD *key_fields, *end, *field;
4687
1698
    for (i=0 ; i < keyuse->elements-1 ; i++,use++)
4688
1699
    {
4689
1700
      if (!use->used_tables && use->optimize != KEY_OPTIMIZE_REF_OR_NULL)
4690
 
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
1701
        use->table->const_key_parts[use->key]|= use->keypart_map;
 
1702
      if (use->key == prev->key && use->table == prev->table)
4691
1703
      {
4692
 
        if (use->key == prev->key && use->table == prev->table)
4693
 
        {
4694
 
          if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
4695
 
            continue;                           /* remove */
4696
 
        }
4697
 
        else if (use->keypart != 0)             // First found must be 0
4698
 
          continue;
 
1704
        if (prev->keypart+1 < use->keypart || ((prev->keypart == use->keypart) && found_eq_constant))
 
1705
          continue;                             /* remove */
4699
1706
      }
 
1707
      else if (use->keypart != 0)               // First found must be 0
 
1708
        continue;
4700
1709
 
4701
1710
#ifdef HAVE_purify
4702
1711
      /* Valgrind complains about overlapped memcpy when save_pos==use. */
4707
1716
      found_eq_constant= !use->used_tables;
4708
1717
      /* Save ptr to first use */
4709
1718
      if (!use->table->reginfo.join_tab->keyuse)
4710
 
        use->table->reginfo.join_tab->keyuse=save_pos;
 
1719
        use->table->reginfo.join_tab->keyuse=save_pos;
4711
1720
      use->table->reginfo.join_tab->checked_keys.set(use->key);
4712
1721
      save_pos++;
4713
1722
    }
4721
1730
/**
4722
1731
  Update some values in keyuse for faster choose_plan() loop.
4723
1732
*/
4724
 
 
4725
 
static void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
 
1733
void optimize_keyuse(JOIN *join, DYNAMIC_ARRAY *keyuse_array)
4726
1734
{
4727
1735
  KEYUSE *end,*keyuse= dynamic_element(keyuse_array, 0, KEYUSE*);
4728
1736
 
4738
1746
      To avoid bad matches, we don't make ref_table_rows less than 100.
4739
1747
    */
4740
1748
    keyuse->ref_table_rows= ~(ha_rows) 0;       // If no ref
4741
 
    if (keyuse->used_tables &
4742
 
        (map= (keyuse->used_tables & ~join->const_table_map &
4743
 
               ~OUTER_REF_TABLE_BIT)))
 
1749
    if (keyuse->used_tables & (map= (keyuse->used_tables & ~join->const_table_map & ~OUTER_REF_TABLE_BIT)))
4744
1750
    {
4745
1751
      uint32_t tablenr;
4746
1752
      for (tablenr=0 ; ! (map & 1) ; map>>=1, tablenr++) ;
4747
1753
      if (map == 1)                     // Only one table
4748
1754
      {
4749
 
        Table *tmp_table=join->all_tables[tablenr];
4750
 
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
 
1755
        Table *tmp_table=join->all_tables[tablenr];
 
1756
        keyuse->ref_table_rows= cmax(tmp_table->file->stats.records, (ha_rows)100);
4751
1757
      }
4752
1758
    }
4753
1759
    /*
4777
1783
  @return
4778
1784
    None
4779
1785
*/
4780
 
 
4781
 
static void
4782
 
add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
 
1786
void add_group_and_distinct_keys(JOIN *join, JOIN_TAB *join_tab)
4783
1787
{
4784
1788
  List<Item_field> indexed_fields;
4785
1789
  List_iterator<Item_field> indexed_fields_it(indexed_fields);
4820
1824
    join_tab->const_keys|= possible_keys;
4821
1825
}
4822
1826
 
4823
 
 
4824
1827
/*****************************************************************************
4825
1828
  Go through all combinations of not marked tables and find the one
4826
1829
  which uses least records
4827
1830
*****************************************************************************/
4828
1831
 
4829
 
/** Save const tables first as used tables. */
4830
 
 
4831
 
static void
4832
 
set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
4833
 
{
4834
 
  join->positions[idx].table= table;
4835
 
  join->positions[idx].key=key;
4836
 
  join->positions[idx].records_read=1.0;        /* This is a const table */
4837
 
  join->positions[idx].ref_depend_map= 0;
4838
 
 
4839
 
  /* Move the const table as down as possible in best_ref */
4840
 
  JOIN_TAB **pos=join->best_ref+idx+1;
4841
 
  JOIN_TAB *next=join->best_ref[idx];
4842
 
  for (;next != table ; pos++)
4843
 
  {
4844
 
    JOIN_TAB *tmp=pos[0];
4845
 
    pos[0]=next;
4846
 
    next=tmp;
4847
 
  }
4848
 
  join->best_ref[idx]=table;
4849
 
}
4850
 
 
4851
 
 
4852
1832
/*
4853
1833
  Given a semi-join nest, find out which of the IN-equalities are bound
4854
1834
 
4865
1845
  RETURN
4866
1846
    Bitmap of bound IN-equalities.
4867
1847
*/
4868
 
 
4869
 
uint64_t get_bound_sj_equalities(TableList *sj_nest,
4870
 
                                  table_map remaining_tables)
 
1848
uint64_t get_bound_sj_equalities(TableList *sj_nest, table_map remaining_tables)
4871
1849
{
4872
1850
  List_iterator<Item> li(sj_nest->nested_join->sj_outer_expr_list);
4873
1851
  Item *item;
4889
1867
  return res;
4890
1868
}
4891
1869
 
4892
 
 
4893
 
/**
4894
 
  Find the best access path for an extension of a partial execution
4895
 
  plan and add this path to the plan.
4896
 
 
4897
 
  The function finds the best access path to table 's' from the passed
4898
 
  partial plan where an access path is the general term for any means to
4899
 
  access the data in 's'. An access path may use either an index or a scan,
4900
 
  whichever is cheaper. The input partial plan is passed via the array
4901
 
  'join->positions' of length 'idx'. The chosen access method for 's' and its
4902
 
  cost are stored in 'join->positions[idx]'.
4903
 
 
4904
 
  @param join             pointer to the structure providing all context info
4905
 
                          for the query
4906
 
  @param s                the table to be joined by the function
4907
 
  @param session              thread for the connection that submitted the query
4908
 
  @param remaining_tables set of tables not included into the partial plan yet
4909
 
  @param idx              the length of the partial plan
4910
 
  @param record_count     estimate for the number of records returned by the
4911
 
                          partial plan
4912
 
  @param read_time        the cost of the partial plan
4913
 
 
4914
 
  @return
4915
 
    None
4916
 
*/
4917
 
 
4918
 
static void
4919
 
best_access_path(JOIN      *join,
4920
 
                 JOIN_TAB  *s,
4921
 
                 Session       *session,
4922
 
                 table_map remaining_tables,
4923
 
                 uint32_t      idx,
4924
 
                 double    record_count,
4925
 
                 double)
4926
 
{
4927
 
  KEYUSE *best_key=         0;
4928
 
  uint32_t best_max_key_part=   0;
4929
 
  bool found_constraint= 0;
4930
 
  double best=              DBL_MAX;
4931
 
  double best_time=         DBL_MAX;
4932
 
  double records=           DBL_MAX;
4933
 
  table_map best_ref_depends_map= 0;
4934
 
  double tmp;
4935
 
  ha_rows rec;
4936
 
  uint32_t best_is_sj_inside_out=    0;
4937
 
 
4938
 
  if (s->keyuse)
4939
 
  {                                            /* Use key if possible */
4940
 
    Table *table= s->table;
4941
 
    KEYUSE *keyuse,*start_key=0;
4942
 
    double best_records= DBL_MAX;
4943
 
    uint32_t max_key_part=0;
4944
 
    uint64_t bound_sj_equalities= 0;
4945
 
    bool try_sj_inside_out= false;
4946
 
    /*
4947
 
      Discover the bound equalites. We need to do this, if
4948
 
        1. The next table is an SJ-inner table, and
4949
 
        2. It is the first table from that semijoin, and
4950
 
        3. We're not within a semi-join range (i.e. all semi-joins either have
4951
 
           all or none of their tables in join_table_map), except
4952
 
           s->emb_sj_nest (which we've just entered).
4953
 
        3. All correlation references from this sj-nest are bound
4954
 
    */
4955
 
    if (s->emb_sj_nest &&                                                 // (1)
4956
 
        s->emb_sj_nest->sj_in_exprs < 64 &&
4957
 
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
4958
 
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
4959
 
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
4960
 
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
4961
 
    {
4962
 
      /* This table is an InsideOut scan candidate */
4963
 
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
4964
 
                                                   remaining_tables);
4965
 
      try_sj_inside_out= true;
4966
 
    }
4967
 
 
4968
 
    /* Test how we can use keys */
4969
 
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
4970
 
    for (keyuse=s->keyuse ; keyuse->table == table ;)
4971
 
    {
4972
 
      key_part_map found_part= 0;
4973
 
      table_map found_ref= 0;
4974
 
      uint32_t key= keyuse->key;
4975
 
      KEY *keyinfo= table->key_info+key;
4976
 
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
4977
 
      key_part_map const_part= 0;
4978
 
      /* The or-null keypart in ref-or-null access: */
4979
 
      key_part_map ref_or_null_part= 0;
4980
 
 
4981
 
      /* Calculate how many key segments of the current key we can use */
4982
 
      start_key= keyuse;
4983
 
      uint64_t handled_sj_equalities=0;
4984
 
      key_part_map sj_insideout_map= 0;
4985
 
 
4986
 
      do /* For each keypart */
4987
 
      {
4988
 
        uint32_t keypart= keyuse->keypart;
4989
 
        table_map best_part_found_ref= 0;
4990
 
        double best_prev_record_reads= DBL_MAX;
4991
 
 
4992
 
        do /* For each way to access the keypart */
4993
 
        {
4994
 
 
4995
 
          /*
4996
 
            if 1. expression doesn't refer to forward tables
4997
 
               2. we won't get two ref-or-null's
4998
 
          */
4999
 
          if (!(remaining_tables & keyuse->used_tables) &&
5000
 
              !(ref_or_null_part && (keyuse->optimize &
5001
 
                                     KEY_OPTIMIZE_REF_OR_NULL)))
5002
 
          {
5003
 
            found_part|= keyuse->keypart_map;
5004
 
            if (!(keyuse->used_tables & ~join->const_table_map))
5005
 
              const_part|= keyuse->keypart_map;
5006
 
 
5007
 
            double tmp2= prev_record_reads(join, idx, (found_ref |
5008
 
                                                      keyuse->used_tables));
5009
 
            if (tmp2 < best_prev_record_reads)
5010
 
            {
5011
 
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
5012
 
              best_prev_record_reads= tmp2;
5013
 
            }
5014
 
            if (rec > keyuse->ref_table_rows)
5015
 
              rec= keyuse->ref_table_rows;
5016
 
            /*
5017
 
              If there is one 'key_column IS NULL' expression, we can
5018
 
              use this ref_or_null optimisation of this field
5019
 
            */
5020
 
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
5021
 
              ref_or_null_part |= keyuse->keypart_map;
5022
 
          }
5023
 
 
5024
 
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
5025
 
          {
5026
 
            if (!(remaining_tables & keyuse->used_tables))
5027
 
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
5028
 
            else
5029
 
            {
5030
 
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
5031
 
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
5032
 
            }
5033
 
          }
5034
 
 
5035
 
          keyuse++;
5036
 
        } while (keyuse->table == table && keyuse->key == key &&
5037
 
                 keyuse->keypart == keypart);
5038
 
        found_ref|= best_part_found_ref;
5039
 
      } while (keyuse->table == table && keyuse->key == key);
5040
 
 
5041
 
      /*
5042
 
        Assume that that each key matches a proportional part of table.
5043
 
      */
5044
 
      if (!found_part && !handled_sj_equalities)
5045
 
        continue;                               // Nothing usable found
5046
 
 
5047
 
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
5048
 
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
5049
 
 
5050
 
      bool sj_inside_out_scan= false;
5051
 
      {
5052
 
        found_constraint= 1;
5053
 
        /*
5054
 
          Check if InsideOut scan is applicable:
5055
 
          1. All IN-equalities are either "bound" or "handled"
5056
 
          2. Index keyparts are
5057
 
             ...
5058
 
        */
5059
 
        if (try_sj_inside_out &&
5060
 
            table->covering_keys.test(key) &&
5061
 
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
5062
 
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
5063
 
        {
5064
 
          uint32_t n_fixed_parts= max_part_bit(found_part);
5065
 
          if (n_fixed_parts != keyinfo->key_parts &&
5066
 
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
5067
 
               PREV_BITS(uint, keyinfo->key_parts))
5068
 
          {
5069
 
            /*
5070
 
              Not all parts are fixed. Produce bitmap of remaining bits and
5071
 
              check if all of them are covered.
5072
 
            */
5073
 
            sj_inside_out_scan= true;
5074
 
            if (!n_fixed_parts)
5075
 
            {
5076
 
              /*
5077
 
                It's a confluent ref scan.
5078
 
 
5079
 
                That is, all found KEYUSE elements refer to IN-equalities,
5080
 
                and there is really no ref access because there is no
5081
 
                  t.keypart0 = {bound expression}
5082
 
 
5083
 
                Calculate the cost of complete loose index scan.
5084
 
              */
5085
 
              records= (double)s->table->file->stats.records;
5086
 
 
5087
 
              /* The cost is entire index scan cost (divided by 2) */
5088
 
              best_time= s->table->file->index_only_read_time(key, records);
5089
 
 
5090
 
              /* Now figure how many different keys we will get */
5091
 
              ulong rpc;
5092
 
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
5093
 
                records= records / rpc;
5094
 
              start_key= NULL;
5095
 
            }
5096
 
          }
5097
 
        }
5098
 
 
5099
 
        /*
5100
 
          Check if we found full key
5101
 
        */
5102
 
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
5103
 
            !ref_or_null_part)
5104
 
        {                                         /* use eq key */
5105
 
          max_key_part= UINT32_MAX;
5106
 
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
5107
 
          {
5108
 
            tmp = prev_record_reads(join, idx, found_ref);
5109
 
            records=1.0;
5110
 
          }
5111
 
          else
5112
 
          {
5113
 
            if (!found_ref)
5114
 
            {                                     /* We found a const key */
5115
 
              /*
5116
 
                ReuseRangeEstimateForRef-1:
5117
 
                We get here if we've found a ref(const) (c_i are constants):
5118
 
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
5119
 
 
5120
 
                If range optimizer was able to construct a "range"
5121
 
                access on this index, then its condition "quick_cond" was
5122
 
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
5123
 
                from the range optimizer.
5124
 
 
5125
 
                Proof of (*): By properties of range and ref optimizers
5126
 
                quick_cond will be equal or tighther than ref_const_cond.
5127
 
                ref_const_cond already covers "smallest" possible interval -
5128
 
                a singlepoint interval over all keyparts. Therefore,
5129
 
                quick_cond is equivalent to ref_const_cond (if it was an
5130
 
                empty interval we wouldn't have got here).
5131
 
              */
5132
 
              if (table->quick_keys.test(key))
5133
 
                records= (double) table->quick_rows[key];
5134
 
              else
5135
 
              {
5136
 
                /* quick_range couldn't use key! */
5137
 
                records= (double) s->records/rec;
5138
 
              }
5139
 
            }
5140
 
            else
5141
 
            {
5142
 
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
5143
 
              {                                   /* Prefer longer keys */
5144
 
                records=
5145
 
                  ((double) s->records / (double) rec *
5146
 
                   (1.0 +
5147
 
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
5148
 
                     (double) table->s->max_key_length)));
5149
 
                if (records < 2.0)
5150
 
                  records=2.0;               /* Can't be as good as a unique */
5151
 
              }
5152
 
              /*
5153
 
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
5154
 
                E(#rows) from range optimizer. Make another try:
5155
 
 
5156
 
                If range optimizer produced E(#rows) for a prefix of the ref
5157
 
                access we're considering, and that E(#rows) is lower then our
5158
 
                current estimate, make an adjustment. The criteria of when we
5159
 
                can make an adjustment is a special case of the criteria used
5160
 
                in ReuseRangeEstimateForRef-3.
5161
 
              */
5162
 
              if (table->quick_keys.test(key) &&
5163
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5164
 
                  table->quick_n_ranges[key] == 1 &&
5165
 
                  records > (double) table->quick_rows[key])
5166
 
              {
5167
 
                records= (double) table->quick_rows[key];
5168
 
              }
5169
 
            }
5170
 
            /* Limit the number of matched rows */
5171
 
            tmp= records;
5172
 
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5173
 
            if (table->covering_keys.test(key))
5174
 
            {
5175
 
              /* we can use only index tree */
5176
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5177
 
            }
5178
 
            else
5179
 
              tmp= record_count*cmin(tmp,s->worst_seeks);
5180
 
          }
5181
 
        }
5182
 
        else
5183
 
        {
5184
 
          /*
5185
 
            Use as much key-parts as possible and a uniq key is better
5186
 
            than a not unique key
5187
 
            Set tmp to (previous record count) * (records / combination)
5188
 
          */
5189
 
          if ((found_part & 1) &&
5190
 
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
5191
 
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
5192
 
          {
5193
 
            max_key_part= max_part_bit(found_part);
5194
 
            /*
5195
 
              ReuseRangeEstimateForRef-3:
5196
 
              We're now considering a ref[or_null] access via
5197
 
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
5198
 
              (same-as-above but with one cond replaced
5199
 
               with "t.keypart_i IS NULL")]  (**)
5200
 
 
5201
 
              Try re-using E(#rows) from "range" optimizer:
5202
 
              We can do so if "range" optimizer used the same intervals as
5203
 
              in (**). The intervals used by range optimizer may be not
5204
 
              available at this point (as "range" access might have choosen to
5205
 
              create quick select over another index), so we can't compare
5206
 
              them to (**). We'll make indirect judgements instead.
5207
 
              The sufficient conditions for re-use are:
5208
 
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
5209
 
                   this is not satisfied we have no way to know which ranges
5210
 
                   will be actually scanned by 'ref' until we execute the
5211
 
                   join)
5212
 
              (C2) max #key parts in 'range' access == K == max_key_part (this
5213
 
                   is apparently a necessary requirement)
5214
 
 
5215
 
              We also have a property that "range optimizer produces equal or
5216
 
              tighter set of scan intervals than ref(const) optimizer". Each
5217
 
              of the intervals in (**) are "tightest possible" intervals when
5218
 
              one limits itself to using keyparts 1..K (which we do in #2).
5219
 
              From here it follows that range access used either one, or
5220
 
              both of the (I1) and (I2) intervals:
5221
 
 
5222
 
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
5223
 
               (same-as-above but with one cond replaced
5224
 
                with "t.keypart_i IS NULL")               (I2)
5225
 
 
5226
 
              The remaining part is to exclude the situation where range
5227
 
              optimizer used one interval while we're considering
5228
 
              ref-or-null and looking for estimate for two intervals. This
5229
 
              is done by last limitation:
5230
 
 
5231
 
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
5232
 
            */
5233
 
            if (table->quick_keys.test(key) && !found_ref &&          //(C1)
5234
 
                table->quick_key_parts[key] == max_key_part &&          //(C2)
5235
 
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
5236
 
            {
5237
 
              tmp= records= (double) table->quick_rows[key];
5238
 
            }
5239
 
            else
5240
 
            {
5241
 
              /* Check if we have statistic about the distribution */
5242
 
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
5243
 
              {
5244
 
                /*
5245
 
                  Fix for the case where the index statistics is too
5246
 
                  optimistic: If
5247
 
                  (1) We're considering ref(const) and there is quick select
5248
 
                      on the same index,
5249
 
                  (2) and that quick select uses more keyparts (i.e. it will
5250
 
                      scan equal/smaller interval then this ref(const))
5251
 
                  (3) and E(#rows) for quick select is higher then our
5252
 
                      estimate,
5253
 
                  Then
5254
 
                    We'll use E(#rows) from quick select.
5255
 
 
5256
 
                  Q: Why do we choose to use 'ref'? Won't quick select be
5257
 
                  cheaper in some cases ?
5258
 
                  TODO: figure this out and adjust the plan choice if needed.
5259
 
                */
5260
 
                if (!found_ref && table->quick_keys.test(key) &&    // (1)
5261
 
                    table->quick_key_parts[key] > max_key_part &&     // (2)
5262
 
                    records < (double)table->quick_rows[key])         // (3)
5263
 
                  records= (double)table->quick_rows[key];
5264
 
 
5265
 
                tmp= records;
5266
 
              }
5267
 
              else
5268
 
              {
5269
 
                /*
5270
 
                  Assume that the first key part matches 1% of the file
5271
 
                  and that the whole key matches 10 (duplicates) or 1
5272
 
                  (unique) records.
5273
 
                  Assume also that more key matches proportionally more
5274
 
                  records
5275
 
                  This gives the formula:
5276
 
                  records = (x * (b-a) + a*c-b)/(c-1)
5277
 
 
5278
 
                  b = records matched by whole key
5279
 
                  a = records matched by first key part (1% of all records?)
5280
 
                  c = number of key parts in key
5281
 
                  x = used key parts (1 <= x <= c)
5282
 
                */
5283
 
                double rec_per_key;
5284
 
                if (!(rec_per_key=(double)
5285
 
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
5286
 
                  rec_per_key=(double) s->records/rec+1;
5287
 
 
5288
 
                if (!s->records)
5289
 
                  tmp = 0;
5290
 
                else if (rec_per_key/(double) s->records >= 0.01)
5291
 
                  tmp = rec_per_key;
5292
 
                else
5293
 
                {
5294
 
                  double a=s->records*0.01;
5295
 
                  if (keyinfo->key_parts > 1)
5296
 
                    tmp= (max_key_part * (rec_per_key - a) +
5297
 
                          a*keyinfo->key_parts - rec_per_key)/
5298
 
                         (keyinfo->key_parts-1);
5299
 
                  else
5300
 
                    tmp= a;
5301
 
                  set_if_bigger(tmp,1.0);
5302
 
                }
5303
 
                records = (uint32_t) tmp;
5304
 
              }
5305
 
 
5306
 
              if (ref_or_null_part)
5307
 
              {
5308
 
                /* We need to do two key searches to find key */
5309
 
                tmp *= 2.0;
5310
 
                records *= 2.0;
5311
 
              }
5312
 
 
5313
 
              /*
5314
 
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
5315
 
                E(#rows) from range optimizer. Make another try:
5316
 
 
5317
 
                If range optimizer produced E(#rows) for a prefix of the ref
5318
 
                access we're considering, and that E(#rows) is lower then our
5319
 
                current estimate, make the adjustment.
5320
 
 
5321
 
                The decision whether we can re-use the estimate from the range
5322
 
                optimizer is the same as in ReuseRangeEstimateForRef-3,
5323
 
                applied to first table->quick_key_parts[key] key parts.
5324
 
              */
5325
 
              if (table->quick_keys.test(key) &&
5326
 
                  table->quick_key_parts[key] <= max_key_part &&
5327
 
                  const_part & (1 << table->quick_key_parts[key]) &&
5328
 
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
5329
 
                                                     const_part) ? 1 : 0) &&
5330
 
                  records > (double) table->quick_rows[key])
5331
 
              {
5332
 
                tmp= records= (double) table->quick_rows[key];
5333
 
              }
5334
 
            }
5335
 
 
5336
 
            /* Limit the number of matched rows */
5337
 
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
5338
 
            if (table->covering_keys.test(key))
5339
 
            {
5340
 
              /* we can use only index tree */
5341
 
              tmp= record_count * table->file->index_only_read_time(key, tmp);
5342
 
            }
5343
 
            else
5344
 
              tmp= record_count * cmin(tmp,s->worst_seeks);
5345
 
          }
5346
 
          else
5347
 
            tmp= best_time;                    // Do nothing
5348
 
        }
5349
 
 
5350
 
        if (sj_inside_out_scan && !start_key)
5351
 
        {
5352
 
          tmp= tmp/2;
5353
 
          if (records)
5354
 
            records= records/2;
5355
 
        }
5356
 
 
5357
 
      }
5358
 
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
5359
 
      {
5360
 
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
5361
 
        best= tmp;
5362
 
        best_records= records;
5363
 
        best_key= start_key;
5364
 
        best_max_key_part= max_key_part;
5365
 
        best_ref_depends_map= found_ref;
5366
 
        best_is_sj_inside_out= sj_inside_out_scan;
5367
 
      }
5368
 
    }
5369
 
    records= best_records;
5370
 
  }
5371
 
 
5372
 
  /*
5373
 
    Don't test table scan if it can't be better.
5374
 
    Prefer key lookup if we would use the same key for scanning.
5375
 
 
5376
 
    Don't do a table scan on InnoDB tables, if we can read the used
5377
 
    parts of the row from any of the used index.
5378
 
    This is because table scans uses index and we would not win
5379
 
    anything by using a table scan.
5380
 
 
5381
 
    A word for word translation of the below if-statement in sergefp's
5382
 
    understanding: we check if we should use table scan if:
5383
 
    (1) The found 'ref' access produces more records than a table scan
5384
 
        (or index scan, or quick select), or 'ref' is more expensive than
5385
 
        any of them.
5386
 
    (2) This doesn't hold: the best way to perform table scan is to to perform
5387
 
        'range' access using index IDX, and the best way to perform 'ref'
5388
 
        access is to use the same index IDX, with the same or more key parts.
5389
 
        (note: it is not clear how this rule is/should be extended to
5390
 
        index_merge quick selects)
5391
 
    (3) See above note about InnoDB.
5392
 
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
5393
 
             path, but there is no quick select)
5394
 
        If the condition in the above brackets holds, then the only possible
5395
 
        "table scan" access method is ALL/index (there is no quick select).
5396
 
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
5397
 
        choose it over ALL/index, there is no need to consider a full table
5398
 
        scan.
5399
 
  */
5400
 
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
5401
 
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
5402
 
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
5403
 
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
5404
 
        ! s->table->covering_keys.none() && best_key && !s->quick) &&// (3)
5405
 
      !(s->table->force_index && best_key && !s->quick))                 // (4)
5406
 
  {                                             // Check full join
5407
 
    ha_rows rnd_records= s->found_records;
5408
 
    /*
5409
 
      If there is a filtering condition on the table (i.e. ref analyzer found
5410
 
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
5411
 
      preceding this table in the join order we're now considering), then
5412
 
      assume that 25% of the rows will be filtered out by this condition.
5413
 
 
5414
 
      This heuristic is supposed to force tables used in exprZ to be before
5415
 
      this table in join order.
5416
 
    */
5417
 
    if (found_constraint)
5418
 
      rnd_records-= rnd_records/4;
5419
 
 
5420
 
    /*
5421
 
      If applicable, get a more accurate estimate. Don't use the two
5422
 
      heuristics at once.
5423
 
    */
5424
 
    if (s->table->quick_condition_rows != s->found_records)
5425
 
      rnd_records= s->table->quick_condition_rows;
5426
 
 
5427
 
    /*
5428
 
      Range optimizer never proposes a RANGE if it isn't better
5429
 
      than FULL: so if RANGE is present, it's always preferred to FULL.
5430
 
      Here we estimate its cost.
5431
 
    */
5432
 
    if (s->quick)
5433
 
    {
5434
 
      /*
5435
 
        For each record we:
5436
 
        - read record range through 'quick'
5437
 
        - skip rows which does not satisfy WHERE constraints
5438
 
        TODO:
5439
 
        We take into account possible use of join cache for ALL/index
5440
 
        access (see first else-branch below), but we don't take it into
5441
 
        account here for range/index_merge access. Find out why this is so.
5442
 
      */
5443
 
      tmp= record_count *
5444
 
        (s->quick->read_time +
5445
 
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
5446
 
    }
5447
 
    else
5448
 
    {
5449
 
      /* Estimate cost of reading table. */
5450
 
      tmp= s->table->file->scan_time();
5451
 
      if (s->table->map & join->outer_join)     // Can't use join cache
5452
 
      {
5453
 
        /*
5454
 
          For each record we have to:
5455
 
          - read the whole table record
5456
 
          - skip rows which does not satisfy join condition
5457
 
        */
5458
 
        tmp= record_count *
5459
 
          (tmp +
5460
 
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
5461
 
      }
5462
 
      else
5463
 
      {
5464
 
        /* We read the table as many times as join buffer becomes full. */
5465
 
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
5466
 
                           record_count /
5467
 
                           (double) session->variables.join_buff_size));
5468
 
        /*
5469
 
            We don't make full cartesian product between rows in the scanned
5470
 
           table and existing records because we skip all rows from the
5471
 
           scanned table, which does not satisfy join condition when
5472
 
           we read the table (see flush_cached_records for details). Here we
5473
 
           take into account cost to read and skip these records.
5474
 
        */
5475
 
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
5476
 
      }
5477
 
    }
5478
 
 
5479
 
    /*
5480
 
      We estimate the cost of evaluating WHERE clause for found records
5481
 
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
5482
 
      tmp give us total cost of using Table SCAN
5483
 
    */
5484
 
    if (best == DBL_MAX ||
5485
 
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
5486
 
         best + record_count/(double) TIME_FOR_COMPARE*records))
5487
 
    {
5488
 
      /*
5489
 
        If the table has a range (s->quick is set) make_join_select()
5490
 
        will ensure that this will be used
5491
 
      */
5492
 
      best= tmp;
5493
 
      records= rows2double(rnd_records);
5494
 
      best_key= 0;
5495
 
      /* range/index_merge/ALL/index access method are "independent", so: */
5496
 
      best_ref_depends_map= 0;
5497
 
      best_is_sj_inside_out= false;
5498
 
    }
5499
 
  }
5500
 
 
5501
 
  /* Update the cost information for the current partial plan */
5502
 
  join->positions[idx].records_read= records;
5503
 
  join->positions[idx].read_time=    best;
5504
 
  join->positions[idx].key=          best_key;
5505
 
  join->positions[idx].table=        s;
5506
 
  join->positions[idx].ref_depend_map= best_ref_depends_map;
5507
 
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
5508
 
 
5509
 
  if (!best_key &&
5510
 
      idx == join->const_tables &&
5511
 
      s->table == join->sort_by_table &&
5512
 
      join->unit->select_limit_cnt >= records)
5513
 
    join->sort_by_table= (Table*) 1;  // Must use temporary table
5514
 
 
5515
 
  return;
5516
 
}
5517
 
 
5518
 
 
5519
 
/**
5520
 
  Selects and invokes a search strategy for an optimal query plan.
5521
 
 
5522
 
  The function checks user-configurable parameters that control the search
5523
 
  strategy for an optimal plan, selects the search method and then invokes
5524
 
  it. Each specific optimization procedure stores the final optimal plan in
5525
 
  the array 'join->best_positions', and the cost of the plan in
5526
 
  'join->best_read'.
5527
 
 
5528
 
  @param join         pointer to the structure providing all context info for
5529
 
                      the query
5530
 
  @param join_tables  set of the tables in the query
5531
 
 
5532
 
  @todo
5533
 
    'MAX_TABLES+2' denotes the old implementation of find_best before
5534
 
    the greedy version. Will be removed when greedy_search is approved.
5535
 
 
5536
 
  @retval
5537
 
    false       ok
5538
 
  @retval
5539
 
    true        Fatal error
5540
 
*/
5541
 
 
5542
 
static bool
5543
 
choose_plan(JOIN *join, table_map join_tables)
5544
 
{
5545
 
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
5546
 
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
5547
 
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
5548
 
 
5549
 
  join->cur_embedding_map= 0;
5550
 
  reset_nj_counters(join->join_list);
5551
 
  /*
5552
 
    if (SELECT_STRAIGHT_JOIN option is set)
5553
 
      reorder tables so dependent tables come after tables they depend
5554
 
      on, otherwise keep tables in the order they were specified in the query
5555
 
    else
5556
 
      Apply heuristic: pre-sort all access plans with respect to the number of
5557
 
      records accessed.
5558
 
  */
5559
 
  my_qsort(join->best_ref + join->const_tables,
5560
 
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
5561
 
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
5562
 
  join->cur_emb_sj_nests= 0;
5563
 
  if (straight_join)
5564
 
  {
5565
 
    optimize_straight_join(join, join_tables);
5566
 
  }
5567
 
  else
5568
 
  {
5569
 
    if (search_depth == MAX_TABLES+2)
5570
 
    { /*
5571
 
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
5572
 
        the greedy version. Will be removed when greedy_search is approved.
5573
 
      */
5574
 
      join->best_read= DBL_MAX;
5575
 
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
5576
 
        return(true);
5577
 
    }
5578
 
    else
5579
 
    {
5580
 
      if (search_depth == 0)
5581
 
        /* Automatically determine a reasonable value for 'search_depth' */
5582
 
        search_depth= determine_search_depth(join);
5583
 
      if (greedy_search(join, join_tables, search_depth, prune_level))
5584
 
        return(true);
5585
 
    }
5586
 
  }
5587
 
 
5588
 
  /*
5589
 
    Store the cost of this query into a user variable
5590
 
    Don't update last_query_cost for statements that are not "flat joins" :
5591
 
    i.e. they have subqueries, unions or call stored procedures.
5592
 
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
5593
 
  */
5594
 
  if (join->session->lex->is_single_level_stmt())
5595
 
    join->session->status_var.last_query_cost= join->best_read;
5596
 
  return(false);
5597
 
}
5598
 
 
5599
 
 
5600
1870
/**
5601
1871
  Compare two JOIN_TAB objects based on the number of accessed records.
5602
1872
 
5621
1891
  @retval
5622
1892
    0  if equal
5623
1893
*/
5624
 
 
5625
1894
int join_tab_cmp(const void* ptr1, const void* ptr2)
5626
1895
{
5627
1896
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5638
1907
  return jt1 > jt2 ? 1 : (jt1 < jt2 ? -1 : 0);
5639
1908
}
5640
1909
 
5641
 
 
5642
1910
/**
5643
1911
  Same as join_tab_cmp, but for use with SELECT_STRAIGHT_JOIN.
5644
1912
*/
5645
 
 
5646
1913
int join_tab_cmp_straight(const void* ptr1, const void* ptr2)
5647
1914
{
5648
1915
  JOIN_TAB *jt1= *(JOIN_TAB**) ptr1;
5656
1923
}
5657
1924
 
5658
1925
/**
5659
 
  Heuristic procedure to automatically guess a reasonable degree of
5660
 
  exhaustiveness for the greedy search procedure.
5661
 
 
5662
 
  The procedure estimates the optimization time and selects a search depth
5663
 
  big enough to result in a near-optimal QEP, that doesn't take too long to
5664
 
  find. If the number of tables in the query exceeds some constant, then
5665
 
  search_depth is set to this constant.
5666
 
 
5667
 
  @param join   pointer to the structure providing all context info for
5668
 
                the query
5669
 
 
5670
 
  @note
5671
 
    This is an extremely simplistic implementation that serves as a stub for a
5672
 
    more advanced analysis of the join. Ideally the search depth should be
5673
 
    determined by learning from previous query optimizations, because it will
5674
 
    depend on the CPU power (and other factors).
5675
 
 
5676
 
  @todo
5677
 
    this value should be determined dynamically, based on statistics:
5678
 
    uint32_t max_tables_for_exhaustive_opt= 7;
5679
 
 
5680
 
  @todo
5681
 
    this value could be determined by some mapping of the form:
5682
 
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5683
 
 
5684
 
  @return
5685
 
    A positive integer that specifies the search depth (and thus the
5686
 
    exhaustiveness) of the depth-first search algorithm used by
5687
 
    'greedy_search'.
5688
 
*/
5689
 
 
5690
 
static uint
5691
 
determine_search_depth(JOIN *join)
5692
 
{
5693
 
  uint32_t table_count=  join->tables - join->const_tables;
5694
 
  uint32_t search_depth;
5695
 
  /* TODO: this value should be determined dynamically, based on statistics: */
5696
 
  uint32_t max_tables_for_exhaustive_opt= 7;
5697
 
 
5698
 
  if (table_count <= max_tables_for_exhaustive_opt)
5699
 
    search_depth= table_count+1; // use exhaustive for small number of tables
5700
 
  else
5701
 
    /*
5702
 
      TODO: this value could be determined by some mapping of the form:
5703
 
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
5704
 
    */
5705
 
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
5706
 
 
5707
 
  return search_depth;
5708
 
}
5709
 
 
5710
 
 
5711
 
/**
5712
 
  Select the best ways to access the tables in a query without reordering them.
5713
 
 
5714
 
    Find the best access paths for each query table and compute their costs
5715
 
    according to their order in the array 'join->best_ref' (thus without
5716
 
    reordering the join tables). The function calls sequentially
5717
 
    'best_access_path' for each table in the query to select the best table
5718
 
    access method. The final optimal plan is stored in the array
5719
 
    'join->best_positions', and the corresponding cost in 'join->best_read'.
5720
 
 
5721
 
  @param join          pointer to the structure providing all context info for
5722
 
                       the query
5723
 
  @param join_tables   set of the tables in the query
5724
 
 
5725
 
  @note
5726
 
    This function can be applied to:
5727
 
    - queries with STRAIGHT_JOIN
5728
 
    - internally to compute the cost of an arbitrary QEP
5729
 
  @par
5730
 
    Thus 'optimize_straight_join' can be used at any stage of the query
5731
 
    optimization process to finalize a QEP as it is.
5732
 
*/
5733
 
 
5734
 
static void
5735
 
optimize_straight_join(JOIN *join, table_map join_tables)
5736
 
{
5737
 
  JOIN_TAB *s;
5738
 
  uint32_t idx= join->const_tables;
5739
 
  double    record_count= 1.0;
5740
 
  double    read_time=    0.0;
5741
 
 
5742
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
5743
 
  {
5744
 
    /* Find the best access method from 's' to the current partial plan */
5745
 
    advance_sj_state(join_tables, s);
5746
 
    best_access_path(join, s, join->session, join_tables, idx,
5747
 
                     record_count, read_time);
5748
 
    /* compute the cost of the new plan extended with 's' */
5749
 
    record_count*= join->positions[idx].records_read;
5750
 
    read_time+=    join->positions[idx].read_time;
5751
 
    join_tables&= ~(s->table->map);
5752
 
    ++idx;
5753
 
  }
5754
 
 
5755
 
  read_time+= record_count / (double) TIME_FOR_COMPARE;
5756
 
  if (join->sort_by_table &&
5757
 
      join->sort_by_table != join->positions[join->const_tables].table->table)
5758
 
    read_time+= record_count;  // We have to make a temp table
5759
 
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
5760
 
  join->best_read= read_time;
5761
 
}
5762
 
 
5763
 
 
5764
 
/**
5765
 
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
5766
 
 
5767
 
    The search procedure uses a hybrid greedy/exhaustive search with controlled
5768
 
    exhaustiveness. The search is performed in N = card(remaining_tables)
5769
 
    steps. Each step evaluates how promising is each of the unoptimized tables,
5770
 
    selects the most promising table, and extends the current partial QEP with
5771
 
    that table.  Currenly the most 'promising' table is the one with least
5772
 
    expensive extension.\
5773
 
 
5774
 
    There are two extreme cases:
5775
 
    -# When (card(remaining_tables) < search_depth), the estimate finds the
5776
 
    best complete continuation of the partial QEP. This continuation can be
5777
 
    used directly as a result of the search.
5778
 
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
5779
 
    consideres the extension of the current QEP with each of the remaining
5780
 
    unoptimized tables.
5781
 
 
5782
 
    All other cases are in-between these two extremes. Thus the parameter
5783
 
    'search_depth' controlls the exhaustiveness of the search. The higher the
5784
 
    value, the longer the optimizaton time and possibly the better the
5785
 
    resulting plan. The lower the value, the fewer alternative plans are
5786
 
    estimated, but the more likely to get a bad QEP.
5787
 
 
5788
 
    All intermediate and final results of the procedure are stored in 'join':
5789
 
    - join->positions     : modified for every partial QEP that is explored
5790
 
    - join->best_positions: modified for the current best complete QEP
5791
 
    - join->best_read     : modified for the current best complete QEP
5792
 
    - join->best_ref      : might be partially reordered
5793
 
 
5794
 
    The final optimal plan is stored in 'join->best_positions', and its
5795
 
    corresponding cost in 'join->best_read'.
5796
 
 
5797
 
  @note
5798
 
    The following pseudocode describes the algorithm of 'greedy_search':
5799
 
 
5800
 
    @code
5801
 
    procedure greedy_search
5802
 
    input: remaining_tables
5803
 
    output: pplan;
5804
 
    {
5805
 
      pplan = <>;
5806
 
      do {
5807
 
        (t, a) = best_extension(pplan, remaining_tables);
5808
 
        pplan = concat(pplan, (t, a));
5809
 
        remaining_tables = remaining_tables - t;
5810
 
      } while (remaining_tables != {})
5811
 
      return pplan;
5812
 
    }
5813
 
 
5814
 
  @endcode
5815
 
    where 'best_extension' is a placeholder for a procedure that selects the
5816
 
    most "promising" of all tables in 'remaining_tables'.
5817
 
    Currently this estimate is performed by calling
5818
 
    'best_extension_by_limited_search' to evaluate all extensions of the
5819
 
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
5820
 
    mainly depends on that of 'best_extension_by_limited_search'.
5821
 
 
5822
 
  @par
5823
 
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
5824
 
    worst-case complexity of this algorithm is <=
5825
 
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
5826
 
    complexity of greedy_search is O(N!).
5827
 
 
5828
 
  @par
5829
 
    In the future, 'greedy_search' might be extended to support other
5830
 
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
5831
 
 
5832
 
  @param join             pointer to the structure providing all context info
5833
 
                          for the query
5834
 
  @param remaining_tables set of tables not included into the partial plan yet
5835
 
  @param search_depth     controlls the exhaustiveness of the search
5836
 
  @param prune_level      the pruning heuristics that should be applied during
5837
 
                          search
5838
 
 
5839
 
  @retval
5840
 
    false       ok
5841
 
  @retval
5842
 
    true        Fatal error
5843
 
*/
5844
 
 
5845
 
static bool
5846
 
greedy_search(JOIN      *join,
5847
 
              table_map remaining_tables,
5848
 
              uint32_t      search_depth,
5849
 
              uint32_t      prune_level)
5850
 
{
5851
 
  double    record_count= 1.0;
5852
 
  double    read_time=    0.0;
5853
 
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
5854
 
  uint32_t      best_idx;
5855
 
  uint32_t      size_remain;    // cardinality of remaining_tables
5856
 
  POSITION  best_pos;
5857
 
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
5858
 
 
5859
 
  /* number of tables that remain to be optimized */
5860
 
  size_remain= my_count_bits(remaining_tables);
5861
 
 
5862
 
  do {
5863
 
    /* Find the extension of the current QEP with the lowest cost */
5864
 
    join->best_read= DBL_MAX;
5865
 
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
5866
 
                                         read_time, search_depth, prune_level))
5867
 
      return(true);
5868
 
 
5869
 
    if (size_remain <= search_depth)
5870
 
    {
5871
 
      /*
5872
 
        'join->best_positions' contains a complete optimal extension of the
5873
 
        current partial QEP.
5874
 
      */
5875
 
      return(false);
5876
 
    }
5877
 
 
5878
 
    /* select the first table in the optimal extension as most promising */
5879
 
    best_pos= join->best_positions[idx];
5880
 
    best_table= best_pos.table;
5881
 
    /*
5882
 
      Each subsequent loop of 'best_extension_by_limited_search' uses
5883
 
      'join->positions' for cost estimates, therefore we have to update its
5884
 
      value.
5885
 
    */
5886
 
    join->positions[idx]= best_pos;
5887
 
 
5888
 
    /* find the position of 'best_table' in 'join->best_ref' */
5889
 
    best_idx= idx;
5890
 
    JOIN_TAB *pos= join->best_ref[best_idx];
5891
 
    while (pos && best_table != pos)
5892
 
      pos= join->best_ref[++best_idx];
5893
 
    assert((pos != NULL)); // should always find 'best_table'
5894
 
    /* move 'best_table' at the first free position in the array of joins */
5895
 
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
5896
 
 
5897
 
    /* compute the cost of the new plan extended with 'best_table' */
5898
 
    record_count*= join->positions[idx].records_read;
5899
 
    read_time+=    join->positions[idx].read_time;
5900
 
 
5901
 
    remaining_tables&= ~(best_table->table->map);
5902
 
    --size_remain;
5903
 
    ++idx;
5904
 
  } while (true);
5905
 
}
5906
 
 
5907
 
 
5908
 
/**
5909
 
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
5910
 
  exhaustive search.
5911
 
 
5912
 
    The procedure searches for the optimal ordering of the query tables in set
5913
 
    'remaining_tables' of size N, and the corresponding optimal access paths to
5914
 
    each table. The choice of a table order and an access path for each table
5915
 
    constitutes a query execution plan (QEP) that fully specifies how to
5916
 
    execute the query.
5917
 
 
5918
 
    The maximal size of the found plan is controlled by the parameter
5919
 
    'search_depth'. When search_depth == N, the resulting plan is complete and
5920
 
    can be used directly as a QEP. If search_depth < N, the found plan consists
5921
 
    of only some of the query tables. Such "partial" optimal plans are useful
5922
 
    only as input to query optimization procedures, and cannot be used directly
5923
 
    to execute a query.
5924
 
 
5925
 
    The algorithm begins with an empty partial plan stored in 'join->positions'
5926
 
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
5927
 
    evaluates the cost of the partial plan extended by all access plans for
5928
 
    each of the relations in 'remaining_tables', expands the current partial
5929
 
    plan with the access plan that results in lowest cost of the expanded
5930
 
    partial plan, and removes the corresponding relation from
5931
 
    'remaining_tables'. The algorithm continues until it either constructs a
5932
 
    complete optimal plan, or constructs an optimal plartial plan with size =
5933
 
    search_depth.
5934
 
 
5935
 
    The final optimal plan is stored in 'join->best_positions'. The
5936
 
    corresponding cost of the optimal plan is in 'join->best_read'.
5937
 
 
5938
 
  @note
5939
 
    The procedure uses a recursive depth-first search where the depth of the
5940
 
    recursion (and thus the exhaustiveness of the search) is controlled by the
5941
 
    parameter 'search_depth'.
5942
 
 
5943
 
  @note
5944
 
    The pseudocode below describes the algorithm of
5945
 
    'best_extension_by_limited_search'. The worst-case complexity of this
5946
 
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
5947
 
    the complexity of greedy_search is O(N!).
5948
 
 
5949
 
    @code
5950
 
    procedure best_extension_by_limited_search(
5951
 
      pplan in,             // in, partial plan of tables-joined-so-far
5952
 
      pplan_cost,           // in, cost of pplan
5953
 
      remaining_tables,     // in, set of tables not referenced in pplan
5954
 
      best_plan_so_far,     // in/out, best plan found so far
5955
 
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
5956
 
      search_depth)         // in, maximum size of the plans being considered
5957
 
    {
5958
 
      for each table T from remaining_tables
5959
 
      {
5960
 
        // Calculate the cost of using table T as above
5961
 
        cost = complex-series-of-calculations;
5962
 
 
5963
 
        // Add the cost to the cost so far.
5964
 
        pplan_cost+= cost;
5965
 
 
5966
 
        if (pplan_cost >= best_plan_so_far_cost)
5967
 
          // pplan_cost already too great, stop search
5968
 
          continue;
5969
 
 
5970
 
        pplan= expand pplan by best_access_method;
5971
 
        remaining_tables= remaining_tables - table T;
5972
 
        if (remaining_tables is not an empty set
5973
 
            and
5974
 
            search_depth > 1)
5975
 
        {
5976
 
          best_extension_by_limited_search(pplan, pplan_cost,
5977
 
                                           remaining_tables,
5978
 
                                           best_plan_so_far,
5979
 
                                           best_plan_so_far_cost,
5980
 
                                           search_depth - 1);
5981
 
        }
5982
 
        else
5983
 
        {
5984
 
          best_plan_so_far_cost= pplan_cost;
5985
 
          best_plan_so_far= pplan;
5986
 
        }
5987
 
      }
5988
 
    }
5989
 
    @endcode
5990
 
 
5991
 
  @note
5992
 
    When 'best_extension_by_limited_search' is called for the first time,
5993
 
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
5994
 
    The actual implementation provides a way to optionally use pruning
5995
 
    heuristic (controlled by the parameter 'prune_level') to reduce the search
5996
 
    space by skipping some partial plans.
5997
 
 
5998
 
  @note
5999
 
    The parameter 'search_depth' provides control over the recursion
6000
 
    depth, and thus the size of the resulting optimal plan.
6001
 
 
6002
 
  @param join             pointer to the structure providing all context info
6003
 
                          for the query
6004
 
  @param remaining_tables set of tables not included into the partial plan yet
6005
 
  @param idx              length of the partial QEP in 'join->positions';
6006
 
                          since a depth-first search is used, also corresponds
6007
 
                          to the current depth of the search tree;
6008
 
                          also an index in the array 'join->best_ref';
6009
 
  @param record_count     estimate for the number of records returned by the
6010
 
                          best partial plan
6011
 
  @param read_time        the cost of the best partial plan
6012
 
  @param search_depth     maximum depth of the recursion and thus size of the
6013
 
                          found optimal plan
6014
 
                          (0 < search_depth <= join->tables+1).
6015
 
  @param prune_level      pruning heuristics that should be applied during
6016
 
                          optimization
6017
 
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
6018
 
 
6019
 
  @retval
6020
 
    false       ok
6021
 
  @retval
6022
 
    true        Fatal error
6023
 
*/
6024
 
 
6025
 
static bool
6026
 
best_extension_by_limited_search(JOIN      *join,
6027
 
                                 table_map remaining_tables,
6028
 
                                 uint32_t      idx,
6029
 
                                 double    record_count,
6030
 
                                 double    read_time,
6031
 
                                 uint32_t      search_depth,
6032
 
                                 uint32_t      prune_level)
6033
 
{
6034
 
  Session *session= join->session;
6035
 
  if (session->killed)  // Abort
6036
 
    return(true);
6037
 
 
6038
 
  /*
6039
 
     'join' is a partial plan with lower cost than the best plan so far,
6040
 
     so continue expanding it further with the tables in 'remaining_tables'.
6041
 
  */
6042
 
  JOIN_TAB *s;
6043
 
  double best_record_count= DBL_MAX;
6044
 
  double best_read_time=    DBL_MAX;
6045
 
 
6046
 
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
6047
 
  {
6048
 
    table_map real_table_bit= s->table->map;
6049
 
    if ((remaining_tables & real_table_bit) &&
6050
 
        !(remaining_tables & s->dependent) &&
6051
 
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6052
 
    {
6053
 
      double current_record_count, current_read_time;
6054
 
      advance_sj_state(remaining_tables, s);
6055
 
 
6056
 
      /*
6057
 
        psergey-insideout-todo:
6058
 
          when best_access_path() detects it could do an InsideOut scan or
6059
 
          some other scan, have it return an insideout scan and a flag that
6060
 
          requests to "fork" this loop iteration. (Q: how does that behave
6061
 
          when the depth is insufficient??)
6062
 
      */
6063
 
      /* Find the best access method from 's' to the current partial plan */
6064
 
      best_access_path(join, s, session, remaining_tables, idx,
6065
 
                       record_count, read_time);
6066
 
      /* Compute the cost of extending the plan with 's' */
6067
 
      current_record_count= record_count * join->positions[idx].records_read;
6068
 
      current_read_time=    read_time + join->positions[idx].read_time;
6069
 
 
6070
 
      /* Expand only partial plans with lower cost than the best QEP so far */
6071
 
      if ((current_read_time +
6072
 
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
6073
 
      {
6074
 
        restore_prev_nj_state(s);
6075
 
        restore_prev_sj_state(remaining_tables, s);
6076
 
        continue;
6077
 
      }
6078
 
 
6079
 
      /*
6080
 
        Prune some less promising partial plans. This heuristic may miss
6081
 
        the optimal QEPs, thus it results in a non-exhaustive search.
6082
 
      */
6083
 
      if (prune_level == 1)
6084
 
      {
6085
 
        if (best_record_count > current_record_count ||
6086
 
            best_read_time > current_read_time ||
6087
 
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
6088
 
        {
6089
 
          if (best_record_count >= current_record_count &&
6090
 
              best_read_time >= current_read_time &&
6091
 
              /* TODO: What is the reasoning behind this condition? */
6092
 
              (!(s->key_dependent & remaining_tables) ||
6093
 
               join->positions[idx].records_read < 2.0))
6094
 
          {
6095
 
            best_record_count= current_record_count;
6096
 
            best_read_time=    current_read_time;
6097
 
          }
6098
 
        }
6099
 
        else
6100
 
        {
6101
 
          restore_prev_nj_state(s);
6102
 
          restore_prev_sj_state(remaining_tables, s);
6103
 
          continue;
6104
 
        }
6105
 
      }
6106
 
 
6107
 
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
6108
 
      { /* Recursively expand the current partial plan */
6109
 
        std::swap(join->best_ref[idx], *pos);
6110
 
        if (best_extension_by_limited_search(join,
6111
 
                                             remaining_tables & ~real_table_bit,
6112
 
                                             idx + 1,
6113
 
                                             current_record_count,
6114
 
                                             current_read_time,
6115
 
                                             search_depth - 1,
6116
 
                                             prune_level))
6117
 
          return(true);
6118
 
        std::swap(join->best_ref[idx], *pos);
6119
 
      }
6120
 
      else
6121
 
      { /*
6122
 
          'join' is either the best partial QEP with 'search_depth' relations,
6123
 
          or the best complete QEP so far, whichever is smaller.
6124
 
        */
6125
 
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
6126
 
        if (join->sort_by_table &&
6127
 
            join->sort_by_table !=
6128
 
            join->positions[join->const_tables].table->table)
6129
 
          /* We have to make a temp table */
6130
 
          current_read_time+= current_record_count;
6131
 
        if ((search_depth == 1) || (current_read_time < join->best_read))
6132
 
        {
6133
 
          memcpy(join->best_positions, join->positions,
6134
 
                 sizeof(POSITION) * (idx + 1));
6135
 
          join->best_read= current_read_time - 0.001;
6136
 
        }
6137
 
      }
6138
 
      restore_prev_nj_state(s);
6139
 
      restore_prev_sj_state(remaining_tables, s);
6140
 
    }
6141
 
  }
6142
 
  return(false);
6143
 
}
6144
 
 
6145
 
 
6146
 
/**
6147
 
  @todo
6148
 
  - TODO: this function is here only temporarily until 'greedy_search' is
6149
 
  tested and accepted.
6150
 
 
6151
 
  RETURN VALUES
6152
 
    false       ok
6153
 
    true        Fatal error
6154
 
*/
6155
 
static bool
6156
 
find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count,
6157
 
          double read_time)
6158
 
{
6159
 
  Session *session= join->session;
6160
 
  if (session->killed)
6161
 
    return(true);
6162
 
  if (!rest_tables)
6163
 
  {
6164
 
    read_time+=record_count/(double) TIME_FOR_COMPARE;
6165
 
    if (join->sort_by_table &&
6166
 
        join->sort_by_table !=
6167
 
        join->positions[join->const_tables].table->table)
6168
 
      read_time+=record_count;                  // We have to make a temp table
6169
 
    if (read_time < join->best_read)
6170
 
    {
6171
 
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
6172
 
      join->best_read= read_time - 0.001;
6173
 
    }
6174
 
    return(false);
6175
 
  }
6176
 
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
6177
 
    return(false);                                      /* Found better before */
6178
 
 
6179
 
  JOIN_TAB *s;
6180
 
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
6181
 
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
6182
 
  {
6183
 
    table_map real_table_bit=s->table->map;
6184
 
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
6185
 
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
6186
 
    {
6187
 
      double records, best;
6188
 
      advance_sj_state(rest_tables, s);
6189
 
      best_access_path(join, s, session, rest_tables, idx, record_count,
6190
 
                       read_time);
6191
 
      records= join->positions[idx].records_read;
6192
 
      best= join->positions[idx].read_time;
6193
 
      /*
6194
 
        Go to the next level only if there hasn't been a better key on
6195
 
        this level! This will cut down the search for a lot simple cases!
6196
 
      */
6197
 
      double current_record_count=record_count*records;
6198
 
      double current_read_time=read_time+best;
6199
 
      if (best_record_count > current_record_count ||
6200
 
          best_read_time > current_read_time ||
6201
 
          (idx == join->const_tables && s->table == join->sort_by_table))
6202
 
      {
6203
 
        if (best_record_count >= current_record_count &&
6204
 
            best_read_time >= current_read_time &&
6205
 
            (!(s->key_dependent & rest_tables) || records < 2.0))
6206
 
        {
6207
 
          best_record_count=current_record_count;
6208
 
          best_read_time=current_read_time;
6209
 
        }
6210
 
        std::swap(join->best_ref[idx], *pos);
6211
 
        if (find_best(join,rest_tables & ~real_table_bit,idx+1,
6212
 
                      current_record_count,current_read_time))
6213
 
          return(true);
6214
 
        std::swap(join->best_ref[idx], *pos);
6215
 
      }
6216
 
      restore_prev_nj_state(s);
6217
 
      restore_prev_sj_state(rest_tables, s);
6218
 
      if (join->select_options & SELECT_STRAIGHT_JOIN)
6219
 
        break;                          // Don't test all combinations
6220
 
    }
6221
 
  }
6222
 
  return(false);
6223
 
}
6224
 
 
6225
 
 
6226
 
/**
6227
1926
  Find how much space the prevous read not const tables takes in cache.
6228
1927
*/
6229
 
 
6230
 
static void calc_used_field_length(Session *, JOIN_TAB *join_tab)
 
1928
void calc_used_field_length(Session *, JOIN_TAB *join_tab)
6231
1929
{
6232
1930
  uint32_t null_fields,blobs,fields,rec_length;
6233
1931
  Field **f_ptr,*field;
6241
1939
      fields++;
6242
1940
      rec_length+=field->pack_length();
6243
1941
      if (flags & BLOB_FLAG)
6244
 
        blobs++;
 
1942
        blobs++;
6245
1943
      if (!(flags & NOT_NULL_FLAG))
6246
 
        null_fields++;
 
1944
        null_fields++;
6247
1945
    }
6248
1946
  }
6249
1947
  if (null_fields)
6261
1959
  join_tab->used_blobs= blobs;
6262
1960
}
6263
1961
 
6264
 
 
6265
 
static uint
6266
 
cache_record_length(JOIN *join,uint32_t idx)
6267
 
{
6268
 
  uint32_t length=0;
6269
 
  JOIN_TAB **pos,**end;
6270
 
  Session *session=join->session;
6271
 
 
6272
 
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
6273
 
       pos != end ;
6274
 
       pos++)
6275
 
  {
6276
 
    JOIN_TAB *join_tab= *pos;
6277
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
6278
 
      calc_used_field_length(session, join_tab);
6279
 
    length+=join_tab->used_fieldlength;
6280
 
  }
6281
 
  return length;
6282
 
}
6283
 
 
6284
 
 
6285
 
/*
6286
 
  Get the number of different row combinations for subset of partial join
6287
 
 
6288
 
  SYNOPSIS
6289
 
    prev_record_reads()
6290
 
      join       The join structure
6291
 
      idx        Number of tables in the partial join order (i.e. the
6292
 
                 partial join order is in join->positions[0..idx-1])
6293
 
      found_ref  Bitmap of tables for which we need to find # of distinct
6294
 
                 row combinations.
6295
 
 
6296
 
  DESCRIPTION
6297
 
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
6298
 
    tables within that join order (specified in found_ref), find out how many
6299
 
    distinct row combinations of subset tables will be in the result of the
6300
 
    partial join order.
6301
 
 
6302
 
    This is used as follows: Suppose we have a table accessed with a ref-based
6303
 
    method. The ref access depends on current rows of tables in found_ref.
6304
 
    We want to count # of different ref accesses. We assume two ref accesses
6305
 
    will be different if at least one of access parameters is different.
6306
 
    Example: consider a query
6307
 
 
6308
 
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
6309
 
 
6310
 
    and a join order:
6311
 
      t1,  ref access on t1.key=c1
6312
 
      t2,  ref access on t2.key=c2
6313
 
      t3,  ref access on t3.key=t1.field
6314
 
 
6315
 
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
6316
 
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
6317
 
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
6318
 
            n_distinct_ref_scans = #records_read(t1)
6319
 
 
6320
 
    The reason for having this function (at least the latest version of it)
6321
 
    is that we need to account for buffering in join execution.
6322
 
 
6323
 
    An edge-case example: if we have a non-first table in join accessed via
6324
 
    ref(const) or ref(param) where there is a small number of different
6325
 
    values of param, then the access will likely hit the disk cache and will
6326
 
    not require any disk seeks.
6327
 
 
6328
 
    The proper solution would be to assume an LRU disk cache of some size,
6329
 
    calculate probability of cache hits, etc. For now we just count
6330
 
    identical ref accesses as one.
6331
 
 
6332
 
  RETURN
6333
 
    Expected number of row combinations
6334
 
*/
6335
 
 
6336
 
static double
6337
 
prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
6338
 
{
6339
 
  double found=1.0;
6340
 
  POSITION *pos_end= join->positions - 1;
6341
 
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
6342
 
  {
6343
 
    if (pos->table->table->map & found_ref)
6344
 
    {
6345
 
      found_ref|= pos->ref_depend_map;
6346
 
      /*
6347
 
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
6348
 
        with no matching row we will get position[t2].records_read==0.
6349
 
        Actually the size of output is one null-complemented row, therefore
6350
 
        we will use value of 1 whenever we get records_read==0.
6351
 
 
6352
 
        Note
6353
 
        - the above case can't occur if inner part of outer join has more
6354
 
          than one table: table with no matches will not be marked as const.
6355
 
 
6356
 
        - Ideally we should add 1 to records_read for every possible null-
6357
 
          complemented row. We're not doing it because: 1. it will require
6358
 
          non-trivial code and add overhead. 2. The value of records_read
6359
 
          is an inprecise estimate and adding 1 (or, in the worst case,
6360
 
          #max_nested_outer_joins=64-1) will not make it any more precise.
6361
 
      */
6362
 
      if (pos->records_read > DBL_EPSILON)
6363
 
        found*= pos->records_read;
6364
 
    }
6365
 
  }
6366
 
  return found;
6367
 
}
6368
 
 
 
1962
StoredKey *get_store_key(Session *session,
 
1963
                         KEYUSE *keyuse,
 
1964
                         table_map used_tables,
 
1965
                               KEY_PART_INFO *key_part,
 
1966
                         unsigned char *key_buff,
 
1967
                         uint32_t maybe_null)
 
1968
{
 
1969
  if (!((~used_tables) & keyuse->used_tables))          // if const item
 
1970
  {
 
1971
    return new store_key_const_item(session,
 
1972
                                    key_part->field,
 
1973
                                    key_buff + maybe_null,
 
1974
                                    maybe_null ? key_buff : 0,
 
1975
                                    key_part->length,
 
1976
                                    keyuse->val);
 
1977
  }
 
1978
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
 
1979
           (keyuse->val->type() == Item::REF_ITEM &&
 
1980
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
 
1981
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
 
1982
             Item_ref::DIRECT_REF &&
 
1983
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
 
1984
    return new store_key_field(session,
 
1985
                               key_part->field,
 
1986
                               key_buff + maybe_null,
 
1987
                               maybe_null ? key_buff : 0,
 
1988
                               key_part->length,
 
1989
                               ((Item_field*) keyuse->val->real_item())->field,
 
1990
                               keyuse->val->full_name());
 
1991
  return new store_key_item(session,
 
1992
                            key_part->field,
 
1993
                            key_buff + maybe_null,
 
1994
                            maybe_null ? key_buff : 0,
 
1995
                            key_part->length,
 
1996
                            keyuse->val);
 
1997
}
6369
1998
 
6370
1999
/**
6371
 
  Set up join struct according to best position.
 
2000
  This function is only called for const items on fields which are keys.
 
2001
 
 
2002
  @return
 
2003
    returns 1 if there was some conversion made when the field was stored.
6372
2004
*/
6373
 
 
6374
 
static bool
6375
 
get_best_combination(JOIN *join)
6376
 
{
6377
 
  uint32_t i,tablenr;
6378
 
  table_map used_tables;
6379
 
  JOIN_TAB *join_tab,*j;
6380
 
  KEYUSE *keyuse;
6381
 
  uint32_t table_count;
6382
 
  Session *session=join->session;
6383
 
 
6384
 
  table_count=join->tables;
6385
 
  if (!(join->join_tab=join_tab=
6386
 
        (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
6387
 
    return(true);
6388
 
 
6389
 
  join->full_join=0;
6390
 
 
6391
 
  used_tables= OUTER_REF_TABLE_BIT;             // Outer row is already read
6392
 
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
2005
bool store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
 
2006
{
 
2007
  bool error;
 
2008
  Table *table= field->table;
 
2009
  Session *session= table->in_use;
 
2010
  ha_rows cuted_fields=session->cuted_fields;
 
2011
 
 
2012
  /*
 
2013
    we should restore old value of count_cuted_fields because
 
2014
    store_val_in_field can be called from mysql_insert
 
2015
    with select_insert, which make count_cuted_fields= 1
 
2016
   */
 
2017
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
 
2018
  session->count_cuted_fields= check_flag;
 
2019
  error= item->save_in_field(field, 1);
 
2020
  session->count_cuted_fields= old_count_cuted_fields;
 
2021
  return error || cuted_fields != session->cuted_fields;
 
2022
}
 
2023
 
 
2024
inline void add_cond_and_fix(Item **e1, Item *e2)
 
2025
{
 
2026
  if (*e1)
6393
2027
  {
6394
 
    Table *form;
6395
 
    *j= *join->best_positions[tablenr].table;
6396
 
    form=join->table[tablenr]=j->table;
6397
 
    used_tables|= form->map;
6398
 
    form->reginfo.join_tab=j;
6399
 
    if (!*j->on_expr_ref)
6400
 
      form->reginfo.not_exists_optimize=0;      // Only with LEFT JOIN
6401
 
    if (j->type == JT_CONST)
6402
 
      continue;                                 // Handled in make_join_stat..
6403
 
 
6404
 
    j->ref.key = -1;
6405
 
    j->ref.key_parts=0;
6406
 
 
6407
 
    if (j->type == JT_SYSTEM)
6408
 
      continue;
6409
 
    if (j->keys.none() || !(keyuse= join->best_positions[tablenr].key))
 
2028
    Item *res;
 
2029
    if ((res= new Item_cond_and(*e1, e2)))
6410
2030
    {
6411
 
      j->type=JT_ALL;
6412
 
      if (tablenr != join->const_tables)
6413
 
        join->full_join=1;
 
2031
      *e1= res;
 
2032
      res->quick_fix_field();
6414
2033
    }
6415
 
    else if (create_ref_for_key(join, j, keyuse, used_tables))
6416
 
      return(true);                        // Something went wrong
6417
2034
  }
6418
 
 
6419
 
  for (i=0 ; i < table_count ; i++)
6420
 
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
6421
 
  update_depend_map(join);
6422
 
  return(0);
 
2035
  else
 
2036
    *e1= e2;
6423
2037
}
6424
2038
 
6425
 
 
6426
 
static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
6427
 
                               table_map used_tables)
 
2039
bool create_ref_for_key(JOIN *join, JOIN_TAB *j, KEYUSE *org_keyuse,
 
2040
             table_map used_tables)
6428
2041
{
6429
2042
  KEYUSE *keyuse=org_keyuse;
6430
2043
  Session  *session= join->session;
6449
2062
    {
6450
2063
      if (!(~used_tables & keyuse->used_tables))
6451
2064
      {
6452
 
        if (keyparts == keyuse->keypart &&
6453
 
            !(found_part_ref_or_null & keyuse->optimize))
6454
 
        {
6455
 
          keyparts++;
6456
 
          length+= keyinfo->key_part[keyuse->keypart].store_length;
6457
 
          found_part_ref_or_null|= keyuse->optimize;
6458
 
        }
 
2065
        if (keyparts == keyuse->keypart &&
 
2066
            !(found_part_ref_or_null & keyuse->optimize))
 
2067
        {
 
2068
          keyparts++;
 
2069
          length+= keyinfo->key_part[keyuse->keypart].store_length;
 
2070
          found_part_ref_or_null|= keyuse->optimize;
 
2071
        }
6459
2072
      }
6460
2073
      keyuse++;
6461
2074
    } while (keyuse->table == table && keyuse->key == key);
6467
2080
  j->ref.key_length=length;
6468
2081
  j->ref.key=(int) key;
6469
2082
  if (!(j->ref.key_buff= (unsigned char*) session->calloc(ALIGN_SIZE(length)*2)) ||
6470
 
      !(j->ref.key_copy= (store_key**) session->alloc((sizeof(store_key*) *
6471
 
                                                   (keyparts+1)))) ||
 
2083
      !(j->ref.key_copy= (StoredKey**) session->alloc((sizeof(StoredKey*) *
 
2084
               (keyparts+1)))) ||
6472
2085
      !(j->ref.items=    (Item**) session->alloc(sizeof(Item*)*keyparts)) ||
6473
2086
      !(j->ref.cond_guards= (bool**) session->alloc(sizeof(uint*)*keyparts)))
6474
2087
  {
6480
2093
  j->ref.disable_cache= false;
6481
2094
  keyuse=org_keyuse;
6482
2095
 
6483
 
  store_key **ref_key= j->ref.key_copy;
 
2096
  StoredKey **ref_key= j->ref.key_copy;
6484
2097
  unsigned char *key_buff=j->ref.key_buff, *null_ref_key= 0;
6485
2098
  bool keyuse_uses_no_tables= true;
6486
2099
  {
6488
2101
    for (i=0 ; i < keyparts ; keyuse++,i++)
6489
2102
    {
6490
2103
      while (keyuse->keypart != i ||
6491
 
             ((~used_tables) & keyuse->used_tables))
6492
 
        keyuse++;                               /* Skip other parts */
 
2104
       ((~used_tables) & keyuse->used_tables))
 
2105
        keyuse++;       /* Skip other parts */
6493
2106
 
6494
2107
      uint32_t maybe_null= test(keyinfo->key_part[i].null_bit);
6495
 
      j->ref.items[i]=keyuse->val;              // Save for cond removal
 
2108
      j->ref.items[i]=keyuse->val;    // Save for cond removal
6496
2109
      j->ref.cond_guards[i]= keyuse->cond_guard;
6497
2110
      if (keyuse->null_rejecting)
6498
2111
        j->ref.null_rejecting |= 1 << i;
6499
2112
      keyuse_uses_no_tables= keyuse_uses_no_tables && !keyuse->used_tables;
6500
 
      if (!keyuse->used_tables &&
6501
 
          !(join->select_options & SELECT_DESCRIBE))
6502
 
      {                                 // Compare against constant
6503
 
        store_key_item tmp(session, keyinfo->key_part[i].field,
 
2113
      if (!keyuse->used_tables &&  !(join->select_options & SELECT_DESCRIBE))
 
2114
      {         // Compare against constant
 
2115
        store_key_item tmp(session, keyinfo->key_part[i].field,
6504
2116
                           key_buff + maybe_null,
6505
2117
                           maybe_null ?  key_buff : 0,
6506
2118
                           keyinfo->key_part[i].length, keyuse->val);
6507
 
        if (session->is_fatal_error)
6508
 
          return(true);
6509
 
        tmp.copy();
 
2119
        if (session->is_fatal_error)
 
2120
          return(true);
 
2121
        tmp.copy();
6510
2122
      }
6511
2123
      else
6512
 
        *ref_key++= get_store_key(session,
6513
 
                                  keyuse,join->const_table_map,
6514
 
                                  &keyinfo->key_part[i],
6515
 
                                  key_buff, maybe_null);
 
2124
        *ref_key++= get_store_key(session,
 
2125
          keyuse,join->const_table_map,
 
2126
          &keyinfo->key_part[i],
 
2127
          key_buff, maybe_null);
6516
2128
      /*
6517
 
        Remember if we are going to use REF_OR_NULL
6518
 
        But only if field _really_ can be null i.e. we force JT_REF
6519
 
        instead of JT_REF_OR_NULL in case if field can't be null
 
2129
        Remember if we are going to use REF_OR_NULL
 
2130
        But only if field _really_ can be null i.e. we force JT_REF
 
2131
        instead of JT_REF_OR_NULL in case if field can't be null
6520
2132
      */
6521
2133
      if ((keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL) && maybe_null)
6522
 
        null_ref_key= key_buff;
 
2134
        null_ref_key= key_buff;
6523
2135
      key_buff+=keyinfo->key_part[i].store_length;
6524
2136
    }
6525
2137
  }
6526
 
  *ref_key=0;                           // end_marker
 
2138
  *ref_key=0;       // end_marker
6527
2139
  if (j->type == JT_CONST)
6528
2140
    j->table->const_table= 1;
6529
2141
  else if (((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) != HA_NOSAME) ||
6549
2161
  return(0);
6550
2162
}
6551
2163
 
6552
 
 
6553
 
 
6554
 
static store_key *
6555
 
get_store_key(Session *session, KEYUSE *keyuse, table_map used_tables,
6556
 
              KEY_PART_INFO *key_part, unsigned char *key_buff, uint32_t maybe_null)
6557
 
{
6558
 
  if (!((~used_tables) & keyuse->used_tables))          // if const item
6559
 
  {
6560
 
    return new store_key_const_item(session,
6561
 
                                    key_part->field,
6562
 
                                    key_buff + maybe_null,
6563
 
                                    maybe_null ? key_buff : 0,
6564
 
                                    key_part->length,
6565
 
                                    keyuse->val);
6566
 
  }
6567
 
  else if (keyuse->val->type() == Item::FIELD_ITEM ||
6568
 
           (keyuse->val->type() == Item::REF_ITEM &&
6569
 
            ((Item_ref*)keyuse->val)->ref_type() == Item_ref::OUTER_REF &&
6570
 
            (*(Item_ref**)((Item_ref*)keyuse->val)->ref)->ref_type() ==
6571
 
             Item_ref::DIRECT_REF &&
6572
 
            keyuse->val->real_item()->type() == Item::FIELD_ITEM))
6573
 
    return new store_key_field(session,
6574
 
                               key_part->field,
6575
 
                               key_buff + maybe_null,
6576
 
                               maybe_null ? key_buff : 0,
6577
 
                               key_part->length,
6578
 
                               ((Item_field*) keyuse->val->real_item())->field,
6579
 
                               keyuse->val->full_name());
6580
 
  return new store_key_item(session,
6581
 
                            key_part->field,
6582
 
                            key_buff + maybe_null,
6583
 
                            maybe_null ? key_buff : 0,
6584
 
                            key_part->length,
6585
 
                            keyuse->val);
6586
 
}
6587
 
 
6588
 
/**
6589
 
  This function is only called for const items on fields which are keys.
6590
 
 
6591
 
  @return
6592
 
    returns 1 if there was some conversion made when the field was stored.
6593
 
*/
6594
 
 
6595
 
bool
6596
 
store_val_in_field(Field *field, Item *item, enum_check_fields check_flag)
6597
 
{
6598
 
  bool error;
6599
 
  Table *table= field->table;
6600
 
  Session *session= table->in_use;
6601
 
  ha_rows cuted_fields=session->cuted_fields;
6602
 
 
6603
 
  /*
6604
 
    we should restore old value of count_cuted_fields because
6605
 
    store_val_in_field can be called from mysql_insert
6606
 
    with select_insert, which make count_cuted_fields= 1
6607
 
   */
6608
 
  enum_check_fields old_count_cuted_fields= session->count_cuted_fields;
6609
 
  session->count_cuted_fields= check_flag;
6610
 
  error= item->save_in_field(field, 1);
6611
 
  session->count_cuted_fields= old_count_cuted_fields;
6612
 
  return error || cuted_fields != session->cuted_fields;
6613
 
}
6614
 
 
6615
 
 
6616
 
static bool
6617
 
make_simple_join(JOIN *join,Table *tmp_table)
6618
 
{
6619
 
  Table **tableptr;
6620
 
  JOIN_TAB *join_tab;
6621
 
 
6622
 
  /*
6623
 
    Reuse Table * and JOIN_TAB if already allocated by a previous call
6624
 
    to this function through JOIN::exec (may happen for sub-queries).
6625
 
  */
6626
 
  if (!join->table_reexec)
6627
 
  {
6628
 
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
6629
 
      return(true);                        /* purecov: inspected */
6630
 
    if (join->tmp_join)
6631
 
      join->tmp_join->table_reexec= join->table_reexec;
6632
 
  }
6633
 
  if (!join->join_tab_reexec)
6634
 
  {
6635
 
    if (!(join->join_tab_reexec=
6636
 
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
6637
 
      return(true);                        /* purecov: inspected */
6638
 
    if (join->tmp_join)
6639
 
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
6640
 
  }
6641
 
  tableptr= join->table_reexec;
6642
 
  join_tab= join->join_tab_reexec;
6643
 
 
6644
 
  join->join_tab=join_tab;
6645
 
  join->table=tableptr; tableptr[0]=tmp_table;
6646
 
  join->tables=1;
6647
 
  join->const_tables=0;
6648
 
  join->const_table_map=0;
6649
 
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
6650
 
    join->tmp_table_param.func_count=0;
6651
 
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
6652
 
  join->first_record=join->sort_and_group=0;
6653
 
  join->send_records=(ha_rows) 0;
6654
 
  join->group=0;
6655
 
  join->row_limit=join->unit->select_limit_cnt;
6656
 
  join->do_send_rows = (join->row_limit) ? 1 : 0;
6657
 
 
6658
 
  join_tab->cache.buff=0;                       /* No caching */
6659
 
  join_tab->table=tmp_table;
6660
 
  join_tab->select=0;
6661
 
  join_tab->select_cond=0;
6662
 
  join_tab->quick=0;
6663
 
  join_tab->type= JT_ALL;                       /* Map through all records */
6664
 
  join_tab->keys.set();                     /* test everything in quick */
6665
 
  join_tab->info=0;
6666
 
  join_tab->on_expr_ref=0;
6667
 
  join_tab->last_inner= 0;
6668
 
  join_tab->first_unmatched= 0;
6669
 
  join_tab->ref.key = -1;
6670
 
  join_tab->not_used_in_distinct=0;
6671
 
  join_tab->read_first_record= join_init_read_record;
6672
 
  join_tab->join=join;
6673
 
  join_tab->ref.key_parts= 0;
6674
 
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
6675
 
  join_tab->do_firstmatch= NULL;
6676
 
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
6677
 
  tmp_table->status=0;
6678
 
  tmp_table->null_row=0;
6679
 
  return(false);
6680
 
}
6681
 
 
6682
 
 
6683
 
inline void add_cond_and_fix(Item **e1, Item *e2)
6684
 
{
6685
 
  if (*e1)
6686
 
  {
6687
 
    Item *res;
6688
 
    if ((res= new Item_cond_and(*e1, e2)))
6689
 
    {
6690
 
      *e1= res;
6691
 
      res->quick_fix_field();
6692
 
    }
6693
 
  }
6694
 
  else
6695
 
    *e1= e2;
6696
 
}
6697
 
 
6698
 
 
6699
2164
/**
6700
2165
  Add to join_tab->select_cond[i] "table.field IS NOT NULL" conditions
6701
2166
  we've inferred from ref/eq_ref access performed.
6746
2211
      3. add_not_null_conds adds "x IS NOT NULL" to join_tab->select_cond of
6747
2212
         appropiate JOIN_TAB members.
6748
2213
*/
6749
 
 
6750
 
static void add_not_null_conds(JOIN *join)
 
2214
void add_not_null_conds(JOIN *join)
6751
2215
{
6752
2216
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
6753
2217
  {
6805
2269
    -  pointer to the guarded predicate, if success
6806
2270
    -  0, otherwise
6807
2271
*/
6808
 
 
6809
 
static COND*
6810
 
add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
 
2272
COND *add_found_match_trig_cond(JOIN_TAB *tab, COND *cond, JOIN_TAB *root_tab)
6811
2273
{
6812
2274
  COND *tmp;
6813
2275
  assert(cond != 0);
6823
2285
  return tmp;
6824
2286
}
6825
2287
 
6826
 
 
6827
 
/**
6828
 
  Fill in outer join related info for the execution plan structure.
6829
 
 
6830
 
    For each outer join operation left after simplification of the
6831
 
    original query the function set up the following pointers in the linear
6832
 
    structure join->join_tab representing the selected execution plan.
6833
 
    The first inner table t0 for the operation is set to refer to the last
6834
 
    inner table tk through the field t0->last_inner.
6835
 
    Any inner table ti for the operation are set to refer to the first
6836
 
    inner table ti->first_inner.
6837
 
    The first inner table t0 for the operation is set to refer to the
6838
 
    first inner table of the embedding outer join operation, if there is any,
6839
 
    through the field t0->first_upper.
6840
 
    The on expression for the outer join operation is attached to the
6841
 
    corresponding first inner table through the field t0->on_expr_ref.
6842
 
    Here ti are structures of the JOIN_TAB type.
6843
 
 
6844
 
  EXAMPLE. For the query:
6845
 
  @code
6846
 
        SELECT * FROM t1
6847
 
                      LEFT JOIN
6848
 
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
6849
 
                      ON (t1.a=t2.a AND t1.b=t3.b)
6850
 
          WHERE t1.c > 5,
6851
 
  @endcode
6852
 
 
6853
 
    given the execution plan with the table order t1,t2,t3,t4
6854
 
    is selected, the following references will be set;
6855
 
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
6856
 
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
6857
 
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
6858
 
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
6859
 
 
6860
 
  @param join   reference to the info fully describing the query
6861
 
 
6862
 
  @note
6863
 
    The function assumes that the simplification procedure has been
6864
 
    already applied to the join query (see simplify_joins).
6865
 
    This function can be called only after the execution plan
6866
 
    has been chosen.
6867
 
*/
6868
 
 
6869
 
static void
6870
 
make_outerjoin_info(JOIN *join)
6871
 
{
6872
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
6873
 
  {
6874
 
    JOIN_TAB *tab=join->join_tab+i;
6875
 
    Table *table=tab->table;
6876
 
    TableList *tbl= table->pos_in_table_list;
6877
 
    TableList *embedding= tbl->embedding;
6878
 
 
6879
 
    if (tbl->outer_join)
6880
 
    {
6881
 
      /*
6882
 
        Table tab is the only one inner table for outer join.
6883
 
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
6884
 
        is in the query above.)
6885
 
      */
6886
 
      tab->last_inner= tab->first_inner= tab;
6887
 
      tab->on_expr_ref= &tbl->on_expr;
6888
 
      tab->cond_equal= tbl->cond_equal;
6889
 
      if (embedding)
6890
 
        tab->first_upper= embedding->nested_join->first_nested;
6891
 
    }
6892
 
    for ( ; embedding ; embedding= embedding->embedding)
6893
 
    {
6894
 
      /* Ignore sj-nests: */
6895
 
      if (!embedding->on_expr)
6896
 
        continue;
6897
 
      nested_join_st *nested_join= embedding->nested_join;
6898
 
      if (!nested_join->counter_)
6899
 
      {
6900
 
        /*
6901
 
          Table tab is the first inner table for nested_join.
6902
 
          Save reference to it in the nested join structure.
6903
 
        */
6904
 
        nested_join->first_nested= tab;
6905
 
        tab->on_expr_ref= &embedding->on_expr;
6906
 
        tab->cond_equal= tbl->cond_equal;
6907
 
        if (embedding->embedding)
6908
 
          tab->first_upper= embedding->embedding->nested_join->first_nested;
6909
 
      }
6910
 
      if (!tab->first_inner)
6911
 
        tab->first_inner= nested_join->first_nested;
6912
 
      if (++nested_join->counter_ < nested_join->join_list.elements)
6913
 
        break;
6914
 
      /* Table tab is the last inner table for nested join. */
6915
 
      nested_join->first_nested->last_inner= tab;
6916
 
    }
6917
 
  }
6918
 
  return;
6919
 
}
6920
 
 
6921
 
 
6922
 
static bool
6923
 
make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
6924
 
{
6925
 
  Session *session= join->session;
6926
 
  if (select)
6927
 
  {
6928
 
    add_not_null_conds(join);
6929
 
    table_map used_tables;
6930
 
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
6931
 
    {                        /* there may be a select without a cond. */
6932
 
      if (join->tables > 1)
6933
 
        cond->update_used_tables();             // Tablenr may have changed
6934
 
      if (join->const_tables == join->tables &&
6935
 
          session->lex->current_select->master_unit() ==
6936
 
          &session->lex->unit)          // not upper level SELECT
6937
 
        join->const_table_map|=RAND_TABLE_BIT;
6938
 
      {                                         // Check const tables
6939
 
        COND *const_cond=
6940
 
          make_cond_for_table(cond,
6941
 
                              join->const_table_map,
6942
 
                              (table_map) 0, 1);
6943
 
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
6944
 
             tab < join->join_tab+join->tables ; tab++)
6945
 
        {
6946
 
          if (*tab->on_expr_ref)
6947
 
          {
6948
 
            JOIN_TAB *cond_tab= tab->first_inner;
6949
 
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
6950
 
                                           join->const_table_map,
6951
 
                                           (  table_map) 0, 0);
6952
 
            if (!tmp)
6953
 
              continue;
6954
 
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
6955
 
            if (!tmp)
6956
 
              return(1);
6957
 
            tmp->quick_fix_field();
6958
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
6959
 
                                    new Item_cond_and(cond_tab->select_cond,
6960
 
                                                      tmp);
6961
 
            if (!cond_tab->select_cond)
6962
 
              return(1);
6963
 
            cond_tab->select_cond->quick_fix_field();
6964
 
          }
6965
 
        }
6966
 
        if (const_cond && !const_cond->val_int())
6967
 
        {
6968
 
          return(1);     // Impossible const condition
6969
 
        }
6970
 
      }
6971
 
    }
6972
 
    used_tables=((select->const_tables=join->const_table_map) |
6973
 
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
6974
 
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
6975
 
    {
6976
 
      JOIN_TAB *tab=join->join_tab+i;
6977
 
      /*
6978
 
        first_inner is the X in queries like:
6979
 
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
6980
 
      */
6981
 
      JOIN_TAB *first_inner_tab= tab->first_inner;
6982
 
      table_map current_map= tab->table->map;
6983
 
      bool use_quick_range=0;
6984
 
      COND *tmp;
6985
 
 
6986
 
      /*
6987
 
        Following force including random expression in last table condition.
6988
 
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
6989
 
      */
6990
 
      if (i == join->tables-1)
6991
 
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
6992
 
      used_tables|=current_map;
6993
 
 
6994
 
      if (tab->type == JT_REF && tab->quick &&
6995
 
          (uint32_t) tab->ref.key == tab->quick->index &&
6996
 
          tab->ref.key_length < tab->quick->max_used_key_length)
6997
 
      {
6998
 
        /* Range uses longer key;  Use this instead of ref on key */
6999
 
        tab->type=JT_ALL;
7000
 
        use_quick_range=1;
7001
 
        tab->use_quick=1;
7002
 
        tab->ref.key= -1;
7003
 
        tab->ref.key_parts=0;           // Don't use ref key.
7004
 
        join->best_positions[i].records_read= rows2double(tab->quick->records);
7005
 
        /*
7006
 
          We will use join cache here : prevent sorting of the first
7007
 
          table only and sort at the end.
7008
 
        */
7009
 
        if (i != join->const_tables && join->tables > join->const_tables + 1)
7010
 
          join->full_join= 1;
7011
 
      }
7012
 
 
7013
 
      tmp= NULL;
7014
 
      if (cond)
7015
 
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
7016
 
      if (cond && !tmp && tab->quick)
7017
 
      {                                         // Outer join
7018
 
        if (tab->type != JT_ALL)
7019
 
        {
7020
 
          /*
7021
 
            Don't use the quick method
7022
 
            We come here in the case where we have 'key=constant' and
7023
 
            the test is removed by make_cond_for_table()
7024
 
          */
7025
 
          delete tab->quick;
7026
 
          tab->quick= 0;
7027
 
        }
7028
 
        else
7029
 
        {
7030
 
          /*
7031
 
            Hack to handle the case where we only refer to a table
7032
 
            in the ON part of an OUTER JOIN. In this case we want the code
7033
 
            below to check if we should use 'quick' instead.
7034
 
          */
7035
 
          tmp= new Item_int((int64_t) 1,1);     // Always true
7036
 
        }
7037
 
 
7038
 
      }
7039
 
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
7040
 
          tab->type == JT_EQ_REF)
7041
 
      {
7042
 
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
7043
 
                                       session->memdup((unsigned char*) select,
7044
 
                                                   sizeof(*select)));
7045
 
        if (!sel)
7046
 
          return(1);                    // End of memory
7047
 
        /*
7048
 
          If tab is an inner table of an outer join operation,
7049
 
          add a match guard to the pushed down predicate.
7050
 
          The guard will turn the predicate on only after
7051
 
          the first match for outer tables is encountered.
7052
 
        */
7053
 
        if (cond && tmp)
7054
 
        {
7055
 
          /*
7056
 
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
7057
 
            a cond, so neutralize the hack above.
7058
 
          */
7059
 
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
7060
 
            return(1);
7061
 
          tab->select_cond=sel->cond=tmp;
7062
 
          /* Push condition to storage engine if this is enabled
7063
 
             and the condition is not guarded */
7064
 
          tab->table->file->pushed_cond= NULL;
7065
 
          if (session->variables.engine_condition_pushdown)
7066
 
          {
7067
 
            COND *push_cond=
7068
 
              make_cond_for_table(tmp, current_map, current_map, 0);
7069
 
            if (push_cond)
7070
 
            {
7071
 
              /* Push condition to handler */
7072
 
              if (!tab->table->file->cond_push(push_cond))
7073
 
                tab->table->file->pushed_cond= push_cond;
7074
 
            }
7075
 
          }
7076
 
        }
7077
 
        else
7078
 
          tab->select_cond= sel->cond= NULL;
7079
 
 
7080
 
        sel->head=tab->table;
7081
 
        if (tab->quick)
7082
 
        {
7083
 
          /* Use quick key read if it's a constant and it's not used
7084
 
             with key reading */
7085
 
          if (tab->needed_reg.none() && tab->type != JT_EQ_REF
7086
 
              && (tab->type != JT_REF || (uint32_t) tab->ref.key == tab->quick->index))
7087
 
          {
7088
 
            sel->quick=tab->quick;              // Use value from get_quick_...
7089
 
            sel->quick_keys.reset();
7090
 
            sel->needed_reg.reset();
7091
 
          }
7092
 
          else
7093
 
          {
7094
 
            delete tab->quick;
7095
 
          }
7096
 
          tab->quick=0;
7097
 
        }
7098
 
        uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
7099
 
        if (i == join->const_tables && ref_key)
7100
 
        {
7101
 
          if (tab->const_keys.any() &&
7102
 
              tab->table->reginfo.impossible_range)
7103
 
            return(1);
7104
 
        }
7105
 
        else if (tab->type == JT_ALL && ! use_quick_range)
7106
 
        {
7107
 
          if (tab->const_keys.any() &&
7108
 
              tab->table->reginfo.impossible_range)
7109
 
            return(1);                          // Impossible range
7110
 
          /*
7111
 
            We plan to scan all rows.
7112
 
            Check again if we should use an index.
7113
 
            We could have used an column from a previous table in
7114
 
            the index if we are using limit and this is the first table
7115
 
          */
7116
 
 
7117
 
          if ((cond && (!((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
7118
 
              (!tab->const_keys.none() && (i == join->const_tables) && (join->unit->select_limit_cnt < join->best_positions[i].records_read) && ((join->select_options & OPTION_FOUND_ROWS) == false)))
7119
 
          {
7120
 
            /* Join with outer join condition */
7121
 
            COND *orig_cond=sel->cond;
7122
 
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
7123
 
 
7124
 
            /*
7125
 
              We can't call sel->cond->fix_fields,
7126
 
              as it will break tab->on_expr if it's AND condition
7127
 
              (fix_fields currently removes extra AND/OR levels).
7128
 
              Yet attributes of the just built condition are not needed.
7129
 
              Thus we call sel->cond->quick_fix_field for safety.
7130
 
            */
7131
 
            if (sel->cond && !sel->cond->fixed)
7132
 
              sel->cond->quick_fix_field();
7133
 
 
7134
 
            if (sel->test_quick_select(session, tab->keys,
7135
 
                                       used_tables & ~ current_map,
7136
 
                                       (join->select_options &
7137
 
                                        OPTION_FOUND_ROWS ?
7138
 
                                        HA_POS_ERROR :
7139
 
                                        join->unit->select_limit_cnt), 0,
7140
 
                                        false) < 0)
7141
 
            {
7142
 
              /*
7143
 
                Before reporting "Impossible WHERE" for the whole query
7144
 
                we have to check isn't it only "impossible ON" instead
7145
 
              */
7146
 
              sel->cond=orig_cond;
7147
 
              if (!*tab->on_expr_ref ||
7148
 
                  sel->test_quick_select(session, tab->keys,
7149
 
                                         used_tables & ~ current_map,
7150
 
                                         (join->select_options &
7151
 
                                          OPTION_FOUND_ROWS ?
7152
 
                                          HA_POS_ERROR :
7153
 
                                          join->unit->select_limit_cnt),0,
7154
 
                                          false) < 0)
7155
 
                return(1);                      // Impossible WHERE
7156
 
            }
7157
 
            else
7158
 
              sel->cond=orig_cond;
7159
 
 
7160
 
            /* Fix for EXPLAIN */
7161
 
            if (sel->quick)
7162
 
              join->best_positions[i].records_read= (double)sel->quick->records;
7163
 
          }
7164
 
          else
7165
 
          {
7166
 
            sel->needed_reg=tab->needed_reg;
7167
 
            sel->quick_keys.reset();
7168
 
          }
7169
 
          if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
7170
 
              !((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
7171
 
          {
7172
 
            tab->keys= sel->quick_keys;
7173
 
            tab->keys|= sel->needed_reg;
7174
 
            tab->use_quick= (!sel->needed_reg.none() &&
7175
 
                             (select->quick_keys.none() ||
7176
 
                              (select->quick &&
7177
 
                               (select->quick->records >= 100L)))) ?
7178
 
              2 : 1;
7179
 
            sel->read_tables= used_tables & ~current_map;
7180
 
          }
7181
 
          if (i != join->const_tables && tab->use_quick != 2)
7182
 
          {                                     /* Read with cache */
7183
 
            if (cond &&
7184
 
                (tmp=make_cond_for_table(cond,
7185
 
                                         join->const_table_map |
7186
 
                                         current_map,
7187
 
                                         current_map, 0)))
7188
 
            {
7189
 
              tab->cache.select=(SQL_SELECT*)
7190
 
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
7191
 
              tab->cache.select->cond=tmp;
7192
 
              tab->cache.select->read_tables=join->const_table_map;
7193
 
            }
7194
 
          }
7195
 
        }
7196
 
      }
7197
 
 
7198
 
      /*
7199
 
        Push down conditions from all on expressions.
7200
 
        Each of these conditions are guarded by a variable
7201
 
        that turns if off just before null complemented row for
7202
 
        outer joins is formed. Thus, the condition from an
7203
 
        'on expression' are guaranteed not to be checked for
7204
 
        the null complemented row.
7205
 
      */
7206
 
 
7207
 
      /* First push down constant conditions from on expressions */
7208
 
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
7209
 
           join_tab < join->join_tab+join->tables ; join_tab++)
7210
 
      {
7211
 
        if (*join_tab->on_expr_ref)
7212
 
        {
7213
 
          JOIN_TAB *cond_tab= join_tab->first_inner;
7214
 
          tmp= make_cond_for_table(*join_tab->on_expr_ref,
7215
 
                                   join->const_table_map,
7216
 
                                   (table_map) 0, 0);
7217
 
          if (!tmp)
7218
 
            continue;
7219
 
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
7220
 
          if (!tmp)
7221
 
            return(1);
7222
 
          tmp->quick_fix_field();
7223
 
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
7224
 
                                    new Item_cond_and(cond_tab->select_cond,tmp);
7225
 
          if (!cond_tab->select_cond)
7226
 
            return(1);
7227
 
          cond_tab->select_cond->quick_fix_field();
7228
 
        }
7229
 
      }
7230
 
 
7231
 
      /* Push down non-constant conditions from on expressions */
7232
 
      JOIN_TAB *last_tab= tab;
7233
 
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
7234
 
      {
7235
 
        /*
7236
 
          Table tab is the last inner table of an outer join.
7237
 
          An on expression is always attached to it.
7238
 
        */
7239
 
        COND *on_expr= *first_inner_tab->on_expr_ref;
7240
 
 
7241
 
        table_map used_tables2= (join->const_table_map |
7242
 
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
7243
 
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
7244
 
        {
7245
 
          current_map= tab->table->map;
7246
 
          used_tables2|= current_map;
7247
 
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
7248
 
                                              current_map, 0);
7249
 
          if (tmp_cond)
7250
 
          {
7251
 
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
7252
 
            /*
7253
 
              First add the guards for match variables of
7254
 
              all embedding outer join operations.
7255
 
            */
7256
 
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
7257
 
                                                     tmp_cond,
7258
 
                                                     first_inner_tab)))
7259
 
              return(1);
7260
 
            /*
7261
 
              Now add the guard turning the predicate off for
7262
 
              the null complemented row.
7263
 
            */
7264
 
            tmp_cond= new Item_func_trig_cond(tmp_cond,
7265
 
                                              &first_inner_tab->
7266
 
                                              not_null_compl);
7267
 
            if (tmp_cond)
7268
 
              tmp_cond->quick_fix_field();
7269
 
            /* Add the predicate to other pushed down predicates */
7270
 
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
7271
 
                                  new Item_cond_and(cond_tab->select_cond,
7272
 
                                                    tmp_cond);
7273
 
            if (!cond_tab->select_cond)
7274
 
              return(1);
7275
 
            cond_tab->select_cond->quick_fix_field();
7276
 
          }
7277
 
        }
7278
 
        first_inner_tab= first_inner_tab->first_upper;
7279
 
      }
7280
 
    }
7281
 
  }
7282
 
  return(0);
7283
 
}
7284
 
 
7285
 
 
7286
2288
/*
7287
2289
  Check if given expression uses only table fields covered by the given index
7288
2290
 
7304
2306
    true   Yes
7305
2307
    false  No
7306
2308
*/
7307
 
 
7308
 
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno,
7309
 
                            bool other_tbls_ok)
 
2309
bool uses_index_fields_only(Item *item, Table *tbl, uint32_t keyno, bool other_tbls_ok)
7310
2310
{
7311
2311
  if (item->const_item())
7312
2312
    return true;
7369
2369
  }
7370
2370
}
7371
2371
 
7372
 
 
7373
2372
#define ICP_COND_USES_INDEX_ONLY 10
7374
2373
 
7375
2374
/*
7399
2398
  RETURN
7400
2399
    Index condition, or NULL if no condition could be inferred.
7401
2400
*/
7402
 
 
7403
 
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno,
7404
 
                          bool other_tbls_ok)
 
2401
Item *make_cond_for_index(Item *cond, Table *table, uint32_t keyno, bool other_tbls_ok)
7405
2402
{
7406
2403
  if (!cond)
7407
2404
    return NULL;
7412
2409
    {
7413
2410
      Item_cond_and *new_cond=new Item_cond_and;
7414
2411
      if (!new_cond)
7415
 
        return (COND*) 0;
 
2412
        return (COND*) 0;
7416
2413
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7417
2414
      Item *item;
7418
2415
      while ((item=li++))
7419
2416
      {
7420
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7421
 
        if (fix)
7422
 
          new_cond->argument_list()->push_back(fix);
 
2417
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
2418
        if (fix)
 
2419
          new_cond->argument_list()->push_back(fix);
7423
2420
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7424
2421
      }
7425
2422
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7426
2423
        cond->marker= ICP_COND_USES_INDEX_ONLY;
7427
2424
      switch (new_cond->argument_list()->elements) {
7428
2425
      case 0:
7429
 
        return (COND*) 0;
 
2426
        return (COND*) 0;
7430
2427
      case 1:
7431
 
        return new_cond->argument_list()->head();
 
2428
        return new_cond->argument_list()->head();
7432
2429
      default:
7433
 
        new_cond->quick_fix_field();
7434
 
        return new_cond;
 
2430
        new_cond->quick_fix_field();
 
2431
        return new_cond;
7435
2432
      }
7436
2433
    }
7437
2434
    else /* It's OR */
7438
2435
    {
7439
2436
      Item_cond_or *new_cond=new Item_cond_or;
7440
2437
      if (!new_cond)
7441
 
        return (COND*) 0;
 
2438
        return (COND*) 0;
7442
2439
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7443
2440
      Item *item;
7444
2441
      while ((item=li++))
7445
2442
      {
7446
 
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
7447
 
        if (!fix)
7448
 
          return (COND*) 0;
7449
 
        new_cond->argument_list()->push_back(fix);
 
2443
        Item *fix= make_cond_for_index(item, table, keyno, other_tbls_ok);
 
2444
        if (!fix)
 
2445
          return (COND*) 0;
 
2446
        new_cond->argument_list()->push_back(fix);
7450
2447
        n_marked += test(item->marker == ICP_COND_USES_INDEX_ONLY);
7451
2448
      }
7452
2449
      if (n_marked ==((Item_cond*)cond)->argument_list()->elements)
7477
2474
      /* Create new top level AND item */
7478
2475
      Item_cond_and *new_cond=new Item_cond_and;
7479
2476
      if (!new_cond)
7480
 
        return (COND*) 0;
 
2477
        return (COND*) 0;
7481
2478
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7482
2479
      Item *item;
7483
2480
      while ((item=li++))
7484
2481
      {
7485
 
        Item *fix= make_cond_remainder(item, exclude_index);
7486
 
        if (fix)
 
2482
        Item *fix= make_cond_remainder(item, exclude_index);
 
2483
        if (fix)
7487
2484
        {
7488
 
          new_cond->argument_list()->push_back(fix);
 
2485
          new_cond->argument_list()->push_back(fix);
7489
2486
          tbl_map |= fix->used_tables();
7490
2487
        }
7491
2488
      }
7492
2489
      switch (new_cond->argument_list()->elements) {
7493
2490
      case 0:
7494
 
        return (COND*) 0;
 
2491
        return (COND*) 0;
7495
2492
      case 1:
7496
 
        return new_cond->argument_list()->head();
 
2493
        return new_cond->argument_list()->head();
7497
2494
      default:
7498
 
        new_cond->quick_fix_field();
 
2495
        new_cond->quick_fix_field();
7499
2496
        ((Item_cond*)new_cond)->used_tables_cache= tbl_map;
7500
 
        return new_cond;
 
2497
        return new_cond;
7501
2498
      }
7502
2499
    }
7503
2500
    else /* It's OR */
7504
2501
    {
7505
2502
      Item_cond_or *new_cond=new Item_cond_or;
7506
2503
      if (!new_cond)
7507
 
        return (COND*) 0;
 
2504
        return (COND*) 0;
7508
2505
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
7509
2506
      Item *item;
7510
2507
      while ((item=li++))
7511
2508
      {
7512
 
        Item *fix= make_cond_remainder(item, false);
7513
 
        if (!fix)
7514
 
          return (COND*) 0;
7515
 
        new_cond->argument_list()->push_back(fix);
 
2509
        Item *fix= make_cond_remainder(item, false);
 
2510
        if (!fix)
 
2511
          return (COND*) 0;
 
2512
        new_cond->argument_list()->push_back(fix);
7516
2513
        tbl_map |= fix->used_tables();
7517
2514
      }
7518
2515
      new_cond->quick_fix_field();
7524
2521
  return cond;
7525
2522
}
7526
2523
 
7527
 
 
7528
2524
/*
7529
2525
  Try to extract and push the index condition
7530
2526
 
7538
2534
  DESCRIPTION
7539
2535
    Try to extract and push the index condition down to table handler
7540
2536
*/
7541
 
 
7542
 
static void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
 
2537
void push_index_cond(JOIN_TAB *tab, uint32_t keyno, bool other_tbls_ok)
7543
2538
{
7544
2539
  Item *idx_cond;
7545
2540
  if (tab->table->file->index_flags(keyno, 0, 1) & HA_DO_INDEX_COND_PUSHDOWN &&
7551
2546
    if (idx_cond)
7552
2547
    {
7553
2548
      tab->pre_idx_push_select_cond= tab->select_cond;
7554
 
      Item *idx_remainder_cond=
7555
 
        tab->table->file->idx_cond_push(keyno, idx_cond);
 
2549
      Item *idx_remainder_cond= tab->table->file->idx_cond_push(keyno, idx_cond);
7556
2550
 
7557
2551
      /*
7558
2552
        Disable eq_ref's "lookup cache" if we've pushed down an index
7574
2568
        else
7575
2569
        {
7576
2570
          tab->select_cond= new Item_cond_and(row_cond, idx_remainder_cond);
7577
 
          tab->select_cond->quick_fix_field();
 
2571
          tab->select_cond->quick_fix_field();
7578
2572
          ((Item_cond_and*)tab->select_cond)->used_tables_cache=
7579
2573
            row_cond->used_tables() | idx_remainder_cond->used_tables();
7580
2574
        }
7590
2584
  return;
7591
2585
}
7592
2586
 
7593
 
 
7594
 
 
7595
 
    /*
7596
 
      Determine if the set is already ordered for order_st BY, so it can
7597
 
      disable join cache because it will change the ordering of the results.
7598
 
      Code handles sort table that is at any location (not only first after
7599
 
      the const tables) despite the fact that it's currently prohibited.
7600
 
      We must disable join cache if the first non-const table alone is
7601
 
      ordered. If there is a temp table the ordering is done as a last
7602
 
      operation and doesn't prevent join cache usage.
7603
 
    */
7604
 
uint32_t make_join_orderinfo(JOIN *join)
7605
 
{
7606
 
  uint32_t i;
7607
 
  if (join->need_tmp)
7608
 
    return join->tables;
7609
 
 
7610
 
  for (i=join->const_tables ; i < join->tables ; i++)
7611
 
  {
7612
 
    JOIN_TAB *tab=join->join_tab+i;
7613
 
    Table *table=tab->table;
7614
 
    if ((table == join->sort_by_table &&
7615
 
         (!join->order || join->skip_sort_order)) ||
7616
 
        (join->sort_by_table == (Table *) 1 && i != join->const_tables))
7617
 
    {
7618
 
      break;
7619
 
    }
7620
 
  }
7621
 
  return i;
7622
 
}
7623
 
 
7624
 
 
7625
 
/*
7626
 
  Plan refinement stage: do various set ups for the executioner
7627
 
 
7628
 
  SYNOPSIS
7629
 
    make_join_readinfo()
7630
 
      join           Join being processed
7631
 
      options        Join's options (checking for SELECT_DESCRIBE,
7632
 
                     SELECT_NO_JOIN_CACHE)
7633
 
      no_jbuf_after  Don't use join buffering after table with this number.
7634
 
 
7635
 
  DESCRIPTION
7636
 
    Plan refinement stage: do various set ups for the executioner
7637
 
      - set up use of join buffering
7638
 
      - push index conditions
7639
 
      - increment counters
7640
 
      - etc
7641
 
 
7642
 
  RETURN
7643
 
    false - OK
7644
 
    true  - Out of memory
7645
 
*/
7646
 
 
7647
 
static bool
7648
 
make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
7649
 
{
7650
 
  uint32_t i;
7651
 
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
7652
 
  bool sorted= 1;
7653
 
 
7654
 
  for (i=join->const_tables ; i < join->tables ; i++)
7655
 
  {
7656
 
    JOIN_TAB *tab=join->join_tab+i;
7657
 
    Table *table=tab->table;
7658
 
    bool using_join_cache;
7659
 
    tab->read_record.table= table;
7660
 
    tab->read_record.file=table->file;
7661
 
    tab->next_select=sub_select;                /* normal select */
7662
 
    /*
7663
 
      TODO: don't always instruct first table's ref/range access method to
7664
 
      produce sorted output.
7665
 
    */
7666
 
    tab->sorted= sorted;
7667
 
    sorted= 0;                                  // only first must be sorted
7668
 
    if (tab->insideout_match_tab)
7669
 
    {
7670
 
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
7671
 
                                                         [tab->index].
7672
 
                                                         key_length)))
7673
 
        return true;
7674
 
    }
7675
 
    switch (tab->type) {
7676
 
    case JT_SYSTEM:                             // Only happens with left join
7677
 
      table->status=STATUS_NO_RECORD;
7678
 
      tab->read_first_record= join_read_system;
7679
 
      tab->read_record.read_record= join_no_more_records;
7680
 
      break;
7681
 
    case JT_CONST:                              // Only happens with left join
7682
 
      table->status=STATUS_NO_RECORD;
7683
 
      tab->read_first_record= join_read_const;
7684
 
      tab->read_record.read_record= join_no_more_records;
7685
 
      if (table->covering_keys.test(tab->ref.key) &&
7686
 
          !table->no_keyread)
7687
 
      {
7688
 
        table->key_read=1;
7689
 
        table->file->extra(HA_EXTRA_KEYREAD);
7690
 
      }
7691
 
      break;
7692
 
    case JT_EQ_REF:
7693
 
      table->status=STATUS_NO_RECORD;
7694
 
      if (tab->select)
7695
 
      {
7696
 
        delete tab->select->quick;
7697
 
        tab->select->quick=0;
7698
 
      }
7699
 
      delete tab->quick;
7700
 
      tab->quick=0;
7701
 
      tab->read_first_record= join_read_key;
7702
 
      tab->read_record.read_record= join_no_more_records;
7703
 
      if (table->covering_keys.test(tab->ref.key) &&
7704
 
          !table->no_keyread)
7705
 
      {
7706
 
        table->key_read=1;
7707
 
        table->file->extra(HA_EXTRA_KEYREAD);
7708
 
      }
7709
 
      else
7710
 
        push_index_cond(tab, tab->ref.key, true);
7711
 
      break;
7712
 
    case JT_REF_OR_NULL:
7713
 
    case JT_REF:
7714
 
      table->status=STATUS_NO_RECORD;
7715
 
      if (tab->select)
7716
 
      {
7717
 
        delete tab->select->quick;
7718
 
        tab->select->quick=0;
7719
 
      }
7720
 
      delete tab->quick;
7721
 
      tab->quick=0;
7722
 
      if (table->covering_keys.test(tab->ref.key) &&
7723
 
          !table->no_keyread)
7724
 
      {
7725
 
        table->key_read=1;
7726
 
        table->file->extra(HA_EXTRA_KEYREAD);
7727
 
      }
7728
 
      else
7729
 
        push_index_cond(tab, tab->ref.key, true);
7730
 
      if (tab->type == JT_REF)
7731
 
      {
7732
 
        tab->read_first_record= join_read_always_key;
7733
 
        tab->read_record.read_record= tab->insideout_match_tab?
7734
 
           join_read_next_same_diff : join_read_next_same;
7735
 
      }
7736
 
      else
7737
 
      {
7738
 
        tab->read_first_record= join_read_always_key_or_null;
7739
 
        tab->read_record.read_record= join_read_next_same_or_null;
7740
 
      }
7741
 
      break;
7742
 
    case JT_ALL:
7743
 
      /*
7744
 
        If previous table use cache
7745
 
        If the incoming data set is already sorted don't use cache.
7746
 
      */
7747
 
      table->status=STATUS_NO_RECORD;
7748
 
      using_join_cache= false;
7749
 
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
7750
 
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
7751
 
          !tab->insideout_match_tab)
7752
 
      {
7753
 
        if ((options & SELECT_DESCRIBE) ||
7754
 
            !join_init_cache(join->session,join->join_tab+join->const_tables,
7755
 
                             i-join->const_tables))
7756
 
        {
7757
 
          using_join_cache= true;
7758
 
          tab[-1].next_select=sub_select_cache; /* Patch previous */
7759
 
        }
7760
 
      }
7761
 
      /* These init changes read_record */
7762
 
      if (tab->use_quick == 2)
7763
 
      {
7764
 
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
7765
 
        tab->read_first_record= join_init_quick_read_record;
7766
 
        if (statistics)
7767
 
          status_var_increment(join->session->status_var.select_range_check_count);
7768
 
      }
7769
 
      else
7770
 
      {
7771
 
        tab->read_first_record= join_init_read_record;
7772
 
        if (i == join->const_tables)
7773
 
        {
7774
 
          if (tab->select && tab->select->quick)
7775
 
          {
7776
 
            if (statistics)
7777
 
              status_var_increment(join->session->status_var.select_range_count);
7778
 
          }
7779
 
          else
7780
 
          {
7781
 
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
7782
 
            if (statistics)
7783
 
              status_var_increment(join->session->status_var.select_scan_count);
7784
 
          }
7785
 
        }
7786
 
        else
7787
 
        {
7788
 
          if (tab->select && tab->select->quick)
7789
 
          {
7790
 
            if (statistics)
7791
 
              status_var_increment(join->session->status_var.select_full_range_join_count);
7792
 
          }
7793
 
          else
7794
 
          {
7795
 
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
7796
 
            if (statistics)
7797
 
              status_var_increment(join->session->status_var.select_full_join_count);
7798
 
          }
7799
 
        }
7800
 
        if (!table->no_keyread)
7801
 
        {
7802
 
          if (tab->select && tab->select->quick &&
7803
 
              tab->select->quick->index != MAX_KEY && //not index_merge
7804
 
              table->covering_keys.test(tab->select->quick->index))
7805
 
          {
7806
 
            table->key_read=1;
7807
 
            table->file->extra(HA_EXTRA_KEYREAD);
7808
 
          }
7809
 
          else if (!table->covering_keys.none() &&
7810
 
                   !(tab->select && tab->select->quick))
7811
 
          {                                     // Only read index tree
7812
 
            if (!tab->insideout_match_tab)
7813
 
            {
7814
 
              /*
7815
 
                See bug #26447: "Using the clustered index for a table scan
7816
 
                is always faster than using a secondary index".
7817
 
              */
7818
 
              if (table->s->primary_key != MAX_KEY &&
7819
 
                  table->file->primary_key_is_clustered())
7820
 
                tab->index= table->s->primary_key;
7821
 
              else
7822
 
                tab->index= table->find_shortest_key(&table->covering_keys);
7823
 
            }
7824
 
            tab->read_first_record= join_read_first;
7825
 
            tab->type=JT_NEXT;          // Read with index_first / index_next
7826
 
          }
7827
 
        }
7828
 
        if (tab->select && tab->select->quick &&
7829
 
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
7830
 
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
7831
 
      }
7832
 
      break;
7833
 
    default:
7834
 
      break;                                    /* purecov: deadcode */
7835
 
    case JT_UNKNOWN:
7836
 
    case JT_MAYBE_REF:
7837
 
      abort();                                  /* purecov: deadcode */
7838
 
    }
7839
 
  }
7840
 
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
7841
 
  return(false);
7842
 
}
7843
 
 
7844
 
 
7845
 
/**
7846
 
  Give error if we some tables are done with a full join.
7847
 
 
7848
 
  This is used by multi_table_update and multi_table_delete when running
7849
 
  in safe mode.
7850
 
 
7851
 
  @param join           Join condition
7852
 
 
7853
 
  @retval
7854
 
    0   ok
7855
 
  @retval
7856
 
    1   Error (full join used)
7857
 
*/
7858
 
 
7859
 
bool error_if_full_join(JOIN *join)
7860
 
{
7861
 
  for (JOIN_TAB *tab=join->join_tab, *end=join->join_tab+join->tables;
7862
 
       tab < end;
7863
 
       tab++)
7864
 
  {
7865
 
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
7866
 
    {
7867
 
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
7868
 
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
7869
 
      return(1);
7870
 
    }
7871
 
  }
7872
 
  return(0);
7873
 
}
7874
 
 
7875
 
 
7876
2587
/**
7877
2588
  cleanup JOIN_TAB.
7878
2589
*/
7879
 
 
7880
2590
void JOIN_TAB::cleanup()
7881
2591
{
7882
2592
  delete select;
7904
2614
  end_read_record(&read_record);
7905
2615
}
7906
2616
 
7907
 
 
7908
 
/**
7909
 
  Partially cleanup JOIN after it has executed: close index or rnd read
7910
 
  (table cursors), free quick selects.
7911
 
 
7912
 
    This function is called in the end of execution of a JOIN, before the used
7913
 
    tables are unlocked and closed.
7914
 
 
7915
 
    For a join that is resolved using a temporary table, the first sweep is
7916
 
    performed against actual tables and an intermediate result is inserted
7917
 
    into the temprorary table.
7918
 
    The last sweep is performed against the temporary table. Therefore,
7919
 
    the base tables and associated buffers used to fill the temporary table
7920
 
    are no longer needed, and this function is called to free them.
7921
 
 
7922
 
    For a join that is performed without a temporary table, this function
7923
 
    is called after all rows are sent, but before EOF packet is sent.
7924
 
 
7925
 
    For a simple SELECT with no subqueries this function performs a full
7926
 
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
7927
 
    tables.
7928
 
 
7929
 
    If a JOIN is executed for a subquery or if it has a subquery, we can't
7930
 
    do the full cleanup and need to do a partial cleanup only.
7931
 
    - If a JOIN is not the top level join, we must not unlock the tables
7932
 
    because the outer select may not have been evaluated yet, and we
7933
 
    can't unlock only selected tables of a query.
7934
 
    - Additionally, if this JOIN corresponds to a correlated subquery, we
7935
 
    should not free quick selects and join buffers because they will be
7936
 
    needed for the next execution of the correlated subquery.
7937
 
    - However, if this is a JOIN for a [sub]select, which is not
7938
 
    a correlated subquery itself, but has subqueries, we can free it
7939
 
    fully and also free JOINs of all its subqueries. The exception
7940
 
    is a subquery in SELECT list, e.g: @n
7941
 
    SELECT a, (select cmax(b) from t1) group by c @n
7942
 
    This subquery will not be evaluated at first sweep and its value will
7943
 
    not be inserted into the temporary table. Instead, it's evaluated
7944
 
    when selecting from the temporary table. Therefore, it can't be freed
7945
 
    here even though it's not correlated.
7946
 
 
7947
 
  @todo
7948
 
    Unlock tables even if the join isn't top level select in the tree
7949
 
*/
7950
 
 
7951
 
void JOIN::join_free()
7952
 
{
7953
 
  Select_Lex_Unit *tmp_unit;
7954
 
  Select_Lex *sl;
7955
 
  /*
7956
 
    Optimization: if not EXPLAIN and we are done with the JOIN,
7957
 
    free all tables.
7958
 
  */
7959
 
  bool full= (!select_lex->uncacheable && !session->lex->describe);
7960
 
  bool can_unlock= full;
7961
 
 
7962
 
  cleanup(full);
7963
 
 
7964
 
  for (tmp_unit= select_lex->first_inner_unit();
7965
 
       tmp_unit;
7966
 
       tmp_unit= tmp_unit->next_unit())
7967
 
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
7968
 
    {
7969
 
      Item_subselect *subselect= sl->master_unit()->item;
7970
 
      bool full_local= full && (!subselect || subselect->is_evaluated());
7971
 
      /*
7972
 
        If this join is evaluated, we can fully clean it up and clean up all
7973
 
        its underlying joins even if they are correlated -- they will not be
7974
 
        used any more anyway.
7975
 
        If this join is not yet evaluated, we still must clean it up to
7976
 
        close its table cursors -- it may never get evaluated, as in case of
7977
 
        ... HAVING false OR a IN (SELECT ...))
7978
 
        but all table cursors must be closed before the unlock.
7979
 
      */
7980
 
      sl->cleanup_all_joins(full_local);
7981
 
      /* Can't unlock if at least one JOIN is still needed */
7982
 
      can_unlock= can_unlock && full_local;
7983
 
    }
7984
 
 
7985
 
  /*
7986
 
    We are not using tables anymore
7987
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
7988
 
  */
7989
 
  if (can_unlock && lock && session->lock &&
7990
 
      !(select_options & SELECT_NO_UNLOCK) &&
7991
 
      !select_lex->subquery_in_having &&
7992
 
      (select_lex == (session->lex->unit.fake_select_lex ?
7993
 
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
7994
 
  {
7995
 
    /*
7996
 
      TODO: unlock tables even if the join isn't top level select in the
7997
 
      tree.
7998
 
    */
7999
 
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
8000
 
    lock= 0;
8001
 
  }
8002
 
 
8003
 
  return;
8004
 
}
8005
 
 
8006
 
 
8007
 
/**
8008
 
  Free resources of given join.
8009
 
 
8010
 
  @param fill   true if we should free all resources, call with full==1
8011
 
                should be last, before it this function can be called with
8012
 
                full==0
8013
 
 
8014
 
  @note
8015
 
    With subquery this function definitely will be called several times,
8016
 
    but even for simple query it can be called several times.
8017
 
*/
8018
 
 
8019
 
void JOIN::cleanup(bool full)
8020
 
{
8021
 
  if (table)
8022
 
  {
8023
 
    JOIN_TAB *tab,*end;
8024
 
    /*
8025
 
      Only a sorted table may be cached.  This sorted table is always the
8026
 
      first non const table in join->table
8027
 
    */
8028
 
    if (tables > const_tables) // Test for not-const tables
8029
 
    {
8030
 
      free_io_cache(table[const_tables]);
8031
 
      filesort_free_buffers(table[const_tables],full);
8032
 
    }
8033
 
 
8034
 
    if (full)
8035
 
    {
8036
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8037
 
        tab->cleanup();
8038
 
      table= 0;
8039
 
    }
8040
 
    else
8041
 
    {
8042
 
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
8043
 
      {
8044
 
        if (tab->table)
8045
 
          tab->table->file->ha_index_or_rnd_end();
8046
 
      }
8047
 
    }
8048
 
    cleanup_sj_tmp_tables(this);//
8049
 
  }
8050
 
  /*
8051
 
    We are not using tables anymore
8052
 
    Unlock all tables. We may be in an INSERT .... SELECT statement.
8053
 
  */
8054
 
  if (full)
8055
 
  {
8056
 
    if (tmp_join)
8057
 
      tmp_table_param.copy_field= 0;
8058
 
    group_fields.delete_elements();
8059
 
    /*
8060
 
      We can't call delete_elements() on copy_funcs as this will cause
8061
 
      problems in free_elements() as some of the elements are then deleted.
8062
 
    */
8063
 
    tmp_table_param.copy_funcs.empty();
8064
 
    /*
8065
 
      If we have tmp_join and 'this' JOIN is not tmp_join and
8066
 
      tmp_table_param.copy_field's  of them are equal then we have to remove
8067
 
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
8068
 
      be removed in tmp_table_param.cleanup().
8069
 
    */
8070
 
    if (tmp_join &&
8071
 
        tmp_join != this &&
8072
 
        tmp_join->tmp_table_param.copy_field ==
8073
 
        tmp_table_param.copy_field)
8074
 
    {
8075
 
      tmp_join->tmp_table_param.copy_field=
8076
 
        tmp_join->tmp_table_param.save_copy_field= 0;
8077
 
    }
8078
 
    tmp_table_param.cleanup();
8079
 
  }
8080
 
  return;
8081
 
}
8082
 
 
 
2617
bool only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
 
2618
{
 
2619
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
 
2620
  {
 
2621
    if (tables & 1 && !eq_ref_table(join, order, *tab))
 
2622
      return 0;
 
2623
  }
 
2624
  return 1;
 
2625
}
8083
2626
 
8084
2627
/**
8085
2628
  Remove the following expressions from order_st BY and GROUP BY:
8100
2643
  SELECT * FROM t1,t2 WHERE t1.a=t2.a order_st BY t2.b,t1.a
8101
2644
  @endcode
8102
2645
*/
8103
 
 
8104
 
static bool
8105
 
eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
 
2646
bool eq_ref_table(JOIN *join, order_st *start_order, JOIN_TAB *tab)
8106
2647
{
8107
2648
  if (tab->cached_eq_ref_table)                 // If cached
8108
2649
    return tab->eq_ref_table;
8124
2665
      order_st *order;
8125
2666
      for (order=start_order ; order ; order=order->next)
8126
2667
      {
8127
 
        if ((*ref_item)->eq(order->item[0],0))
8128
 
          break;
 
2668
        if ((*ref_item)->eq(order->item[0],0))
 
2669
          break;
8129
2670
      }
8130
2671
      if (order)
8131
2672
      {
8132
 
        found++;
8133
 
        assert(!(order->used & map));
8134
 
        order->used|=map;
8135
 
        continue;                               // Used in order_st BY
 
2673
        found++;
 
2674
        assert(!(order->used & map));
 
2675
        order->used|=map;
 
2676
        continue;                               // Used in order_st BY
8136
2677
      }
8137
2678
      if (!only_eq_ref_tables(join,start_order, (*ref_item)->used_tables()))
8138
 
        return (tab->eq_ref_table=0);
 
2679
        return (tab->eq_ref_table= 0);
8139
2680
    }
8140
2681
  }
8141
2682
  /* Check that there was no reference to table before sort order */
8147
2688
      continue;
8148
2689
    }
8149
2690
    if (start_order->depend_map & map)
8150
 
      return (tab->eq_ref_table=0);
8151
 
  }
8152
 
  return tab->eq_ref_table=1;
8153
 
}
8154
 
 
8155
 
 
8156
 
static bool
8157
 
only_eq_ref_tables(JOIN *join,order_st *order,table_map tables)
8158
 
{
8159
 
  for (JOIN_TAB **tab=join->map2table ; tables ; tab++, tables>>=1)
8160
 
  {
8161
 
    if (tables & 1 && !eq_ref_table(join, order, *tab))
8162
 
      return 0;
8163
 
  }
8164
 
  return 1;
8165
 
}
8166
 
 
8167
 
 
8168
 
/** Update the dependency map for the tables. */
8169
 
 
8170
 
static void update_depend_map(JOIN *join)
8171
 
{
8172
 
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
8173
 
 
8174
 
  for (; join_tab != end ; join_tab++)
8175
 
  {
8176
 
    TABLE_REF *ref= &join_tab->ref;
8177
 
    table_map depend_map=0;
8178
 
    Item **item=ref->items;
8179
 
    uint32_t i;
8180
 
    for (i=0 ; i < ref->key_parts ; i++,item++)
8181
 
      depend_map|=(*item)->used_tables();
8182
 
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
8183
 
    depend_map&= ~OUTER_REF_TABLE_BIT;
8184
 
    for (JOIN_TAB **tab=join->map2table;
8185
 
         depend_map ;
8186
 
         tab++,depend_map>>=1 )
8187
 
    {
8188
 
      if (depend_map & 1)
8189
 
        ref->depend_map|=(*tab)->ref.depend_map;
8190
 
    }
8191
 
  }
8192
 
}
8193
 
 
8194
 
 
8195
 
/** Update the dependency map for the sort order. */
8196
 
 
8197
 
static void update_depend_map(JOIN *join, order_st *order)
8198
 
{
8199
 
  for (; order ; order=order->next)
8200
 
  {
8201
 
    table_map depend_map;
8202
 
    order->item[0]->update_used_tables();
8203
 
    order->depend_map=depend_map=order->item[0]->used_tables();
8204
 
    // Not item_sum(), RAND() and no reference to table outside of sub select
8205
 
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
8206
 
        && !order->item[0]->with_sum_func)
8207
 
    {
8208
 
      for (JOIN_TAB **tab=join->map2table;
8209
 
           depend_map ;
8210
 
           tab++, depend_map>>=1)
8211
 
      {
8212
 
        if (depend_map & 1)
8213
 
          order->depend_map|=(*tab)->ref.depend_map;
8214
 
      }
8215
 
    }
8216
 
  }
8217
 
}
8218
 
 
8219
 
 
8220
 
/**
8221
 
  Remove all constants and check if order_st only contains simple
8222
 
  expressions.
8223
 
 
8224
 
  simple_order is set to 1 if sort_order only uses fields from head table
8225
 
  and the head table is not a LEFT JOIN table.
8226
 
 
8227
 
  @param join                   Join handler
8228
 
  @param first_order            List of SORT or GROUP order
8229
 
  @param cond                   WHERE statement
8230
 
  @param change_list            Set to 1 if we should remove things from list.
8231
 
                               If this is not set, then only simple_order is
8232
 
                               calculated.
8233
 
  @param simple_order           Set to 1 if we are only using simple expressions
8234
 
 
8235
 
  @return
8236
 
    Returns new sort order
8237
 
*/
8238
 
 
8239
 
static order_st *
8240
 
remove_constants(JOIN *join,order_st *first_order, COND *cond,
8241
 
                 bool change_list, bool *simple_order)
8242
 
{
8243
 
  if (join->tables == join->const_tables)
8244
 
    return change_list ? 0 : first_order;               // No need to sort
8245
 
 
8246
 
  order_st *order,**prev_ptr;
8247
 
  table_map first_table= join->join_tab[join->const_tables].table->map;
8248
 
  table_map not_const_tables= ~join->const_table_map;
8249
 
  table_map ref;
8250
 
 
8251
 
  prev_ptr= &first_order;
8252
 
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
8253
 
 
8254
 
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
8255
 
 
8256
 
  update_depend_map(join, first_order);
8257
 
  for (order=first_order; order ; order=order->next)
8258
 
  {
8259
 
    table_map order_tables=order->item[0]->used_tables();
8260
 
    if (order->item[0]->with_sum_func)
8261
 
      *simple_order=0;                          // Must do a temp table to sort
8262
 
    else if (!(order_tables & not_const_tables))
8263
 
    {
8264
 
      if (order->item[0]->with_subselect)
8265
 
        order->item[0]->val_str(&order->item[0]->str_value);
8266
 
      continue;                                 // skip const item
8267
 
    }
8268
 
    else
8269
 
    {
8270
 
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
8271
 
        *simple_order=0;
8272
 
      else
8273
 
      {
8274
 
        Item *comp_item=0;
8275
 
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
8276
 
        {
8277
 
          continue;
8278
 
        }
8279
 
        if ((ref=order_tables & (not_const_tables ^ first_table)))
8280
 
        {
8281
 
          if (!(order_tables & first_table) &&
8282
 
              only_eq_ref_tables(join,first_order, ref))
8283
 
          {
8284
 
            continue;
8285
 
          }
8286
 
          *simple_order=0;                      // Must do a temp table to sort
8287
 
        }
8288
 
      }
8289
 
    }
8290
 
    if (change_list)
8291
 
      *prev_ptr= order;                         // use this entry
8292
 
    prev_ptr= &order->next;
8293
 
  }
8294
 
  if (change_list)
8295
 
    *prev_ptr=0;
8296
 
  if (prev_ptr == &first_order)                 // Nothing to sort/group
8297
 
    *simple_order=1;
8298
 
  return(first_order);
8299
 
}
8300
 
 
8301
 
 
8302
 
static int
8303
 
return_zero_rows(JOIN *join, select_result *result,TableList *tables,
8304
 
                 List<Item> &fields, bool send_row, uint64_t select_options,
8305
 
                 const char *info, Item *having)
8306
 
{
8307
 
  if (select_options & SELECT_DESCRIBE)
8308
 
  {
8309
 
    select_describe(join, false, false, false, info);
8310
 
    return(0);
8311
 
  }
8312
 
 
8313
 
  join->join_free();
8314
 
 
8315
 
  if (send_row)
8316
 
  {
8317
 
    for (TableList *table= tables; table; table= table->next_leaf)
8318
 
      table->table->mark_as_null_row();         // All fields are NULL
8319
 
    if (having && having->val_int() == 0)
8320
 
      send_row=0;
8321
 
  }
8322
 
  if (!(result->send_fields(fields,
8323
 
                              Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
8324
 
  {
8325
 
    if (send_row)
8326
 
    {
8327
 
      List_iterator_fast<Item> it(fields);
8328
 
      Item *item;
8329
 
      while ((item= it++))
8330
 
        item->no_rows_in_result();
8331
 
      result->send_data(fields);
8332
 
    }
8333
 
    result->send_eof();                         // Should be safe
8334
 
  }
8335
 
  /* Update results for FOUND_ROWS */
8336
 
  join->session->limit_found_rows= join->session->examined_row_count= 0;
8337
 
  return(0);
8338
 
}
8339
 
 
8340
 
/*
8341
 
  used only in JOIN::clear
8342
 
*/
8343
 
static void clear_tables(JOIN *join)
8344
 
{
8345
 
  /*
8346
 
    must clear only the non-const tables, as const tables
8347
 
    are not re-calculated.
8348
 
  */
8349
 
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
8350
 
    join->table[i]->mark_as_null_row();         // All fields are NULL
8351
 
}
8352
 
 
8353
 
/*****************************************************************************
8354
 
  Make som simple condition optimization:
8355
 
  If there is a test 'field = const' change all refs to 'field' to 'const'
8356
 
  Remove all dummy tests 'item = item', 'const op const'.
8357
 
  Remove all 'item is NULL', when item can never be null!
8358
 
  item->marker should be 0 for all items on entry
8359
 
  Return in cond_value false if condition is impossible (1 = 2)
8360
 
*****************************************************************************/
8361
 
 
8362
 
class COND_CMP :public ilink {
8363
 
public:
8364
 
  static void *operator new(size_t size)
8365
 
  {
8366
 
    return (void*) sql_alloc((uint32_t) size);
8367
 
  }
8368
 
  static void operator delete(void *, size_t)
8369
 
  { TRASH(ptr, size); }
8370
 
 
8371
 
  Item *and_level;
8372
 
  Item_func *cmp_func;
8373
 
  COND_CMP(Item *a,Item_func *b) :and_level(a),cmp_func(b) {}
8374
 
};
8375
 
 
8376
 
#ifdef HAVE_EXPLICIT_TEMPLATE_INSTANTIATION
8377
 
template class I_List<COND_CMP>;
8378
 
template class I_List_iterator<COND_CMP>;
8379
 
#endif
8380
 
 
 
2691
      return (tab->eq_ref_table= 0);
 
2692
  }
 
2693
  return tab->eq_ref_table= 1;
 
2694
}
8381
2695
 
8382
2696
/**
8383
2697
  Find the multiple equality predicate containing a field.
8397
2711
    - Item_equal for the found multiple equality predicate if a success;
8398
2712
    - NULL otherwise.
8399
2713
*/
8400
 
 
8401
 
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field,
8402
 
                            bool *inherited_fl)
 
2714
Item_equal *find_item_equal(COND_EQUAL *cond_equal, Field *field, bool *inherited_fl)
8403
2715
{
8404
2716
  Item_equal *item= 0;
8405
2717
  bool in_upper_level= false;
8420
2732
  return item;
8421
2733
}
8422
2734
 
8423
 
 
8424
2735
/**
8425
2736
  Check whether an equality can be used to build multiple equalities.
8426
2737
 
8502
2813
  @retval
8503
2814
    false   otherwise
8504
2815
*/
8505
 
 
8506
 
static bool check_simple_equality(Item *left_item, Item *right_item,
8507
 
                                  Item *item, COND_EQUAL *cond_equal)
 
2816
static bool check_simple_equality(Item *left_item,
 
2817
                                  Item *right_item,
 
2818
                                  Item *item,
 
2819
                                  COND_EQUAL *cond_equal)
8508
2820
{
8509
2821
  if (left_item->type() == Item::FIELD_ITEM &&
8510
2822
      right_item->type() == Item::FIELD_ITEM &&
8665
2977
  return false;
8666
2978
}
8667
2979
 
8668
 
 
8669
2980
/**
8670
2981
  Convert row equalities into a conjunction of regular equalities.
8671
2982
 
8691
3002
  @retval
8692
3003
    false   otherwise
8693
3004
*/
8694
 
 
8695
 
static bool check_row_equality(Session *session, Item *left_row, Item_row *right_row,
8696
 
                               COND_EQUAL *cond_equal, List<Item>* eq_list)
 
3005
static bool check_row_equality(Session *session,
 
3006
                               Item *left_row, 
 
3007
                               Item_row *right_row,
 
3008
                               COND_EQUAL *cond_equal,
 
3009
                               List<Item>* eq_list)
8697
3010
{
8698
3011
  uint32_t n= left_row->cols();
8699
3012
  for (uint32_t i= 0 ; i < n; i++)
8730
3043
  return true;
8731
3044
}
8732
3045
 
8733
 
 
8734
3046
/**
8735
3047
  Eliminate row equalities and form multiple equalities predicates.
8736
3048
 
8760
3072
           or, if the equality is neither a simple one nor a row equality,
8761
3073
           or, if the procedure fails by a fatal error.
8762
3074
*/
8763
 
 
8764
 
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal,
8765
 
                           List<Item> *eq_list)
 
3075
static bool check_equality(Session *session, Item *item, COND_EQUAL *cond_equal, List<Item> *eq_list)
8766
3076
{
8767
3077
  if (item->type() == Item::FUNC_ITEM &&
8768
3078
         ((Item_func*) item)->functype() == Item_func::EQ_FUNC)
8785
3095
  return false;
8786
3096
}
8787
3097
 
8788
 
 
8789
3098
/**
8790
3099
  Replace all equality predicates in a condition by multiple equality items.
8791
3100
 
8849
3158
  @return
8850
3159
    pointer to the transformed condition
8851
3160
*/
8852
 
 
8853
 
static COND *build_equal_items_for_cond(Session *session, COND *cond,
8854
 
                                        COND_EQUAL *inherited)
 
3161
static COND *build_equal_items_for_cond(Session *session, COND *cond, COND_EQUAL *inherited)
8855
3162
{
8856
3163
  Item_equal *item_equal;
8857
3164
  COND_EQUAL cond_equal;
8947
3254
        {
8948
3255
          item_equal->fix_length_and_dec();
8949
3256
          item_equal->update_used_tables();
8950
 
        }
 
3257
        }
8951
3258
        else
8952
3259
          item_equal= (Item_equal *) eq_list.pop();
8953
3260
        set_if_bigger(session->lex->current_select->max_equal_elems,
8959
3266
        /*
8960
3267
          Here a new AND level must be created. It can happen only
8961
3268
          when a row equality is processed as a standalone predicate.
8962
 
        */
 
3269
        */
8963
3270
        Item_cond_and *and_cond= new Item_cond_and(eq_list);
8964
3271
        and_cond->quick_fix_field();
8965
3272
        List<Item> *args= and_cond->argument_list();
8993
3300
  return cond;
8994
3301
}
8995
3302
 
8996
 
 
8997
3303
/**
8998
3304
  Build multiple equalities for a condition and all on expressions that
8999
3305
  inherit these multiple equalities.
9059
3365
  @return
9060
3366
    pointer to the transformed condition containing multiple equalities
9061
3367
*/
9062
 
 
9063
3368
static COND *build_equal_items(Session *session, COND *cond,
9064
3369
                               COND_EQUAL *inherited,
9065
3370
                               List<TableList> *join_list,
9113
3418
  return cond;
9114
3419
}
9115
3420
 
9116
 
 
9117
3421
/**
9118
3422
  Compare field items by table order in the execution plan.
9119
3423
 
9133
3437
  @retval
9134
3438
    0  otherwise
9135
3439
*/
9136
 
 
9137
3440
static int compare_fields_by_table_order(Item_field *field1,
9138
 
                                  Item_field *field2,
9139
 
                                  void *table_join_idx)
 
3441
                                         Item_field *field2,
 
3442
                                         void *table_join_idx)
9140
3443
{
9141
3444
  int cmp= 0;
9142
3445
  bool outer_ref= 0;
9157
3460
  return cmp < 0 ? -1 : (cmp ? 1 : 0);
9158
3461
}
9159
3462
 
9160
 
 
9161
3463
/**
9162
3464
  Generate minimal set of simple equalities equivalent to a multiple equality.
9163
3465
 
9197
3499
    a pointer to the simple generated equality, if success.
9198
3500
    - 0, otherwise.
9199
3501
*/
9200
 
 
9201
 
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels,
9202
 
                                  Item_equal *item_equal)
 
3502
static Item *eliminate_item_equal(COND *cond, COND_EQUAL *upper_levels, Item_equal *item_equal)
9203
3503
{
9204
3504
  List<Item> eq_list;
9205
3505
  Item_func_eq *eq_item= 0;
9269
3569
  return cond;
9270
3570
}
9271
3571
 
9272
 
 
9273
3572
/**
9274
3573
  Substitute every field reference in a condition by the best equal field
9275
3574
  and eliminate all multiple equality predicates.
9297
3596
  @return
9298
3597
    The transformed condition
9299
3598
*/
9300
 
 
9301
 
static COND* substitute_for_best_equal_field(COND *cond,
9302
 
                                             COND_EQUAL *cond_equal,
9303
 
                                             void *table_join_idx)
 
3599
COND* substitute_for_best_equal_field(COND *cond, COND_EQUAL *cond_equal, void *table_join_idx)
9304
3600
{
9305
3601
  Item_equal *item_equal;
9306
3602
 
9368
3664
  return cond;
9369
3665
}
9370
3666
 
9371
 
 
9372
3667
/**
9373
3668
  Check appearance of new constant items in multiple equalities
9374
3669
  of a condition after reading a constant table.
9381
3676
  @param cond       condition whose multiple equalities are to be checked
9382
3677
  @param table      constant table that has been read
9383
3678
*/
9384
 
 
9385
3679
static void update_const_equal_items(COND *cond, JOIN_TAB *tab)
9386
3680
{
9387
3681
  if (!(cond->used_tables() & tab->table->map))
9434
3728
  }
9435
3729
}
9436
3730
 
9437
 
 
9438
3731
/*
9439
3732
  change field = field to field = const for each found field = const in the
9440
3733
  and_level
9441
3734
*/
9442
 
 
9443
 
static void
9444
 
change_cond_ref_to_const(Session *session, I_List<COND_CMP> *save_list,
9445
 
                         Item *and_father, Item *cond,
9446
 
                         Item *field, Item *value)
 
3735
static void change_cond_ref_to_const(Session *session,
 
3736
                                     I_List<COND_CMP> *save_list,
 
3737
                                     Item *and_father,
 
3738
                                     Item *cond,
 
3739
                                     Item *field,
 
3740
                                     Item *value)
9447
3741
{
9448
3742
  if (cond->type() == Item::COND_ITEM)
9449
3743
  {
9452
3746
    List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
9453
3747
    Item *item;
9454
3748
    while ((item=li++))
9455
 
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item,
9456
 
                               field, value);
 
3749
      change_cond_ref_to_const(session, save_list,and_level ? cond : item, item, field, value);
9457
3750
    return;
9458
3751
  }
9459
3752
  if (cond->eq_cmp_result() == Item::COND_OK)
9478
3771
    {
9479
3772
      session->change_item_tree(args + 1, tmp);
9480
3773
      func->update_used_tables();
9481
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9482
 
          && and_father != cond && !left_item->const_item())
 
3774
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
3775
                and_father != cond && 
 
3776
          ! left_item->const_item())
9483
3777
      {
9484
 
        cond->marker=1;
9485
 
        COND_CMP *tmp2;
9486
 
        if ((tmp2=new COND_CMP(and_father,func)))
9487
 
          save_list->push_back(tmp2);
 
3778
        cond->marker=1;
 
3779
        COND_CMP *tmp2;
 
3780
        if ((tmp2=new COND_CMP(and_father,func)))
 
3781
          save_list->push_back(tmp2);
9488
3782
      }
9489
3783
      func->set_cmp_func();
9490
3784
    }
9503
3797
      session->change_item_tree(args, tmp);
9504
3798
      value= tmp;
9505
3799
      func->update_used_tables();
9506
 
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC)
9507
 
          && and_father != cond && !right_item->const_item())
 
3800
      if ((functype == Item_func::EQ_FUNC || functype == Item_func::EQUAL_FUNC) &&
 
3801
          and_father != cond && 
 
3802
          ! right_item->const_item())
9508
3803
      {
9509
3804
        args[0]= args[1];                       // For easy check
9510
3805
        session->change_item_tree(args + 1, value);
9511
 
        cond->marker=1;
9512
 
        COND_CMP *tmp2;
9513
 
        if ((tmp2=new COND_CMP(and_father,func)))
9514
 
          save_list->push_back(tmp2);
 
3806
        cond->marker=1;
 
3807
        COND_CMP *tmp2;
 
3808
        if ((tmp2=new COND_CMP(and_father,func)))
 
3809
          save_list->push_back(tmp2);
9515
3810
      }
9516
3811
      func->set_cmp_func();
9517
3812
    }
9526
3821
  @return
9527
3822
    new conditions
9528
3823
*/
9529
 
 
9530
 
static Item *remove_additional_cond(Item* conds)
 
3824
Item *remove_additional_cond(Item* conds)
9531
3825
{
9532
3826
  if (conds->name == in_additional_cond)
9533
3827
    return 0;
9550
3844
  return conds;
9551
3845
}
9552
3846
 
9553
 
static void
9554
 
propagate_cond_constants(Session *session, I_List<COND_CMP> *save_list,
9555
 
                         COND *and_father, COND *cond)
 
3847
static void propagate_cond_constants(Session *session, 
 
3848
                                     I_List<COND_CMP> *save_list, 
 
3849
                                     COND *and_father, 
 
3850
                                     COND *cond)
9556
3851
{
9557
3852
  if (cond->type() == Item::COND_ITEM)
9558
3853
  {
9581
3876
  else if (and_father != cond && !cond->marker)         // In a AND group
9582
3877
  {
9583
3878
    if (cond->type() == Item::FUNC_ITEM &&
9584
 
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
9585
 
         ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
 
3879
        (((Item_func*) cond)->functype() == Item_func::EQ_FUNC ||
 
3880
        ((Item_func*) cond)->functype() == Item_func::EQUAL_FUNC))
9586
3881
    {
9587
3882
      Item_func_eq *func=(Item_func_eq*) cond;
9588
3883
      Item **args= func->arguments();
9591
3886
      if (!(left_const && right_const) &&
9592
3887
          args[0]->result_type() == args[1]->result_type())
9593
3888
      {
9594
 
        if (right_const)
9595
 
        {
9596
 
          resolve_const_item(session, &args[1], args[0]);
9597
 
          func->update_used_tables();
9598
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
9599
 
                                   args[0], args[1]);
9600
 
        }
9601
 
        else if (left_const)
9602
 
        {
9603
 
          resolve_const_item(session, &args[0], args[1]);
9604
 
          func->update_used_tables();
9605
 
          change_cond_ref_to_const(session, save_list, and_father, and_father,
9606
 
                                   args[1], args[0]);
9607
 
        }
9608
 
      }
9609
 
    }
9610
 
  }
9611
 
}
9612
 
 
9613
 
 
9614
 
/**
9615
 
  Simplify joins replacing outer joins by inner joins whenever it's
9616
 
  possible.
9617
 
 
9618
 
    The function, during a retrieval of join_list,  eliminates those
9619
 
    outer joins that can be converted into inner join, possibly nested.
9620
 
    It also moves the on expressions for the converted outer joins
9621
 
    and from inner joins to conds.
9622
 
    The function also calculates some attributes for nested joins:
9623
 
    - used_tables
9624
 
    - not_null_tables
9625
 
    - dep_tables.
9626
 
    - on_expr_dep_tables
9627
 
    The first two attributes are used to test whether an outer join can
9628
 
    be substituted for an inner join. The third attribute represents the
9629
 
    relation 'to be dependent on' for tables. If table t2 is dependent
9630
 
    on table t1, then in any evaluated execution plan table access to
9631
 
    table t2 must precede access to table t2. This relation is used also
9632
 
    to check whether the query contains  invalid cross-references.
9633
 
    The forth attribute is an auxiliary one and is used to calculate
9634
 
    dep_tables.
9635
 
    As the attribute dep_tables qualifies possibles orders of tables in the
9636
 
    execution plan, the dependencies required by the straight join
9637
 
    modifiers are reflected in this attribute as well.
9638
 
    The function also removes all braces that can be removed from the join
9639
 
    expression without changing its meaning.
9640
 
 
9641
 
  @note
9642
 
    An outer join can be replaced by an inner join if the where condition
9643
 
    or the on expression for an embedding nested join contains a conjunctive
9644
 
    predicate rejecting null values for some attribute of the inner tables.
9645
 
 
9646
 
    E.g. in the query:
9647
 
    @code
9648
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9649
 
    @endcode
9650
 
    the predicate t2.b < 5 rejects nulls.
9651
 
    The query is converted first to:
9652
 
    @code
9653
 
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
9654
 
    @endcode
9655
 
    then to the equivalent form:
9656
 
    @code
9657
 
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
9658
 
    @endcode
9659
 
 
9660
 
 
9661
 
    Similarly the following query:
9662
 
    @code
9663
 
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
9664
 
        WHERE t2.c < 5
9665
 
    @endcode
9666
 
    is converted to:
9667
 
    @code
9668
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
9669
 
 
9670
 
    @endcode
9671
 
 
9672
 
    One conversion might trigger another:
9673
 
    @code
9674
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
9675
 
                       LEFT JOIN t3 ON t3.b=t2.b
9676
 
        WHERE t3 IS NOT NULL =>
9677
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
9678
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
9679
 
      SELECT * FROM t1, t2, t3
9680
 
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
9681
 
  @endcode
9682
 
 
9683
 
    The function removes all unnecessary braces from the expression
9684
 
    produced by the conversions.
9685
 
    E.g.
9686
 
    @code
9687
 
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9688
 
    @endcode
9689
 
    finally is converted to:
9690
 
    @code
9691
 
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
9692
 
 
9693
 
    @endcode
9694
 
 
9695
 
 
9696
 
    It also will remove braces from the following queries:
9697
 
    @code
9698
 
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
9699
 
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
9700
 
    @endcode
9701
 
 
9702
 
    The benefit of this simplification procedure is that it might return
9703
 
    a query for which the optimizer can evaluate execution plan with more
9704
 
    join orders. With a left join operation the optimizer does not
9705
 
    consider any plan where one of the inner tables is before some of outer
9706
 
    tables.
9707
 
 
9708
 
  IMPLEMENTATION
9709
 
    The function is implemented by a recursive procedure.  On the recursive
9710
 
    ascent all attributes are calculated, all outer joins that can be
9711
 
    converted are replaced and then all unnecessary braces are removed.
9712
 
    As join list contains join tables in the reverse order sequential
9713
 
    elimination of outer joins does not require extra recursive calls.
9714
 
 
9715
 
  SEMI-JOIN NOTES
9716
 
    Remove all semi-joins that have are within another semi-join (i.e. have
9717
 
    an "ancestor" semi-join nest)
9718
 
 
9719
 
  EXAMPLES
9720
 
    Here is an example of a join query with invalid cross references:
9721
 
    @code
9722
 
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
9723
 
    @endcode
9724
 
 
9725
 
  @param join        reference to the query info
9726
 
  @param join_list   list representation of the join to be converted
9727
 
  @param conds       conditions to add on expressions for converted joins
9728
 
  @param top         true <=> conds is the where condition
9729
 
 
9730
 
  @return
9731
 
    - The new condition, if success
9732
 
    - 0, otherwise
9733
 
*/
9734
 
 
9735
 
static COND *
9736
 
simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top,
9737
 
               bool in_sj)
9738
 
{
9739
 
  TableList *table;
9740
 
  nested_join_st *nested_join;
9741
 
  TableList *prev_table= 0;
9742
 
  List_iterator<TableList> li(*join_list);
9743
 
 
9744
 
  /*
9745
 
    Try to simplify join operations from join_list.
9746
 
    The most outer join operation is checked for conversion first.
9747
 
  */
9748
 
  while ((table= li++))
9749
 
  {
9750
 
    table_map used_tables;
9751
 
    table_map not_null_tables= (table_map) 0;
9752
 
 
9753
 
    if ((nested_join= table->nested_join))
9754
 
    {
9755
 
      /*
9756
 
         If the element of join_list is a nested join apply
9757
 
         the procedure to its nested join list first.
9758
 
      */
9759
 
      if (table->on_expr)
9760
 
      {
9761
 
        Item *expr= table->on_expr;
9762
 
        /*
9763
 
           If an on expression E is attached to the table,
9764
 
           check all null rejected predicates in this expression.
9765
 
           If such a predicate over an attribute belonging to
9766
 
           an inner table of an embedded outer join is found,
9767
 
           the outer join is converted to an inner join and
9768
 
           the corresponding on expression is added to E.
9769
 
        */
9770
 
        expr= simplify_joins(join, &nested_join->join_list,
9771
 
                             expr, false, in_sj || table->sj_on_expr);
9772
 
 
9773
 
        if (!table->prep_on_expr || expr != table->on_expr)
9774
 
        {
9775
 
          assert(expr);
9776
 
 
9777
 
          table->on_expr= expr;
9778
 
          table->prep_on_expr= expr->copy_andor_structure(join->session);
9779
 
        }
9780
 
      }
9781
 
      nested_join->used_tables= (table_map) 0;
9782
 
      nested_join->not_null_tables=(table_map) 0;
9783
 
      conds= simplify_joins(join, &nested_join->join_list, conds, top,
9784
 
                            in_sj || table->sj_on_expr);
9785
 
      used_tables= nested_join->used_tables;
9786
 
      not_null_tables= nested_join->not_null_tables;
9787
 
    }
9788
 
    else
9789
 
    {
9790
 
      if (!table->prep_on_expr)
9791
 
        table->prep_on_expr= table->on_expr;
9792
 
      used_tables= table->table->map;
9793
 
      if (conds)
9794
 
        not_null_tables= conds->not_null_tables();
9795
 
    }
9796
 
 
9797
 
    if (table->embedding)
9798
 
    {
9799
 
      table->embedding->nested_join->used_tables|= used_tables;
9800
 
      table->embedding->nested_join->not_null_tables|= not_null_tables;
9801
 
    }
9802
 
 
9803
 
    if (!table->outer_join || (used_tables & not_null_tables))
9804
 
    {
9805
 
      /*
9806
 
        For some of the inner tables there are conjunctive predicates
9807
 
        that reject nulls => the outer join can be replaced by an inner join.
9808
 
      */
9809
 
      table->outer_join= 0;
9810
 
      if (table->on_expr)
9811
 
      {
9812
 
        /* Add ON expression to the WHERE or upper-level ON condition. */
9813
 
        if (conds)
9814
 
        {
9815
 
          conds= and_conds(conds, table->on_expr);
9816
 
          conds->top_level_item();
9817
 
          /* conds is always a new item as both cond and on_expr existed */
9818
 
          assert(!conds->fixed);
9819
 
          conds->fix_fields(join->session, &conds);
9820
 
        }
9821
 
        else
9822
 
          conds= table->on_expr;
9823
 
        table->prep_on_expr= table->on_expr= 0;
9824
 
      }
9825
 
    }
9826
 
 
9827
 
    if (!top)
9828
 
      continue;
9829
 
 
9830
 
    /*
9831
 
      Only inner tables of non-convertible outer joins
9832
 
      remain with on_expr.
9833
 
    */
9834
 
    if (table->on_expr)
9835
 
    {
9836
 
      table->dep_tables|= table->on_expr->used_tables();
9837
 
      if (table->embedding)
9838
 
      {
9839
 
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
9840
 
        /*
9841
 
           Embedding table depends on tables used
9842
 
           in embedded on expressions.
9843
 
        */
9844
 
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
9845
 
      }
9846
 
      else
9847
 
        table->dep_tables&= ~table->table->map;
9848
 
    }
9849
 
 
9850
 
    if (prev_table)
9851
 
    {
9852
 
      /* The order of tables is reverse: prev_table follows table */
9853
 
      if (prev_table->straight)
9854
 
        prev_table->dep_tables|= used_tables;
9855
 
      if (prev_table->on_expr)
9856
 
      {
9857
 
        prev_table->dep_tables|= table->on_expr_dep_tables;
9858
 
        table_map prev_used_tables= prev_table->nested_join ?
9859
 
                                    prev_table->nested_join->used_tables :
9860
 
                                    prev_table->table->map;
9861
 
        /*
9862
 
          If on expression contains only references to inner tables
9863
 
          we still make the inner tables dependent on the outer tables.
9864
 
          It would be enough to set dependency only on one outer table
9865
 
          for them. Yet this is really a rare case.
9866
 
        */
9867
 
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
9868
 
          prev_table->dep_tables|= used_tables;
9869
 
      }
9870
 
    }
9871
 
    prev_table= table;
9872
 
  }
9873
 
 
9874
 
  /*
9875
 
    Flatten nested joins that can be flattened.
9876
 
    no ON expression and not a semi-join => can be flattened.
9877
 
  */
9878
 
  li.rewind();
9879
 
  while ((table= li++))
9880
 
  {
9881
 
    nested_join= table->nested_join;
9882
 
    if (table->sj_on_expr && !in_sj)
9883
 
    {
9884
 
       /*
9885
 
         If this is a semi-join that is not contained within another semi-join,
9886
 
         leave it intact (otherwise it is flattened)
9887
 
       */
9888
 
      join->select_lex->sj_nests.push_back(table);
9889
 
    }
9890
 
    else if (nested_join && !table->on_expr)
9891
 
    {
9892
 
      TableList *tbl;
9893
 
      List_iterator<TableList> it(nested_join->join_list);
9894
 
      while ((tbl= it++))
9895
 
      {
9896
 
        tbl->embedding= table->embedding;
9897
 
        tbl->join_list= table->join_list;
9898
 
      }
9899
 
      li.replace(nested_join->join_list);
9900
 
    }
9901
 
  }
9902
 
  return(conds);
9903
 
}
9904
 
 
9905
 
 
9906
 
/**
9907
 
  Assign each nested join structure a bit in nested_join_map.
9908
 
 
9909
 
    Assign each nested join structure (except "confluent" ones - those that
9910
 
    embed only one element) a bit in nested_join_map.
9911
 
 
9912
 
  @param join          Join being processed
9913
 
  @param join_list     List of tables
9914
 
  @param first_unused  Number of first unused bit in nested_join_map before the
9915
 
                       call
9916
 
 
9917
 
  @note
9918
 
    This function is called after simplify_joins(), when there are no
9919
 
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
9920
 
    we will not run out of bits in nested_join_map.
9921
 
 
9922
 
  @return
9923
 
    First unused bit in nested_join_map after the call.
9924
 
*/
9925
 
 
9926
 
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list,
9927
 
                                          uint32_t first_unused)
9928
 
{
9929
 
  List_iterator<TableList> li(*join_list);
9930
 
  TableList *table;
9931
 
  while ((table= li++))
9932
 
  {
9933
 
    nested_join_st *nested_join;
9934
 
    if ((nested_join= table->nested_join))
9935
 
    {
9936
 
      /*
9937
 
        It is guaranteed by simplify_joins() function that a nested join
9938
 
        that has only one child is either
9939
 
         - a single-table view (the child is the underlying table), or
9940
 
         - a single-table semi-join nest
9941
 
 
9942
 
        We don't assign bits to such sj-nests because
9943
 
        1. it is redundant (a "sequence" of one table cannot be interleaved
9944
 
            with anything)
9945
 
        2. we could run out bits in nested_join_map otherwise.
9946
 
      */
9947
 
      if (nested_join->join_list.elements != 1)
9948
 
      {
9949
 
        /* Don't assign bits to sj-nests */
9950
 
        if (table->on_expr)
9951
 
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
9952
 
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
9953
 
                                                    first_unused);
9954
 
      }
9955
 
    }
9956
 
  }
9957
 
  return(first_unused);
9958
 
}
9959
 
 
9960
 
 
9961
 
/**
9962
 
  Set nested_join_st::counter=0 in all nested joins in passed list.
9963
 
 
9964
 
    Recursively set nested_join_st::counter=0 for all nested joins contained in
9965
 
    the passed join_list.
9966
 
 
9967
 
  @param join_list  List of nested joins to process. It may also contain base
9968
 
                    tables which will be ignored.
9969
 
*/
9970
 
 
9971
 
static void reset_nj_counters(List<TableList> *join_list)
9972
 
{
9973
 
  List_iterator<TableList> li(*join_list);
9974
 
  TableList *table;
9975
 
  while ((table= li++))
9976
 
  {
9977
 
    nested_join_st *nested_join;
9978
 
    if ((nested_join= table->nested_join))
9979
 
    {
9980
 
      nested_join->counter_= 0;
9981
 
      reset_nj_counters(&nested_join->join_list);
9982
 
    }
9983
 
  }
9984
 
  return;
9985
 
}
9986
 
 
 
3889
        if (right_const)
 
3890
        {
 
3891
                resolve_const_item(session, &args[1], args[0]);
 
3892
          func->update_used_tables();
 
3893
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
3894
                                        args[0], args[1]);
 
3895
        }
 
3896
        else if (left_const)
 
3897
        {
 
3898
                resolve_const_item(session, &args[0], args[1]);
 
3899
          func->update_used_tables();
 
3900
                change_cond_ref_to_const(session, save_list, and_father, and_father,
 
3901
                                        args[1], args[0]);
 
3902
        }
 
3903
      }
 
3904
    }
 
3905
  }
 
3906
}
9987
3907
 
9988
3908
/**
9989
3909
  Check interleaving with an inner tables of an outer join for
10077
3997
  @retval
10078
3998
    true   Requested join order extension not allowed.
10079
3999
*/
10080
 
 
10081
 
static bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
 
4000
bool check_interleaving_with_nj(JOIN_TAB *last_tab, JOIN_TAB *next_tab)
10082
4001
{
10083
4002
  TableList *next_emb= next_tab->table->pos_in_table_list->embedding;
10084
4003
  JOIN *join= last_tab->join;
10122
4041
  return false;
10123
4042
}
10124
4043
 
10125
 
 
10126
 
/**
10127
 
  Nested joins perspective: Remove the last table from the join order.
10128
 
 
10129
 
    Remove the last table from the partial join order and update the nested
10130
 
    joins counters and join->cur_embedding_map. It is ok to call this
10131
 
    function for the first table in join order (for which
10132
 
    check_interleaving_with_nj has not been called)
10133
 
 
10134
 
  @param last  join table to remove, it is assumed to be the last in current
10135
 
               partial join order.
10136
 
*/
10137
 
 
10138
 
static void restore_prev_nj_state(JOIN_TAB *last)
10139
 
{
10140
 
  TableList *last_emb= last->table->pos_in_table_list->embedding;
10141
 
  JOIN *join= last->join;
10142
 
  while (last_emb)
10143
 
  {
10144
 
    if (last_emb->on_expr)
10145
 
    {
10146
 
      if (!(--last_emb->nested_join->counter_))
10147
 
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
10148
 
      else if (last_emb->nested_join->join_list.elements-1 ==
10149
 
               last_emb->nested_join->counter_)
10150
 
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
10151
 
      else
10152
 
        break;
10153
 
    }
10154
 
    last_emb= last_emb->embedding;
10155
 
  }
10156
 
}
10157
 
 
10158
 
 
10159
 
 
10160
 
static
10161
4044
void advance_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10162
4045
{
10163
4046
  TableList *emb_sj_nest;
10170
4053
  }
10171
4054
}
10172
4055
 
10173
 
 
10174
4056
/*
10175
4057
  we assume remaining_tables doesnt contain @tab.
10176
4058
*/
10177
 
 
10178
 
static void restore_prev_sj_state(const table_map remaining_tables,
10179
 
                                  const JOIN_TAB *tab)
 
4059
void restore_prev_sj_state(const table_map remaining_tables, const JOIN_TAB *tab)
10180
4060
{
10181
4061
  TableList *emb_sj_nest;
10182
4062
  if ((emb_sj_nest= tab->emb_sj_nest))
10190
4070
  }
10191
4071
}
10192
4072
 
10193
 
 
10194
 
static COND *
10195
 
optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list,
10196
 
              Item::cond_result *cond_value)
 
4073
COND *optimize_cond(JOIN *join, COND *conds, List<TableList> *join_list, Item::cond_result *cond_value)
10197
4074
{
10198
4075
  Session *session= join->session;
10199
4076
 
10223
4100
  return(conds);
10224
4101
}
10225
4102
 
10226
 
 
10227
4103
/**
10228
4104
  Remove const and eq items.
10229
4105
 
10234
4110
    - COND_TRUE   : always true ( 1 = 1 )
10235
4111
    - COND_FALSE  : always false        ( 1 = 2 )
10236
4112
*/
10237
 
 
10238
 
COND *
10239
 
remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
 
4113
COND *remove_eq_conds(Session *session, COND *cond, Item::cond_result *cond_value)
10240
4114
{
10241
4115
  if (cond->type() == Item::COND_ITEM)
10242
4116
  {
10425
4299
    true    can be used
10426
4300
    false   cannot be used
10427
4301
*/
10428
 
static bool
10429
 
test_if_equality_guarantees_uniqueness(Item *l, Item *r)
 
4302
static bool test_if_equality_guarantees_uniqueness(Item *l, Item *r)
10430
4303
{
10431
4304
  return r->const_item() &&
10432
4305
    /* elements must be compared as dates */
10441
4314
/**
10442
4315
  Return true if the item is a const value in all the WHERE clause.
10443
4316
*/
10444
 
 
10445
 
static bool
10446
 
const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
 
4317
bool const_expression_in_where(COND *cond, Item *comp_item, Item **const_item)
10447
4318
{
10448
4319
  if (cond->type() == Item::COND_ITEM)
10449
4320
  {
10456
4327
      bool res=const_expression_in_where(item, comp_item, const_item);
10457
4328
      if (res)                                  // Is a const value
10458
4329
      {
10459
 
        if (and_level)
10460
 
          return 1;
 
4330
        if (and_level)
 
4331
          return 1;
10461
4332
      }
10462
4333
      else if (!and_level)
10463
 
        return 0;
 
4334
        return 0;
10464
4335
    }
10465
4336
    return and_level ? 0 : 1;
10466
4337
  }
10468
4339
  {                                             // boolan compare function
10469
4340
    Item_func* func= (Item_func*) cond;
10470
4341
    if (func->functype() != Item_func::EQUAL_FUNC &&
10471
 
        func->functype() != Item_func::EQ_FUNC)
 
4342
              func->functype() != Item_func::EQ_FUNC)
10472
4343
      return 0;
10473
4344
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
10474
4345
    Item *right_item= ((Item_func*) cond)->arguments()[1];
10476
4347
    {
10477
4348
      if (test_if_equality_guarantees_uniqueness (left_item, right_item))
10478
4349
      {
10479
 
        if (*const_item)
10480
 
          return right_item->eq(*const_item, 1);
10481
 
        *const_item=right_item;
10482
 
        return 1;
 
4350
        if (*const_item)
 
4351
          return right_item->eq(*const_item, 1);
 
4352
        *const_item=right_item;
 
4353
        return 1;
10483
4354
      }
10484
4355
    }
10485
4356
    else if (right_item->eq(comp_item,1))
10486
4357
    {
10487
4358
      if (test_if_equality_guarantees_uniqueness (right_item, left_item))
10488
4359
      {
10489
 
        if (*const_item)
10490
 
          return left_item->eq(*const_item, 1);
10491
 
        *const_item=left_item;
10492
 
        return 1;
 
4360
        if (*const_item)
 
4361
          return left_item->eq(*const_item, 1);
 
4362
        *const_item=left_item;
 
4363
        return 1;
10493
4364
      }
10494
4365
    }
10495
4366
  }
10496
4367
  return 0;
10497
4368
}
10498
4369
 
10499
 
 
10500
4370
/**
10501
4371
  @details
10502
4372
  Rows produced by a join sweep may end up in a temporary table or be sent
10508
4378
  @return
10509
4379
    end_select function to use. This function can't fail.
10510
4380
*/
10511
 
 
10512
4381
Next_select_func setup_end_select_func(JOIN *join)
10513
4382
{
10514
4383
  Table *table= join->tmp_table;
10523
4392
    {
10524
4393
      if (table->s->keys)
10525
4394
      {
10526
 
        end_select=end_update;
 
4395
        end_select= end_update;
10527
4396
      }
10528
4397
      else
10529
4398
      {
10530
 
        end_select=end_unique_update;
 
4399
        end_select= end_unique_update;
10531
4400
      }
10532
4401
    }
10533
4402
    else if (join->sort_and_group && !tmp_tbl->precomputed_group_by)
10564
4433
  return end_select;
10565
4434
}
10566
4435
 
10567
 
 
10568
4436
/**
10569
4437
  Make a join of all tables and write it on socket or to table.
10570
4438
 
10575
4443
  @retval
10576
4444
    -1  if error should be sent
10577
4445
*/
10578
 
 
10579
 
static int
10580
 
do_select(JOIN *join,List<Item> *fields,Table *table)
 
4446
int do_select(JOIN *join, List<Item> *fields, Table *table)
10581
4447
{
10582
4448
  int rc= 0;
10583
4449
  enum_nested_loop_state error= NESTED_LOOP_OK;
10613
4479
    {
10614
4480
      error= (*end_select)(join, 0, 0);
10615
4481
      if (error == NESTED_LOOP_OK || error == NESTED_LOOP_QUERY_LIMIT)
10616
 
        error= (*end_select)(join, 0, 1);
 
4482
              error= (*end_select)(join, 0, 1);
10617
4483
 
10618
4484
      /*
10619
4485
        If we don't go through evaluate_join_record(), do the counting
10651
4517
    if (!table)                                 // If sending data to client
10652
4518
    {
10653
4519
      /*
10654
 
        The following will unlock all cursors if the command wasn't an
10655
 
        update command
 
4520
        The following will unlock all cursors if the command wasn't an
 
4521
        update command
10656
4522
      */
10657
4523
      join->join_free();                        // Unlock all cursors
10658
4524
      if (join->result->send_eof())
10659
 
        rc= 1;                                  // Don't send error
 
4525
        rc= 1;                                  // Don't send error
10660
4526
    }
10661
4527
  }
10662
4528
  else
10678
4544
  return(join->session->is_error() ? -1 : rc);
10679
4545
}
10680
4546
 
10681
 
 
10682
 
enum_nested_loop_state
10683
 
sub_select_cache(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
4547
enum_nested_loop_state sub_select_cache(JOIN *join, JOIN_TAB *join_tab, bool end_of_records)
10684
4548
{
10685
4549
  enum_nested_loop_state rc;
10686
4550
 
10698
4562
  }
10699
4563
  if (join_tab->use_quick != 2 || test_if_quick_select(join_tab) <= 0)
10700
4564
  {
10701
 
    if (!store_record_in_cache(&join_tab->cache))
 
4565
    if (! store_record_in_cache(&join_tab->cache))
10702
4566
      return NESTED_LOOP_OK;                     // There is more room in cache
10703
4567
    return flush_cached_records(join,join_tab,false);
10704
4568
  }
10827
4691
  @return
10828
4692
    return one of enum_nested_loop_state, except NESTED_LOOP_NO_MORE_ROWS.
10829
4693
*/
10830
 
int do_sj_reset(SJ_TMP_TABLE *sj_tbl);
10831
 
 
10832
 
enum_nested_loop_state
10833
 
sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
 
4694
enum_nested_loop_state sub_select(JOIN *join,JOIN_TAB *join_tab,bool end_of_records)
10834
4695
{
10835
4696
  join_tab->table->null_row=0;
10836
4697
  if (end_of_records)
10896
4757
  return rc;
10897
4758
}
10898
4759
 
10899
 
 
10900
 
 
10901
 
 
10902
 
/*
10903
 
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
10904
 
 
10905
 
  SYNPOSIS
10906
 
    do_sj_dups_weedout()
10907
 
 
10908
 
  RETURN
10909
 
    -1  Error
10910
 
    1   The row combination is a duplicate (discard it)
10911
 
    0   The row combination is not a duplicate (continue)
10912
 
*/
10913
 
 
10914
 
int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
10915
 
{
10916
 
  int error;
10917
 
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
10918
 
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
10919
 
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
10920
 
  unsigned char *nulls_ptr= ptr;
10921
 
 
10922
 
  /* Put the the rowids tuple into table->record[0]: */
10923
 
 
10924
 
  // 1. Store the length
10925
 
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
10926
 
  {
10927
 
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
10928
 
    ptr++;
10929
 
  }
10930
 
  else
10931
 
  {
10932
 
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
10933
 
    ptr += 2;
10934
 
  }
10935
 
 
10936
 
  // 2. Zero the null bytes
10937
 
  if (sjtbl->null_bytes)
10938
 
  {
10939
 
    memset(ptr, 0, sjtbl->null_bytes);
10940
 
    ptr += sjtbl->null_bytes;
10941
 
  }
10942
 
 
10943
 
  // 3. Put the rowids
10944
 
  for (uint32_t i=0; tab != tab_end; tab++, i++)
10945
 
  {
10946
 
    handler *h= tab->join_tab->table->file;
10947
 
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
10948
 
    {
10949
 
      /* It's a NULL-complemented row */
10950
 
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
10951
 
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
10952
 
    }
10953
 
    else
10954
 
    {
10955
 
      /* Copy the rowid value */
10956
 
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
10957
 
        h->position(tab->join_tab->table->record[0]);
10958
 
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
10959
 
    }
10960
 
  }
10961
 
 
10962
 
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
10963
 
  if (error)
10964
 
  {
10965
 
    /* create_myisam_from_heap will generate error if needed */
10966
 
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
10967
 
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
10968
 
                                &sjtbl->recinfo, error, 1))
10969
 
      return -1;
10970
 
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
10971
 
    return 1;
10972
 
  }
10973
 
  return 0;
10974
 
}
10975
 
 
10976
 
 
10977
4760
/*
10978
4761
  SemiJoinDuplicateElimination: Reset the temporary table
10979
4762
*/
10980
 
 
10981
4763
int do_sj_reset(SJ_TMP_TABLE *sj_tbl)
10982
4764
{
10983
4765
  if (sj_tbl->tmp_table)
10985
4767
  return 0;
10986
4768
}
10987
4769
 
10988
 
/*
10989
 
  Process one record of the nested loop join.
10990
 
 
10991
 
    This function will evaluate parts of WHERE/ON clauses that are
10992
 
    applicable to the partial record on hand and in case of success
10993
 
    submit this record to the next level of the nested loop.
10994
 
*/
10995
 
 
10996
 
static enum_nested_loop_state
10997
 
evaluate_join_record(JOIN *join, JOIN_TAB *join_tab,
10998
 
                     int error)
10999
 
{
11000
 
  bool not_used_in_distinct=join_tab->not_used_in_distinct;
11001
 
  ha_rows found_records=join->found_records;
11002
 
  COND *select_cond= join_tab->select_cond;
11003
 
 
11004
 
  if (error > 0 || (join->session->is_error()))     // Fatal error
11005
 
    return NESTED_LOOP_ERROR;
11006
 
  if (error < 0)
11007
 
    return NESTED_LOOP_NO_MORE_ROWS;
11008
 
  if (join->session->killed)                    // Aborted by user
11009
 
  {
11010
 
    join->session->send_kill_message();
11011
 
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
11012
 
  }
11013
 
  if (!select_cond || select_cond->val_int())
11014
 
  {
11015
 
    /*
11016
 
      There is no select condition or the attached pushed down
11017
 
      condition is true => a match is found.
11018
 
    */
11019
 
    bool found= 1;
11020
 
    while (join_tab->first_unmatched && found)
11021
 
    {
11022
 
      /*
11023
 
        The while condition is always false if join_tab is not
11024
 
        the last inner join table of an outer join operation.
11025
 
      */
11026
 
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
11027
 
      /*
11028
 
        Mark that a match for current outer table is found.
11029
 
        This activates push down conditional predicates attached
11030
 
        to the all inner tables of the outer join.
11031
 
      */
11032
 
      first_unmatched->found= 1;
11033
 
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
11034
 
      {
11035
 
        if (tab->table->reginfo.not_exists_optimize)
11036
 
          return NESTED_LOOP_NO_MORE_ROWS;
11037
 
        /* Check all predicates that has just been activated. */
11038
 
        /*
11039
 
          Actually all predicates non-guarded by first_unmatched->found
11040
 
          will be re-evaluated again. It could be fixed, but, probably,
11041
 
          it's not worth doing now.
11042
 
        */
11043
 
        if (tab->select_cond && !tab->select_cond->val_int())
11044
 
        {
11045
 
          /* The condition attached to table tab is false */
11046
 
          if (tab == join_tab)
11047
 
            found= 0;
11048
 
          else
11049
 
          {
11050
 
            /*
11051
 
              Set a return point if rejected predicate is attached
11052
 
              not to the last table of the current nest level.
11053
 
            */
11054
 
            join->return_tab= tab;
11055
 
            return NESTED_LOOP_OK;
11056
 
          }
11057
 
        }
11058
 
      }
11059
 
      /*
11060
 
        Check whether join_tab is not the last inner table
11061
 
        for another embedding outer join.
11062
 
      */
11063
 
      if ((first_unmatched= first_unmatched->first_upper) &&
11064
 
          first_unmatched->last_inner != join_tab)
11065
 
        first_unmatched= 0;
11066
 
      join_tab->first_unmatched= first_unmatched;
11067
 
    }
11068
 
 
11069
 
    JOIN_TAB *return_tab= join->return_tab;
11070
 
    join_tab->found_match= true;
11071
 
    if (join_tab->check_weed_out_table)
11072
 
    {
11073
 
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
11074
 
      if (res == -1)
11075
 
        return NESTED_LOOP_ERROR;
11076
 
      if (res == 1)
11077
 
        return NESTED_LOOP_OK;
11078
 
    }
11079
 
    else if (join_tab->do_firstmatch)
11080
 
    {
11081
 
      /*
11082
 
        We should return to the join_tab->do_firstmatch after we have
11083
 
        enumerated all the suffixes for current prefix row combination
11084
 
      */
11085
 
      return_tab= join_tab->do_firstmatch;
11086
 
    }
11087
 
 
11088
 
    /*
11089
 
      It was not just a return to lower loop level when one
11090
 
      of the newly activated predicates is evaluated as false
11091
 
      (See above join->return_tab= tab).
11092
 
    */
11093
 
    join->examined_rows++;
11094
 
    join->session->row_count++;
11095
 
 
11096
 
    if (found)
11097
 
    {
11098
 
      enum enum_nested_loop_state rc;
11099
 
      /* A match from join_tab is found for the current partial join. */
11100
 
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
11101
 
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
11102
 
        return rc;
11103
 
      if (return_tab < join->return_tab)
11104
 
        join->return_tab= return_tab;
11105
 
 
11106
 
      if (join->return_tab < join_tab)
11107
 
        return NESTED_LOOP_OK;
11108
 
      /*
11109
 
        Test if this was a SELECT DISTINCT query on a table that
11110
 
        was not in the field list;  In this case we can abort if
11111
 
        we found a row, as no new rows can be added to the result.
11112
 
      */
11113
 
      if (not_used_in_distinct && found_records != join->found_records)
11114
 
        return NESTED_LOOP_NO_MORE_ROWS;
11115
 
    }
11116
 
    else
11117
 
      join_tab->read_record.file->unlock_row();
11118
 
  }
11119
 
  else
11120
 
  {
11121
 
    /*
11122
 
      The condition pushed down to the table join_tab rejects all rows
11123
 
      with the beginning coinciding with the current partial join.
11124
 
    */
11125
 
    join->examined_rows++;
11126
 
    join->session->row_count++;
11127
 
    join_tab->read_record.file->unlock_row();
11128
 
  }
11129
 
  return NESTED_LOOP_OK;
11130
 
}
11131
 
 
11132
 
 
11133
 
/**
11134
 
 
11135
 
  @details
11136
 
    Construct a NULL complimented partial join record and feed it to the next
11137
 
    level of the nested loop. This function is used in case we have
11138
 
    an OUTER join and no matching record was found.
11139
 
*/
11140
 
 
11141
 
static enum_nested_loop_state
11142
 
evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
11143
 
{
11144
 
  /*
11145
 
    The table join_tab is the first inner table of a outer join operation
11146
 
    and no matches has been found for the current outer row.
11147
 
  */
11148
 
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
11149
 
  /* Cache variables for faster loop */
11150
 
  COND *select_cond;
11151
 
  for ( ; join_tab <= last_inner_tab ; join_tab++)
11152
 
  {
11153
 
    /* Change the the values of guard predicate variables. */
11154
 
    join_tab->found= 1;
11155
 
    join_tab->not_null_compl= 0;
11156
 
    /* The outer row is complemented by nulls for each inner tables */
11157
 
    join_tab->table->restoreRecordAsDefault();  // Make empty record
11158
 
    join_tab->table->mark_as_null_row();       // For group by without error
11159
 
    select_cond= join_tab->select_cond;
11160
 
    /* Check all attached conditions for inner table rows. */
11161
 
    if (select_cond && !select_cond->val_int())
11162
 
      return NESTED_LOOP_OK;
11163
 
  }
11164
 
  join_tab--;
11165
 
  /*
11166
 
    The row complemented by nulls might be the first row
11167
 
    of embedding outer joins.
11168
 
    If so, perform the same actions as in the code
11169
 
    for the first regular outer join row above.
11170
 
  */
11171
 
  for ( ; ; )
11172
 
  {
11173
 
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
11174
 
    if ((first_unmatched= first_unmatched->first_upper) &&
11175
 
        first_unmatched->last_inner != join_tab)
11176
 
      first_unmatched= 0;
11177
 
    join_tab->first_unmatched= first_unmatched;
11178
 
    if (!first_unmatched)
11179
 
      break;
11180
 
    first_unmatched->found= 1;
11181
 
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
11182
 
    {
11183
 
      if (tab->select_cond && !tab->select_cond->val_int())
11184
 
      {
11185
 
        join->return_tab= tab;
11186
 
        return NESTED_LOOP_OK;
11187
 
      }
11188
 
    }
11189
 
  }
11190
 
  /*
11191
 
    The row complemented by nulls satisfies all conditions
11192
 
    attached to inner tables.
11193
 
    Send the row complemented by nulls to be joined with the
11194
 
    remaining tables.
11195
 
  */
11196
 
  return (*join_tab->next_select)(join, join_tab+1, 0);
11197
 
}
11198
 
 
11199
 
 
11200
 
static enum_nested_loop_state
11201
 
flush_cached_records(JOIN *join,JOIN_TAB *join_tab,bool skip_last)
11202
 
{
11203
 
  enum_nested_loop_state rc= NESTED_LOOP_OK;
11204
 
  int error;
11205
 
  READ_RECORD *info;
11206
 
 
11207
 
  join_tab->table->null_row= 0;
11208
 
  if (!join_tab->cache.records)
11209
 
    return NESTED_LOOP_OK;                      /* Nothing to do */
11210
 
  if (skip_last)
11211
 
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
11212
 
  if (join_tab->use_quick == 2)
11213
 
  {
11214
 
    if (join_tab->select->quick)
11215
 
    {                                   /* Used quick select last. reset it */
11216
 
      delete join_tab->select->quick;
11217
 
      join_tab->select->quick=0;
11218
 
    }
11219
 
  }
11220
 
 /* read through all records */
11221
 
  if ((error=join_init_read_record(join_tab)))
11222
 
  {
11223
 
    reset_cache_write(&join_tab->cache);
11224
 
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
11225
 
  }
11226
 
 
11227
 
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
11228
 
  {
11229
 
    tmp->status=tmp->table->status;
11230
 
    tmp->table->status=0;
11231
 
  }
11232
 
 
11233
 
  info= &join_tab->read_record;
11234
 
  do
11235
 
  {
11236
 
    if (join->session->killed)
11237
 
    {
11238
 
      join->session->send_kill_message();
11239
 
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
11240
 
    }
11241
 
    SQL_SELECT *select=join_tab->select;
11242
 
    if (rc == NESTED_LOOP_OK &&
11243
 
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
11244
 
    {
11245
 
      uint32_t i;
11246
 
      reset_cache_read(&join_tab->cache);
11247
 
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
11248
 
      {
11249
 
        read_cached_record(join_tab);
11250
 
        if (!select || !select->skip_record())
11251
 
        {
11252
 
          int res= 0;
11253
 
          if (!join_tab->check_weed_out_table ||
11254
 
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
11255
 
          {
11256
 
            rc= (join_tab->next_select)(join,join_tab+1,0);
11257
 
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
11258
 
            {
11259
 
              reset_cache_write(&join_tab->cache);
11260
 
              return rc;
11261
 
            }
11262
 
          }
11263
 
          if (res == -1)
11264
 
            return NESTED_LOOP_ERROR;
11265
 
        }
11266
 
      }
11267
 
    }
11268
 
  } while (!(error=info->read_record(info)));
11269
 
 
11270
 
  if (skip_last)
11271
 
    read_cached_record(join_tab);               // Restore current record
11272
 
  reset_cache_write(&join_tab->cache);
11273
 
  if (error > 0)                                // Fatal error
11274
 
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
11275
 
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
11276
 
    tmp2->table->status=tmp2->status;
11277
 
  return NESTED_LOOP_OK;
11278
 
}
11279
 
 
11280
4770
int safe_index_read(JOIN_TAB *tab)
11281
4771
{
11282
4772
  int error;
11289
4779
  return 0;
11290
4780
}
11291
4781
 
11292
 
 
11293
 
static int
11294
 
join_read_const_table(JOIN_TAB *tab, POSITION *pos)
 
4782
int join_read_const_table(JOIN_TAB *tab, POSITION *pos)
11295
4783
{
11296
4784
  int error;
11297
4785
  Table *table=tab->table;
11308
4796
      pos->records_read=0.0;
11309
4797
      pos->ref_depend_map= 0;
11310
4798
      if (!table->maybe_null || error > 0)
11311
 
        return(error);
 
4799
        return(error);
11312
4800
    }
11313
4801
  }
11314
4802
  else
11315
4803
  {
11316
 
    if (!table->key_read && table->covering_keys.test(tab->ref.key) &&
11317
 
        !table->no_keyread &&
 
4804
    if (!table->key_read && 
 
4805
        table->covering_keys.test(tab->ref.key) && 
 
4806
        !table->no_keyread &&
11318
4807
        (int) table->reginfo.lock_type <= (int) TL_READ_WITH_SHARED_LOCKS)
11319
4808
    {
11320
4809
      table->key_read=1;
11334
4823
      pos->records_read=0.0;
11335
4824
      pos->ref_depend_map= 0;
11336
4825
      if (!table->maybe_null || error > 0)
11337
 
        return(error);
 
4826
        return(error);
11338
4827
    }
11339
4828
  }
11340
4829
  if (*tab->on_expr_ref && !table->null_row)
11368
4857
  return(0);
11369
4858
}
11370
4859
 
11371
 
 
11372
 
static int
11373
 
join_read_system(JOIN_TAB *tab)
 
4860
int join_read_system(JOIN_TAB *tab)
11374
4861
{
11375
4862
  Table *table= tab->table;
11376
4863
  int error;
11380
4867
                                           table->s->primary_key)))
11381
4868
    {
11382
4869
      if (error != HA_ERR_END_OF_FILE)
11383
 
        return table->report_error(error);
 
4870
        return table->report_error(error);
11384
4871
      tab->table->mark_as_null_row();
11385
4872
      table->emptyRecord();                     // Make empty record
11386
4873
      return -1;
11393
4880
  return table->status ? -1 : 0;
11394
4881
}
11395
4882
 
11396
 
 
11397
4883
/**
11398
4884
  Read a (constant) table when there is at most one matching row.
11399
4885
 
11406
4892
  @retval
11407
4893
    1   Got an error (other than row not found) during read
11408
4894
*/
11409
 
 
11410
 
static int
11411
 
join_read_const(JOIN_TAB *tab)
 
4895
int join_read_const(JOIN_TAB *tab)
11412
4896
{
11413
4897
  int error;
11414
4898
  Table *table= tab->table;
11416
4900
  {
11417
4901
    table->status= 0;
11418
4902
    if (cp_buffer_from_ref(tab->join->session, &tab->ref))
11419
 
      error=HA_ERR_KEY_NOT_FOUND;
 
4903
      error= HA_ERR_KEY_NOT_FOUND;
11420
4904
    else
11421
4905
    {
11422
4906
      error=table->file->index_read_idx_map(table->record[0],tab->ref.key,
11430
4914
      tab->table->mark_as_null_row();
11431
4915
      table->emptyRecord();
11432
4916
      if (error != HA_ERR_KEY_NOT_FOUND && error != HA_ERR_END_OF_FILE)
11433
 
        return table->report_error(error);
 
4917
        return table->report_error(error);
11434
4918
      return -1;
11435
4919
    }
11436
4920
    table->storeRecord();
11444
4928
  return table->status ? -1 : 0;
11445
4929
}
11446
4930
 
11447
 
 
11448
4931
/*
11449
4932
  eq_ref access method implementation: "read_first" function
11450
4933
 
11461
4944
   -1  - Row not found
11462
4945
    1  - Error
11463
4946
*/
11464
 
 
11465
 
static int
11466
 
join_read_key(JOIN_TAB *tab)
 
4947
int join_read_key(JOIN_TAB *tab)
11467
4948
{
11468
4949
  int error;
11469
4950
  Table *table= tab->table;
11493
4974
  return table->status ? -1 : 0;
11494
4975
}
11495
4976
 
11496
 
 
11497
4977
/*
11498
4978
  ref access method implementation: "read_first" function
11499
4979
 
11502
4982
      tab  JOIN_TAB of the accessed table
11503
4983
 
11504
4984
  DESCRIPTION
11505
 
    This is "read_fist" function for the "ref" access method.
 
4985
    This is "read_first" function for the "ref" access method.
11506
4986
 
11507
4987
    The functon must leave the index initialized when it returns.
11508
4988
    ref_or_null access implementation depends on that.
11512
4992
   -1  - Row not found
11513
4993
    1  - Error
11514
4994
*/
11515
 
 
11516
 
static int
11517
 
join_read_always_key(JOIN_TAB *tab)
 
4995
int join_read_always_key(JOIN_TAB *tab)
11518
4996
{
11519
4997
  int error;
11520
4998
  Table *table= tab->table;
11545
5023
  return 0;
11546
5024
}
11547
5025
 
11548
 
 
11549
5026
/**
11550
5027
  This function is used when optimizing away order_st BY in
11551
5028
  SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC.
11552
5029
*/
11553
 
 
11554
 
static int
11555
 
join_read_last_key(JOIN_TAB *tab)
 
5030
int join_read_last_key(JOIN_TAB *tab)
11556
5031
{
11557
5032
  int error;
11558
5033
  Table *table= tab->table;
11572
5047
  return 0;
11573
5048
}
11574
5049
 
11575
 
 
11576
 
        /* ARGSUSED */
11577
 
static int
11578
 
join_no_more_records(READ_RECORD *)
 
5050
int join_no_more_records(READ_RECORD *)
11579
5051
{
11580
5052
  return -1;
11581
5053
}
11582
5054
 
11583
 
static int
11584
 
join_read_next_same_diff(READ_RECORD *info)
 
5055
int join_read_next_same_diff(READ_RECORD *info)
11585
5056
{
11586
5057
  Table *table= info->table;
11587
5058
  JOIN_TAB *tab=table->reginfo.join_tab;
11612
5083
    return join_read_next_same(info);
11613
5084
}
11614
5085
 
11615
 
static int
11616
 
join_read_next_same(READ_RECORD *info)
 
5086
int join_read_next_same(READ_RECORD *info)
11617
5087
{
11618
5088
  int error;
11619
5089
  Table *table= info->table;
11632
5102
  return 0;
11633
5103
}
11634
5104
 
11635
 
 
11636
 
static int
11637
 
join_read_prev_same(READ_RECORD *info)
 
5105
int join_read_prev_same(READ_RECORD *info)
11638
5106
{
11639
5107
  int error;
11640
5108
  Table *table= info->table;
11648
5116
    table->status=STATUS_NOT_FOUND;
11649
5117
    error= -1;
11650
5118
  }
11651
 
 
11652
5119
  return error;
11653
5120
}
11654
5121
 
11655
 
 
11656
 
static int
11657
 
join_init_quick_read_record(JOIN_TAB *tab)
 
5122
int join_init_quick_read_record(JOIN_TAB *tab)
11658
5123
{
11659
5124
  if (test_if_quick_select(tab) == -1)
11660
5125
    return -1;                                  /* No possible records */
11661
5126
  return join_init_read_record(tab);
11662
5127
}
11663
5128
 
11664
 
 
11665
5129
int rr_sequential(READ_RECORD *info);
11666
5130
int init_read_record_seq(JOIN_TAB *tab)
11667
5131
{
11671
5135
  return (*tab->read_record.read_record)(&tab->read_record);
11672
5136
}
11673
5137
 
11674
 
static int
11675
 
test_if_quick_select(JOIN_TAB *tab)
 
5138
int test_if_quick_select(JOIN_TAB *tab)
11676
5139
{
11677
5140
  delete tab->select->quick;
11678
 
  tab->select->quick=0;
 
5141
  tab->select->quick= 0;
11679
5142
  return tab->select->test_quick_select(tab->join->session, tab->keys,
11680
 
                                        (table_map) 0, HA_POS_ERROR, 0,
11681
 
                                        false);
 
5143
                                        (table_map) 0, HA_POS_ERROR, 0, false);
11682
5144
}
11683
5145
 
11684
 
 
11685
 
static int
11686
 
join_init_read_record(JOIN_TAB *tab)
 
5146
int join_init_read_record(JOIN_TAB *tab)
11687
5147
{
11688
5148
  if (tab->select && tab->select->quick && tab->select->quick->reset())
11689
5149
    return 1;
11692
5152
  return (*tab->read_record.read_record)(&tab->read_record);
11693
5153
}
11694
5154
 
11695
 
 
11696
 
static int
11697
 
join_read_first(JOIN_TAB *tab)
 
5155
int join_read_first(JOIN_TAB *tab)
11698
5156
{
11699
5157
  int error;
11700
5158
  Table *table=tab->table;
11733
5191
  return 0;
11734
5192
}
11735
5193
 
11736
 
 
11737
 
static int
11738
 
join_read_next_different(READ_RECORD *info)
 
5194
int join_read_next_different(READ_RECORD *info)
11739
5195
{
11740
5196
  JOIN_TAB *tab= info->do_insideout_scan;
11741
5197
  if (tab->insideout_match_tab->found_match)
11758
5214
    return join_read_next(info);
11759
5215
}
11760
5216
 
11761
 
 
11762
 
static int
11763
 
join_read_next(READ_RECORD *info)
 
5217
int join_read_next(READ_RECORD *info)
11764
5218
{
11765
5219
  int error;
11766
5220
  if ((error=info->file->index_next(info->record)))
11768
5222
  return 0;
11769
5223
}
11770
5224
 
11771
 
 
11772
 
static int
11773
 
join_read_last(JOIN_TAB *tab)
 
5225
int join_read_last(JOIN_TAB *tab)
11774
5226
{
11775
5227
  Table *table=tab->table;
11776
5228
  int error;
11794
5246
  return 0;
11795
5247
}
11796
5248
 
11797
 
 
11798
 
static int
11799
 
join_read_prev(READ_RECORD *info)
 
5249
int join_read_prev(READ_RECORD *info)
11800
5250
{
11801
5251
  int error;
11802
5252
  if ((error= info->file->index_prev(info->record)))
11808
5258
/**
11809
5259
  Reading of key with key reference and one part that may be NULL.
11810
5260
*/
11811
 
 
11812
 
int
11813
 
join_read_always_key_or_null(JOIN_TAB *tab)
 
5261
int join_read_always_key_or_null(JOIN_TAB *tab)
11814
5262
{
11815
5263
  int res;
11816
5264
 
11824
5272
  return safe_index_read(tab);
11825
5273
}
11826
5274
 
11827
 
 
11828
 
int
11829
 
join_read_next_same_or_null(READ_RECORD *info)
 
5275
int join_read_next_same_or_null(READ_RECORD *info)
11830
5276
{
11831
5277
  int error;
11832
5278
  if ((error= join_read_next_same(info)) >= 0)
11840
5286
  return safe_index_read(tab);                  // then read null keys
11841
5287
}
11842
5288
 
11843
 
 
11844
 
/*****************************************************************************
11845
 
  DESCRIPTION
11846
 
    Functions that end one nested loop iteration. Different functions
11847
 
    are used to support GROUP BY clause and to redirect records
11848
 
    to a table (e.g. in case of SELECT into a temporary table) or to the
11849
 
    network client.
11850
 
 
11851
 
  RETURN VALUES
11852
 
    NESTED_LOOP_OK           - the record has been successfully handled
11853
 
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
11854
 
                               was detected
11855
 
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
11856
 
                               the record
11857
 
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
11858
 
                               additionally, the nested loop produced the
11859
 
                               number of rows specified in the LIMIT clause
11860
 
                               for the query
11861
 
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
11862
 
                               additionally, there is a cursor and the nested
11863
 
                               loop algorithm produced the number of rows
11864
 
                               that is specified for current cursor fetch
11865
 
                               operation.
11866
 
   All return values except NESTED_LOOP_OK abort the nested loop.
11867
 
*****************************************************************************/
11868
 
 
11869
 
/* ARGSUSED */
11870
 
static enum_nested_loop_state
11871
 
end_send(JOIN *join, JOIN_TAB *,
11872
 
         bool end_of_records)
11873
 
{
11874
 
  if (!end_of_records)
11875
 
  {
11876
 
    int error;
11877
 
    if (join->having && join->having->val_int() == 0)
11878
 
      return(NESTED_LOOP_OK);               // Didn't match having
11879
 
    error=0;
11880
 
    if (join->do_send_rows)
11881
 
      error=join->result->send_data(*join->fields);
11882
 
    if (error)
11883
 
      return(NESTED_LOOP_ERROR); /* purecov: inspected */
11884
 
    if (++join->send_records >= join->unit->select_limit_cnt &&
11885
 
        join->do_send_rows)
11886
 
    {
11887
 
      if (join->select_options & OPTION_FOUND_ROWS)
11888
 
      {
11889
 
        JOIN_TAB *jt=join->join_tab;
11890
 
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
11891
 
            && !join->send_group_parts && !join->having && !jt->select_cond &&
11892
 
            !(jt->select && jt->select->quick) &&
11893
 
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
11894
 
            (jt->ref.key < 0))
11895
 
        {
11896
 
          /* Join over all rows in table;  Return number of found rows */
11897
 
          Table *table=jt->table;
11898
 
 
11899
 
          join->select_options ^= OPTION_FOUND_ROWS;
11900
 
          if (table->sort.record_pointers ||
11901
 
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
11902
 
          {
11903
 
            /* Using filesort */
11904
 
            join->send_records= table->sort.found_records;
11905
 
          }
11906
 
          else
11907
 
          {
11908
 
            table->file->info(HA_STATUS_VARIABLE);
11909
 
            join->send_records= table->file->stats.records;
11910
 
          }
11911
 
        }
11912
 
        else
11913
 
        {
11914
 
          join->do_send_rows= 0;
11915
 
          if (join->unit->fake_select_lex)
11916
 
            join->unit->fake_select_lex->select_limit= 0;
11917
 
          return(NESTED_LOOP_OK);
11918
 
        }
11919
 
      }
11920
 
      return(NESTED_LOOP_QUERY_LIMIT);      // Abort nicely
11921
 
    }
11922
 
    else if (join->send_records >= join->fetch_limit)
11923
 
    {
11924
 
      /*
11925
 
        There is a server side cursor and all rows for
11926
 
        this fetch request are sent.
11927
 
      */
11928
 
      return(NESTED_LOOP_CURSOR_LIMIT);
11929
 
    }
11930
 
  }
11931
 
 
11932
 
  return(NESTED_LOOP_OK);
11933
 
}
11934
 
 
11935
 
 
11936
 
/* ARGSUSED */
11937
 
enum_nested_loop_state
11938
 
end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
 
5289
enum_nested_loop_state end_send_group(JOIN *join, JOIN_TAB *, bool end_of_records)
11939
5290
{
11940
5291
  int idx= -1;
11941
5292
  enum_nested_loop_state ok_code= NESTED_LOOP_OK;
11948
5299
    {
11949
5300
      if (idx < (int) join->send_group_parts)
11950
5301
      {
11951
 
        int error=0;
11952
 
        {
11953
 
          if (!join->first_record)
11954
 
          {
11955
 
            List_iterator_fast<Item> it(*join->fields);
11956
 
            Item *item;
11957
 
            /* No matching rows for group function */
11958
 
            join->clear();
 
5302
        int error=0;
 
5303
        {
 
5304
          if (!join->first_record)
 
5305
          {
 
5306
                  List_iterator_fast<Item> it(*join->fields);
 
5307
                  Item *item;
 
5308
            /* No matching rows for group function */
 
5309
            join->clear();
11959
5310
 
11960
5311
            while ((item= it++))
11961
5312
              item->no_rows_in_result();
11962
 
          }
11963
 
          if (join->having && join->having->val_int() == 0)
11964
 
            error= -1;                          // Didn't satisfy having
11965
 
          else
11966
 
          {
11967
 
            if (join->do_send_rows)
11968
 
              error=join->result->send_data(*join->fields) ? 1 : 0;
11969
 
            join->send_records++;
11970
 
          }
11971
 
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
11972
 
          {
11973
 
            if (join->rollup_send_data((uint32_t) (idx+1)))
11974
 
              error= 1;
11975
 
          }
11976
 
        }
11977
 
        if (error > 0)
 
5313
          }
 
5314
          if (join->having && join->having->val_int() == 0)
 
5315
            error= -1;                          // Didn't satisfy having
 
5316
          else
 
5317
          {
 
5318
            if (join->do_send_rows)
 
5319
              error=join->result->send_data(*join->fields) ? 1 : 0;
 
5320
            join->send_records++;
 
5321
          }
 
5322
          if (join->rollup.state != ROLLUP::STATE_NONE && error <= 0)
 
5323
          {
 
5324
            if (join->rollup_send_data((uint32_t) (idx+1)))
 
5325
              error= 1;
 
5326
          }
 
5327
        }
 
5328
        if (error > 0)
11978
5329
          return(NESTED_LOOP_ERROR);        /* purecov: inspected */
11979
 
        if (end_of_records)
11980
 
          return(NESTED_LOOP_OK);
11981
 
        if (join->send_records >= join->unit->select_limit_cnt &&
11982
 
            join->do_send_rows)
11983
 
        {
11984
 
          if (!(join->select_options & OPTION_FOUND_ROWS))
11985
 
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
11986
 
          join->do_send_rows=0;
11987
 
          join->unit->select_limit_cnt = HA_POS_ERROR;
 
5330
        if (end_of_records)
 
5331
          return(NESTED_LOOP_OK);
 
5332
        if (join->send_records >= join->unit->select_limit_cnt &&
 
5333
            join->do_send_rows)
 
5334
        {
 
5335
          if (!(join->select_options & OPTION_FOUND_ROWS))
 
5336
            return(NESTED_LOOP_QUERY_LIMIT); // Abort nicely
 
5337
          join->do_send_rows=0;
 
5338
          join->unit->select_limit_cnt = HA_POS_ERROR;
11988
5339
        }
11989
5340
        else if (join->send_records >= join->fetch_limit)
11990
5341
        {
12003
5354
    else
12004
5355
    {
12005
5356
      if (end_of_records)
12006
 
        return(NESTED_LOOP_OK);
 
5357
        return(NESTED_LOOP_OK);
12007
5358
      join->first_record=1;
12008
5359
      test_if_item_cache_changed(join->group_fields);
12009
5360
    }
12015
5366
      */
12016
5367
      copy_fields(&join->tmp_table_param);
12017
5368
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12018
 
        return(NESTED_LOOP_ERROR);
 
5369
        return(NESTED_LOOP_ERROR);
12019
5370
      return(ok_code);
12020
5371
    }
12021
5372
  }
12024
5375
  return(NESTED_LOOP_OK);
12025
5376
}
12026
5377
 
12027
 
 
12028
 
/* ARGSUSED */
12029
 
enum_nested_loop_state
12030
 
end_write(JOIN *join, JOIN_TAB *,
12031
 
          bool end_of_records)
12032
 
{
12033
 
  Table *table=join->tmp_table;
12034
 
 
12035
 
  if (join->session->killed)                    // Aborted by user
12036
 
  {
12037
 
    join->session->send_kill_message();
12038
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12039
 
  }
12040
 
  if (!end_of_records)
12041
 
  {
12042
 
    copy_fields(&join->tmp_table_param);
12043
 
    copy_funcs(join->tmp_table_param.items_to_copy);
12044
 
    if (!join->having || join->having->val_int())
12045
 
    {
12046
 
      int error;
12047
 
      join->found_records++;
12048
 
      if ((error=table->file->ha_write_row(table->record[0])))
12049
 
      {
12050
 
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
12051
 
          goto end;
12052
 
        if (create_myisam_from_heap(join->session, table,
12053
 
                                    join->tmp_table_param.start_recinfo,
12054
 
                                    &join->tmp_table_param.recinfo,
12055
 
                                    error, 1))
12056
 
          return(NESTED_LOOP_ERROR);        // Not a table_is_full error
12057
 
        table->s->uniques=0;                    // To ensure rows are the same
12058
 
      }
12059
 
      if (++join->send_records >= join->tmp_table_param.end_write_records &&
12060
 
          join->do_send_rows)
12061
 
      {
12062
 
        if (!(join->select_options & OPTION_FOUND_ROWS))
12063
 
          return(NESTED_LOOP_QUERY_LIMIT);
12064
 
        join->do_send_rows=0;
12065
 
        join->unit->select_limit_cnt = HA_POS_ERROR;
12066
 
        return(NESTED_LOOP_OK);
12067
 
      }
12068
 
    }
12069
 
  }
12070
 
end:
12071
 
  return(NESTED_LOOP_OK);
12072
 
}
12073
 
 
12074
 
/* ARGSUSED */
12075
 
/** Group by searching after group record and updating it if possible. */
12076
 
 
12077
 
static enum_nested_loop_state
12078
 
end_update(JOIN *join, JOIN_TAB *,
12079
 
           bool end_of_records)
12080
 
{
12081
 
  Table *table=join->tmp_table;
12082
 
  order_st   *group;
12083
 
  int     error;
12084
 
 
12085
 
  if (end_of_records)
12086
 
    return(NESTED_LOOP_OK);
12087
 
  if (join->session->killed)                    // Aborted by user
12088
 
  {
12089
 
    join->session->send_kill_message();
12090
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12091
 
  }
12092
 
 
12093
 
  join->found_records++;
12094
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
12095
 
  /* Make a key of group index */
12096
 
  for (group=table->group ; group ; group=group->next)
12097
 
  {
12098
 
    Item *item= *group->item;
12099
 
    item->save_org_in_field(group->field);
12100
 
    /* Store in the used key if the field was 0 */
12101
 
    if (item->maybe_null)
12102
 
      group->buff[-1]= (char) group->field->is_null();
12103
 
  }
12104
 
  if (!table->file->index_read_map(table->record[1],
12105
 
                                   join->tmp_table_param.group_buff,
12106
 
                                   HA_WHOLE_KEY,
12107
 
                                   HA_READ_KEY_EXACT))
12108
 
  {                                             /* Update old record */
12109
 
    table->restoreRecord();
12110
 
    update_tmptable_sum_func(join->sum_funcs,table);
12111
 
    if ((error=table->file->ha_update_row(table->record[1],
12112
 
                                          table->record[0])))
12113
 
    {
12114
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12115
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12116
 
    }
12117
 
    return(NESTED_LOOP_OK);
12118
 
  }
12119
 
 
12120
 
  /*
12121
 
    Copy null bits from group key to table
12122
 
    We can't copy all data as the key may have different format
12123
 
    as the row data (for example as with VARCHAR keys)
12124
 
  */
12125
 
  KEY_PART_INFO *key_part;
12126
 
  for (group=table->group,key_part=table->key_info[0].key_part;
12127
 
       group ;
12128
 
       group=group->next,key_part++)
12129
 
  {
12130
 
    if (key_part->null_bit)
12131
 
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
12132
 
  }
12133
 
  init_tmptable_sum_functions(join->sum_funcs);
12134
 
  copy_funcs(join->tmp_table_param.items_to_copy);
12135
 
  if ((error=table->file->ha_write_row(table->record[0])))
12136
 
  {
12137
 
    if (create_myisam_from_heap(join->session, table,
12138
 
                                join->tmp_table_param.start_recinfo,
12139
 
                                &join->tmp_table_param.recinfo,
12140
 
                                error, 0))
12141
 
      return(NESTED_LOOP_ERROR);            // Not a table_is_full error
12142
 
    /* Change method to update rows */
12143
 
    table->file->ha_index_init(0, 0);
12144
 
    join->join_tab[join->tables-1].next_select=end_unique_update;
12145
 
  }
12146
 
  join->send_records++;
12147
 
  return(NESTED_LOOP_OK);
12148
 
}
12149
 
 
12150
 
 
12151
 
/** Like end_update, but this is done with unique constraints instead of keys.  */
12152
 
 
12153
 
static enum_nested_loop_state
12154
 
end_unique_update(JOIN *join, JOIN_TAB *,
12155
 
                  bool end_of_records)
12156
 
{
12157
 
  Table *table=join->tmp_table;
12158
 
  int     error;
12159
 
 
12160
 
  if (end_of_records)
12161
 
    return(NESTED_LOOP_OK);
12162
 
  if (join->session->killed)                    // Aborted by user
12163
 
  {
12164
 
    join->session->send_kill_message();
12165
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
12166
 
  }
12167
 
 
12168
 
  init_tmptable_sum_functions(join->sum_funcs);
12169
 
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
12170
 
  copy_funcs(join->tmp_table_param.items_to_copy);
12171
 
 
12172
 
  if (!(error=table->file->ha_write_row(table->record[0])))
12173
 
    join->send_records++;                       // New group
12174
 
  else
12175
 
  {
12176
 
    if ((int) table->file->get_dup_key(error) < 0)
12177
 
    {
12178
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12179
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12180
 
    }
12181
 
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
12182
 
    {
12183
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12184
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12185
 
    }
12186
 
    table->restoreRecord();
12187
 
    update_tmptable_sum_func(join->sum_funcs,table);
12188
 
    if ((error=table->file->ha_update_row(table->record[1],
12189
 
                                          table->record[0])))
12190
 
    {
12191
 
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
12192
 
      return(NESTED_LOOP_ERROR);            /* purecov: inspected */
12193
 
    }
12194
 
  }
12195
 
  return(NESTED_LOOP_OK);
12196
 
}
12197
 
 
12198
 
 
12199
 
/* ARGSUSED */
12200
 
enum_nested_loop_state
12201
 
end_write_group(JOIN *join, JOIN_TAB *,
12202
 
                bool end_of_records)
 
5378
enum_nested_loop_state end_write_group(JOIN *join, JOIN_TAB *, bool end_of_records)
12203
5379
{
12204
5380
  Table *table=join->tmp_table;
12205
5381
  int     idx= -1;
12207
5383
  if (join->session->killed)
12208
5384
  {                                             // Aborted by user
12209
5385
    join->session->send_kill_message();
12210
 
    return(NESTED_LOOP_KILLED);             /* purecov: inspected */
 
5386
    return NESTED_LOOP_KILLED;             /* purecov: inspected */
12211
5387
  }
12212
5388
  if (!join->first_record || end_of_records ||
12213
5389
      (idx=test_if_item_cache_changed(join->group_fields)) >= 0)
12217
5393
      int send_group_parts= join->send_group_parts;
12218
5394
      if (idx < send_group_parts)
12219
5395
      {
12220
 
        if (!join->first_record)
12221
 
        {
12222
 
          /* No matching rows for group function */
12223
 
          join->clear();
12224
 
        }
12225
 
        copy_sum_funcs(join->sum_funcs,
12226
 
                       join->sum_funcs_end[send_group_parts]);
12227
 
        if (!join->having || join->having->val_int())
12228
 
        {
 
5396
        if (!join->first_record)
 
5397
        {
 
5398
          /* No matching rows for group function */
 
5399
          join->clear();
 
5400
        }
 
5401
        copy_sum_funcs(join->sum_funcs, join->sum_funcs_end[send_group_parts]);
 
5402
        if (!join->having || join->having->val_int())
 
5403
        {
12229
5404
          int error= table->file->ha_write_row(table->record[0]);
12230
5405
          if (error && create_myisam_from_heap(join->session, table,
12231
 
                                               join->tmp_table_param.start_recinfo,
 
5406
                                              join->tmp_table_param.start_recinfo,
12232
5407
                                                &join->tmp_table_param.recinfo,
12233
 
                                               error, 0))
12234
 
            return(NESTED_LOOP_ERROR);
 
5408
                                              error, 0))
 
5409
          return NESTED_LOOP_ERROR;
12235
5410
        }
12236
5411
        if (join->rollup.state != ROLLUP::STATE_NONE)
12237
 
        {
12238
 
          if (join->rollup_write_data((uint32_t) (idx+1), table))
12239
 
            return(NESTED_LOOP_ERROR);
12240
 
        }
12241
 
        if (end_of_records)
12242
 
          return(NESTED_LOOP_OK);
 
5412
        {
 
5413
          if (join->rollup_write_data((uint32_t) (idx+1), table))
 
5414
            return NESTED_LOOP_ERROR;
 
5415
        }
 
5416
        if (end_of_records)
 
5417
          return NESTED_LOOP_OK;
12243
5418
      }
12244
5419
    }
12245
5420
    else
12246
5421
    {
12247
5422
      if (end_of_records)
12248
 
        return(NESTED_LOOP_OK);
 
5423
        return NESTED_LOOP_OK;
12249
5424
      join->first_record=1;
12250
5425
      test_if_item_cache_changed(join->group_fields);
12251
5426
    }
12254
5429
      copy_fields(&join->tmp_table_param);
12255
5430
      copy_funcs(join->tmp_table_param.items_to_copy);
12256
5431
      if (init_sum_functions(join->sum_funcs, join->sum_funcs_end[idx+1]))
12257
 
        return(NESTED_LOOP_ERROR);
12258
 
      return(NESTED_LOOP_OK);
 
5432
        return NESTED_LOOP_ERROR;
 
5433
      return NESTED_LOOP_OK;
12259
5434
    }
12260
5435
  }
12261
5436
  if (update_sum_func(join->sum_funcs))
12262
 
    return(NESTED_LOOP_ERROR);
12263
 
  return(NESTED_LOOP_OK);
 
5437
    return NESTED_LOOP_ERROR;
 
5438
  return NESTED_LOOP_OK;
12264
5439
}
12265
5440
 
12266
 
 
12267
5441
/*****************************************************************************
12268
5442
  Remove calculation with tables that aren't yet read. Remove also tests
12269
5443
  against fields that are read through key where the table is not a
12276
5450
  @return
12277
5451
    1 if right_item is used removable reference key on left_item
12278
5452
*/
12279
 
 
12280
 
static bool test_if_ref(Item_field *left_item,Item *right_item)
 
5453
bool test_if_ref(Item_field *left_item,Item *right_item)
12281
5454
{
12282
5455
  Field *field=left_item->field;
12283
5456
  // No need to change const test. We also have to keep tests on LEFT JOIN
12288
5461
    {
12289
5462
      right_item= right_item->real_item();
12290
5463
      if (right_item->type() == Item::FIELD_ITEM)
12291
 
        return (field->eq_def(((Item_field *) right_item)->field));
 
5464
        return (field->eq_def(((Item_field *) right_item)->field));
12292
5465
      /* remove equalities injected by IN->EXISTS transformation */
12293
5466
      else if (right_item->type() == Item::CACHE_ITEM)
12294
5467
        return ((Item_cache *)right_item)->eq_def (field);
12295
5468
      if (right_item->const_item() && !(right_item->is_null()))
12296
5469
      {
12297
 
        /*
12298
 
          We can remove binary fields and numerical fields except float,
12299
 
          as float comparison isn't 100 % secure
12300
 
          We have to keep normal strings to be able to check for end spaces
 
5470
        /*
 
5471
          We can remove binary fields and numerical fields except float,
 
5472
          as float comparison isn't 100 % secure
 
5473
          We have to keep normal strings to be able to check for end spaces
12301
5474
 
12302
 
          sergefp: the above seems to be too restrictive. Counterexample:
12303
 
            create table t100 (v varchar(10), key(v)) default charset=latin1;
12304
 
            insert into t100 values ('a'),('a ');
12305
 
            explain select * from t100 where v='a';
12306
 
          The EXPLAIN shows 'using Where'. Running the query returns both
12307
 
          rows, so it seems there are no problems with endspace in the most
12308
 
          frequent case?
12309
 
        */
12310
 
        if (field->binary() &&
12311
 
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
12312
 
            field->decimals() == 0)
12313
 
        {
12314
 
          return !store_val_in_field(field, right_item, CHECK_FIELD_WARN);
12315
 
        }
 
5475
                sergefp: the above seems to be too restrictive. Counterexample:
 
5476
                  create table t100 (v varchar(10), key(v)) default charset=latin1;
 
5477
                  insert into t100 values ('a'),('a ');
 
5478
                  explain select * from t100 where v='a';
 
5479
                The EXPLAIN shows 'using Where'. Running the query returns both
 
5480
                rows, so it seems there are no problems with endspace in the most
 
5481
                frequent case?
 
5482
        */
 
5483
        if (field->binary() &&
 
5484
            field->real_type() != DRIZZLE_TYPE_VARCHAR &&
 
5485
            field->decimals() == 0)
 
5486
        {
 
5487
          return ! store_val_in_field(field, right_item, CHECK_FIELD_WARN);
 
5488
        }
12316
5489
      }
12317
5490
    }
12318
5491
  }
12319
 
  return 0;                                     // keep test
12320
 
}
12321
 
 
12322
 
/**
12323
 
   @brief Replaces an expression destructively inside the expression tree of
12324
 
   the WHERE clase.
12325
 
 
12326
 
   @note Because of current requirements for semijoin flattening, we do not
12327
 
   need to recurse here, hence this function will only examine the top-level
12328
 
   AND conditions. (see JOIN::prepare, comment above the line
12329
 
   'if (do_materialize)'
12330
 
 
12331
 
   @param join The top-level query.
12332
 
   @param old_cond The expression to be replaced.
12333
 
   @param new_cond The expression to be substituted.
12334
 
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
12335
 
   the new expression.
12336
 
   @return <code>true</code> if there was an error, <code>false</code> if
12337
 
   successful.
12338
 
*/
12339
 
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
12340
 
                                       Item *new_cond, bool do_fix_fields)
12341
 
{
12342
 
  if (join->conds == old_cond) {
12343
 
    join->conds= new_cond;
12344
 
    if (do_fix_fields)
12345
 
      new_cond->fix_fields(join->session, &join->conds);
12346
 
    return false;
12347
 
  }
12348
 
 
12349
 
  if (join->conds->type() == Item::COND_ITEM) {
12350
 
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
12351
 
    Item *item;
12352
 
    while ((item= li++))
12353
 
      if (item == old_cond)
12354
 
      {
12355
 
        li.replace(new_cond);
12356
 
        if (do_fix_fields)
12357
 
          new_cond->fix_fields(join->session, li.ref());
12358
 
        return false;
12359
 
      }
12360
 
  }
12361
 
 
12362
 
  return true;
 
5492
  return 0;
12363
5493
}
12364
5494
 
12365
5495
/*
12393
5523
  RETURN
12394
5524
    Extracted condition
12395
5525
*/
12396
 
 
12397
 
static COND *
12398
 
make_cond_for_table(COND *cond, table_map tables, table_map used_table,
12399
 
                    bool exclude_expensive_cond)
 
5526
COND *make_cond_for_table(COND *cond, table_map tables, table_map used_table, bool exclude_expensive_cond)
12400
5527
{
12401
5528
  if (used_table && !(cond->used_tables() & used_table) &&
12402
 
      /*
12403
 
        Exclude constant conditions not checked at optimization time if
12404
 
        the table we are pushing conditions to is the first one.
12405
 
        As a result, such conditions are not considered as already checked
12406
 
        and will be checked at execution time, attached to the first table.
12407
 
      */
12408
 
      !((used_table & 1) && cond->is_expensive()))
 
5529
    /*
 
5530
      Exclude constant conditions not checked at optimization time if
 
5531
      the table we are pushing conditions to is the first one.
 
5532
      As a result, such conditions are not considered as already checked
 
5533
      and will be checked at execution time, attached to the first table.
 
5534
    */
 
5535
    !((used_table & 1) && cond->is_expensive()))
12409
5536
    return (COND*) 0;                           // Already checked
12410
5537
  if (cond->type() == Item::COND_ITEM)
12411
5538
  {
12414
5541
      /* Create new top level AND item */
12415
5542
      Item_cond_and *new_cond=new Item_cond_and;
12416
5543
      if (!new_cond)
12417
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
5544
        return (COND*) 0;                       // OOM /* purecov: inspected */
12418
5545
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
12419
5546
      Item *item;
12420
5547
      while ((item=li++))
12421
5548
      {
12422
 
        Item *fix=make_cond_for_table(item,tables,used_table,
12423
 
                                      exclude_expensive_cond);
12424
 
        if (fix)
12425
 
          new_cond->argument_list()->push_back(fix);
 
5549
        Item *fix= make_cond_for_table(item,tables,used_table,
 
5550
                                            exclude_expensive_cond);
 
5551
        if (fix)
 
5552
          new_cond->argument_list()->push_back(fix);
12426
5553
      }
12427
 
      switch (new_cond->argument_list()->elements) {
12428
 
      case 0:
12429
 
        return (COND*) 0;                       // Always true
12430
 
      case 1:
12431
 
        return new_cond->argument_list()->head();
12432
 
      default:
12433
 
        /*
12434
 
          Item_cond_and do not need fix_fields for execution, its parameters
12435
 
          are fixed or do not need fix_fields, too
12436
 
        */
12437
 
        new_cond->quick_fix_field();
12438
 
        new_cond->used_tables_cache=
12439
 
          ((Item_cond_and*) cond)->used_tables_cache &
12440
 
          tables;
12441
 
        return new_cond;
 
5554
      switch (new_cond->argument_list()->elements) 
 
5555
      {
 
5556
        case 0:
 
5557
          return (COND*) 0;                     // Always true
 
5558
        case 1:
 
5559
          return new_cond->argument_list()->head();
 
5560
        default:
 
5561
          /*
 
5562
            Item_cond_and do not need fix_fields for execution, its parameters
 
5563
            are fixed or do not need fix_fields, too
 
5564
          */
 
5565
          new_cond->quick_fix_field();
 
5566
          new_cond->used_tables_cache= ((Item_cond_and*) cond)->used_tables_cache & tables;
 
5567
          return new_cond;
12442
5568
      }
12443
5569
    }
12444
5570
    else
12445
5571
    {                                           // Or list
12446
5572
      Item_cond_or *new_cond=new Item_cond_or;
12447
5573
      if (!new_cond)
12448
 
        return (COND*) 0;                       // OOM /* purecov: inspected */
 
5574
        return (COND*) 0;                       // OOM /* purecov: inspected */
12449
5575
      List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
12450
5576
      Item *item;
12451
5577
      while ((item=li++))
12452
5578
      {
12453
 
        Item *fix=make_cond_for_table(item,tables,0L, exclude_expensive_cond);
12454
 
        if (!fix)
12455
 
          return (COND*) 0;                     // Always true
12456
 
        new_cond->argument_list()->push_back(fix);
 
5579
        Item *fix= make_cond_for_table(item,tables,0L, exclude_expensive_cond);
 
5580
        if (!fix)
 
5581
          return (COND*) 0;                     // Always true
 
5582
        new_cond->argument_list()->push_back(fix);
12457
5583
      }
12458
5584
      /*
12459
 
        Item_cond_and do not need fix_fields for execution, its parameters
12460
 
        are fixed or do not need fix_fields, too
 
5585
        Item_cond_and do not need fix_fields for execution, its parameters
 
5586
        are fixed or do not need fix_fields, too
12461
5587
      */
12462
5588
      new_cond->quick_fix_field();
12463
5589
      new_cond->used_tables_cache= ((Item_cond_or*) cond)->used_tables_cache;
12490
5616
  {
12491
5617
    Item *left_item=    ((Item_func*) cond)->arguments()[0];
12492
5618
    Item *right_item= ((Item_func*) cond)->arguments()[1];
12493
 
    if (left_item->type() == Item::FIELD_ITEM &&
12494
 
        test_if_ref((Item_field*) left_item,right_item))
 
5619
    if (left_item->type() == Item::FIELD_ITEM && test_if_ref((Item_field*) left_item,right_item))
12495
5620
    {
12496
5621
      cond->marker=3;                   // Checked when read
12497
5622
      return (COND*) 0;
12498
5623
    }
12499
 
    if (right_item->type() == Item::FIELD_ITEM &&
12500
 
        test_if_ref((Item_field*) right_item,left_item))
 
5624
    if (right_item->type() == Item::FIELD_ITEM &&       test_if_ref((Item_field*) right_item,left_item))
12501
5625
    {
12502
5626
      cond->marker=3;                   // Checked when read
12503
5627
      return (COND*) 0;
12507
5631
  return cond;
12508
5632
}
12509
5633
 
12510
 
 
12511
 
static Item *
12512
 
part_of_refkey(Table *table,Field *field)
 
5634
static Item *part_of_refkey(Table *table,Field *field)
12513
5635
{
12514
5636
  if (!table->reginfo.join_tab)
12515
5637
    return (Item*) 0;             // field from outer non-select (UPDATE,...)
12535
5657
  return (Item*) 0;
12536
5658
}
12537
5659
 
12538
 
 
12539
5660
/**
12540
5661
  Test if one can use the key to resolve order_st BY.
12541
5662
 
12556
5677
  @retval
12557
5678
    -1   Reverse key can be used
12558
5679
*/
12559
 
 
12560
 
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx,
12561
 
                                uint32_t *used_key_parts)
 
5680
static int test_if_order_by_key(order_st *order, Table *table, uint32_t idx, uint32_t *used_key_parts)
12562
5681
{
12563
5682
  KEY_PART_INFO *key_part,*key_part_end;
12564
5683
  key_part=table->key_info[idx].key_part;
12627
5746
  return(reverse);
12628
5747
}
12629
5748
 
12630
 
 
12631
5749
/**
12632
5750
  Test if a second key is the subkey of the first one.
12633
5751
 
12643
5761
  @retval
12644
5762
    0   no sub key
12645
5763
*/
12646
 
 
12647
 
inline bool
12648
 
is_subkey(KEY_PART_INFO *key_part, KEY_PART_INFO *ref_key_part,
12649
 
          KEY_PART_INFO *ref_key_part_end)
 
5764
inline bool is_subkey(KEY_PART_INFO *key_part,
 
5765
                      KEY_PART_INFO *ref_key_part,
 
5766
                            KEY_PART_INFO *ref_key_part_end)
12650
5767
{
12651
5768
  for (; ref_key_part < ref_key_part_end; key_part++, ref_key_part++)
12652
5769
    if (!key_part->field->eq(ref_key_part->field))
12665
5782
    - MAX_KEY                   If we can't use other key
12666
5783
    - the number of found key   Otherwise
12667
5784
*/
12668
 
 
12669
 
static uint
12670
 
test_if_subkey(order_st *order, Table *table, uint32_t ref, uint32_t ref_key_parts,
12671
 
               const key_map *usable_keys)
 
5785
static uint32_t test_if_subkey(order_st *order,
 
5786
                               Table *table,
 
5787
                               uint32_t ref,
 
5788
                               uint32_t ref_key_parts,
 
5789
                                     const key_map *usable_keys)
12672
5790
{
12673
5791
  uint32_t nr;
12674
5792
  uint32_t min_length= UINT32_MAX;
12693
5811
  return best;
12694
5812
}
12695
5813
 
12696
 
 
12697
5814
/**
12698
5815
  Check if GROUP BY/DISTINCT can be optimized away because the set is
12699
5816
  already known to be distinct.
12725
5842
  @retval
12726
5843
    0                    not found.
12727
5844
*/
12728
 
 
12729
 
static bool
12730
 
list_contains_unique_index(Table *table,
12731
 
                          bool (*find_func) (Field *, void *), void *data)
 
5845
bool list_contains_unique_index(Table *table, bool (*find_func) (Field *, void *), void *data)
12732
5846
{
12733
5847
  for (uint32_t keynr= 0; keynr < table->s->keys; keynr++)
12734
5848
  {
12754
5868
  return 0;
12755
5869
}
12756
5870
 
12757
 
 
12758
5871
/**
12759
5872
  Helper function for list_contains_unique_index.
12760
5873
  Find a field reference in a list of order_st structures.
12768
5881
  @retval
12769
5882
    0                    not found.
12770
5883
*/
12771
 
 
12772
 
static bool
12773
 
find_field_in_order_list (Field *field, void *data)
 
5884
bool find_field_in_order_list (Field *field, void *data)
12774
5885
{
12775
5886
  order_st *group= (order_st *) data;
12776
5887
  bool part_found= 0;
12787
5898
  return part_found;
12788
5899
}
12789
5900
 
12790
 
 
12791
5901
/**
12792
5902
  Helper function for list_contains_unique_index.
12793
5903
  Find a field reference in a dynamic list of Items.
12801
5911
  @retval
12802
5912
    0                    not found.
12803
5913
*/
12804
 
 
12805
 
static bool
12806
 
find_field_in_item_list (Field *field, void *data)
 
5914
bool find_field_in_item_list (Field *field, void *data)
12807
5915
{
12808
5916
  List<Item> *fields= (List<Item> *) data;
12809
5917
  bool part_found= 0;
12822
5930
  return part_found;
12823
5931
}
12824
5932
 
12825
 
 
12826
5933
/**
12827
5934
  Test if we can skip the order_st BY by using an index.
12828
5935
 
12848
5955
  @retval
12849
5956
    1    We can use an index.
12850
5957
*/
12851
 
 
12852
 
static bool
12853
 
test_if_skip_sort_order(JOIN_TAB *tab,order_st *order,ha_rows select_limit,
12854
 
                        bool no_changes, const key_map *map)
 
5958
bool test_if_skip_sort_order(JOIN_TAB *tab, order_st *order, ha_rows select_limit, bool no_changes, const key_map *map)
12855
5959
{
12856
5960
  int32_t ref_key;
12857
5961
  uint32_t ref_key_parts;
12916
6020
    if (! usable_keys.test(ref_key))
12917
6021
    {
12918
6022
      /*
12919
 
        We come here when ref_key is not among usable_keys
 
6023
        We come here when ref_key is not among usable_keys
12920
6024
      */
12921
6025
      uint32_t new_ref_key;
12922
6026
      /*
12923
 
        If using index only read, only consider other possible index only
12924
 
        keys
 
6027
        If using index only read, only consider other possible index only
 
6028
        keys
12925
6029
      */
12926
6030
      if (table->covering_keys.test(ref_key))
12927
 
        usable_keys&= table->covering_keys;
 
6031
        usable_keys&= table->covering_keys;
12928
6032
      if (tab->pre_idx_push_select_cond)
12929
6033
        tab->select_cond= tab->select->cond= tab->pre_idx_push_select_cond;
12930
6034
      if ((new_ref_key= test_if_subkey(order, table, ref_key, ref_key_parts,
12931
6035
                                       &usable_keys)) < MAX_KEY)
12932
6036
      {
12933
 
        /* Found key that can be used to retrieve data in sorted order */
12934
 
        if (tab->ref.key >= 0)
12935
 
        {
 
6037
        /* Found key that can be used to retrieve data in sorted order */
 
6038
        if (tab->ref.key >= 0)
 
6039
        {
12936
6040
          /*
12937
6041
            We'll use ref access method on key new_ref_key. In general case
12938
6042
            the index search tuple for new_ref_key will be different (e.g.
12948
6052
          if (create_ref_for_key(tab->join, tab, keyuse,
12949
6053
                                 tab->join->const_table_map))
12950
6054
            return(0);
12951
 
        }
12952
 
        else
12953
 
        {
 
6055
        }
 
6056
        else
 
6057
        {
12954
6058
          /*
12955
6059
            The range optimizer constructed QUICK_RANGE for ref_key, and
12956
6060
            we want to use instead new_ref_key as the index. We can't
12971
6075
                                        true) <=
12972
6076
              0)
12973
6077
            return(0);
12974
 
        }
 
6078
        }
12975
6079
        ref_key= new_ref_key;
12976
6080
      }
12977
6081
    }
13019
6123
      keys|= table->covering_keys;
13020
6124
 
13021
6125
      /*
13022
 
        We are adding here also the index specified in FORCE INDEX clause,
13023
 
        if any.
 
6126
        We are adding here also the index specified in FORCE INDEX clause,
 
6127
        if any.
13024
6128
        This is to allow users to use index in order_st BY.
13025
6129
      */
13026
6130
      if (table->force_index)
13027
 
        keys|= (group ? table->keys_in_use_for_group_by :
13028
 
                           table->keys_in_use_for_order_by);
 
6131
        keys|= (group ? table->keys_in_use_for_group_by :
 
6132
                                table->keys_in_use_for_order_by);
13029
6133
      keys&= usable_keys;
13030
6134
    }
13031
6135
    else
13051
6155
          rationally. It's easy to demonstrate that using
13052
6156
          temporary table + filesort could be cheaper for grouping
13053
6157
          queries too.
13054
 
        */
 
6158
        */
13055
6159
        if (is_covering ||
13056
6160
            select_limit != HA_POS_ERROR ||
13057
6161
            (ref_key < 0 && (group || table->force_index)))
13069
6173
              With a grouping query each group containing on average
13070
6174
              rec_per_key records produces only one row that will
13071
6175
              be included into the result set.
13072
 
            */
 
6176
            */
13073
6177
            if (select_limit > table_records/rec_per_key)
13074
6178
                select_limit= table_records;
13075
6179
            else
13084
6188
            So the estimate for L/fanout(tk,tn) will be too optimistic
13085
6189
            and as result we'll choose an index scan when using ref/range
13086
6190
            access + filesort will be cheaper.
13087
 
          */
 
6191
          */
13088
6192
          select_limit= (ha_rows) (select_limit < fanout ?
13089
6193
                                   1 : select_limit/fanout);
13090
6194
          /*
13115
6219
            TODO. Use the formula for a disk sweep sequential access
13116
6220
            to calculate the cost of accessing data rows for one
13117
6221
            index entry.
13118
 
          */
 
6222
          */
13119
6223
          index_scan_time= select_limit/rec_per_key *
13120
6224
                           cmin(rec_per_key, table->file->scan_time());
13121
6225
          if (is_covering || (ref_key < 0 && (group || table->force_index)) ||
13138
6242
              best_key_direction= direction;
13139
6243
            }
13140
6244
          }
13141
 
        }
 
6245
        }
13142
6246
      }
13143
6247
    }
13144
6248
    if (best_key >= 0)
13159
6263
      if (!no_changes)
13160
6264
      {
13161
6265
        if (!quick_created)
13162
 
        {
 
6266
        {
13163
6267
          tab->index= best_key;
13164
6268
          tab->read_first_record= best_key_direction > 0 ?
13165
6269
                                  join_read_first:join_read_last;
13214
6318
    if (select && select->quick)
13215
6319
    {
13216
6320
      /*
13217
 
        Don't reverse the sort order, if it's already done.
 
6321
        Don't reverse the sort order, if it's already done.
13218
6322
        (In some cases test_if_order_by_key() can be called multiple times
13219
6323
      */
13220
6324
      if (!select->quick->reverse_sorted())
13233
6337
        }
13234
6338
 
13235
6339
        /* order_st BY range_key DESC */
13236
 
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
13237
 
                                    used_key_parts, &error);
13238
 
        if (!tmp || error)
13239
 
        {
13240
 
          delete tmp;
13241
 
          select->quick= save_quick;
13242
 
          tab->limit= 0;
13243
 
          return(0);            // Reverse sort not supported
13244
 
        }
13245
 
        select->quick=tmp;
 
6340
        tmp= new QUICK_SELECT_DESC((QUICK_RANGE_SELECT*)(select->quick),
 
6341
                                          used_key_parts, &error);
 
6342
        if (!tmp || error)
 
6343
        {
 
6344
          delete tmp;
 
6345
                select->quick= save_quick;
 
6346
                tab->limit= 0;
 
6347
          return(0);            // Reverse sort not supported
 
6348
        }
 
6349
        select->quick=tmp;
13246
6350
      }
13247
6351
    }
13248
6352
    else if (tab->type != JT_NEXT &&
13249
6353
             tab->ref.key >= 0 && tab->ref.key_parts <= used_key_parts)
13250
6354
    {
13251
6355
      /*
13252
 
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
 
6356
        SELECT * FROM t1 WHERE a=1 order_st BY a DESC,b DESC
13253
6357
 
13254
 
        Use a traversal function that starts by reading the last row
13255
 
        with key part (A) and then traverse the index backwards.
 
6358
        Use a traversal function that starts by reading the last row
 
6359
        with key part (A) and then traverse the index backwards.
13256
6360
      */
13257
6361
      tab->read_first_record= join_read_last_key;
13258
6362
      tab->read_record.read_record= join_read_prev_same;
13263
6367
  return(1);
13264
6368
}
13265
6369
 
13266
 
 
13267
6370
/*
13268
6371
  If not selecting by given key, create an index how records should be read
13269
6372
 
13292
6395
    -1          Some fatal error
13293
6396
    1           No records
13294
6397
*/
13295
 
 
13296
 
static int
13297
 
create_sort_index(Session *session, JOIN *join, order_st *order,
13298
 
                  ha_rows filesort_limit, ha_rows select_limit,
13299
 
                  bool is_order_by)
 
6398
int create_sort_index(Session *session, JOIN *join, order_st *order, ha_rows filesort_limit, ha_rows select_limit, bool is_order_by)
13300
6399
{
13301
6400
  uint32_t length= 0;
13302
6401
  ha_rows examined_rows;
13346
6445
      */
13347
6446
      if (table->key_read && ((uint32_t) tab->ref.key != select->quick->index))
13348
6447
      {
13349
 
        table->key_read=0;
13350
 
        table->file->extra(HA_EXTRA_NO_KEYREAD);
 
6448
        table->key_read=0;
 
6449
        table->file->extra(HA_EXTRA_NO_KEYREAD);
13351
6450
      }
13352
6451
    }
13353
6452
    else
13354
6453
    {
13355
6454
      /*
13356
 
        We have a ref on a const;  Change this to a range that filesort
13357
 
        can use.
13358
 
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
13359
 
        field, quick will contain an empty record set.
 
6455
        We have a ref on a const;  Change this to a range that filesort
 
6456
        can use.
 
6457
        For impossible ranges (like when doing a lookup on NULL on a NOT NULL
 
6458
        field, quick will contain an empty record set.
13360
6459
      */
13361
6460
      if (!(select->quick= (get_quick_select_for_ref(session, table, &tab->ref,
13362
6461
                                                     tab->found_records))))
13363
 
        goto err;
 
6462
        goto err;
13364
6463
    }
13365
6464
  }
13366
6465
 
13396
6495
  return(-1);
13397
6496
}
13398
6497
 
13399
 
static bool copy_blobs(Field **ptr)
13400
 
{
13401
 
  for (; *ptr ; ptr++)
13402
 
  {
13403
 
    if ((*ptr)->flags & BLOB_FLAG)
13404
 
      if (((Field_blob *) (*ptr))->copy())
13405
 
        return 1;                               // Error
13406
 
  }
13407
 
  return 0;
13408
 
}
13409
 
 
13410
 
static void free_blobs(Field **ptr)
13411
 
{
13412
 
  for (; *ptr ; ptr++)
13413
 
  {
13414
 
    if ((*ptr)->flags & BLOB_FLAG)
13415
 
      ((Field_blob *) (*ptr))->free();
13416
 
  }
13417
 
}
13418
 
 
13419
 
 
13420
 
static int
13421
 
remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
13422
 
{
13423
 
  int error;
13424
 
  uint32_t reclength,offset;
13425
 
  uint32_t field_count;
13426
 
  Session *session= join->session;
13427
 
 
13428
 
  entry->reginfo.lock_type=TL_WRITE;
13429
 
 
13430
 
  /* Calculate how many saved fields there is in list */
13431
 
  field_count=0;
13432
 
  List_iterator<Item> it(fields);
13433
 
  Item *item;
13434
 
  while ((item=it++))
13435
 
  {
13436
 
    if (item->get_tmp_table_field() && ! item->const_item())
13437
 
      field_count++;
13438
 
  }
13439
 
 
13440
 
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
13441
 
  {                    // only const items with no OPTION_FOUND_ROWS
13442
 
    join->unit->select_limit_cnt= 1;            // Only send first row
13443
 
    return(0);
13444
 
  }
13445
 
  Field **first_field=entry->field+entry->s->fields - field_count;
13446
 
  offset= (field_count ?
13447
 
           entry->field[entry->s->fields - field_count]->
13448
 
           offset(entry->record[0]) : 0);
13449
 
  reclength= entry->s->reclength-offset;
13450
 
 
13451
 
  free_io_cache(entry);                         // Safety
13452
 
  entry->file->info(HA_STATUS_VARIABLE);
13453
 
  if (entry->s->db_type() == heap_engine ||
13454
 
      (!entry->s->blob_fields &&
13455
 
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
13456
 
        session->variables.sortbuff_size)))
13457
 
    error= remove_dup_with_hash_index(join->session, entry,
13458
 
                                     field_count, first_field,
13459
 
                                     reclength, having);
13460
 
  else
13461
 
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
13462
 
                                  having);
13463
 
 
13464
 
  free_blobs(first_field);
13465
 
  return(error);
13466
 
}
13467
 
 
13468
 
 
13469
 
static int remove_dup_with_compare(Session *session, Table *table, Field **first_field,
13470
 
                                   uint32_t offset, Item *having)
 
6498
int remove_dup_with_compare(Session *session, Table *table, Field **first_field, uint32_t offset, Item *having)
13471
6499
{
13472
6500
  handler *file=table->file;
13473
6501
  char *org_record,*new_record;
13491
6519
    if (error)
13492
6520
    {
13493
6521
      if (error == HA_ERR_RECORD_DELETED)
13494
 
        continue;
 
6522
        continue;
13495
6523
      if (error == HA_ERR_END_OF_FILE)
13496
 
        break;
 
6524
        break;
13497
6525
      goto err;
13498
6526
    }
13499
6527
    if (having && !having->val_int())
13500
6528
    {
13501
6529
      if ((error=file->ha_delete_row(record)))
13502
 
        goto err;
 
6530
        goto err;
13503
6531
      error=file->rnd_next(record);
13504
6532
      continue;
13505
6533
    }
13517
6545
    {
13518
6546
      if ((error=file->rnd_next(record)))
13519
6547
      {
13520
 
        if (error == HA_ERR_RECORD_DELETED)
13521
 
          continue;
13522
 
        if (error == HA_ERR_END_OF_FILE)
13523
 
          break;
13524
 
        goto err;
 
6548
        if (error == HA_ERR_RECORD_DELETED)
 
6549
          continue;
 
6550
        if (error == HA_ERR_END_OF_FILE)
 
6551
          break;
 
6552
        goto err;
13525
6553
      }
13526
6554
      if (table->compare_record(first_field) == 0)
13527
6555
      {
13528
 
        if ((error=file->ha_delete_row(record)))
13529
 
          goto err;
 
6556
        if ((error=file->ha_delete_row(record)))
 
6557
          goto err;
13530
6558
      }
13531
6559
      else if (!found)
13532
6560
      {
13533
 
        found=1;
13534
 
        file->position(record); // Remember position
 
6561
        found= 1;
 
6562
        file->position(record); // Remember position
13535
6563
      }
13536
6564
    }
13537
6565
    if (!found)
13549
6577
  return(1);
13550
6578
}
13551
6579
 
13552
 
 
13553
6580
/**
13554
6581
  Generate a hash index for each row to quickly find duplicate rows.
13555
6582
 
13556
6583
  @note
13557
6584
    Note that this will not work on tables with blobs!
13558
6585
*/
13559
 
 
13560
 
static int remove_dup_with_hash_index(Session *session, Table *table,
13561
 
                                      uint32_t field_count,
13562
 
                                      Field **first_field,
13563
 
                                      uint32_t key_length,
13564
 
                                      Item *having)
 
6586
int remove_dup_with_hash_index(Session *session, 
 
6587
                               Table *table,
 
6588
                               uint32_t field_count,
 
6589
                               Field **first_field,
 
6590
                               uint32_t key_length,
 
6591
                               Item *having)
13565
6592
{
13566
6593
  unsigned char *key_buffer, *key_pos, *record=table->record[0];
13567
6594
  int error;
13614
6641
    if ((error=file->rnd_next(record)))
13615
6642
    {
13616
6643
      if (error == HA_ERR_RECORD_DELETED)
13617
 
        continue;
 
6644
        continue;
13618
6645
      if (error == HA_ERR_END_OF_FILE)
13619
 
        break;
 
6646
        break;
13620
6647
      goto err;
13621
6648
    }
13622
6649
    if (having && !having->val_int())
13623
6650
    {
13624
6651
      if ((error=file->ha_delete_row(record)))
13625
 
        goto err;
 
6652
        goto err;
13626
6653
      continue;
13627
6654
    }
13628
6655
 
13639
6666
    {
13640
6667
      /* Duplicated found ; Remove the row */
13641
6668
      if ((error=file->ha_delete_row(record)))
13642
 
        goto err;
 
6669
        goto err;
13643
6670
    }
13644
6671
    else
13645
6672
      (void) my_hash_insert(&hash, org_key_pos);
13661
6688
  return(1);
13662
6689
}
13663
6690
 
13664
 
 
13665
 
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length,
13666
 
                                  SORT_FIELD *sortorder)
 
6691
SORT_FIELD *make_unireg_sortorder(order_st *order, uint32_t *length, SORT_FIELD *sortorder)
13667
6692
{
13668
6693
  uint32_t count;
13669
6694
  SORT_FIELD *sort,*pos;
13699
6724
  return(sort);
13700
6725
}
13701
6726
 
13702
 
 
13703
 
/*****************************************************************************
13704
 
  Fill join cache with packed records
13705
 
  Records are stored in tab->cache.buffer and last record in
13706
 
  last record is stored with pointers to blobs to support very big
13707
 
  records
13708
 
******************************************************************************/
13709
 
 
13710
 
static int
13711
 
join_init_cache(Session *session,JOIN_TAB *tables,uint32_t table_count)
13712
 
{
13713
 
  register unsigned int i;
13714
 
  unsigned int length, blobs;
13715
 
  size_t size;
13716
 
  CACHE_FIELD *copy,**blob_ptr;
13717
 
  JOIN_CACHE  *cache;
13718
 
  JOIN_TAB *join_tab;
13719
 
 
13720
 
  cache= &tables[table_count].cache;
13721
 
  cache->fields=blobs=0;
13722
 
 
13723
 
  join_tab=tables;
13724
 
  for (i=0 ; i < table_count ; i++,join_tab++)
13725
 
  {
13726
 
    if (!join_tab->used_fieldlength)            /* Not calced yet */
13727
 
      calc_used_field_length(session, join_tab);
13728
 
    cache->fields+=join_tab->used_fields;
13729
 
    blobs+=join_tab->used_blobs;
13730
 
 
13731
 
    /* SemiJoinDuplicateElimination: reserve space for rowid */
13732
 
    if (join_tab->rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
13733
 
    {
13734
 
      cache->fields++;
13735
 
      join_tab->used_fieldlength += join_tab->table->file->ref_length;
13736
 
    }
13737
 
  }
13738
 
  if (!(cache->field=(CACHE_FIELD*)
13739
 
        sql_alloc(sizeof(CACHE_FIELD)*(cache->fields+table_count*2)+(blobs+1)*
13740
 
 
13741
 
                  sizeof(CACHE_FIELD*))))
13742
 
  {
13743
 
    free((unsigned char*) cache->buff);         /* purecov: inspected */
13744
 
    cache->buff=0;                              /* purecov: inspected */
13745
 
    return(1);                          /* purecov: inspected */
13746
 
  }
13747
 
  copy=cache->field;
13748
 
  blob_ptr=cache->blob_ptr=(CACHE_FIELD**)
13749
 
    (cache->field+cache->fields+table_count*2);
13750
 
 
13751
 
  length=0;
13752
 
  for (i=0 ; i < table_count ; i++)
13753
 
  {
13754
 
    uint32_t null_fields=0, used_fields;
13755
 
    Field **f_ptr,*field;
13756
 
    for (f_ptr=tables[i].table->field,used_fields=tables[i].used_fields ;
13757
 
         used_fields ;
13758
 
         f_ptr++)
13759
 
    {
13760
 
      field= *f_ptr;
13761
 
      if (field->isReadSet())
13762
 
      {
13763
 
        used_fields--;
13764
 
        length+=field->fill_cache_field(copy);
13765
 
        if (copy->blob_field)
13766
 
          (*blob_ptr++)=copy;
13767
 
        if (field->maybe_null())
13768
 
          null_fields++;
13769
 
        copy->get_rowid= NULL;
13770
 
        copy++;
13771
 
      }
13772
 
    }
13773
 
    /* Copy null bits from table */
13774
 
    if (null_fields && tables[i].table->getNullFields())
13775
 
    {                                           /* must copy null bits */
13776
 
      copy->str= tables[i].table->null_flags;
13777
 
      copy->length= tables[i].table->s->null_bytes;
13778
 
      copy->strip=0;
13779
 
      copy->blob_field=0;
13780
 
      copy->get_rowid= NULL;
13781
 
      length+=copy->length;
13782
 
      copy++;
13783
 
      cache->fields++;
13784
 
    }
13785
 
    /* If outer join table, copy null_row flag */
13786
 
    if (tables[i].table->maybe_null)
13787
 
    {
13788
 
      copy->str= (unsigned char*) &tables[i].table->null_row;
13789
 
      copy->length=sizeof(tables[i].table->null_row);
13790
 
      copy->strip=0;
13791
 
      copy->blob_field=0;
13792
 
      copy->get_rowid= NULL;
13793
 
      length+=copy->length;
13794
 
      copy++;
13795
 
      cache->fields++;
13796
 
    }
13797
 
    /* SemiJoinDuplicateElimination: Allocate space for rowid if needed */
13798
 
    if (tables[i].rowid_keep_flags & JOIN_TAB::KEEP_ROWID)
13799
 
    {
13800
 
      copy->str= tables[i].table->file->ref;
13801
 
      copy->length= tables[i].table->file->ref_length;
13802
 
      copy->strip=0;
13803
 
      copy->blob_field=0;
13804
 
      copy->get_rowid= NULL;
13805
 
      if (tables[i].rowid_keep_flags & JOIN_TAB::CALL_POSITION)
13806
 
      {
13807
 
        /* We will need to call h->position(): */
13808
 
        copy->get_rowid= tables[i].table;
13809
 
        /* And those after us won't have to: */
13810
 
        tables[i].rowid_keep_flags &=  ~((int)JOIN_TAB::CALL_POSITION);
13811
 
      }
13812
 
      copy++;
13813
 
    }
13814
 
  }
13815
 
 
13816
 
  cache->length=length+blobs*sizeof(char*);
13817
 
  cache->blobs=blobs;
13818
 
  *blob_ptr= NULL;                                      /* End sequentel */
13819
 
  size= max((size_t)session->variables.join_buff_size,
13820
 
            (size_t)cache->length);
13821
 
  if (!(cache->buff=(unsigned char*) malloc(size)))
13822
 
    return 1;                           /* Don't use cache */ /* purecov: inspected */
13823
 
  cache->end=cache->buff+size;
13824
 
  reset_cache_write(cache);
13825
 
  return 0;
13826
 
}
13827
 
 
13828
 
 
13829
 
static uint32_t used_blob_length(CACHE_FIELD **ptr)
13830
 
{
13831
 
  uint32_t length,blob_length;
13832
 
  for (length=0 ; *ptr ; ptr++)
13833
 
  {
13834
 
    (*ptr)->blob_length=blob_length=(*ptr)->blob_field->get_length();
13835
 
    length+=blob_length;
13836
 
    (*ptr)->blob_field->get_ptr(&(*ptr)->str);
13837
 
  }
13838
 
  return length;
13839
 
}
13840
 
 
13841
 
 
13842
 
static bool
13843
 
store_record_in_cache(JOIN_CACHE *cache)
13844
 
{
13845
 
  uint32_t length;
13846
 
  unsigned char *pos;
13847
 
  CACHE_FIELD *copy,*end_field;
13848
 
  bool last_record;
13849
 
 
13850
 
  pos=cache->pos;
13851
 
  end_field=cache->field+cache->fields;
13852
 
 
13853
 
  length=cache->length;
13854
 
  if (cache->blobs)
13855
 
    length+= used_blob_length(cache->blob_ptr);
13856
 
  if ((last_record= (length + cache->length > (size_t) (cache->end - pos))))
13857
 
    cache->ptr_record=cache->records;
13858
 
  /*
13859
 
    There is room in cache. Put record there
13860
 
  */
13861
 
  cache->records++;
13862
 
  for (copy=cache->field ; copy < end_field; copy++)
13863
 
  {
13864
 
    if (copy->blob_field)
13865
 
    {
13866
 
      if (last_record)
13867
 
      {
13868
 
        copy->blob_field->get_image(pos, copy->length+sizeof(char*),
13869
 
                                    copy->blob_field->charset());
13870
 
        pos+=copy->length+sizeof(char*);
13871
 
      }
13872
 
      else
13873
 
      {
13874
 
        copy->blob_field->get_image(pos, copy->length, // blob length
13875
 
                                    copy->blob_field->charset());
13876
 
        memcpy(pos+copy->length,copy->str,copy->blob_length);  // Blob data
13877
 
        pos+=copy->length+copy->blob_length;
13878
 
      }
13879
 
    }
13880
 
    else
13881
 
    {
13882
 
      // SemiJoinDuplicateElimination: Get the rowid into table->ref:
13883
 
      if (copy->get_rowid)
13884
 
        copy->get_rowid->file->position(copy->get_rowid->record[0]);
13885
 
 
13886
 
      if (copy->strip)
13887
 
      {
13888
 
        unsigned char *str,*end;
13889
 
        for (str=copy->str,end= str+copy->length;
13890
 
             end > str && end[-1] == ' ' ;
13891
 
             end--) ;
13892
 
        length=(uint32_t) (end-str);
13893
 
        memcpy(pos+2, str, length);
13894
 
        int2store(pos, length);
13895
 
        pos+= length+2;
13896
 
      }
13897
 
      else
13898
 
      {
13899
 
        memcpy(pos,copy->str,copy->length);
13900
 
        pos+=copy->length;
13901
 
      }
13902
 
    }
13903
 
  }
13904
 
  cache->pos=pos;
13905
 
  return last_record || (size_t) (cache->end - pos) < cache->length;
13906
 
}
13907
 
 
13908
 
 
13909
 
static void
13910
 
reset_cache_read(JOIN_CACHE *cache)
13911
 
{
13912
 
  cache->record_nr=0;
13913
 
  cache->pos=cache->buff;
13914
 
}
13915
 
 
13916
 
 
13917
 
static void reset_cache_write(JOIN_CACHE *cache)
13918
 
{
13919
 
  reset_cache_read(cache);
13920
 
  cache->records= 0;
13921
 
  cache->ptr_record= UINT32_MAX;
13922
 
}
13923
 
 
13924
 
 
13925
 
static void
13926
 
read_cached_record(JOIN_TAB *tab)
13927
 
{
13928
 
  unsigned char *pos;
13929
 
  uint32_t length;
13930
 
  bool last_record;
13931
 
  CACHE_FIELD *copy,*end_field;
13932
 
 
13933
 
  last_record=tab->cache.record_nr++ == tab->cache.ptr_record;
13934
 
  pos=tab->cache.pos;
13935
 
  for (copy=tab->cache.field,end_field=copy+tab->cache.fields ;
 
6727
void read_cached_record(JOIN_TAB *tab)
 
6728
{
 
6729
  unsigned char *pos;
 
6730
  uint32_t length;
 
6731
  bool last_record;
 
6732
  CACHE_FIELD *copy,*end_field;
 
6733
 
 
6734
  last_record= tab->cache.record_nr++ == tab->cache.ptr_record;
 
6735
  pos= tab->cache.pos;
 
6736
  for (copy= tab->cache.field, end_field= copy+tab->cache.fields;
13936
6737
       copy < end_field;
13937
6738
       copy++)
13938
6739
  {
13940
6741
    {
13941
6742
      if (last_record)
13942
6743
      {
13943
 
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
13944
 
                                    copy->blob_field->charset());
13945
 
        pos+=copy->length+sizeof(char*);
 
6744
        copy->blob_field->set_image(pos, copy->length+sizeof(char*),
 
6745
                  copy->blob_field->charset());
 
6746
        pos+=copy->length+sizeof(char*);
13946
6747
      }
13947
6748
      else
13948
6749
      {
13949
 
        copy->blob_field->set_ptr(pos, pos+copy->length);
13950
 
        pos+=copy->length+copy->blob_field->get_length();
 
6750
        copy->blob_field->set_ptr(pos, pos+copy->length);
 
6751
        pos+=copy->length+copy->blob_field->get_length();
13951
6752
      }
13952
6753
    }
13953
6754
    else
13955
6756
      if (copy->strip)
13956
6757
      {
13957
6758
        length= uint2korr(pos);
13958
 
        memcpy(copy->str, pos+2, length);
13959
 
        memset(copy->str+length, ' ', copy->length-length);
13960
 
        pos+= 2 + length;
 
6759
        memcpy(copy->str, pos+2, length);
 
6760
        memset(copy->str+length, ' ', copy->length-length);
 
6761
        pos+= 2 + length;
13961
6762
      }
13962
6763
      else
13963
6764
      {
13964
 
        memcpy(copy->str,pos,copy->length);
13965
 
        pos+=copy->length;
 
6765
        memcpy(copy->str,pos,copy->length);
 
6766
        pos+=copy->length;
13966
6767
      }
13967
6768
    }
13968
6769
  }
13970
6771
  return;
13971
6772
}
13972
6773
 
13973
 
 
13974
6774
/*
13975
6775
  eq_ref: Create the lookup key and check if it is the same as saved key
13976
6776
 
13989
6789
    false  The created key is the same as the previous one (and the record
13990
6790
           is already in table->record)
13991
6791
*/
13992
 
 
13993
 
static bool
13994
 
cmp_buffer_with_ref(JOIN_TAB *tab)
 
6792
static bool cmp_buffer_with_ref(JOIN_TAB *tab)
13995
6793
{
13996
6794
  bool no_prev_key;
13997
6795
  if (!tab->ref.disable_cache)
14011
6809
    != 0;
14012
6810
}
14013
6811
 
14014
 
 
14015
 
bool
14016
 
cp_buffer_from_ref(Session *session, TABLE_REF *ref)
 
6812
bool cp_buffer_from_ref(Session *session, TABLE_REF *ref)
14017
6813
{
14018
6814
  enum enum_check_fields save_count_cuted_fields= session->count_cuted_fields;
14019
6815
  session->count_cuted_fields= CHECK_FIELD_IGNORE;
14020
6816
  bool result= 0;
14021
6817
 
14022
 
  for (store_key **copy=ref->key_copy ; *copy ; copy++)
 
6818
  for (StoredKey **copy=ref->key_copy ; *copy ; copy++)
14023
6819
  {
14024
6820
    if ((*copy)->copy() & 1)
14025
6821
    {
14031
6827
  return result;
14032
6828
}
14033
6829
 
14034
 
 
14035
6830
/*****************************************************************************
14036
6831
  Group and order functions
14037
6832
*****************************************************************************/
14068
6863
  @retval
14069
6864
    true  if error occurred
14070
6865
*/
14071
 
 
14072
 
static bool
14073
 
find_order_in_list(Session *session, Item **ref_pointer_array, TableList *tables,
14074
 
                   order_st *order, List<Item> &fields, List<Item> &all_fields,
14075
 
                   bool is_group_field)
 
6866
static bool find_order_in_list(Session *session, 
 
6867
                               Item **ref_pointer_array, 
 
6868
                               TableList *tables,
 
6869
                               order_st *order,
 
6870
                               List<Item> &fields,
 
6871
                               List<Item> &all_fields,
 
6872
                               bool is_group_field)
14076
6873
{
14077
6874
  Item *order_item= *order->item; /* The item from the GROUP/order_st caluse. */
14078
6875
  Item::Type order_item_type;
14198
6995
  return false;
14199
6996
}
14200
6997
 
14201
 
 
14202
6998
/**
14203
6999
  Change order to point at item in select list.
14204
7000
 
14205
7001
  If item isn't a number and doesn't exits in the select list, add it the
14206
7002
  the field list.
14207
7003
*/
14208
 
 
14209
 
int setup_order(Session *session, Item **ref_pointer_array, TableList *tables,
14210
 
                List<Item> &fields, List<Item> &all_fields, order_st *order)
 
7004
int setup_order(Session *session,
 
7005
                Item **ref_pointer_array,
 
7006
                TableList *tables,
 
7007
                            List<Item> &fields,
 
7008
                List<Item> &all_fields,
 
7009
                order_st *order)
14211
7010
{
14212
7011
  session->where="order clause";
14213
7012
  for (; order; order=order->next)
14219
7018
  return 0;
14220
7019
}
14221
7020
 
14222
 
 
14223
7021
/**
14224
7022
  Intitialize the GROUP BY list.
14225
7023
 
14245
7043
  @retval
14246
7044
    1  error (probably out of memory)
14247
7045
*/
14248
 
 
14249
 
int
14250
 
setup_group(Session *session, Item **ref_pointer_array, TableList *tables,
14251
 
            List<Item> &fields, List<Item> &all_fields, order_st *order,
14252
 
            bool *hidden_group_fields)
 
7046
int setup_group(Session *session,
 
7047
                Item **ref_pointer_array,
 
7048
                TableList *tables,
 
7049
                      List<Item> &fields,
 
7050
                List<Item> &all_fields,
 
7051
                order_st *order,
 
7052
                      bool *hidden_group_fields)
14253
7053
{
14254
7054
  *hidden_group_fields=0;
14255
7055
  order_st *ord;
14342
7142
  Try to use the fields in the order given by 'order' to allow one to
14343
7143
  optimize away 'order by'.
14344
7144
*/
14345
 
 
14346
 
static order_st *
14347
 
create_distinct_group(Session *session, Item **ref_pointer_array,
14348
 
                      order_st *order_list, List<Item> &fields,
14349
 
                      List<Item> &, bool *all_order_by_fields_used)
 
7145
order_st *create_distinct_group(Session *session,
 
7146
                                Item **ref_pointer_array,
 
7147
                                order_st *order_list,
 
7148
                                List<Item> &fields,
 
7149
                                List<Item> &,
 
7150
                                bool *all_order_by_fields_used)
14350
7151
{
14351
7152
  List_iterator<Item> li(fields);
14352
7153
  Item *item;
14363
7164
    {
14364
7165
      order_st *ord=(order_st*) session->memdup((char*) order,sizeof(order_st));
14365
7166
      if (!ord)
14366
 
        return 0;
 
7167
        return 0;
14367
7168
      *prev=ord;
14368
7169
      prev= &ord->next;
14369
7170
      (*ord->item)->marker=1;
14388
7189
 
14389
7190
      order_st *ord=(order_st*) session->calloc(sizeof(order_st));
14390
7191
      if (!ord)
14391
 
        return 0;
 
7192
        return 0;
14392
7193
 
14393
7194
      /*
14394
7195
        We have here only field_list (not all_field_list), so we can use
14407
7208
  return group;
14408
7209
}
14409
7210
 
14410
 
 
14411
7211
/**
14412
7212
  Update join with count of the different type of fields.
14413
7213
*/
14414
 
 
14415
 
void
14416
 
count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param,
14417
 
                  List<Item> &fields, bool reset_with_sum_func)
 
7214
void count_field_types(Select_Lex *select_lex, Tmp_Table_Param *param, List<Item> &fields, bool reset_with_sum_func)
14418
7215
{
14419
7216
  List_iterator<Item> li(fields);
14420
7217
  Item *field;
14431
7228
    {
14432
7229
      if (! field->const_item())
14433
7230
      {
14434
 
        Item_sum *sum_item=(Item_sum*) field->real_item();
 
7231
        Item_sum *sum_item=(Item_sum*) field->real_item();
14435
7232
        if (!sum_item->depended_from() ||
14436
7233
            sum_item->depended_from() == select_lex)
14437
7234
        {
14454
7251
    {
14455
7252
      param->func_count++;
14456
7253
      if (reset_with_sum_func)
14457
 
        field->with_sum_func=0;
14458
 
    }
14459
 
  }
14460
 
}
14461
 
 
14462
 
 
14463
 
/**
14464
 
  Return 1 if second is a subpart of first argument.
14465
 
 
14466
 
  If first parts has different direction, change it to second part
14467
 
  (group is sorted like order)
14468
 
*/
14469
 
 
14470
 
static bool
14471
 
test_if_subpart(order_st *a,order_st *b)
14472
 
{
14473
 
  for (; a && b; a=a->next,b=b->next)
14474
 
  {
14475
 
    if ((*a->item)->eq(*b->item,1))
14476
 
      a->asc=b->asc;
14477
 
    else
14478
 
      return 0;
14479
 
  }
14480
 
  return test(!b);
14481
 
}
14482
 
 
14483
 
/**
14484
 
  Return table number if there is only one table in sort order
14485
 
  and group and order is compatible, else return 0.
14486
 
*/
14487
 
 
14488
 
static Table *
14489
 
get_sort_by_table(order_st *a,order_st *b,TableList *tables)
14490
 
{
14491
 
  table_map map= (table_map) 0;
14492
 
 
14493
 
  if (!a)
14494
 
    a=b;                                        // Only one need to be given
14495
 
  else if (!b)
14496
 
    b=a;
14497
 
 
14498
 
  for (; a && b; a=a->next,b=b->next)
14499
 
  {
14500
 
    if (!(*a->item)->eq(*b->item,1))
14501
 
      return(0);
14502
 
    map|=a->item[0]->used_tables();
14503
 
  }
14504
 
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
14505
 
    return(0);
14506
 
 
14507
 
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
14508
 
  if (map != tables->table->map)
14509
 
    return(0);                          // More than one table
14510
 
  return(tables->table);
14511
 
}
14512
 
 
14513
 
 
14514
 
/**
14515
 
  calc how big buffer we need for comparing group entries.
14516
 
*/
14517
 
 
14518
 
static void
14519
 
calc_group_buffer(JOIN *join,order_st *group)
14520
 
{
14521
 
  uint32_t key_length=0, parts=0, null_parts=0;
14522
 
 
14523
 
  if (group)
14524
 
    join->group= 1;
14525
 
  for (; group ; group=group->next)
14526
 
  {
14527
 
    Item *group_item= *group->item;
14528
 
    Field *field= group_item->get_tmp_table_field();
14529
 
    if (field)
14530
 
    {
14531
 
      enum_field_types type;
14532
 
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
14533
 
        key_length+=MAX_BLOB_WIDTH;             // Can't be used as a key
14534
 
      else if (type == DRIZZLE_TYPE_VARCHAR)
14535
 
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
14536
 
      else
14537
 
        key_length+= field->pack_length();
14538
 
    }
14539
 
    else
14540
 
    {
14541
 
      switch (group_item->result_type()) {
14542
 
      case REAL_RESULT:
14543
 
        key_length+= sizeof(double);
14544
 
        break;
14545
 
      case INT_RESULT:
14546
 
        key_length+= sizeof(int64_t);
14547
 
        break;
14548
 
      case DECIMAL_RESULT:
14549
 
        key_length+= my_decimal_get_binary_size(group_item->max_length -
14550
 
                                                (group_item->decimals ? 1 : 0),
14551
 
                                                group_item->decimals);
14552
 
        break;
14553
 
      case STRING_RESULT:
14554
 
      {
14555
 
        enum enum_field_types type= group_item->field_type();
14556
 
        /*
14557
 
          As items represented as DATE/TIME fields in the group buffer
14558
 
          have STRING_RESULT result type, we increase the length
14559
 
          by 8 as maximum pack length of such fields.
14560
 
        */
14561
 
        if (type == DRIZZLE_TYPE_DATE ||
14562
 
            type == DRIZZLE_TYPE_DATETIME ||
14563
 
            type == DRIZZLE_TYPE_TIMESTAMP)
14564
 
        {
14565
 
          key_length+= 8;
14566
 
        }
14567
 
        else
14568
 
        {
14569
 
          /*
14570
 
            Group strings are taken as varstrings and require an length field.
14571
 
            A field is not yet created by create_tmp_field()
14572
 
            and the sizes should match up.
14573
 
          */
14574
 
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
14575
 
        }
14576
 
        break;
14577
 
      }
14578
 
      default:
14579
 
        /* This case should never be choosen */
14580
 
        assert(0);
14581
 
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
14582
 
      }
14583
 
    }
14584
 
    parts++;
14585
 
    if (group_item->maybe_null)
14586
 
      null_parts++;
14587
 
  }
14588
 
  join->tmp_table_param.group_length=key_length+null_parts;
14589
 
  join->tmp_table_param.group_parts=parts;
14590
 
  join->tmp_table_param.group_null_parts=null_parts;
14591
 
}
14592
 
 
14593
 
 
14594
 
/**
14595
 
  allocate group fields or take prepared (cached).
14596
 
 
14597
 
  @param main_join   join of current select
14598
 
  @param curr_join   current join (join of current select or temporary copy
14599
 
                     of it)
14600
 
 
14601
 
  @retval
14602
 
    0   ok
14603
 
  @retval
14604
 
    1   failed
14605
 
*/
14606
 
 
14607
 
static bool
14608
 
make_group_fields(JOIN *main_join, JOIN *curr_join)
14609
 
{
14610
 
  if (main_join->group_fields_cache.elements)
14611
 
  {
14612
 
    curr_join->group_fields= main_join->group_fields_cache;
14613
 
    curr_join->sort_and_group= 1;
14614
 
  }
14615
 
  else
14616
 
  {
14617
 
    if (alloc_group_fields(curr_join, curr_join->group_list))
14618
 
      return (1);
14619
 
    main_join->group_fields_cache= curr_join->group_fields;
14620
 
  }
14621
 
  return (0);
14622
 
}
14623
 
 
14624
 
 
14625
 
/**
14626
 
  Get a list of buffers for saveing last group.
14627
 
 
14628
 
  Groups are saved in reverse order for easyer check loop.
14629
 
*/
14630
 
 
14631
 
static bool
14632
 
alloc_group_fields(JOIN *join,order_st *group)
14633
 
{
14634
 
  if (group)
14635
 
  {
14636
 
    for (; group ; group=group->next)
14637
 
    {
14638
 
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
14639
 
      if (!tmp || join->group_fields.push_front(tmp))
14640
 
        return true;
14641
 
    }
14642
 
  }
14643
 
  join->sort_and_group=1;                       /* Mark for do_select */
14644
 
  return false;
14645
 
}
14646
 
 
 
7254
        field->with_sum_func=0;
 
7255
    }
 
7256
  }
 
7257
}
14647
7258
 
14648
7259
/*
14649
7260
  Test if a single-row cache of items changed, and update the cache.
14657
7268
  @return -1 if no item changed
14658
7269
  @return index of the first item that changed
14659
7270
*/
14660
 
 
14661
7271
int test_if_item_cache_changed(List<Cached_item> &list)
14662
7272
{
14663
7273
  List_iterator<Cached_item> li(list);
14672
7282
  return(idx);
14673
7283
}
14674
7284
 
14675
 
 
14676
7285
/**
14677
7286
  Setup copy_fields to save fields at start of new group.
14678
7287
 
14701
7310
  @retval
14702
7311
    !=0   error
14703
7312
*/
14704
 
 
14705
 
bool
14706
 
setup_copy_fields(Session *session, Tmp_Table_Param *param,
14707
 
                  Item **ref_pointer_array,
14708
 
                  List<Item> &res_selected_fields, List<Item> &res_all_fields,
14709
 
                  uint32_t elements, List<Item> &all_fields)
 
7313
bool setup_copy_fields(Session *session,
 
7314
                       Tmp_Table_Param *param,
 
7315
                       Item **ref_pointer_array,
 
7316
                       List<Item> &res_selected_fields,
 
7317
                       List<Item> &res_all_fields,
 
7318
                       uint32_t elements,
 
7319
                       List<Item> &all_fields)
14710
7320
{
14711
7321
  Item *pos;
14712
7322
  List_iterator_fast<Item> li(all_fields);
14731
7341
    {
14732
7342
      Item_field *item;
14733
7343
      if (!(item= new Item_field(session, ((Item_field*) real_pos))))
14734
 
        goto err;
 
7344
        goto err;
14735
7345
      if (pos->type() == Item::REF_ITEM)
14736
7346
      {
14737
7347
        /* preserve the names of the ref when dereferncing */
14743
7353
      pos= item;
14744
7354
      if (item->field->flags & BLOB_FLAG)
14745
7355
      {
14746
 
        if (!(pos= new Item_copy_string(pos)))
14747
 
          goto err;
14748
 
       /*
14749
 
         Item_copy_string::copy for function can call
14750
 
         Item_copy_string::val_int for blob via Item_ref.
14751
 
         But if Item_copy_string::copy for blob isn't called before,
14752
 
         it's value will be wrong
14753
 
         so let's insert Item_copy_string for blobs in the beginning of
14754
 
         copy_funcs
14755
 
         (to see full test case look at having.test, BUG #4358)
14756
 
       */
14757
 
        if (param->copy_funcs.push_front(pos))
14758
 
          goto err;
 
7356
        if (!(pos= new Item_copy_string(pos)))
 
7357
          goto err;
 
7358
            /*
 
7359
              Item_copy_string::copy for function can call
 
7360
              Item_copy_string::val_int for blob via Item_ref.
 
7361
              But if Item_copy_string::copy for blob isn't called before,
 
7362
              it's value will be wrong
 
7363
              so let's insert Item_copy_string for blobs in the beginning of
 
7364
              copy_funcs
 
7365
              (to see full test case look at having.test, BUG #4358)
 
7366
            */
 
7367
        if (param->copy_funcs.push_front(pos))
 
7368
          goto err;
14759
7369
      }
14760
7370
      else
14761
7371
      {
14762
 
        /*
14763
 
           set up save buffer and change result_field to point at
14764
 
           saved value
14765
 
        */
14766
 
        field= item->field;
14767
 
        item->result_field=field->new_field(session->mem_root,field->table, 1);
14768
7372
        /*
14769
 
          We need to allocate one extra byte for null handling and
14770
 
          another extra byte to not get warnings from purify in
14771
 
          Field_varstring::val_int
 
7373
          set up save buffer and change result_field to point at
 
7374
          saved value
14772
7375
        */
14773
 
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
14774
 
          goto err;
 
7376
        field= item->field;
 
7377
        item->result_field=field->new_field(session->mem_root,field->table, 1);
 
7378
              /*
 
7379
                We need to allocate one extra byte for null handling and
 
7380
                another extra byte to not get warnings from purify in
 
7381
                Field_varstring::val_int
 
7382
              */
 
7383
        if (!(tmp= (unsigned char*) sql_alloc(field->pack_length()+2)))
 
7384
          goto err;
14775
7385
        if (copy)
14776
7386
        {
14777
7387
          copy->set(tmp, item->result_field);
14791
7401
    {                                           // Save for send fields
14792
7402
      pos= real_pos;
14793
7403
      /* TODO:
14794
 
         In most cases this result will be sent to the user.
14795
 
         This should be changed to use copy_int or copy_real depending
14796
 
         on how the value is to be used: In some cases this may be an
14797
 
         argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
 
7404
        In most cases this result will be sent to the user.
 
7405
        This should be changed to use copy_int or copy_real depending
 
7406
        on how the value is to be used: In some cases this may be an
 
7407
        argument in a group function, like: IF(ISNULL(col),0,COUNT(*))
14798
7408
      */
14799
7409
      if (!(pos=new Item_copy_string(pos)))
14800
 
        goto err;
 
7410
        goto err;
14801
7411
      if (i < border)                           // HAVING, order_st and GROUP BY
14802
7412
      {
14803
7413
        if (extra_funcs.push_back(pos))
14804
7414
          goto err;
14805
7415
      }
14806
7416
      else if (param->copy_funcs.push_back(pos))
14807
 
        goto err;
 
7417
        goto err;
14808
7418
    }
14809
7419
    res_all_fields.push_back(pos);
14810
7420
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
14823
7433
 
14824
7434
  return(0);
14825
7435
 
14826
 
 err:
 
7436
err:
14827
7437
  if (copy)
14828
7438
    delete [] param->copy_field;                        // This is never 0
14829
7439
  param->copy_field=0;
14831
7441
  return(true);
14832
7442
}
14833
7443
 
14834
 
 
14835
7444
/**
14836
7445
  Make a copy of all simple SELECT'ed items.
14837
7446
 
14838
7447
  This is done at the start of a new group so that we can retrieve
14839
7448
  these later when the group changes.
14840
7449
*/
14841
 
 
14842
 
void
14843
 
copy_fields(Tmp_Table_Param *param)
 
7450
void copy_fields(Tmp_Table_Param *param)
14844
7451
{
14845
7452
  Copy_field *ptr=param->copy_field;
14846
7453
  Copy_field *end=param->copy_field_end;
14854
7461
    item->copy();
14855
7462
}
14856
7463
 
14857
 
 
14858
 
/**
14859
 
  Make an array of pointers to sum_functions to speed up
14860
 
  sum_func calculation.
14861
 
 
14862
 
  @retval
14863
 
    0   ok
14864
 
  @retval
14865
 
    1   Error
14866
 
*/
14867
 
 
14868
 
bool JOIN::alloc_func_list()
14869
 
{
14870
 
  uint32_t func_count, group_parts;
14871
 
 
14872
 
  func_count= tmp_table_param.sum_func_count;
14873
 
  /*
14874
 
    If we are using rollup, we need a copy of the summary functions for
14875
 
    each level
14876
 
  */
14877
 
  if (rollup.state != ROLLUP::STATE_NONE)
14878
 
    func_count*= (send_group_parts+1);
14879
 
 
14880
 
  group_parts= send_group_parts;
14881
 
  /*
14882
 
    If distinct, reserve memory for possible
14883
 
    disctinct->group_by optimization
14884
 
  */
14885
 
  if (select_distinct)
14886
 
  {
14887
 
    group_parts+= fields_list.elements;
14888
 
    /*
14889
 
      If the order_st clause is specified then it's possible that
14890
 
      it also will be optimized, so reserve space for it too
14891
 
    */
14892
 
    if (order)
14893
 
    {
14894
 
      order_st *ord;
14895
 
      for (ord= order; ord; ord= ord->next)
14896
 
        group_parts++;
14897
 
    }
14898
 
  }
14899
 
 
14900
 
  /* This must use calloc() as rollup_make_fields depends on this */
14901
 
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
14902
 
                                      sizeof(Item_sum***) * (group_parts+1));
14903
 
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
14904
 
  return(sum_funcs == 0);
14905
 
}
14906
 
 
14907
 
 
14908
 
/**
14909
 
  Initialize 'sum_funcs' array with all Item_sum objects.
14910
 
 
14911
 
  @param field_list        All items
14912
 
  @param send_fields       Items in select list
14913
 
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
14914
 
  @param recompute         Set to true if sum_funcs must be recomputed
14915
 
 
14916
 
  @retval
14917
 
    0  ok
14918
 
  @retval
14919
 
    1  error
14920
 
*/
14921
 
 
14922
 
bool JOIN::make_sum_func_list(List<Item> &field_list, List<Item> &send_fields,
14923
 
                              bool before_group_by, bool recompute)
14924
 
{
14925
 
  List_iterator_fast<Item> it(field_list);
14926
 
  Item_sum **func;
14927
 
  Item *item;
14928
 
 
14929
 
  if (*sum_funcs && !recompute)
14930
 
    return(false); /* We have already initialized sum_funcs. */
14931
 
 
14932
 
  func= sum_funcs;
14933
 
  while ((item=it++))
14934
 
  {
14935
 
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
14936
 
        (!((Item_sum*) item)->depended_from() ||
14937
 
         ((Item_sum *)item)->depended_from() == select_lex))
14938
 
      *func++= (Item_sum*) item;
14939
 
  }
14940
 
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
14941
 
  {
14942
 
    rollup.state= ROLLUP::STATE_READY;
14943
 
    if (rollup_make_fields(field_list, send_fields, &func))
14944
 
      return(true);                     // Should never happen
14945
 
  }
14946
 
  else if (rollup.state == ROLLUP::STATE_NONE)
14947
 
  {
14948
 
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
14949
 
      sum_funcs_end[i]= func;
14950
 
  }
14951
 
  else if (rollup.state == ROLLUP::STATE_READY)
14952
 
    return(false);                         // Don't put end marker
14953
 
  *func=0;                                      // End marker
14954
 
  return(false);
14955
 
}
14956
 
 
14957
 
 
14958
7464
/**
14959
7465
  Change all funcs and sum_funcs to fields in tmp table, and create
14960
7466
  new list of all items.
14971
7477
  @retval
14972
7478
    !=0   error
14973
7479
*/
14974
 
 
14975
 
static bool
14976
 
change_to_use_tmp_fields(Session *session, Item **ref_pointer_array,
14977
 
                         List<Item> &res_selected_fields,
14978
 
                         List<Item> &res_all_fields,
14979
 
                         uint32_t elements, List<Item> &all_fields)
 
7480
bool change_to_use_tmp_fields(Session *session,
 
7481
                              Item **ref_pointer_array,
 
7482
                                                List<Item> &res_selected_fields,
 
7483
                                                List<Item> &res_all_fields,
 
7484
                                                uint32_t elements,
 
7485
                              List<Item> &all_fields)
14980
7486
{
14981
7487
  List_iterator_fast<Item> it(all_fields);
14982
7488
  Item *item_field,*item;
14997
7503
    {
14998
7504
      if (item->type() == Item::FIELD_ITEM)
14999
7505
      {
15000
 
        item_field= item->get_tmp_table_item(session);
 
7506
        item_field= item->get_tmp_table_item(session);
15001
7507
      }
15002
7508
      else if ((field= item->get_tmp_table_field()))
15003
7509
      {
15004
 
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
15005
 
          item_field= ((Item_sum*) item)->result_item(field);
15006
 
        else
15007
 
          item_field= (Item*) new Item_field(field);
15008
 
        if (!item_field)
15009
 
          return(true);                    // Fatal error
 
7510
        if (item->type() == Item::SUM_FUNC_ITEM && field->table->group)
 
7511
          item_field= ((Item_sum*) item)->result_item(field);
 
7512
        else
 
7513
          item_field= (Item*) new Item_field(field);
 
7514
        if (!item_field)
 
7515
          return(true);                    // Fatal error
15010
7516
 
15011
7517
        if (item->real_item()->type() != Item::FIELD_ITEM)
15012
7518
          field->orig_table= 0;
15013
 
        item_field->name= item->name;
 
7519
        item_field->name= item->name;
15014
7520
        if (item->type() == Item::REF_ITEM)
15015
7521
        {
15016
7522
          Item_field *ifield= (Item_field *) item_field;
15020
7526
        }
15021
7527
      }
15022
7528
      else
15023
 
        item_field= item;
 
7529
        item_field= item;
15024
7530
    }
15025
7531
    res_all_fields.push_back(item_field);
15026
7532
    ref_pointer_array[((i < border)? all_fields.elements-i-1 : i-border)]=
15034
7540
  return(false);
15035
7541
}
15036
7542
 
15037
 
 
15038
7543
/**
15039
7544
  Change all sum_func refs to fields to point at fields in tmp table.
15040
7545
  Change all funcs to be fields in tmp table.
15051
7556
  @retval
15052
7557
    1   error
15053
7558
*/
15054
 
 
15055
 
static bool
15056
 
change_refs_to_tmp_fields(Session *session, Item **ref_pointer_array,
15057
 
                          List<Item> &res_selected_fields,
15058
 
                          List<Item> &res_all_fields, uint32_t elements,
15059
 
                          List<Item> &all_fields)
 
7559
bool change_refs_to_tmp_fields(Session *session,
 
7560
                               Item **ref_pointer_array,
 
7561
                               List<Item> &res_selected_fields,
 
7562
                               List<Item> &res_all_fields,
 
7563
                               uint32_t elements,
 
7564
                                                 List<Item> &all_fields)
15060
7565
{
15061
7566
  List_iterator_fast<Item> it(all_fields);
15062
7567
  Item *item, *new_item;
15079
7584
  return session->is_fatal_error;
15080
7585
}
15081
7586
 
15082
 
 
15083
 
 
15084
7587
/******************************************************************************
15085
7588
  Code for calculating functions
15086
7589
******************************************************************************/
15087
7590
 
15088
 
 
15089
7591
/**
15090
7592
  Call ::setup for all sum functions.
15091
7593
 
15097
7599
  @retval
15098
7600
    true   error
15099
7601
*/
15100
 
 
15101
 
static bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
 
7602
bool setup_sum_funcs(Session *session, Item_sum **func_ptr)
15102
7603
{
15103
7604
  Item_sum *func;
15104
7605
  while ((func= *(func_ptr++)))
15109
7610
  return(false);
15110
7611
}
15111
7612
 
15112
 
 
15113
 
static void
15114
 
init_tmptable_sum_functions(Item_sum **func_ptr)
 
7613
void init_tmptable_sum_functions(Item_sum **func_ptr)
15115
7614
{
15116
7615
  Item_sum *func;
15117
7616
  while ((func= *(func_ptr++)))
15118
7617
    func->reset_field();
15119
7618
}
15120
7619
 
15121
 
 
15122
7620
/** Update record 0 in tmp_table from record 1. */
15123
 
 
15124
 
static void
15125
 
update_tmptable_sum_func(Item_sum **func_ptr, Table *)
 
7621
void update_tmptable_sum_func(Item_sum **func_ptr, Table *)
15126
7622
{
15127
7623
  Item_sum *func;
15128
7624
  while ((func= *(func_ptr++)))
15129
7625
    func->update_field();
15130
7626
}
15131
7627
 
15132
 
 
15133
7628
/** Copy result of sum functions to record in tmp_table. */
15134
 
 
15135
 
static void
15136
 
copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
 
7629
void copy_sum_funcs(Item_sum **func_ptr, Item_sum **end_ptr)
15137
7630
{
15138
7631
  for (; func_ptr != end_ptr ; func_ptr++)
15139
7632
    (void) (*func_ptr)->save_in_result_field(1);
15140
7633
  return;
15141
7634
}
15142
7635
 
15143
 
 
15144
 
static bool
15145
 
init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
 
7636
bool init_sum_functions(Item_sum **func_ptr, Item_sum **end_ptr)
15146
7637
{
15147
7638
  for (; func_ptr != end_ptr ;func_ptr++)
15148
7639
  {
15158
7649
  return 0;
15159
7650
}
15160
7651
 
15161
 
 
15162
 
static bool
15163
 
update_sum_func(Item_sum **func_ptr)
 
7652
bool update_sum_func(Item_sum **func_ptr)
15164
7653
{
15165
7654
  Item_sum *func;
15166
7655
  for (; (func= (Item_sum*) *func_ptr) ; func_ptr++)
15170
7659
}
15171
7660
 
15172
7661
/** Copy result of functions to record in tmp_table. */
15173
 
 
15174
 
void
15175
 
copy_funcs(Item **func_ptr)
 
7662
void copy_funcs(Item **func_ptr)
15176
7663
{
15177
7664
  Item *func;
15178
7665
  for (; (func = *func_ptr) ; func_ptr++)
15179
7666
    func->save_in_result_field(1);
15180
7667
}
15181
7668
 
15182
 
 
15183
 
/**
15184
 
  Create a condition for a const reference and add this to the
15185
 
  currenct select for the table.
15186
 
*/
15187
 
 
15188
 
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
15189
 
{
15190
 
  if (!join_tab->ref.key_parts)
15191
 
    return(false);
15192
 
 
15193
 
  Item_cond_and *cond=new Item_cond_and();
15194
 
  Table *table=join_tab->table;
15195
 
  int error;
15196
 
  if (!cond)
15197
 
    return(true);
15198
 
 
15199
 
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
15200
 
  {
15201
 
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
15202
 
                              fieldnr-1];
15203
 
    Item *value=join_tab->ref.items[i];
15204
 
    cond->add(new Item_func_equal(new Item_field(field), value));
15205
 
  }
15206
 
  if (session->is_fatal_error)
15207
 
    return(true);
15208
 
 
15209
 
  if (!cond->fixed)
15210
 
    cond->fix_fields(session, (Item**)&cond);
15211
 
  if (join_tab->select)
15212
 
  {
15213
 
    error=(int) cond->add(join_tab->select->cond);
15214
 
    join_tab->select_cond=join_tab->select->cond=cond;
15215
 
  }
15216
 
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
15217
 
                                          &error)))
15218
 
    join_tab->select_cond=cond;
15219
 
 
15220
 
  return(error ? true : false);
15221
 
}
15222
 
 
15223
 
 
15224
7669
/**
15225
7670
  Free joins of subselect of this select.
15226
7671
 
15227
7672
  @param session      Session pointer
15228
7673
  @param select   pointer to Select_Lex which subselects joins we will free
15229
7674
*/
15230
 
 
15231
7675
void free_underlaid_joins(Session *, Select_Lex *select)
15232
7676
{
15233
7677
  for (Select_Lex_Unit *unit= select->first_inner_unit();
15279
7723
  @retval
15280
7724
    1   on error
15281
7725
*/
15282
 
 
15283
 
static bool change_group_ref(Session *session, Item_func *expr, order_st *group_list,
15284
 
                             bool *changed)
 
7726
bool change_group_ref(Session *session, Item_func *expr, order_st *group_list, bool *changed)
15285
7727
{
15286
7728
  if (expr->arg_count)
15287
7729
  {
15324
7766
  return 0;
15325
7767
}
15326
7768
 
15327
 
 
15328
 
/** Allocate memory needed for other rollup functions. */
15329
 
 
15330
 
bool JOIN::rollup_init()
15331
 
{
15332
 
  uint32_t i,j;
15333
 
  Item **ref_array;
15334
 
 
15335
 
  tmp_table_param.quick_group= 0;       // Can't create groups in tmp table
15336
 
  rollup.state= ROLLUP::STATE_INITED;
15337
 
 
15338
 
  /*
15339
 
    Create pointers to the different sum function groups
15340
 
    These are updated by rollup_make_fields()
15341
 
  */
15342
 
  tmp_table_param.group_parts= send_group_parts;
15343
 
 
15344
 
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
15345
 
                                                sizeof(Item**) +
15346
 
                                                sizeof(List<Item>) +
15347
 
                                                ref_pointer_array_size)
15348
 
                                                * send_group_parts )))
15349
 
    return 1;
15350
 
 
15351
 
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
15352
 
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
15353
 
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
15354
 
 
15355
 
  /*
15356
 
    Prepare space for field list for the different levels
15357
 
    These will be filled up in rollup_make_fields()
15358
 
  */
15359
 
  for (i= 0 ; i < send_group_parts ; i++)
15360
 
  {
15361
 
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
15362
 
    List<Item> *rollup_fields= &rollup.fields[i];
15363
 
    rollup_fields->empty();
15364
 
    rollup.ref_pointer_arrays[i]= ref_array;
15365
 
    ref_array+= all_fields.elements;
15366
 
  }
15367
 
  for (i= 0 ; i < send_group_parts; i++)
15368
 
  {
15369
 
    for (j=0 ; j < fields_list.elements ; j++)
15370
 
      rollup.fields[i].push_back(rollup.null_items[i]);
15371
 
  }
15372
 
  List_iterator<Item> it(all_fields);
15373
 
  Item *item;
15374
 
  while ((item= it++))
15375
 
  {
15376
 
    order_st *group_tmp;
15377
 
    bool found_in_group= 0;
15378
 
 
15379
 
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
15380
 
    {
15381
 
      if (*group_tmp->item == item)
15382
 
      {
15383
 
        item->maybe_null= 1;
15384
 
        found_in_group= 1;
15385
 
        if (item->const_item())
15386
 
        {
15387
 
          /*
15388
 
            For ROLLUP queries each constant item referenced in GROUP BY list
15389
 
            is wrapped up into an Item_func object yielding the same value
15390
 
            as the constant item. The objects of the wrapper class are never
15391
 
            considered as constant items and besides they inherit all
15392
 
            properties of the Item_result_field class.
15393
 
            This wrapping allows us to ensure writing constant items
15394
 
            into temporary tables whenever the result of the ROLLUP
15395
 
            operation has to be written into a temporary table, e.g. when
15396
 
            ROLLUP is used together with DISTINCT in the SELECT list.
15397
 
            Usually when creating temporary tables for a intermidiate
15398
 
            result we do not include fields for constant expressions.
15399
 
          */
15400
 
          Item* new_item= new Item_func_rollup_const(item);
15401
 
          if (!new_item)
15402
 
            return 1;
15403
 
          new_item->fix_fields(session, (Item **) 0);
15404
 
          session->change_item_tree(it.ref(), new_item);
15405
 
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
15406
 
          {
15407
 
            if (*tmp->item == item)
15408
 
              session->change_item_tree(tmp->item, new_item);
15409
 
          }
15410
 
        }
15411
 
      }
15412
 
    }
15413
 
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
15414
 
    {
15415
 
      bool changed= false;
15416
 
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
15417
 
        return 1;
15418
 
      /*
15419
 
        We have to prevent creation of a field in a temporary table for
15420
 
        an expression that contains GROUP BY attributes.
15421
 
        Marking the expression item as 'with_sum_func' will ensure this.
15422
 
      */
15423
 
      if (changed)
15424
 
        item->with_sum_func= 1;
15425
 
    }
15426
 
  }
15427
 
  return 0;
15428
 
}
15429
 
 
15430
 
 
15431
 
/**
15432
 
  Fill up rollup structures with pointers to fields to use.
15433
 
 
15434
 
  Creates copies of item_sum items for each sum level.
15435
 
 
15436
 
  @param fields_arg             List of all fields (hidden and real ones)
15437
 
  @param sel_fields             Pointer to selected fields
15438
 
  @param func                   Store here a pointer to all fields
15439
 
 
15440
 
  @retval
15441
 
    0   if ok;
15442
 
    In this case func is pointing to next not used element.
15443
 
  @retval
15444
 
    1    on error
15445
 
*/
15446
 
 
15447
 
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields,
15448
 
                              Item_sum ***func)
15449
 
{
15450
 
  List_iterator_fast<Item> it(fields_arg);
15451
 
  Item *first_field= sel_fields.head();
15452
 
  uint32_t level;
15453
 
 
15454
 
  /*
15455
 
    Create field lists for the different levels
15456
 
 
15457
 
    The idea here is to have a separate field list for each rollup level to
15458
 
    avoid all runtime checks of which columns should be NULL.
15459
 
 
15460
 
    The list is stored in reverse order to get sum function in such an order
15461
 
    in func that it makes it easy to reset them with init_sum_functions()
15462
 
 
15463
 
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
15464
 
 
15465
 
    rollup.fields[0] will contain list where a,b,c is NULL
15466
 
    rollup.fields[1] will contain list where b,c is NULL
15467
 
    ...
15468
 
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
15469
 
    ...
15470
 
    sum_funcs_end[0] points to all sum functions
15471
 
    sum_funcs_end[1] points to all sum functions, except grand totals
15472
 
    ...
15473
 
  */
15474
 
 
15475
 
  for (level=0 ; level < send_group_parts ; level++)
15476
 
  {
15477
 
    uint32_t i;
15478
 
    uint32_t pos= send_group_parts - level -1;
15479
 
    bool real_fields= 0;
15480
 
    Item *item;
15481
 
    List_iterator<Item> new_it(rollup.fields[pos]);
15482
 
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
15483
 
    order_st *start_group;
15484
 
 
15485
 
    /* Point to first hidden field */
15486
 
    Item **ref_array= ref_array_start + fields_arg.elements-1;
15487
 
 
15488
 
    /* Remember where the sum functions ends for the previous level */
15489
 
    sum_funcs_end[pos+1]= *func;
15490
 
 
15491
 
    /* Find the start of the group for this level */
15492
 
    for (i= 0, start_group= group_list ;
15493
 
         i++ < pos ;
15494
 
         start_group= start_group->next)
15495
 
      ;
15496
 
 
15497
 
    it.rewind();
15498
 
    while ((item= it++))
15499
 
    {
15500
 
      if (item == first_field)
15501
 
      {
15502
 
        real_fields= 1;                         // End of hidden fields
15503
 
        ref_array= ref_array_start;
15504
 
      }
15505
 
 
15506
 
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
15507
 
          (!((Item_sum*) item)->depended_from() ||
15508
 
           ((Item_sum *)item)->depended_from() == select_lex))
15509
 
 
15510
 
      {
15511
 
        /*
15512
 
          This is a top level summary function that must be replaced with
15513
 
          a sum function that is reset for this level.
15514
 
 
15515
 
          NOTE: This code creates an object which is not that nice in a
15516
 
          sub select.  Fortunately it's not common to have rollup in
15517
 
          sub selects.
15518
 
        */
15519
 
        item= item->copy_or_same(session);
15520
 
        ((Item_sum*) item)->make_unique();
15521
 
        *(*func)= (Item_sum*) item;
15522
 
        (*func)++;
15523
 
      }
15524
 
      else
15525
 
      {
15526
 
        /* Check if this is something that is part of this group by */
15527
 
        order_st *group_tmp;
15528
 
        for (group_tmp= start_group, i= pos ;
15529
 
             group_tmp ; group_tmp= group_tmp->next, i++)
15530
 
        {
15531
 
          if (*group_tmp->item == item)
15532
 
          {
15533
 
            /*
15534
 
              This is an element that is used by the GROUP BY and should be
15535
 
              set to NULL in this level
15536
 
            */
15537
 
            Item_null_result *null_item= new (session->mem_root) Item_null_result();
15538
 
            if (!null_item)
15539
 
              return 1;
15540
 
            item->maybe_null= 1;                // Value will be null sometimes
15541
 
            null_item->result_field= item->get_tmp_table_field();
15542
 
            item= null_item;
15543
 
            break;
15544
 
          }
15545
 
        }
15546
 
      }
15547
 
      *ref_array= item;
15548
 
      if (real_fields)
15549
 
      {
15550
 
        (void) new_it++;                        // Point to next item
15551
 
        new_it.replace(item);                   // Replace previous
15552
 
        ref_array++;
15553
 
      }
15554
 
      else
15555
 
        ref_array--;
15556
 
    }
15557
 
  }
15558
 
  sum_funcs_end[0]= *func;                      // Point to last function
15559
 
  return 0;
15560
 
}
15561
 
 
15562
 
/**
15563
 
  Send all rollup levels higher than the current one to the client.
15564
 
 
15565
 
  @b SAMPLE
15566
 
    @code
15567
 
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
15568
 
  @endcode
15569
 
 
15570
 
  @param idx            Level we are on:
15571
 
                        - 0 = Total sum level
15572
 
                        - 1 = First group changed  (a)
15573
 
                        - 2 = Second group changed (a,b)
15574
 
 
15575
 
  @retval
15576
 
    0   ok
15577
 
  @retval
15578
 
    1   If send_data_failed()
15579
 
*/
15580
 
 
15581
 
int JOIN::rollup_send_data(uint32_t idx)
15582
 
{
15583
 
  uint32_t i;
15584
 
  for (i= send_group_parts ; i-- > idx ; )
15585
 
  {
15586
 
    /* Get reference pointers to sum functions in place */
15587
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
15588
 
           ref_pointer_array_size);
15589
 
    if ((!having || having->val_int()))
15590
 
    {
15591
 
      if (send_records < unit->select_limit_cnt && do_send_rows &&
15592
 
          result->send_data(rollup.fields[i]))
15593
 
        return 1;
15594
 
      send_records++;
15595
 
    }
15596
 
  }
15597
 
  /* Restore ref_pointer_array */
15598
 
  set_items_ref_array(current_ref_pointer_array);
15599
 
  return 0;
15600
 
}
15601
 
 
15602
 
/**
15603
 
  Write all rollup levels higher than the current one to a temp table.
15604
 
 
15605
 
  @b SAMPLE
15606
 
    @code
15607
 
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
15608
 
  @endcode
15609
 
 
15610
 
  @param idx                 Level we are on:
15611
 
                               - 0 = Total sum level
15612
 
                               - 1 = First group changed  (a)
15613
 
                               - 2 = Second group changed (a,b)
15614
 
  @param table               reference to temp table
15615
 
 
15616
 
  @retval
15617
 
    0   ok
15618
 
  @retval
15619
 
    1   if write_data_failed()
15620
 
*/
15621
 
 
15622
 
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
15623
 
{
15624
 
  uint32_t i;
15625
 
  for (i= send_group_parts ; i-- > idx ; )
15626
 
  {
15627
 
    /* Get reference pointers to sum functions in place */
15628
 
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
15629
 
           ref_pointer_array_size);
15630
 
    if ((!having || having->val_int()))
15631
 
    {
15632
 
      int write_error;
15633
 
      Item *item;
15634
 
      List_iterator_fast<Item> it(rollup.fields[i]);
15635
 
      while ((item= it++))
15636
 
      {
15637
 
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
15638
 
          item->save_in_result_field(1);
15639
 
      }
15640
 
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
15641
 
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
15642
 
      {
15643
 
        if (create_myisam_from_heap(session, table_arg,
15644
 
                                    tmp_table_param.start_recinfo,
15645
 
                                    &tmp_table_param.recinfo,
15646
 
                                    write_error, 0))
15647
 
          return 1;
15648
 
      }
15649
 
    }
15650
 
  }
15651
 
  /* Restore ref_pointer_array */
15652
 
  set_items_ref_array(current_ref_pointer_array);
15653
 
  return 0;
15654
 
}
15655
 
 
15656
 
/**
15657
 
  clear results if there are not rows found for group
15658
 
  (end_send_group/end_write_group)
15659
 
*/
15660
 
 
15661
 
void JOIN::clear()
15662
 
{
15663
 
  clear_tables(this);
15664
 
  copy_fields(&tmp_table_param);
15665
 
 
15666
 
  if (sum_funcs)
15667
 
  {
15668
 
    Item_sum *func, **func_ptr= sum_funcs;
15669
 
    while ((func= *(func_ptr++)))
15670
 
      func->clear();
15671
 
  }
15672
 
}
15673
 
 
15674
7769
/**
15675
7770
  EXPLAIN handling.
15676
7771
 
15677
7772
  Send a description about what how the select will be done to stdout.
15678
7773
*/
15679
 
 
15680
7774
void select_describe(JOIN *join, bool need_tmp_table, bool need_order,
15681
7775
                     bool distinct,const char *message)
15682
7776
{
15870
7964
                keylen_str_buf;
15871
7965
        item_list.push_back(new Item_string(keylen_str_buf, length,
15872
7966
                                            system_charset_info));
15873
 
        for (store_key **ref=tab->ref.key_copy ; *ref ; ref++)
 
7967
        for (StoredKey **ref=tab->ref.key_copy ; *ref ; ref++)
15874
7968
        {
15875
7969
          if (tmp2.length())
15876
7970
            tmp2.append(',');
16170
8264
  return;
16171
8265
}
16172
8266
 
16173
 
 
16174
8267
bool mysql_explain_union(Session *session, Select_Lex_Unit *unit, select_result *result)
16175
8268
{
16176
8269
  bool res= false;
16226
8319
  return(res || session->is_error());
16227
8320
}
16228
8321
 
16229
 
 
16230
8322
static void print_table_array(Session *session, String *str, TableList **table,
16231
8323
                              TableList **end)
16232
8324
{
16256
8348
  }
16257
8349
}
16258
8350
 
16259
 
 
16260
8351
/**
16261
8352
  Print joins from the FROM clause.
16262
8353
  @param session     thread handler
16264
8355
  @param tables  list of tables in join
16265
8356
  @query_type    type of the query is being generated
16266
8357
*/
16267
 
 
16268
8358
static void print_join(Session *session, String *str,
16269
8359
                       List<TableList> *tables, enum_query_type)
16270
8360
{
16372
8462
  }
16373
8463
}
16374
8464
 
16375
 
 
16376
8465
void Select_Lex::print(Session *session, String *str, enum_query_type query_type)
16377
8466
{
16378
8467
  /* QQ: session may not be set for sub queries, but this should be fixed */
16484
8573
  // PROCEDURE unsupported here
16485
8574
}
16486
8575
 
16487
 
 
16488
 
/**
16489
 
  change select_result object of JOIN.
16490
 
 
16491
 
  @param res            new select_result object
16492
 
 
16493
 
  @retval
16494
 
    false   OK
16495
 
  @retval
16496
 
    true    error
16497
 
*/
16498
 
 
16499
 
bool JOIN::change_result(select_result *res)
16500
 
{
16501
 
  result= res;
16502
 
  if (result->prepare(fields_list, select_lex->master_unit()))
16503
 
  {
16504
 
    return(true);
16505
 
  }
16506
 
  return(false);
16507
 
}
16508
 
 
16509
8576
/**
16510
8577
  @} (end of group Query_Optimizer)
16511
8578
*/