~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/join.cc

  • Committer: Brian Aker
  • Date: 2009-06-05 05:40:36 UTC
  • mfrom: (1046.1.15 merge)
  • Revision ID: brian@gaz-20090605054036-1024tm3gv0oloipw
Merge of Brian

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008-2009 Sun Microsystems
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; either version 2 of the License, or
 
9
 *  (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU General Public License
 
17
 *  along with this program; if not, write to the Free Software
 
18
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
19
 */
 
20
 
 
21
/**
 
22
 * @file
 
23
 *
 
24
 * Implementation of the JOIN class
 
25
 * 
 
26
 * @defgroup Query_Optimizer  Query Optimizer
 
27
 * @{
 
28
 */
 
29
 
 
30
#include "drizzled/server_includes.h"
 
31
#include "drizzled/sj_tmp_table.h"
 
32
#include "drizzled/table_map_iterator.h"
 
33
#include "drizzled/item/cache.h"
 
34
#include "drizzled/item/cmpfunc.h"
 
35
#include "drizzled/item/copy_string.h"
 
36
#include "drizzled/item/uint.h"
 
37
#include "drizzled/cached_item.h"
 
38
#include "drizzled/sql_base.h"
 
39
#include "drizzled/sql_select.h" /* include join.h */
 
40
#include "drizzled/lock.h"
 
41
#include "drizzled/nested_join.h"
 
42
#include "drizzled/join.h"
 
43
#include "drizzled/join_cache.h"
 
44
#include "drizzled/show.h"
 
45
#include "drizzled/field/blob.h"
 
46
#include "mysys/my_bit.h"
 
47
 
 
48
/** Declarations of static functions used in this source file. */
 
49
static bool make_group_fields(JOIN *main_join, JOIN *curr_join);
 
50
static void calc_group_buffer(JOIN *join,order_st *group);
 
51
static bool alloc_group_fields(JOIN *join,order_st *group);
 
52
/*
 
53
  TODO: 'find_best' is here only temporarily until 'greedy_search' is
 
54
  tested and approved.
 
55
*/
 
56
static bool find_best(JOIN *join,table_map rest_tables,uint32_t index, double record_count,double read_time);
 
57
static uint32_t cache_record_length(JOIN *join, uint32_t index);
 
58
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref);
 
59
static bool get_best_combination(JOIN *join);
 
60
static void set_position(JOIN *join,uint32_t index,JOIN_TAB *table,KEYUSE *key);
 
61
static bool choose_plan(JOIN *join,table_map join_tables);
 
62
static void best_access_path(JOIN *join, JOIN_TAB *s,
 
63
                             Session *session,
 
64
                             table_map remaining_tables,
 
65
                             uint32_t idx,
 
66
                             double record_count,
 
67
                             double read_time);
 
68
static void optimize_straight_join(JOIN *join, table_map join_tables);
 
69
static bool greedy_search(JOIN *join, table_map remaining_tables, uint32_t depth, uint32_t prune_level);
 
70
static bool best_extension_by_limited_search(JOIN *join,
 
71
                                             table_map remaining_tables,
 
72
                                             uint32_t idx,
 
73
                                             double record_count,
 
74
                                             double read_time,
 
75
                                             uint32_t depth,
 
76
                                             uint32_t prune_level);
 
77
static uint32_t determine_search_depth(JOIN* join);
 
78
static bool make_simple_join(JOIN *join,Table *tmp_table);
 
79
static void make_outerjoin_info(JOIN *join);
 
80
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *item);
 
81
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
82
static void update_depend_map(JOIN *join);
 
83
static void update_depend_map(JOIN *join, order_st *order);
 
84
static order_st *remove_constants(JOIN *join,order_st *first_order,COND *cond, bool change_list, bool *simple_order);
 
85
static int return_zero_rows(JOIN *join,
 
86
                            select_result *res,
 
87
                            TableList *tables,
 
88
                            List<Item> &fields,
 
89
                            bool send_row,
 
90
                            uint64_t select_options,
 
91
                            const char *info,
 
92
                            Item *having);
 
93
static COND *simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top, bool in_sj);
 
94
static int remove_duplicates(JOIN *join,Table *entry,List<Item> &fields, Item *having);
 
95
static int setup_without_group(Session *session, 
 
96
                               Item **ref_pointer_array,
 
97
                               TableList *tables,
 
98
                               TableList *,
 
99
                               List<Item> &fields,
 
100
                               List<Item> &all_fields,
 
101
                               COND **conds,
 
102
                               order_st *order,
 
103
                               order_st *group,
 
104
                               bool *hidden_group_fields);
 
105
static bool make_join_statistics(JOIN *join, TableList *leaves, COND *conds, DYNAMIC_ARRAY *keyuse);
 
106
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused);
 
107
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables);
 
108
static void reset_nj_counters(List<TableList> *join_list);
 
109
static bool test_if_subpart(order_st *a,order_st *b);
 
110
static void restore_prev_nj_state(JOIN_TAB *last);
 
111
static uint32_t make_join_orderinfo(JOIN *join);
 
112
static int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after);
 
113
static void cleanup_sj_tmp_tables(JOIN *join);
 
114
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab);
 
115
static bool replace_where_subcondition(JOIN *join, Item *old_cond, Item *new_cond, bool fix_fields);
 
116
static int pull_out_semijoin_tables(JOIN *join);
 
117
static int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl);
 
118
static void free_blobs(Field **ptr); /* Rename this method...conflicts with another in global namespace... */
 
119
static bool bitmap_covers(const table_map x, const table_map y);
 
120
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab);
 
121
 
 
122
/**
 
123
  Prepare of whole select (including sub queries in future).
 
124
 
 
125
  @todo
 
126
    Add check of calculation of GROUP functions and fields:
 
127
    SELECT COUNT(*)+table.col1 from table1;
 
128
 
 
129
  @retval
 
130
    -1   on error
 
131
  @retval
 
132
    0   on success
 
133
*/
 
134
int JOIN::prepare(Item ***rref_pointer_array,
 
135
                  TableList *tables_init,
 
136
                  uint32_t wild_num,
 
137
                  COND *conds_init,
 
138
                  uint32_t og_num,
 
139
                  order_st *order_init,
 
140
                  order_st *group_init,
 
141
                  Item *having_init,
 
142
                  Select_Lex *select_lex_arg,
 
143
                  Select_Lex_Unit *unit_arg)
 
144
{
 
145
  // to prevent double initialization on EXPLAIN
 
146
  if (optimized)
 
147
    return 0;
 
148
 
 
149
  conds= conds_init;
 
150
  order= order_init;
 
151
  group_list= group_init;
 
152
  having= having_init;
 
153
  tables_list= tables_init;
 
154
  select_lex= select_lex_arg;
 
155
  select_lex->join= this;
 
156
  join_list= &select_lex->top_join_list;
 
157
  union_part= unit_arg->is_union();
 
158
 
 
159
  session->lex->current_select->is_item_list_lookup= 1;
 
160
  /*
 
161
    If we have already executed SELECT, then it have not sense to prevent
 
162
    its table from update (see unique_table())
 
163
  */
 
164
  if (session->derived_tables_processing)
 
165
    select_lex->exclude_from_table_unique_test= true;
 
166
 
 
167
  /* Check that all tables, fields, conds and order are ok */
 
168
 
 
169
  if (!(select_options & OPTION_SETUP_TABLES_DONE) &&
 
170
      setup_tables_and_check_access(session, &select_lex->context, join_list,
 
171
                                    tables_list, &select_lex->leaf_tables,
 
172
                                    false))
 
173
      return(-1);
 
174
 
 
175
  TableList *table_ptr;
 
176
  for (table_ptr= select_lex->leaf_tables;
 
177
       table_ptr;
 
178
       table_ptr= table_ptr->next_leaf)
 
179
    tables++;
 
180
 
 
181
  if (setup_wild(session, fields_list, &all_fields, wild_num) ||
 
182
      select_lex->setup_ref_array(session, og_num) ||
 
183
      setup_fields(session, (*rref_pointer_array), fields_list, MARK_COLUMNS_READ,
 
184
       &all_fields, 1) ||
 
185
      setup_without_group(session, (*rref_pointer_array), tables_list,
 
186
        select_lex->leaf_tables, fields_list,
 
187
        all_fields, &conds, order, group_list,
 
188
        &hidden_group_fields))
 
189
    return(-1);       /* purecov: inspected */
 
190
 
 
191
  ref_pointer_array= *rref_pointer_array;
 
192
 
 
193
  if (having)
 
194
  {
 
195
    nesting_map save_allow_sum_func= session->lex->allow_sum_func;
 
196
    session->where="having clause";
 
197
    session->lex->allow_sum_func|= 1 << select_lex_arg->nest_level;
 
198
    select_lex->having_fix_field= 1;
 
199
    bool having_fix_rc= (!having->fixed &&
 
200
       (having->fix_fields(session, &having) ||
 
201
        having->check_cols(1)));
 
202
    select_lex->having_fix_field= 0;
 
203
    if (having_fix_rc || session->is_error())
 
204
      return(-1);       /* purecov: inspected */
 
205
    session->lex->allow_sum_func= save_allow_sum_func;
 
206
  }
 
207
 
 
208
  {
 
209
    Item_subselect *subselect;
 
210
    Item_in_subselect *in_subs= NULL;
 
211
    /*
 
212
      Are we in a subquery predicate?
 
213
      TODO: the block below will be executed for every PS execution without need.
 
214
    */
 
215
    if ((subselect= select_lex->master_unit()->item))
 
216
    {
 
217
      bool do_semijoin= !test(session->variables.optimizer_switch &
 
218
                              OPTIMIZER_SWITCH_NO_SEMIJOIN);
 
219
      if (subselect->substype() == Item_subselect::IN_SUBS)
 
220
        in_subs= (Item_in_subselect*)subselect;
 
221
 
 
222
      /*
 
223
        Check if we're in subquery that is a candidate for flattening into a
 
224
        semi-join (which is done done in flatten_subqueries()). The
 
225
        requirements are:
 
226
          1. Subquery predicate is an IN/=ANY subq predicate
 
227
          2. Subquery is a single SELECT (not a UNION)
 
228
          3. Subquery does not have GROUP BY or order_st BY
 
229
          4. Subquery does not use aggregate functions or HAVING
 
230
          5. Subquery predicate is at the AND-top-level of ON/WHERE clause
 
231
          6. No execution method was already chosen (by a prepared statement).
 
232
 
 
233
          (*). We are not in a subquery of a single table UPDATE/DELETE that
 
234
               doesn't have a JOIN (TODO: We should handle this at some
 
235
               point by switching to multi-table UPDATE/DELETE)
 
236
 
 
237
          (**). We're not in a confluent table-less subquery, like
 
238
                "SELECT 1".
 
239
      */
 
240
      if (in_subs &&                                                    // 1
 
241
          !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
242
          !select_lex->group_list.elements && !order &&                 // 3
 
243
          !having && !select_lex->with_sum_func &&                      // 4
 
244
          session->session_marker &&                                            // 5
 
245
          select_lex->outer_select()->join &&                           // (*)
 
246
          select_lex->master_unit()->first_select()->leaf_tables &&     // (**)
 
247
          do_semijoin &&
 
248
          in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED)   // 6
 
249
      {
 
250
        {
 
251
          if (!in_subs->left_expr->fixed &&
 
252
               in_subs->left_expr->fix_fields(session, &in_subs->left_expr))
 
253
          {
 
254
            return(-1);
 
255
          }
 
256
          /*
 
257
            Check that the right part of the subselect contains no more than one
 
258
            column. E.g. in SELECT 1 IN (SELECT * ..) the right part is (SELECT * ...)
 
259
          */
 
260
          if (subselect->substype() == Item_subselect::IN_SUBS &&
 
261
             (select_lex->item_list.elements !=
 
262
              ((Item_in_subselect*)subselect)->left_expr->cols()))
 
263
          {
 
264
            my_error(ER_OPERAND_COLUMNS, MYF(0), ((Item_in_subselect*)subselect)->left_expr->cols());
 
265
            return(-1);
 
266
          }
 
267
        }
 
268
 
 
269
        /* Register the subquery for further processing */
 
270
        select_lex->outer_select()->join->sj_subselects.append(session->mem_root, in_subs);
 
271
        in_subs->expr_join_nest= (TableList*)session->session_marker;
 
272
      }
 
273
      else
 
274
      {
 
275
        bool do_materialize= !test(session->variables.optimizer_switch &
 
276
                                   OPTIMIZER_SWITCH_NO_MATERIALIZATION);
 
277
        /*
 
278
          Check if the subquery predicate can be executed via materialization.
 
279
          The required conditions are:
 
280
          1. Subquery predicate is an IN/=ANY subq predicate
 
281
          2. Subquery is a single SELECT (not a UNION)
 
282
          3. Subquery is not a table-less query. In this case there is no
 
283
             point in materializing.
 
284
          4. Subquery predicate is a top-level predicate
 
285
             (this implies it is not negated)
 
286
             TODO: this is a limitation that should be lifeted once we
 
287
             implement correct NULL semantics (WL#3830)
 
288
          5. Subquery is non-correlated
 
289
             TODO:
 
290
             This is an overly restrictive condition. It can be extended to:
 
291
             (Subquery is non-correlated ||
 
292
              Subquery is correlated to any query outer to IN predicate ||
 
293
              (Subquery is correlated to the immediate outer query &&
 
294
               Subquery !contains {GROUP BY, order_st BY [LIMIT],
 
295
               aggregate functions) && subquery predicate is not under "NOT IN"))
 
296
          6. No execution method was already chosen (by a prepared statement).
 
297
 
 
298
          (*) The subquery must be part of a SELECT statement. The current
 
299
               condition also excludes multi-table update statements.
 
300
 
 
301
          We have to determine whether we will perform subquery materialization
 
302
          before calling the IN=>EXISTS transformation, so that we know whether to
 
303
          perform the whole transformation or only that part of it which wraps
 
304
          Item_in_subselect in an Item_in_optimizer.
 
305
        */
 
306
        if (do_materialize &&
 
307
            in_subs  &&                                                   // 1
 
308
            !select_lex->master_unit()->first_select()->next_select() &&  // 2
 
309
            select_lex->master_unit()->first_select()->leaf_tables &&     // 3
 
310
            session->lex->sql_command == SQLCOM_SELECT)                       // *
 
311
        {
 
312
          if (in_subs->is_top_level_item() &&                             // 4
 
313
              !in_subs->is_correlated &&                                  // 5
 
314
              in_subs->exec_method == Item_in_subselect::NOT_TRANSFORMED) // 6
 
315
            in_subs->exec_method= Item_in_subselect::MATERIALIZATION;
 
316
        }
 
317
 
 
318
        Item_subselect::trans_res trans_res;
 
319
        if ((trans_res= subselect->select_transformer(this)) !=
 
320
            Item_subselect::RES_OK)
 
321
        {
 
322
          return((trans_res == Item_subselect::RES_ERROR));
 
323
        }
 
324
      }
 
325
    }
 
326
  }
 
327
 
 
328
  if (order)
 
329
  {
 
330
    order_st *ord;
 
331
    for (ord= order; ord; ord= ord->next)
 
332
    {
 
333
      Item *item= *ord->item;
 
334
      if (item->with_sum_func && item->type() != Item::SUM_FUNC_ITEM)
 
335
        item->split_sum_func(session, ref_pointer_array, all_fields);
 
336
    }
 
337
  }
 
338
 
 
339
  if (having && having->with_sum_func)
 
340
    having->split_sum_func(session, ref_pointer_array, all_fields,
 
341
                           &having, true);
 
342
  if (select_lex->inner_sum_func_list)
 
343
  {
 
344
    Item_sum *end=select_lex->inner_sum_func_list;
 
345
    Item_sum *item_sum= end;
 
346
    do
 
347
    {
 
348
      item_sum= item_sum->next;
 
349
      item_sum->split_sum_func(session, ref_pointer_array,
 
350
                               all_fields, item_sum->ref_by, false);
 
351
    } while (item_sum != end);
 
352
  }
 
353
 
 
354
  if (select_lex->inner_refs_list.elements &&
 
355
      fix_inner_refs(session, all_fields, select_lex, ref_pointer_array))
 
356
    return(-1);
 
357
 
 
358
  /*
 
359
    Check if there are references to un-aggregated columns when computing
 
360
    aggregate functions with implicit grouping (there is no GROUP BY).
 
361
 
 
362
    MODE_ONLY_FULL_GROUP_BY is enabled here by default
 
363
  */
 
364
  if (!group_list && select_lex->full_group_by_flag == (NON_AGG_FIELD_USED | SUM_FUNC_USED))
 
365
  {
 
366
    my_message(ER_MIX_OF_GROUP_FUNC_AND_FIELDS,
 
367
               ER(ER_MIX_OF_GROUP_FUNC_AND_FIELDS), MYF(0));
 
368
    return(-1);
 
369
  }
 
370
  {
 
371
    /* Caclulate the number of groups */
 
372
    send_group_parts= 0;
 
373
    for (order_st *group_tmp= group_list ; group_tmp ; group_tmp= group_tmp->next)
 
374
      send_group_parts++;
 
375
  }
 
376
 
 
377
  if (error)
 
378
    goto err;         /* purecov: inspected */
 
379
 
 
380
  if (result && result->prepare(fields_list, unit_arg))
 
381
    goto err;         /* purecov: inspected */
 
382
 
 
383
  /* Init join struct */
 
384
  count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
385
  ref_pointer_array_size= all_fields.elements*sizeof(Item*);
 
386
  this->group= group_list != 0;
 
387
  unit= unit_arg;
 
388
 
 
389
#ifdef RESTRICTED_GROUP
 
390
  if (sum_func_count && !group_list && (func_count || field_count))
 
391
  {
 
392
    my_message(ER_WRONG_SUM_SELECT,ER(ER_WRONG_SUM_SELECT),MYF(0));
 
393
    goto err;
 
394
  }
 
395
#endif
 
396
  if (select_lex->olap == ROLLUP_TYPE && rollup_init())
 
397
    goto err;
 
398
  if (alloc_func_list())
 
399
    goto err;
 
400
 
 
401
  return(0); // All OK
 
402
 
 
403
err:
 
404
  return(-1);       /* purecov: inspected */
 
405
}
 
406
 
 
407
/*
 
408
  Remove the predicates pushed down into the subquery
 
409
 
 
410
  SYNOPSIS
 
411
    JOIN::remove_subq_pushed_predicates()
 
412
      where   IN  Must be NULL
 
413
              OUT The remaining WHERE condition, or NULL
 
414
 
 
415
  DESCRIPTION
 
416
    Given that this join will be executed using (unique|index)_subquery,
 
417
    without "checking NULL", remove the predicates that were pushed down
 
418
    into the subquery.
 
419
 
 
420
    If the subquery compares scalar values, we can remove the condition that
 
421
    was wrapped into trig_cond (it will be checked when needed by the subquery
 
422
    engine)
 
423
 
 
424
    If the subquery compares row values, we need to keep the wrapped
 
425
    equalities in the WHERE clause: when the left (outer) tuple has both NULL
 
426
    and non-NULL values, we'll do a full table scan and will rely on the
 
427
    equalities corresponding to non-NULL parts of left tuple to filter out
 
428
    non-matching records.
 
429
 
 
430
    TODO: We can remove the equalities that will be guaranteed to be true by the
 
431
    fact that subquery engine will be using index lookup. This must be done only
 
432
    for cases where there are no conversion errors of significance, e.g. 257
 
433
    that is searched in a byte. But this requires homogenization of the return
 
434
    codes of all Field*::store() methods.
 
435
*/
 
436
void JOIN::remove_subq_pushed_predicates(Item **where)
 
437
{
 
438
  if (conds->type() == Item::FUNC_ITEM &&
 
439
      ((Item_func *)this->conds)->functype() == Item_func::EQ_FUNC &&
 
440
      ((Item_func *)conds)->arguments()[0]->type() == Item::REF_ITEM &&
 
441
      ((Item_func *)conds)->arguments()[1]->type() == Item::FIELD_ITEM &&
 
442
      test_if_ref ((Item_field *)((Item_func *)conds)->arguments()[1],
 
443
                   ((Item_func *)conds)->arguments()[0]))
 
444
  {
 
445
    *where= 0;
 
446
    return;
 
447
  }
 
448
}
 
449
 
 
450
/**
 
451
  global select optimisation.
 
452
 
 
453
  @note
 
454
    error code saved in field 'error'
 
455
 
 
456
  @retval
 
457
    0   success
 
458
  @retval
 
459
    1   error
 
460
*/
 
461
int JOIN::optimize()
 
462
{
 
463
  // to prevent double initialization on EXPLAIN
 
464
  if (optimized)
 
465
    return(0);
 
466
  optimized= 1;
 
467
 
 
468
  session->set_proc_info("optimizing");
 
469
  row_limit= ((select_distinct || order || group_list) ? HA_POS_ERROR :
 
470
        unit->select_limit_cnt);
 
471
  /* select_limit is used to decide if we are likely to scan the whole table */
 
472
  select_limit= unit->select_limit_cnt;
 
473
  if (having || (select_options & OPTION_FOUND_ROWS))
 
474
    select_limit= HA_POS_ERROR;
 
475
  do_send_rows = (unit->select_limit_cnt) ? 1 : 0;
 
476
  // Ignore errors of execution if option IGNORE present
 
477
  if (session->lex->ignore)
 
478
    session->lex->current_select->no_error= 1;
 
479
 
 
480
#ifdef HAVE_REF_TO_FIELDS     // Not done yet
 
481
  /* Add HAVING to WHERE if possible */
 
482
  if (having && !group_list && !sum_func_count)
 
483
  {
 
484
    if (!conds)
 
485
    {
 
486
      conds= having;
 
487
      having= 0;
 
488
    }
 
489
    else if ((conds=new Item_cond_and(conds,having)))
 
490
    {
 
491
      /*
 
492
        Item_cond_and can't be fixed after creation, so we do not check
 
493
        conds->fixed
 
494
      */
 
495
      conds->fix_fields(session, &conds);
 
496
      conds->change_ref_to_fields(session, tables_list);
 
497
      conds->top_level_item();
 
498
      having= 0;
 
499
    }
 
500
  }
 
501
#endif
 
502
 
 
503
  /* Convert all outer joins to inner joins if possible */
 
504
  conds= simplify_joins(this, join_list, conds, true, false);
 
505
  build_bitmap_for_nested_joins(join_list, 0);
 
506
 
 
507
  conds= optimize_cond(this, conds, join_list, &cond_value);
 
508
  if (session->is_error())
 
509
  {
 
510
    error= 1;
 
511
    return(1);
 
512
  }
 
513
 
 
514
  {
 
515
    having= optimize_cond(this, having, join_list, &having_value);
 
516
    if (session->is_error())
 
517
    {
 
518
      error= 1;
 
519
      return(1);
 
520
    }
 
521
    if (select_lex->where)
 
522
      select_lex->cond_value= cond_value;
 
523
    if (select_lex->having)
 
524
      select_lex->having_value= having_value;
 
525
 
 
526
    if (cond_value == Item::COND_FALSE || having_value == Item::COND_FALSE ||
 
527
        (!unit->select_limit_cnt && !(select_options & OPTION_FOUND_ROWS)))
 
528
    {           /* Impossible cond */
 
529
      zero_result_cause=  having_value == Item::COND_FALSE ?
 
530
                           "Impossible HAVING" : "Impossible WHERE";
 
531
      error= 0;
 
532
      return(0);
 
533
    }
 
534
  }
 
535
 
 
536
  /* Optimize count(*), cmin() and cmax() */
 
537
  if (tables_list && tmp_table_param.sum_func_count && ! group_list)
 
538
  {
 
539
    int res;
 
540
    /*
 
541
      opt_sum_query() returns HA_ERR_KEY_NOT_FOUND if no rows match
 
542
      to the WHERE conditions,
 
543
      or 1 if all items were resolved,
 
544
      or 0, or an error number HA_ERR_...
 
545
    */
 
546
    if ((res=opt_sum_query(select_lex->leaf_tables, all_fields, conds)))
 
547
    {
 
548
      if (res == HA_ERR_KEY_NOT_FOUND)
 
549
      {
 
550
        zero_result_cause= "No matching min/max row";
 
551
        error=0;
 
552
        return(0);
 
553
      }
 
554
      if (res > 1)
 
555
      {
 
556
        error= res;
 
557
        return(1);
 
558
      }
 
559
      if (res < 0)
 
560
      {
 
561
        zero_result_cause= "No matching min/max row";
 
562
        error=0;
 
563
        return(0);
 
564
      }
 
565
      zero_result_cause= "Select tables optimized away";
 
566
      tables_list= 0;       // All tables resolved
 
567
      /*
 
568
        Extract all table-independent conditions and replace the WHERE
 
569
        clause with them. All other conditions were computed by opt_sum_query
 
570
        and the MIN/MAX/COUNT function(s) have been replaced by constants,
 
571
        so there is no need to compute the whole WHERE clause again.
 
572
        Notice that make_cond_for_table() will always succeed to remove all
 
573
        computed conditions, because opt_sum_query() is applicable only to
 
574
        conjunctions.
 
575
        Preserve conditions for EXPLAIN.
 
576
      */
 
577
      if (conds && !(session->lex->describe & DESCRIBE_EXTENDED))
 
578
      {
 
579
        COND *table_independent_conds= make_cond_for_table(conds, PSEUDO_TABLE_BITS, 0, 0);
 
580
        conds= table_independent_conds;
 
581
      }
 
582
    }
 
583
  }
 
584
  if (!tables_list)
 
585
  {
 
586
    error= 0;
 
587
    return(0);
 
588
  }
 
589
  error= -1;          // Error is sent to client
 
590
  sort_by_table= get_sort_by_table(order, group_list, select_lex->leaf_tables);
 
591
 
 
592
  /* Calculate how to do the join */
 
593
  session->set_proc_info("statistics");
 
594
  if (make_join_statistics(this, select_lex->leaf_tables, conds, &keyuse) ||
 
595
      session->is_fatal_error)
 
596
  {
 
597
    return(1);
 
598
  }
 
599
 
 
600
  /* Remove distinct if only const tables */
 
601
  select_distinct= select_distinct && (const_tables != tables);
 
602
  session->set_proc_info("preparing");
 
603
  if (result->initialize_tables(this))
 
604
  {
 
605
    return(1);        // error == -1
 
606
  }
 
607
  if (const_table_map != found_const_table_map &&
 
608
      !(select_options & SELECT_DESCRIBE) &&
 
609
      (!conds ||
 
610
       !(conds->used_tables() & RAND_TABLE_BIT) ||
 
611
       select_lex->master_unit() == &session->lex->unit)) // upper level SELECT
 
612
  {
 
613
    zero_result_cause= "no matching row in const table";
 
614
    error= 0;
 
615
    return(0);
 
616
  }
 
617
  if (!(session->options & OPTION_BIG_SELECTS) &&
 
618
      best_read > (double) session->variables.max_join_size &&
 
619
      !(select_options & SELECT_DESCRIBE))
 
620
  {           /* purecov: inspected */
 
621
    my_message(ER_TOO_BIG_SELECT, ER(ER_TOO_BIG_SELECT), MYF(0));
 
622
    error= -1;
 
623
    return(1);
 
624
  }
 
625
  if (const_tables && !session->locked_tables &&
 
626
      !(select_options & SELECT_NO_UNLOCK))
 
627
    mysql_unlock_some_tables(session, table, const_tables);
 
628
  if (!conds && outer_join)
 
629
  {
 
630
    /* Handle the case where we have an OUTER JOIN without a WHERE */
 
631
    conds=new Item_int((int64_t) 1,1);  // Always true
 
632
  }
 
633
  select= make_select(*table, const_table_map,
 
634
                      const_table_map, conds, 1, &error);
 
635
  if (error)
 
636
  {           /* purecov: inspected */
 
637
    error= -1;          /* purecov: inspected */
 
638
    return(1);
 
639
  }
 
640
 
 
641
  reset_nj_counters(join_list);
 
642
  make_outerjoin_info(this);
 
643
 
 
644
  /*
 
645
    Among the equal fields belonging to the same multiple equality
 
646
    choose the one that is to be retrieved first and substitute
 
647
    all references to these in where condition for a reference for
 
648
    the selected field.
 
649
  */
 
650
  if (conds)
 
651
  {
 
652
    conds= substitute_for_best_equal_field(conds, cond_equal, map2table);
 
653
    conds->update_used_tables();
 
654
  }
 
655
 
 
656
  /*
 
657
    Permorm the the optimization on fields evaluation mentioned above
 
658
    for all on expressions.
 
659
  */
 
660
  for (JOIN_TAB *tab= join_tab + const_tables; tab < join_tab + tables ; tab++)
 
661
  {
 
662
    if (*tab->on_expr_ref)
 
663
    {
 
664
      *tab->on_expr_ref= substitute_for_best_equal_field(*tab->on_expr_ref,
 
665
                                                         tab->cond_equal,
 
666
                                                         map2table);
 
667
      (*tab->on_expr_ref)->update_used_tables();
 
668
    }
 
669
  }
 
670
 
 
671
  if (conds &&!outer_join && const_table_map != found_const_table_map &&
 
672
      (select_options & SELECT_DESCRIBE) &&
 
673
      select_lex->master_unit() == &session->lex->unit) // upper level SELECT
 
674
  {
 
675
    conds=new Item_int((int64_t) 0,1);  // Always false
 
676
  }
 
677
  if (make_join_select(this, select, conds))
 
678
  {
 
679
    zero_result_cause=
 
680
      "Impossible WHERE noticed after reading const tables";
 
681
    return(0);        // error == 0
 
682
  }
 
683
 
 
684
  error= -1;          /* if goto err */
 
685
 
 
686
  /* Optimize distinct away if possible */
 
687
  {
 
688
    order_st *org_order= order;
 
689
    order= remove_constants(this, order,conds,1, &simple_order);
 
690
    if (session->is_error())
 
691
    {
 
692
      error= 1;
 
693
      return(1);
 
694
    }
 
695
 
 
696
    /*
 
697
      If we are using order_st BY NULL or order_st BY const_expression,
 
698
      return result in any order (even if we are using a GROUP BY)
 
699
    */
 
700
    if (!order && org_order)
 
701
      skip_sort_order= 1;
 
702
  }
 
703
  /*
 
704
     Check if we can optimize away GROUP BY/DISTINCT.
 
705
     We can do that if there are no aggregate functions, the
 
706
     fields in DISTINCT clause (if present) and/or columns in GROUP BY
 
707
     (if present) contain direct references to all key parts of
 
708
     an unique index (in whatever order) and if the key parts of the
 
709
     unique index cannot contain NULLs.
 
710
     Note that the unique keys for DISTINCT and GROUP BY should not
 
711
     be the same (as long as they are unique).
 
712
 
 
713
     The FROM clause must contain a single non-constant table.
 
714
  */
 
715
  if (tables - const_tables == 1 && (group_list || select_distinct) &&
 
716
      !tmp_table_param.sum_func_count &&
 
717
      (!join_tab[const_tables].select ||
 
718
       !join_tab[const_tables].select->quick ||
 
719
       join_tab[const_tables].select->quick->get_type() !=
 
720
       QUICK_SELECT_I::QS_TYPE_GROUP_MIN_MAX))
 
721
  {
 
722
    if (group_list && list_contains_unique_index(join_tab[const_tables].table, find_field_in_order_list, (void *) group_list))
 
723
    {
 
724
      /*
 
725
        We have found that grouping can be removed since groups correspond to
 
726
        only one row anyway, but we still have to guarantee correct result
 
727
        order. The line below effectively rewrites the query from GROUP BY
 
728
        <fields> to order_st BY <fields>. There are two exceptions:
 
729
        - if skip_sort_order is set (see above), then we can simply skip
 
730
          GROUP BY;
 
731
        - we can only rewrite order_st BY if the order_st BY fields are 'compatible'
 
732
          with the GROUP BY ones, i.e. either one is a prefix of another.
 
733
          We only check if the order_st BY is a prefix of GROUP BY. In this case
 
734
          test_if_subpart() copies the ASC/DESC attributes from the original
 
735
          order_st BY fields.
 
736
          If GROUP BY is a prefix of order_st BY, then it is safe to leave
 
737
          'order' as is.
 
738
       */
 
739
      if (!order || test_if_subpart(group_list, order))
 
740
          order= skip_sort_order ? 0 : group_list;
 
741
      /*
 
742
        If we have an IGNORE INDEX FOR GROUP BY(fields) clause, this must be
 
743
        rewritten to IGNORE INDEX FOR order_st BY(fields).
 
744
      */
 
745
      join_tab->table->keys_in_use_for_order_by=
 
746
        join_tab->table->keys_in_use_for_group_by;
 
747
      group_list= 0;
 
748
      group= 0;
 
749
    }
 
750
    if (select_distinct &&
 
751
       list_contains_unique_index(join_tab[const_tables].table,
 
752
                                 find_field_in_item_list,
 
753
                                 (void *) &fields_list))
 
754
    {
 
755
      select_distinct= 0;
 
756
    }
 
757
  }
 
758
  if (group_list || tmp_table_param.sum_func_count)
 
759
  {
 
760
    if (! hidden_group_fields && rollup.state == ROLLUP::STATE_NONE)
 
761
      select_distinct=0;
 
762
  }
 
763
  else if (select_distinct && tables - const_tables == 1)
 
764
  {
 
765
    /*
 
766
      We are only using one table. In this case we change DISTINCT to a
 
767
      GROUP BY query if:
 
768
      - The GROUP BY can be done through indexes (no sort) and the order_st
 
769
        BY only uses selected fields.
 
770
        (In this case we can later optimize away GROUP BY and order_st BY)
 
771
      - We are scanning the whole table without LIMIT
 
772
        This can happen if:
 
773
        - We are using CALC_FOUND_ROWS
 
774
        - We are using an order_st BY that can't be optimized away.
 
775
 
 
776
      We don't want to use this optimization when we are using LIMIT
 
777
      because in this case we can just create a temporary table that
 
778
      holds LIMIT rows and stop when this table is full.
 
779
    */
 
780
    JOIN_TAB *tab= &join_tab[const_tables];
 
781
    bool all_order_fields_used;
 
782
    if (order)
 
783
      skip_sort_order= test_if_skip_sort_order(tab, order, select_limit, 1,
 
784
        &tab->table->keys_in_use_for_order_by);
 
785
    if ((group_list=create_distinct_group(session, select_lex->ref_pointer_array,
 
786
                                          order, fields_list, all_fields,
 
787
                  &all_order_fields_used)))
 
788
    {
 
789
      bool skip_group= (skip_sort_order &&
 
790
        test_if_skip_sort_order(tab, group_list, select_limit, 1,
 
791
                                &tab->table->keys_in_use_for_group_by) != 0);
 
792
      count_field_types(select_lex, &tmp_table_param, all_fields, 0);
 
793
      if ((skip_group && all_order_fields_used) ||
 
794
          select_limit == HA_POS_ERROR ||
 
795
          (order && !skip_sort_order))
 
796
      {
 
797
        /*  Change DISTINCT to GROUP BY */
 
798
        select_distinct= 0;
 
799
        no_order= !order;
 
800
        if (all_order_fields_used)
 
801
        {
 
802
          if (order && skip_sort_order)
 
803
          {
 
804
            /*
 
805
              Force MySQL to read the table in sorted order to get result in
 
806
              order_st BY order.
 
807
            */
 
808
            tmp_table_param.quick_group=0;
 
809
          }
 
810
          order=0;
 
811
        }
 
812
        group=1;        // For end_write_group
 
813
      }
 
814
      else
 
815
        group_list= 0;
 
816
    }
 
817
    else if (session->is_fatal_error)     // End of memory
 
818
      return(1);
 
819
  }
 
820
  simple_group= 0;
 
821
  {
 
822
    order_st *old_group_list;
 
823
    group_list= remove_constants(this, (old_group_list= group_list), conds,
 
824
                                 rollup.state == ROLLUP::STATE_NONE,
 
825
                                 &simple_group);
 
826
    if (session->is_error())
 
827
    {
 
828
      error= 1;
 
829
      return(1);
 
830
    }
 
831
    if (old_group_list && !group_list)
 
832
      select_distinct= 0;
 
833
  }
 
834
  if (!group_list && group)
 
835
  {
 
836
    order=0;          // The output has only one row
 
837
    simple_order=1;
 
838
    select_distinct= 0;                       // No need in distinct for 1 row
 
839
    group_optimized_away= 1;
 
840
  }
 
841
 
 
842
  calc_group_buffer(this, group_list);
 
843
  send_group_parts= tmp_table_param.group_parts; /* Save org parts */
 
844
 
 
845
  if (test_if_subpart(group_list, order) ||
 
846
      (!group_list && tmp_table_param.sum_func_count))
 
847
    order=0;
 
848
 
 
849
  // Can't use sort on head table if using row cache
 
850
  if (full_join)
 
851
  {
 
852
    if (group_list)
 
853
      simple_group=0;
 
854
    if (order)
 
855
      simple_order=0;
 
856
  }
 
857
 
 
858
  /*
 
859
    Check if we need to create a temporary table.
 
860
    This has to be done if all tables are not already read (const tables)
 
861
    and one of the following conditions holds:
 
862
    - We are using DISTINCT (simple distinct's are already optimized away)
 
863
    - We are using an order_st BY or GROUP BY on fields not in the first table
 
864
    - We are using different order_st BY and GROUP BY orders
 
865
    - The user wants us to buffer the result.
 
866
  */
 
867
  need_tmp= (const_tables != tables &&
 
868
       ((select_distinct || !simple_order || !simple_group) ||
 
869
        (group_list && order) ||
 
870
        test(select_options & OPTION_BUFFER_RESULT)));
 
871
 
 
872
  uint32_t no_jbuf_after= make_join_orderinfo(this);
 
873
  uint64_t select_opts_for_readinfo=
 
874
    (select_options & (SELECT_DESCRIBE | SELECT_NO_JOIN_CACHE)) | (0);
 
875
 
 
876
  sj_tmp_tables= NULL;
 
877
  if (!select_lex->sj_nests.is_empty())
 
878
    setup_semijoin_dups_elimination(this, select_opts_for_readinfo, no_jbuf_after);
 
879
 
 
880
  // No cache for MATCH == 'Don't use join buffering when we use MATCH'.
 
881
  if (make_join_readinfo(this, select_opts_for_readinfo, no_jbuf_after))
 
882
    return(1);
 
883
 
 
884
  /* Create all structures needed for materialized subquery execution. */
 
885
  if (setup_subquery_materialization())
 
886
    return(1);
 
887
 
 
888
  /*
 
889
    is this simple IN subquery?
 
890
  */
 
891
  if (!group_list && !order &&
 
892
      unit->item && unit->item->substype() == Item_subselect::IN_SUBS &&
 
893
      tables == 1 && conds &&
 
894
      !unit->is_union())
 
895
  {
 
896
    if (!having)
 
897
    {
 
898
      Item *where= conds;
 
899
      if (join_tab[0].type == JT_EQ_REF && join_tab[0].ref.items[0]->name == in_left_expr_name)
 
900
      {
 
901
        remove_subq_pushed_predicates(&where);
 
902
        save_index_subquery_explain_info(join_tab, where);
 
903
        join_tab[0].type= JT_UNIQUE_SUBQUERY;
 
904
        error= 0;
 
905
        return(unit->item->
 
906
                    change_engine(new
 
907
                                  subselect_uniquesubquery_engine(session,
 
908
                                                                  join_tab,
 
909
                                                                  unit->item,
 
910
                                                                  where)));
 
911
      }
 
912
      else if (join_tab[0].type == JT_REF &&
 
913
         join_tab[0].ref.items[0]->name == in_left_expr_name)
 
914
      {
 
915
        remove_subq_pushed_predicates(&where);
 
916
        save_index_subquery_explain_info(join_tab, where);
 
917
        join_tab[0].type= JT_INDEX_SUBQUERY;
 
918
        error= 0;
 
919
        return(unit->item->
 
920
                    change_engine(new
 
921
                                  subselect_indexsubquery_engine(session,
 
922
                                                                 join_tab,
 
923
                                                                 unit->item,
 
924
                                                                 where,
 
925
                                                                 NULL,
 
926
                                                                 0)));
 
927
      }
 
928
    } 
 
929
    else if (join_tab[0].type == JT_REF_OR_NULL &&
 
930
         join_tab[0].ref.items[0]->name == in_left_expr_name &&
 
931
               having->name == in_having_cond)
 
932
    {
 
933
      join_tab[0].type= JT_INDEX_SUBQUERY;
 
934
      error= 0;
 
935
      conds= remove_additional_cond(conds);
 
936
      save_index_subquery_explain_info(join_tab, conds);
 
937
      return(unit->item->
 
938
      change_engine(new subselect_indexsubquery_engine(session,
 
939
                   join_tab,
 
940
                   unit->item,
 
941
                   conds,
 
942
                                                                   having,
 
943
                   1)));
 
944
    }
 
945
 
 
946
  }
 
947
  /*
 
948
    Need to tell handlers that to play it safe, it should fetch all
 
949
    columns of the primary key of the tables: this is because MySQL may
 
950
    build row pointers for the rows, and for all columns of the primary key
 
951
    the read set has not necessarily been set by the server code.
 
952
  */
 
953
  if (need_tmp || select_distinct || group_list || order)
 
954
  {
 
955
    for (uint32_t i = const_tables; i < tables; i++)
 
956
      join_tab[i].table->prepare_for_position();
 
957
  }
 
958
 
 
959
  if (const_tables != tables)
 
960
  {
 
961
    /*
 
962
      Because filesort always does a full table scan or a quick range scan
 
963
      we must add the removed reference to the select for the table.
 
964
      We only need to do this when we have a simple_order or simple_group
 
965
      as in other cases the join is done before the sort.
 
966
    */
 
967
    if ((order || group_list) &&
 
968
        (join_tab[const_tables].type != JT_ALL) &&
 
969
        (join_tab[const_tables].type != JT_REF_OR_NULL) &&
 
970
        ((order && simple_order) || (group_list && simple_group)))
 
971
    {
 
972
      if (add_ref_to_table_cond(session,&join_tab[const_tables])) {
 
973
        return(1);
 
974
      }
 
975
    }
 
976
 
 
977
    if (!(select_options & SELECT_BIG_RESULT) &&
 
978
        ((group_list &&
 
979
          (!simple_group ||
 
980
           !test_if_skip_sort_order(&join_tab[const_tables], group_list,
 
981
                                    unit->select_limit_cnt, 0,
 
982
                                    &join_tab[const_tables].table->
 
983
                                    keys_in_use_for_group_by))) ||
 
984
         select_distinct) &&
 
985
        tmp_table_param.quick_group)
 
986
    {
 
987
      need_tmp=1; simple_order=simple_group=0;  // Force tmp table without sort
 
988
    }
 
989
    if (order)
 
990
    {
 
991
      /*
 
992
        Force using of tmp table if sorting by a SP or UDF function due to
 
993
        their expensive and probably non-deterministic nature.
 
994
      */
 
995
      for (order_st *tmp_order= order; tmp_order ; tmp_order=tmp_order->next)
 
996
      {
 
997
        Item *item= *tmp_order->item;
 
998
        if (item->is_expensive())
 
999
        {
 
1000
          /* Force tmp table without sort */
 
1001
          need_tmp=1; simple_order=simple_group=0;
 
1002
          break;
 
1003
        }
 
1004
      }
 
1005
    }
 
1006
  }
 
1007
 
 
1008
  tmp_having= having;
 
1009
  if (select_options & SELECT_DESCRIBE)
 
1010
  {
 
1011
    error= 0;
 
1012
    return(0);
 
1013
  }
 
1014
  having= 0;
 
1015
 
 
1016
  /*
 
1017
    The loose index scan access method guarantees that all grouping or
 
1018
    duplicate row elimination (for distinct) is already performed
 
1019
    during data retrieval, and that all MIN/MAX functions are already
 
1020
    computed for each group. Thus all MIN/MAX functions should be
 
1021
    treated as regular functions, and there is no need to perform
 
1022
    grouping in the main execution loop.
 
1023
    Notice that currently loose index scan is applicable only for
 
1024
    single table queries, thus it is sufficient to test only the first
 
1025
    join_tab element of the plan for its access method.
 
1026
  */
 
1027
  if (join_tab->is_using_loose_index_scan())
 
1028
    tmp_table_param.precomputed_group_by= true;
 
1029
 
 
1030
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1031
  if (need_tmp)
 
1032
  {
 
1033
    session->set_proc_info("Creating tmp table");
 
1034
 
 
1035
    init_items_ref_array();
 
1036
 
 
1037
    tmp_table_param.hidden_field_count= (all_fields.elements -
 
1038
           fields_list.elements);
 
1039
    order_st *tmp_group= ((!simple_group && !(test_flags & TEST_NO_KEY_GROUP)) ? group_list :
 
1040
                                                             (order_st*) 0);
 
1041
    /*
 
1042
      Pushing LIMIT to the temporary table creation is not applicable
 
1043
      when there is order_st BY or GROUP BY or there is no GROUP BY, but
 
1044
      there are aggregate functions, because in all these cases we need
 
1045
      all result rows.
 
1046
    */
 
1047
    ha_rows tmp_rows_limit= ((order == 0 || skip_sort_order) &&
 
1048
                             !tmp_group &&
 
1049
                             !session->lex->current_select->with_sum_func) ?
 
1050
                            select_limit : HA_POS_ERROR;
 
1051
 
 
1052
    if (!(exec_tmp_table1=
 
1053
    create_tmp_table(session, &tmp_table_param, all_fields,
 
1054
                           tmp_group,
 
1055
         group_list ? 0 : select_distinct,
 
1056
         group_list && simple_group,
 
1057
         select_options,
 
1058
                           tmp_rows_limit,
 
1059
         (char *) "")))
 
1060
    {
 
1061
      return(1);
 
1062
    }
 
1063
 
 
1064
    /*
 
1065
      We don't have to store rows in temp table that doesn't match HAVING if:
 
1066
      - we are sorting the table and writing complete group rows to the
 
1067
        temp table.
 
1068
      - We are using DISTINCT without resolving the distinct as a GROUP BY
 
1069
        on all columns.
 
1070
 
 
1071
      If having is not handled here, it will be checked before the row
 
1072
      is sent to the client.
 
1073
    */
 
1074
    if (tmp_having && (sort_and_group || (exec_tmp_table1->distinct && !group_list)))
 
1075
      having= tmp_having;
 
1076
 
 
1077
    /* if group or order on first table, sort first */
 
1078
    if (group_list && simple_group)
 
1079
    {
 
1080
      session->set_proc_info("Sorting for group");
 
1081
      if (create_sort_index(session, this, group_list,
 
1082
          HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1083
          alloc_group_fields(this, group_list) ||
 
1084
          make_sum_func_list(all_fields, fields_list, 1) ||
 
1085
          setup_sum_funcs(session, sum_funcs))
 
1086
      {
 
1087
        return(1);
 
1088
      }
 
1089
      group_list=0;
 
1090
    }
 
1091
    else
 
1092
    {
 
1093
      if (make_sum_func_list(all_fields, fields_list, 0) ||
 
1094
          setup_sum_funcs(session, sum_funcs))
 
1095
      {
 
1096
        return(1);
 
1097
      }
 
1098
 
 
1099
      if (!group_list && ! exec_tmp_table1->distinct && order && simple_order)
 
1100
      {
 
1101
        session->set_proc_info("Sorting for order");
 
1102
        if (create_sort_index(session, this, order,
 
1103
                              HA_POS_ERROR, HA_POS_ERROR, true))
 
1104
        {
 
1105
          return(1);
 
1106
        }
 
1107
        order=0;
 
1108
      }
 
1109
    }
 
1110
 
 
1111
    /*
 
1112
      Optimize distinct when used on some of the tables
 
1113
      SELECT DISTINCT t1.a FROM t1,t2 WHERE t1.b=t2.b
 
1114
      In this case we can stop scanning t2 when we have found one t1.a
 
1115
    */
 
1116
 
 
1117
    if (exec_tmp_table1->distinct)
 
1118
    {
 
1119
      table_map used_tables= session->used_tables;
 
1120
      JOIN_TAB *last_join_tab= join_tab+tables-1;
 
1121
      do
 
1122
      {
 
1123
        if (used_tables & last_join_tab->table->map)
 
1124
          break;
 
1125
        last_join_tab->not_used_in_distinct=1;
 
1126
      } while (last_join_tab-- != join_tab);
 
1127
      /* Optimize "select distinct b from t1 order by key_part_1 limit #" */
 
1128
      if (order && skip_sort_order)
 
1129
      {
 
1130
        /* Should always succeed */
 
1131
        if (test_if_skip_sort_order(&join_tab[const_tables],
 
1132
                  order, unit->select_limit_cnt, 0,
 
1133
                                          &join_tab[const_tables].table->
 
1134
                                            keys_in_use_for_order_by))
 
1135
          order= 0;
 
1136
      }
 
1137
    }
 
1138
 
 
1139
    /*
 
1140
      If this join belongs to an uncacheable subquery save
 
1141
      the original join
 
1142
    */
 
1143
    if (select_lex->uncacheable && !is_top_level_join() &&
 
1144
        init_save_join_tab())
 
1145
      return(-1);                         /* purecov: inspected */
 
1146
  }
 
1147
 
 
1148
  error= 0;
 
1149
  return(0);
 
1150
}
 
1151
 
 
1152
/**
 
1153
  Restore values in temporary join.
 
1154
*/
 
1155
void JOIN::restore_tmp()
 
1156
{
 
1157
  memcpy(tmp_join, this, (size_t) sizeof(JOIN));
 
1158
}
 
1159
 
 
1160
int JOIN::reinit()
 
1161
{
 
1162
  unit->offset_limit_cnt= (ha_rows)(select_lex->offset_limit ?
 
1163
                                    select_lex->offset_limit->val_uint() :
 
1164
                                    0UL);
 
1165
 
 
1166
  first_record= 0;
 
1167
 
 
1168
  if (exec_tmp_table1)
 
1169
  {
 
1170
    exec_tmp_table1->file->extra(HA_EXTRA_RESET_STATE);
 
1171
    exec_tmp_table1->file->ha_delete_all_rows();
 
1172
    free_io_cache(exec_tmp_table1);
 
1173
    filesort_free_buffers(exec_tmp_table1,0);
 
1174
  }
 
1175
  if (exec_tmp_table2)
 
1176
  {
 
1177
    exec_tmp_table2->file->extra(HA_EXTRA_RESET_STATE);
 
1178
    exec_tmp_table2->file->ha_delete_all_rows();
 
1179
    free_io_cache(exec_tmp_table2);
 
1180
    filesort_free_buffers(exec_tmp_table2,0);
 
1181
  }
 
1182
  if (items0)
 
1183
    set_items_ref_array(items0);
 
1184
 
 
1185
  if (join_tab_save)
 
1186
    memcpy(join_tab, join_tab_save, sizeof(JOIN_TAB) * tables);
 
1187
 
 
1188
  if (tmp_join)
 
1189
    restore_tmp();
 
1190
 
 
1191
  /* Reset of sum functions */
 
1192
  if (sum_funcs)
 
1193
  {
 
1194
    Item_sum *func, **func_ptr= sum_funcs;
 
1195
    while ((func= *(func_ptr++)))
 
1196
      func->clear();
 
1197
  }
 
1198
 
 
1199
  return(0);
 
1200
}
 
1201
 
 
1202
/**
 
1203
   @brief Save the original join layout
 
1204
 
 
1205
   @details Saves the original join layout so it can be reused in
 
1206
   re-execution and for EXPLAIN.
 
1207
 
 
1208
   @return Operation status
 
1209
   @retval 0      success.
 
1210
   @retval 1      error occurred.
 
1211
*/
 
1212
bool JOIN::init_save_join_tab()
 
1213
{
 
1214
  if (!(tmp_join= (JOIN*)session->alloc(sizeof(JOIN))))
 
1215
    return 1;                                  /* purecov: inspected */
 
1216
  error= 0;              // Ensure that tmp_join.error= 0
 
1217
  restore_tmp();
 
1218
  return 0;
 
1219
}
 
1220
 
 
1221
bool JOIN::save_join_tab()
 
1222
{
 
1223
  if (!join_tab_save && select_lex->master_unit()->uncacheable)
 
1224
  {
 
1225
    if (!(join_tab_save= (JOIN_TAB*)session->memdup((unsigned char*) join_tab,
 
1226
            sizeof(JOIN_TAB) * tables)))
 
1227
      return 1;
 
1228
  }
 
1229
  return 0;
 
1230
}
 
1231
 
 
1232
/**
 
1233
  Exec select.
 
1234
 
 
1235
  @todo
 
1236
    Note, that create_sort_index calls test_if_skip_sort_order and may
 
1237
    finally replace sorting with index scan if there is a LIMIT clause in
 
1238
    the query.  It's never shown in EXPLAIN!
 
1239
 
 
1240
  @todo
 
1241
    When can we have here session->net.report_error not zero?
 
1242
*/
 
1243
void JOIN::exec()
 
1244
{
 
1245
  List<Item> *columns_list= &fields_list;
 
1246
  int      tmp_error;
 
1247
 
 
1248
  session->set_proc_info("executing");
 
1249
  error= 0;
 
1250
 
 
1251
  if (!tables_list && (tables || !select_lex->with_sum_func))
 
1252
  {                                           
 
1253
    /* Only test of functions */
 
1254
    if (select_options & SELECT_DESCRIBE)
 
1255
      select_describe(this, false, false, false, (zero_result_cause?zero_result_cause:"No tables used"));
 
1256
    else
 
1257
    {
 
1258
      result->send_fields(*columns_list, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
1259
      /*
 
1260
        We have to test for 'conds' here as the WHERE may not be constant
 
1261
        even if we don't have any tables for prepared statements or if
 
1262
        conds uses something like 'rand()'.
 
1263
      */
 
1264
      if (cond_value != Item::COND_FALSE &&
 
1265
          (!conds || conds->val_int()) &&
 
1266
          (!having || having->val_int()))
 
1267
      {
 
1268
        if (do_send_rows && result->send_data(fields_list))
 
1269
          error= 1;
 
1270
        else
 
1271
        {
 
1272
          error= (int) result->send_eof();
 
1273
          send_records= ((select_options & OPTION_FOUND_ROWS) ? 1 : session->sent_row_count);
 
1274
        }
 
1275
      }
 
1276
      else
 
1277
      {
 
1278
        error= (int) result->send_eof();
 
1279
        send_records= 0;
 
1280
      }
 
1281
    }
 
1282
    /* Single select (without union) always returns 0 or 1 row */
 
1283
    session->limit_found_rows= send_records;
 
1284
    session->examined_row_count= 0;
 
1285
    return;
 
1286
  }
 
1287
  /*
 
1288
    Don't reset the found rows count if there're no tables as
 
1289
    FOUND_ROWS() may be called. Never reset the examined row count here.
 
1290
    It must be accumulated from all join iterations of all join parts.
 
1291
  */
 
1292
  if (tables)
 
1293
    session->limit_found_rows= 0;
 
1294
 
 
1295
  if (zero_result_cause)
 
1296
  {
 
1297
    (void) return_zero_rows(this, result, select_lex->leaf_tables,
 
1298
                            *columns_list,
 
1299
          send_row_on_empty_set(),
 
1300
          select_options,
 
1301
          zero_result_cause,
 
1302
          having);
 
1303
    return;
 
1304
  }
 
1305
 
 
1306
  if ((this->select_lex->options & OPTION_SCHEMA_TABLE) && get_schema_tables_result(this, PROCESSED_BY_JOIN_EXEC))
 
1307
    return;
 
1308
 
 
1309
  if (select_options & SELECT_DESCRIBE)
 
1310
  {
 
1311
    /*
 
1312
      Check if we managed to optimize order_st BY away and don't use temporary
 
1313
      table to resolve order_st BY: in that case, we only may need to do
 
1314
      filesort for GROUP BY.
 
1315
    */
 
1316
    if (!order && !no_order && (!skip_sort_order || !need_tmp))
 
1317
    {
 
1318
      /* Reset 'order' to 'group_list' and reinit variables describing 'order' */
 
1319
      order= group_list;
 
1320
      simple_order= simple_group;
 
1321
      skip_sort_order= 0;
 
1322
    }
 
1323
    if (order && (order != group_list || !(select_options & SELECT_BIG_RESULT)))
 
1324
    {
 
1325
      if (const_tables == tables 
 
1326
        || ((simple_order || skip_sort_order) 
 
1327
          && test_if_skip_sort_order(&join_tab[const_tables], order, select_limit, 0, &join_tab[const_tables].table->keys_in_use_for_query)))
 
1328
      order= 0;
 
1329
    }
 
1330
    having= tmp_having;
 
1331
    select_describe(this, need_tmp, order != 0 && !skip_sort_order,  select_distinct, !tables ? "No tables used" : NULL);
 
1332
    return;
 
1333
  }
 
1334
 
 
1335
  JOIN *curr_join= this;
 
1336
  List<Item> *curr_all_fields= &all_fields;
 
1337
  List<Item> *curr_fields_list= &fields_list;
 
1338
  Table *curr_tmp_table= 0;
 
1339
  /*
 
1340
    Initialize examined rows here because the values from all join parts
 
1341
    must be accumulated in examined_row_count. Hence every join
 
1342
    iteration must count from zero.
 
1343
  */
 
1344
  curr_join->examined_rows= 0;
 
1345
 
 
1346
  /* Create a tmp table if distinct or if the sort is too complicated */
 
1347
  if (need_tmp)
 
1348
  {
 
1349
    if (tmp_join)
 
1350
    {
 
1351
      /*
 
1352
        We are in a non cacheable sub query. Get the saved join structure
 
1353
        after optimization.
 
1354
        (curr_join may have been modified during last exection and we need
 
1355
        to reset it)
 
1356
      */
 
1357
      curr_join= tmp_join;
 
1358
    }
 
1359
    curr_tmp_table= exec_tmp_table1;
 
1360
 
 
1361
    /* Copy data to the temporary table */
 
1362
    session->set_proc_info("Copying to tmp table");
 
1363
    if (! curr_join->sort_and_group && curr_join->const_tables != curr_join->tables)
 
1364
      curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
1365
    if ((tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1366
    {
 
1367
      error= tmp_error;
 
1368
      return;
 
1369
    }
 
1370
    curr_tmp_table->file->info(HA_STATUS_VARIABLE);
 
1371
 
 
1372
    if (curr_join->having)
 
1373
      curr_join->having= curr_join->tmp_having= 0; // Allready done
 
1374
 
 
1375
    /* Change sum_fields reference to calculated fields in tmp_table */
 
1376
    curr_join->all_fields= *curr_all_fields;
 
1377
    if (!items1)
 
1378
    {
 
1379
      items1= items0 + all_fields.elements;
 
1380
      if (sort_and_group || curr_tmp_table->group)
 
1381
      {
 
1382
        if (change_to_use_tmp_fields(session, items1,
 
1383
                  tmp_fields_list1, tmp_all_fields1,
 
1384
                  fields_list.elements, all_fields))
 
1385
          return;
 
1386
      }
 
1387
      else
 
1388
      {
 
1389
        if (change_refs_to_tmp_fields(session, items1,
 
1390
                    tmp_fields_list1, tmp_all_fields1,
 
1391
                    fields_list.elements, all_fields))
 
1392
          return;
 
1393
      }
 
1394
      curr_join->tmp_all_fields1= tmp_all_fields1;
 
1395
      curr_join->tmp_fields_list1= tmp_fields_list1;
 
1396
      curr_join->items1= items1;
 
1397
    }
 
1398
    curr_all_fields= &tmp_all_fields1;
 
1399
    curr_fields_list= &tmp_fields_list1;
 
1400
    curr_join->set_items_ref_array(items1);
 
1401
 
 
1402
    if (sort_and_group || curr_tmp_table->group)
 
1403
    {
 
1404
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count
 
1405
                                             + curr_join->tmp_table_param.func_count;
 
1406
      curr_join->tmp_table_param.sum_func_count= 0;
 
1407
      curr_join->tmp_table_param.func_count= 0;
 
1408
    }
 
1409
    else
 
1410
    {
 
1411
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.func_count;
 
1412
      curr_join->tmp_table_param.func_count= 0;
 
1413
    }
 
1414
 
 
1415
    if (curr_tmp_table->group)
 
1416
    {           // Already grouped
 
1417
      if (!curr_join->order && !curr_join->no_order && !skip_sort_order)
 
1418
        curr_join->order= curr_join->group_list;  /* order by group */
 
1419
      curr_join->group_list= 0;
 
1420
    }
 
1421
 
 
1422
    /*
 
1423
      If we have different sort & group then we must sort the data by group
 
1424
      and copy it to another tmp table
 
1425
      This code is also used if we are using distinct something
 
1426
      we haven't been able to store in the temporary table yet
 
1427
      like SEC_TO_TIME(SUM(...)).
 
1428
    */
 
1429
 
 
1430
    if ((curr_join->group_list && (!test_if_subpart(curr_join->group_list, curr_join->order) || curr_join->select_distinct)) 
 
1431
        || (curr_join->select_distinct && curr_join->tmp_table_param.using_indirect_summary_function))
 
1432
    {         /* Must copy to another table */
 
1433
      /* Free first data from old join */
 
1434
      curr_join->join_free();
 
1435
      if (make_simple_join(curr_join, curr_tmp_table))
 
1436
        return;
 
1437
      calc_group_buffer(curr_join, group_list);
 
1438
      count_field_types(select_lex, &curr_join->tmp_table_param,
 
1439
      curr_join->tmp_all_fields1,
 
1440
      curr_join->select_distinct && !curr_join->group_list);
 
1441
      curr_join->tmp_table_param.hidden_field_count= curr_join->tmp_all_fields1.elements
 
1442
                                                   - curr_join->tmp_fields_list1.elements;
 
1443
 
 
1444
      if (exec_tmp_table2)
 
1445
        curr_tmp_table= exec_tmp_table2;
 
1446
      else
 
1447
      {
 
1448
        /* group data to new table */
 
1449
 
 
1450
        /*
 
1451
          If the access method is loose index scan then all MIN/MAX
 
1452
          functions are precomputed, and should be treated as regular
 
1453
          functions. See extended comment in JOIN::exec.
 
1454
        */
 
1455
        if (curr_join->join_tab->is_using_loose_index_scan())
 
1456
          curr_join->tmp_table_param.precomputed_group_by= true;
 
1457
 
 
1458
        if (!(curr_tmp_table=
 
1459
              exec_tmp_table2= create_tmp_table(session,
 
1460
                                                &curr_join->tmp_table_param,
 
1461
                                                *curr_all_fields,
 
1462
                                                (order_st*) 0,
 
1463
                                                curr_join->select_distinct &&
 
1464
                                                !curr_join->group_list,
 
1465
                                                1, curr_join->select_options,
 
1466
                                                HA_POS_ERROR,
 
1467
                                                (char *) "")))
 
1468
          return;
 
1469
        curr_join->exec_tmp_table2= exec_tmp_table2;
 
1470
      }
 
1471
      if (curr_join->group_list)
 
1472
      {
 
1473
        session->set_proc_info("Creating sort index");
 
1474
        if (curr_join->join_tab == join_tab && save_join_tab())
 
1475
        {
 
1476
          return;
 
1477
        }
 
1478
        if (create_sort_index(session, curr_join, curr_join->group_list,
 
1479
                  HA_POS_ERROR, HA_POS_ERROR, false) ||
 
1480
            make_group_fields(this, curr_join))
 
1481
        {
 
1482
          return;
 
1483
        }
 
1484
        sortorder= curr_join->sortorder;
 
1485
      }
 
1486
 
 
1487
      session->set_proc_info("Copying to group table");
 
1488
      tmp_error= -1;
 
1489
      if (curr_join != this)
 
1490
      {
 
1491
        if (sum_funcs2)
 
1492
        {
 
1493
          curr_join->sum_funcs= sum_funcs2;
 
1494
          curr_join->sum_funcs_end= sum_funcs_end2;
 
1495
        }
 
1496
        else
 
1497
        {
 
1498
          curr_join->alloc_func_list();
 
1499
          sum_funcs2= curr_join->sum_funcs;
 
1500
          sum_funcs_end2= curr_join->sum_funcs_end;
 
1501
        }
 
1502
      }
 
1503
      if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list, 1, true))
 
1504
        return;
 
1505
      curr_join->group_list= 0;
 
1506
 
 
1507
      if (!curr_join->sort_and_group && (curr_join->const_tables != curr_join->tables))
 
1508
        curr_join->join_tab[curr_join->const_tables].sorted= 0;
 
1509
      
 
1510
      if (setup_sum_funcs(curr_join->session, curr_join->sum_funcs) 
 
1511
        || (tmp_error= do_select(curr_join, (List<Item> *) 0, curr_tmp_table)))
 
1512
      {
 
1513
        error= tmp_error;
 
1514
        return;
 
1515
      }
 
1516
      end_read_record(&curr_join->join_tab->read_record);
 
1517
      curr_join->const_tables= curr_join->tables; // Mark free for cleanup()
 
1518
      curr_join->join_tab[0].table= 0;           // Table is freed
 
1519
 
 
1520
      // No sum funcs anymore
 
1521
      if (!items2)
 
1522
      {
 
1523
        items2= items1 + all_fields.elements;
 
1524
        if (change_to_use_tmp_fields(session, items2,
 
1525
                  tmp_fields_list2, tmp_all_fields2,
 
1526
                  fields_list.elements, tmp_all_fields1))
 
1527
          return;
 
1528
        curr_join->tmp_fields_list2= tmp_fields_list2;
 
1529
        curr_join->tmp_all_fields2= tmp_all_fields2;
 
1530
      }
 
1531
      curr_fields_list= &curr_join->tmp_fields_list2;
 
1532
      curr_all_fields= &curr_join->tmp_all_fields2;
 
1533
      curr_join->set_items_ref_array(items2);
 
1534
      curr_join->tmp_table_param.field_count+= curr_join->tmp_table_param.sum_func_count;
 
1535
      curr_join->tmp_table_param.sum_func_count= 0;
 
1536
    }
 
1537
    if (curr_tmp_table->distinct)
 
1538
      curr_join->select_distinct=0;   /* Each row is unique */
 
1539
 
 
1540
    curr_join->join_free();     /* Free quick selects */
 
1541
    if (curr_join->select_distinct && ! curr_join->group_list)
 
1542
    {
 
1543
      session->set_proc_info("Removing duplicates");
 
1544
      if (curr_join->tmp_having)
 
1545
        curr_join->tmp_having->update_used_tables();
 
1546
 
 
1547
      if (remove_duplicates(curr_join, curr_tmp_table,
 
1548
          *curr_fields_list, curr_join->tmp_having))
 
1549
        return;
 
1550
      
 
1551
      curr_join->tmp_having=0;
 
1552
      curr_join->select_distinct=0;
 
1553
    }
 
1554
    curr_tmp_table->reginfo.lock_type= TL_UNLOCK;
 
1555
    if (make_simple_join(curr_join, curr_tmp_table))
 
1556
      return;
 
1557
    calc_group_buffer(curr_join, curr_join->group_list);
 
1558
    count_field_types(select_lex, &curr_join->tmp_table_param, *curr_all_fields, 0);
 
1559
 
 
1560
  }
 
1561
 
 
1562
  if (curr_join->group || curr_join->tmp_table_param.sum_func_count)
 
1563
  {
 
1564
    if (make_group_fields(this, curr_join))
 
1565
      return;
 
1566
 
 
1567
    if (! items3)
 
1568
    {
 
1569
      if (! items0)
 
1570
        init_items_ref_array();
 
1571
      items3= ref_pointer_array + (all_fields.elements*4);
 
1572
      setup_copy_fields(session, &curr_join->tmp_table_param,
 
1573
      items3, tmp_fields_list3, tmp_all_fields3,
 
1574
      curr_fields_list->elements, *curr_all_fields);
 
1575
      tmp_table_param.save_copy_funcs= curr_join->tmp_table_param.copy_funcs;
 
1576
      tmp_table_param.save_copy_field= curr_join->tmp_table_param.copy_field;
 
1577
      tmp_table_param.save_copy_field_end= curr_join->tmp_table_param.copy_field_end;
 
1578
      curr_join->tmp_all_fields3= tmp_all_fields3;
 
1579
      curr_join->tmp_fields_list3= tmp_fields_list3;
 
1580
    }
 
1581
    else
 
1582
    {
 
1583
      curr_join->tmp_table_param.copy_funcs= tmp_table_param.save_copy_funcs;
 
1584
      curr_join->tmp_table_param.copy_field= tmp_table_param.save_copy_field;
 
1585
      curr_join->tmp_table_param.copy_field_end= tmp_table_param.save_copy_field_end;
 
1586
    }
 
1587
    curr_fields_list= &tmp_fields_list3;
 
1588
    curr_all_fields= &tmp_all_fields3;
 
1589
    curr_join->set_items_ref_array(items3);
 
1590
 
 
1591
    if (curr_join->make_sum_func_list(*curr_all_fields, *curr_fields_list,
 
1592
              1, true) ||
 
1593
        setup_sum_funcs(curr_join->session, curr_join->sum_funcs) ||
 
1594
        session->is_fatal_error)
 
1595
      return;
 
1596
  }
 
1597
  if (curr_join->group_list || curr_join->order)
 
1598
  {
 
1599
    session->set_proc_info("Sorting result");
 
1600
    /* If we have already done the group, add HAVING to sorted table */
 
1601
    if (curr_join->tmp_having && ! curr_join->group_list && ! curr_join->sort_and_group)
 
1602
    {
 
1603
      // Some tables may have been const
 
1604
      curr_join->tmp_having->update_used_tables();
 
1605
      JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables];
 
1606
      table_map used_tables= (curr_join->const_table_map |
 
1607
            curr_table->table->map);
 
1608
 
 
1609
      Item* sort_table_cond= make_cond_for_table(curr_join->tmp_having, used_tables, used_tables, 0);
 
1610
      if (sort_table_cond)
 
1611
      {
 
1612
        if (!curr_table->select)
 
1613
          if (!(curr_table->select= new SQL_SELECT))
 
1614
            return;
 
1615
        if (!curr_table->select->cond)
 
1616
          curr_table->select->cond= sort_table_cond;
 
1617
        else          // This should never happen
 
1618
        {
 
1619
          if (!(curr_table->select->cond=
 
1620
          new Item_cond_and(curr_table->select->cond,
 
1621
                sort_table_cond)))
 
1622
            return;
 
1623
          /*
 
1624
            Item_cond_and do not need fix_fields for execution, its parameters
 
1625
            are fixed or do not need fix_fields, too
 
1626
          */
 
1627
          curr_table->select->cond->quick_fix_field();
 
1628
        }
 
1629
        curr_table->select_cond= curr_table->select->cond;
 
1630
        curr_table->select_cond->top_level_item();
 
1631
        curr_join->tmp_having= make_cond_for_table(curr_join->tmp_having,
 
1632
                    ~ (table_map) 0,
 
1633
                    ~used_tables, 0);
 
1634
      }
 
1635
    }
 
1636
    {
 
1637
      if (group)
 
1638
        curr_join->select_limit= HA_POS_ERROR;
 
1639
      else
 
1640
      {
 
1641
        /*
 
1642
          We can abort sorting after session->select_limit rows if we there is no
 
1643
          WHERE clause for any tables after the sorted one.
 
1644
        */
 
1645
        JOIN_TAB *curr_table= &curr_join->join_tab[curr_join->const_tables+1];
 
1646
        JOIN_TAB *end_table= &curr_join->join_tab[curr_join->tables];
 
1647
        for (; curr_table < end_table ; curr_table++)
 
1648
        {
 
1649
          /*
 
1650
            table->keyuse is set in the case there was an original WHERE clause
 
1651
            on the table that was optimized away.
 
1652
          */
 
1653
          if (curr_table->select_cond ||
 
1654
              (curr_table->keyuse && !curr_table->first_inner))
 
1655
          {
 
1656
            /* We have to sort all rows */
 
1657
            curr_join->select_limit= HA_POS_ERROR;
 
1658
            break;
 
1659
          }
 
1660
        }
 
1661
      }
 
1662
      if (curr_join->join_tab == join_tab && save_join_tab())
 
1663
        return;
 
1664
      /*
 
1665
        Here we sort rows for order_st BY/GROUP BY clause, if the optimiser
 
1666
        chose FILESORT to be faster than INDEX SCAN or there is no
 
1667
        suitable index present.
 
1668
        Note, that create_sort_index calls test_if_skip_sort_order and may
 
1669
        finally replace sorting with index scan if there is a LIMIT clause in
 
1670
        the query. XXX: it's never shown in EXPLAIN!
 
1671
        OPTION_FOUND_ROWS supersedes LIMIT and is taken into account.
 
1672
      */
 
1673
      if (create_sort_index(session, curr_join,
 
1674
          curr_join->group_list ?
 
1675
          curr_join->group_list : curr_join->order,
 
1676
          curr_join->select_limit,
 
1677
          (select_options & OPTION_FOUND_ROWS ?
 
1678
           HA_POS_ERROR : unit->select_limit_cnt),
 
1679
                            curr_join->group_list ? true : false))
 
1680
        return;
 
1681
 
 
1682
      sortorder= curr_join->sortorder;
 
1683
      if (curr_join->const_tables != curr_join->tables &&
 
1684
          !curr_join->join_tab[curr_join->const_tables].table->sort.io_cache)
 
1685
      {
 
1686
        /*
 
1687
          If no IO cache exists for the first table then we are using an
 
1688
          INDEX SCAN and no filesort. Thus we should not remove the sorted
 
1689
          attribute on the INDEX SCAN.
 
1690
        */
 
1691
        skip_sort_order= 1;
 
1692
      }
 
1693
    }
 
1694
  }
 
1695
  /* XXX: When can we have here session->is_error() not zero? */
 
1696
  if (session->is_error())
 
1697
  {
 
1698
    error= session->is_error();
 
1699
    return;
 
1700
  }
 
1701
  curr_join->having= curr_join->tmp_having;
 
1702
  curr_join->fields= curr_fields_list;
 
1703
 
 
1704
  session->set_proc_info("Sending data");
 
1705
  result->send_fields(*curr_fields_list,
 
1706
                      Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF);
 
1707
  error= do_select(curr_join, curr_fields_list, NULL);
 
1708
  session->limit_found_rows= curr_join->send_records;
 
1709
 
 
1710
  /* Accumulate the counts from all join iterations of all join parts. */
 
1711
  session->examined_row_count+= curr_join->examined_rows;
 
1712
 
 
1713
  /*
 
1714
    With EXPLAIN EXTENDED we have to restore original ref_array
 
1715
    for a derived table which is always materialized.
 
1716
    Otherwise we would not be able to print the query  correctly.
 
1717
  */
 
1718
  if (items0 && (session->lex->describe & DESCRIBE_EXTENDED) && select_lex->linkage == DERIVED_TABLE_TYPE)
 
1719
    set_items_ref_array(items0);
 
1720
 
 
1721
  return;
 
1722
}
 
1723
 
 
1724
/**
 
1725
  Clean up join.
 
1726
 
 
1727
  @return
 
1728
    Return error that hold JOIN.
 
1729
*/
 
1730
int JOIN::destroy()
 
1731
{
 
1732
  select_lex->join= 0;
 
1733
 
 
1734
  if (tmp_join)
 
1735
  {
 
1736
    if (join_tab != tmp_join->join_tab)
 
1737
    {
 
1738
      JOIN_TAB *tab, *end;
 
1739
      for (tab= join_tab, end= tab+tables ; tab != end ; tab++)
 
1740
        tab->cleanup();
 
1741
    }
 
1742
    tmp_join->tmp_join= 0;
 
1743
    tmp_table_param.copy_field=0;
 
1744
    return(tmp_join->destroy());
 
1745
  }
 
1746
  cond_equal= 0;
 
1747
 
 
1748
  cleanup(1);
 
1749
  if (exec_tmp_table1)
 
1750
    exec_tmp_table1->free_tmp_table(session);
 
1751
  if (exec_tmp_table2)
 
1752
    exec_tmp_table2->free_tmp_table(session);
 
1753
  delete select;
 
1754
  delete_dynamic(&keyuse);
 
1755
  return(error);
 
1756
}
 
1757
 
 
1758
/*
 
1759
  Convert candidate subquery predicates to semi-joins
 
1760
 
 
1761
  SYNOPSIS
 
1762
    JOIN::flatten_subqueries()
 
1763
 
 
1764
  DESCRIPTION
 
1765
    Convert candidate subquery predicates to semi-joins.
 
1766
 
 
1767
  RETURN
 
1768
    false  OK
 
1769
    true   Error
 
1770
*/
 
1771
bool JOIN::flatten_subqueries()
 
1772
{
 
1773
  Item_in_subselect **in_subq;
 
1774
  Item_in_subselect **in_subq_end;
 
1775
 
 
1776
  if (sj_subselects.elements() == 0)
 
1777
    return(false);
 
1778
 
 
1779
  /* 1. Fix children subqueries */
 
1780
  for (in_subq= sj_subselects.front(), in_subq_end= sj_subselects.back();
 
1781
       in_subq != in_subq_end; in_subq++)
 
1782
  {
 
1783
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
1784
    child_join->outer_tables = child_join->tables;
 
1785
    if (child_join->flatten_subqueries())
 
1786
      return(true);
 
1787
    (*in_subq)->sj_convert_priority=
 
1788
      (*in_subq)->is_correlated * MAX_TABLES + child_join->outer_tables;
 
1789
  }
 
1790
  
 
1791
  bool outer_join_disable_semi_join= false;
 
1792
  /*
 
1793
   * Temporary measure: disable semi-joins when they are together with outer
 
1794
   * joins.
 
1795
   *
 
1796
   * @see LP Bug #314911
 
1797
   */
 
1798
  for (TableList *tbl= select_lex->leaf_tables; tbl; tbl=tbl->next_leaf)
 
1799
  {
 
1800
    TableList *embedding= tbl->embedding;
 
1801
    if (tbl->on_expr || (tbl->embedding && !(embedding->sj_on_expr && 
 
1802
                                            !embedding->embedding)))
 
1803
    {
 
1804
      in_subq= sj_subselects.front();
 
1805
      outer_join_disable_semi_join= true;
 
1806
    }
 
1807
  }
 
1808
 
 
1809
  if (! outer_join_disable_semi_join)
 
1810
  {
 
1811
    /*
 
1812
      2. Pick which subqueries to convert:
 
1813
        sort the subquery array
 
1814
        - prefer correlated subqueries over uncorrelated;
 
1815
        - prefer subqueries that have greater number of outer tables;
 
1816
    */
 
1817
    sj_subselects.sort(subq_sj_candidate_cmp);
 
1818
    // #tables-in-parent-query + #tables-in-subquery < MAX_TABLES
 
1819
    /* Replace all subqueries to be flattened with Item_int(1) */
 
1820
    for (in_subq= sj_subselects.front();
 
1821
        in_subq != in_subq_end &&
 
1822
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
1823
        in_subq++)
 
1824
    {
 
1825
      if (replace_where_subcondition(this, *in_subq, new Item_int(1), false))
 
1826
        return(true);
 
1827
    }
 
1828
 
 
1829
    for (in_subq= sj_subselects.front();
 
1830
        in_subq != in_subq_end &&
 
1831
        tables + ((*in_subq)->sj_convert_priority % MAX_TABLES) < MAX_TABLES;
 
1832
        in_subq++)
 
1833
    {
 
1834
      if (convert_subq_to_sj(this, *in_subq))
 
1835
        return(true);
 
1836
    }
 
1837
  }
 
1838
 
 
1839
  /* 3. Finalize those we didn't convert */
 
1840
  for (; in_subq!= in_subq_end; in_subq++)
 
1841
  {
 
1842
    JOIN *child_join= (*in_subq)->unit->first_select()->join;
 
1843
    Item_subselect::trans_res res;
 
1844
    (*in_subq)->changed= 0;
 
1845
    (*in_subq)->fixed= 0;
 
1846
    res= (*in_subq)->select_transformer(child_join);
 
1847
    if (res == Item_subselect::RES_ERROR)
 
1848
      return(true);
 
1849
 
 
1850
    (*in_subq)->changed= 1;
 
1851
    (*in_subq)->fixed= 1;
 
1852
 
 
1853
    Item *substitute= (*in_subq)->substitution;
 
1854
    bool do_fix_fields= !(*in_subq)->substitution->fixed;
 
1855
    if (replace_where_subcondition(this, *in_subq, substitute, do_fix_fields))
 
1856
      return(true);
 
1857
 
 
1858
    //if ((*in_subq)->fix_fields(session, (*in_subq)->ref_ptr))
 
1859
    //  return(true);
 
1860
  }
 
1861
  sj_subselects.clear();
 
1862
  return(false);
 
1863
}
 
1864
 
 
1865
/**
 
1866
  Setup for execution all subqueries of a query, for which the optimizer
 
1867
  chose hash semi-join.
 
1868
 
 
1869
  @details Iterate over all subqueries of the query, and if they are under an
 
1870
  IN predicate, and the optimizer chose to compute it via hash semi-join:
 
1871
  - try to initialize all data structures needed for the materialized execution
 
1872
    of the IN predicate,
 
1873
  - if this fails, then perform the IN=>EXISTS transformation which was
 
1874
    previously blocked during JOIN::prepare.
 
1875
 
 
1876
  This method is part of the "code generation" query processing phase.
 
1877
 
 
1878
  This phase must be called after substitute_for_best_equal_field() because
 
1879
  that function may replace items with other items from a multiple equality,
 
1880
  and we need to reference the correct items in the index access method of the
 
1881
  IN predicate.
 
1882
 
 
1883
  @return Operation status
 
1884
  @retval false     success.
 
1885
  @retval true      error occurred.
 
1886
*/
 
1887
bool JOIN::setup_subquery_materialization()
 
1888
{
 
1889
  for (Select_Lex_Unit *un= select_lex->first_inner_unit(); un;
 
1890
       un= un->next_unit())
 
1891
  {
 
1892
    for (Select_Lex *sl= un->first_select(); sl; sl= sl->next_select())
 
1893
    {
 
1894
      Item_subselect *subquery_predicate= sl->master_unit()->item;
 
1895
      if (subquery_predicate &&
 
1896
          subquery_predicate->substype() == Item_subselect::IN_SUBS)
 
1897
      {
 
1898
        Item_in_subselect *in_subs= (Item_in_subselect*) subquery_predicate;
 
1899
        if (in_subs->exec_method == Item_in_subselect::MATERIALIZATION &&
 
1900
            in_subs->setup_engine())
 
1901
          return true;
 
1902
      }
 
1903
    }
 
1904
  }
 
1905
  return false;
 
1906
}
 
1907
 
 
1908
/**
 
1909
  Partially cleanup JOIN after it has executed: close index or rnd read
 
1910
  (table cursors), free quick selects.
 
1911
 
 
1912
    This function is called in the end of execution of a JOIN, before the used
 
1913
    tables are unlocked and closed.
 
1914
 
 
1915
    For a join that is resolved using a temporary table, the first sweep is
 
1916
    performed against actual tables and an intermediate result is inserted
 
1917
    into the temprorary table.
 
1918
    The last sweep is performed against the temporary table. Therefore,
 
1919
    the base tables and associated buffers used to fill the temporary table
 
1920
    are no longer needed, and this function is called to free them.
 
1921
 
 
1922
    For a join that is performed without a temporary table, this function
 
1923
    is called after all rows are sent, but before EOF packet is sent.
 
1924
 
 
1925
    For a simple SELECT with no subqueries this function performs a full
 
1926
    cleanup of the JOIN and calls mysql_unlock_read_tables to free used base
 
1927
    tables.
 
1928
 
 
1929
    If a JOIN is executed for a subquery or if it has a subquery, we can't
 
1930
    do the full cleanup and need to do a partial cleanup only.
 
1931
    - If a JOIN is not the top level join, we must not unlock the tables
 
1932
    because the outer select may not have been evaluated yet, and we
 
1933
    can't unlock only selected tables of a query.
 
1934
    - Additionally, if this JOIN corresponds to a correlated subquery, we
 
1935
    should not free quick selects and join buffers because they will be
 
1936
    needed for the next execution of the correlated subquery.
 
1937
    - However, if this is a JOIN for a [sub]select, which is not
 
1938
    a correlated subquery itself, but has subqueries, we can free it
 
1939
    fully and also free JOINs of all its subqueries. The exception
 
1940
    is a subquery in SELECT list, e.g: @n
 
1941
    SELECT a, (select cmax(b) from t1) group by c @n
 
1942
    This subquery will not be evaluated at first sweep and its value will
 
1943
    not be inserted into the temporary table. Instead, it's evaluated
 
1944
    when selecting from the temporary table. Therefore, it can't be freed
 
1945
    here even though it's not correlated.
 
1946
 
 
1947
  @todo
 
1948
    Unlock tables even if the join isn't top level select in the tree
 
1949
*/
 
1950
void JOIN::join_free()
 
1951
{
 
1952
  Select_Lex_Unit *tmp_unit;
 
1953
  Select_Lex *sl;
 
1954
  /*
 
1955
    Optimization: if not EXPLAIN and we are done with the JOIN,
 
1956
    free all tables.
 
1957
  */
 
1958
  bool full= (!select_lex->uncacheable && !session->lex->describe);
 
1959
  bool can_unlock= full;
 
1960
 
 
1961
  cleanup(full);
 
1962
 
 
1963
  for (tmp_unit= select_lex->first_inner_unit();
 
1964
       tmp_unit;
 
1965
       tmp_unit= tmp_unit->next_unit())
 
1966
    for (sl= tmp_unit->first_select(); sl; sl= sl->next_select())
 
1967
    {
 
1968
      Item_subselect *subselect= sl->master_unit()->item;
 
1969
      bool full_local= full && (!subselect || subselect->is_evaluated());
 
1970
      /*
 
1971
        If this join is evaluated, we can fully clean it up and clean up all
 
1972
        its underlying joins even if they are correlated -- they will not be
 
1973
        used any more anyway.
 
1974
        If this join is not yet evaluated, we still must clean it up to
 
1975
        close its table cursors -- it may never get evaluated, as in case of
 
1976
        ... HAVING false OR a IN (SELECT ...))
 
1977
        but all table cursors must be closed before the unlock.
 
1978
      */
 
1979
      sl->cleanup_all_joins(full_local);
 
1980
      /* Can't unlock if at least one JOIN is still needed */
 
1981
      can_unlock= can_unlock && full_local;
 
1982
    }
 
1983
 
 
1984
  /*
 
1985
    We are not using tables anymore
 
1986
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
1987
  */
 
1988
  if (can_unlock && lock && session->lock &&
 
1989
      !(select_options & SELECT_NO_UNLOCK) &&
 
1990
      !select_lex->subquery_in_having &&
 
1991
      (select_lex == (session->lex->unit.fake_select_lex ?
 
1992
                      session->lex->unit.fake_select_lex : &session->lex->select_lex)))
 
1993
  {
 
1994
    /*
 
1995
      TODO: unlock tables even if the join isn't top level select in the
 
1996
      tree.
 
1997
    */
 
1998
    mysql_unlock_read_tables(session, lock);           // Don't free join->lock
 
1999
    lock= 0;
 
2000
  }
 
2001
 
 
2002
  return;
 
2003
}
 
2004
 
 
2005
 
 
2006
/**
 
2007
  Free resources of given join.
 
2008
 
 
2009
  @param fill   true if we should free all resources, call with full==1
 
2010
                should be last, before it this function can be called with
 
2011
                full==0
 
2012
 
 
2013
  @note
 
2014
    With subquery this function definitely will be called several times,
 
2015
    but even for simple query it can be called several times.
 
2016
*/
 
2017
void JOIN::cleanup(bool full)
 
2018
{
 
2019
  if (table)
 
2020
  {
 
2021
    JOIN_TAB *tab,*end;
 
2022
    /*
 
2023
      Only a sorted table may be cached.  This sorted table is always the
 
2024
      first non const table in join->table
 
2025
    */
 
2026
    if (tables > const_tables) // Test for not-const tables
 
2027
    {
 
2028
      free_io_cache(table[const_tables]);
 
2029
      filesort_free_buffers(table[const_tables],full);
 
2030
    }
 
2031
 
 
2032
    if (full)
 
2033
    {
 
2034
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
2035
        tab->cleanup();
 
2036
      table= 0;
 
2037
    }
 
2038
    else
 
2039
    {
 
2040
      for (tab= join_tab, end= tab+tables; tab != end; tab++)
 
2041
      {
 
2042
        if (tab->table)
 
2043
          tab->table->file->ha_index_or_rnd_end();
 
2044
      }
 
2045
    }
 
2046
    cleanup_sj_tmp_tables(this);//
 
2047
  }
 
2048
  /*
 
2049
    We are not using tables anymore
 
2050
    Unlock all tables. We may be in an INSERT .... SELECT statement.
 
2051
  */
 
2052
  if (full)
 
2053
  {
 
2054
    if (tmp_join)
 
2055
      tmp_table_param.copy_field= 0;
 
2056
    group_fields.delete_elements();
 
2057
    /*
 
2058
      We can't call delete_elements() on copy_funcs as this will cause
 
2059
      problems in free_elements() as some of the elements are then deleted.
 
2060
    */
 
2061
    tmp_table_param.copy_funcs.empty();
 
2062
    /*
 
2063
      If we have tmp_join and 'this' JOIN is not tmp_join and
 
2064
      tmp_table_param.copy_field's  of them are equal then we have to remove
 
2065
      pointer to  tmp_table_param.copy_field from tmp_join, because it qill
 
2066
      be removed in tmp_table_param.cleanup().
 
2067
    */
 
2068
    if (tmp_join &&
 
2069
        tmp_join != this &&
 
2070
        tmp_join->tmp_table_param.copy_field ==
 
2071
        tmp_table_param.copy_field)
 
2072
    {
 
2073
      tmp_join->tmp_table_param.copy_field=
 
2074
        tmp_join->tmp_table_param.save_copy_field= 0;
 
2075
    }
 
2076
    tmp_table_param.cleanup();
 
2077
  }
 
2078
  return;
 
2079
}
 
2080
 
 
2081
/*
 
2082
  used only in JOIN::clear
 
2083
*/
 
2084
static void clear_tables(JOIN *join)
 
2085
{
 
2086
  /*
 
2087
    must clear only the non-const tables, as const tables
 
2088
    are not re-calculated.
 
2089
  */
 
2090
  for (uint32_t i= join->const_tables; i < join->tables; i++)
 
2091
    join->table[i]->mark_as_null_row();   // All fields are NULL
 
2092
}
 
2093
 
 
2094
/**
 
2095
  Make an array of pointers to sum_functions to speed up
 
2096
  sum_func calculation.
 
2097
 
 
2098
  @retval
 
2099
    0 ok
 
2100
  @retval
 
2101
    1 Error
 
2102
*/
 
2103
bool JOIN::alloc_func_list()
 
2104
{
 
2105
  uint32_t func_count, group_parts;
 
2106
 
 
2107
  func_count= tmp_table_param.sum_func_count;
 
2108
  /*
 
2109
    If we are using rollup, we need a copy of the summary functions for
 
2110
    each level
 
2111
  */
 
2112
  if (rollup.state != ROLLUP::STATE_NONE)
 
2113
    func_count*= (send_group_parts+1);
 
2114
 
 
2115
  group_parts= send_group_parts;
 
2116
  /*
 
2117
    If distinct, reserve memory for possible
 
2118
    disctinct->group_by optimization
 
2119
  */
 
2120
  if (select_distinct)
 
2121
  {
 
2122
    group_parts+= fields_list.elements;
 
2123
    /*
 
2124
      If the order_st clause is specified then it's possible that
 
2125
      it also will be optimized, so reserve space for it too
 
2126
    */
 
2127
    if (order)
 
2128
    {
 
2129
      order_st *ord;
 
2130
      for (ord= order; ord; ord= ord->next)
 
2131
        group_parts++;
 
2132
    }
 
2133
  }
 
2134
 
 
2135
  /* This must use calloc() as rollup_make_fields depends on this */
 
2136
  sum_funcs= (Item_sum**) session->calloc(sizeof(Item_sum**) * (func_count+1) +
 
2137
              sizeof(Item_sum***) * (group_parts+1));
 
2138
  sum_funcs_end= (Item_sum***) (sum_funcs+func_count+1);
 
2139
  return(sum_funcs == 0);
 
2140
}
 
2141
 
 
2142
/**
 
2143
  Initialize 'sum_funcs' array with all Item_sum objects.
 
2144
 
 
2145
  @param field_list        All items
 
2146
  @param send_fields       Items in select list
 
2147
  @param before_group_by   Set to 1 if this is called before GROUP BY handling
 
2148
  @param recompute         Set to true if sum_funcs must be recomputed
 
2149
 
 
2150
  @retval
 
2151
    0  ok
 
2152
  @retval
 
2153
    1  error
 
2154
*/
 
2155
bool JOIN::make_sum_func_list(List<Item> &field_list, 
 
2156
                              List<Item> &send_fields,
 
2157
                              bool before_group_by, 
 
2158
                              bool recompute)
 
2159
{
 
2160
  List_iterator_fast<Item> it(field_list);
 
2161
  Item_sum **func;
 
2162
  Item *item;
 
2163
 
 
2164
  if (*sum_funcs && !recompute)
 
2165
    return(false); /* We have already initialized sum_funcs. */
 
2166
 
 
2167
  func= sum_funcs;
 
2168
  while ((item=it++))
 
2169
  {
 
2170
    if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
2171
        (!((Item_sum*) item)->depended_from() ||
 
2172
         ((Item_sum *)item)->depended_from() == select_lex))
 
2173
      *func++= (Item_sum*) item;
 
2174
  }
 
2175
  if (before_group_by && rollup.state == ROLLUP::STATE_INITED)
 
2176
  {
 
2177
    rollup.state= ROLLUP::STATE_READY;
 
2178
    if (rollup_make_fields(field_list, send_fields, &func))
 
2179
      return(true);     // Should never happen
 
2180
  }
 
2181
  else if (rollup.state == ROLLUP::STATE_NONE)
 
2182
  {
 
2183
    for (uint32_t i=0 ; i <= send_group_parts ;i++)
 
2184
      sum_funcs_end[i]= func;
 
2185
  }
 
2186
  else if (rollup.state == ROLLUP::STATE_READY)
 
2187
    return(false);                         // Don't put end marker
 
2188
  *func=0;          // End marker
 
2189
  return(false);
 
2190
}
 
2191
 
 
2192
/** Allocate memory needed for other rollup functions. */
 
2193
bool JOIN::rollup_init()
 
2194
{
 
2195
  uint32_t i,j;
 
2196
  Item **ref_array;
 
2197
 
 
2198
  tmp_table_param.quick_group= 0; // Can't create groups in tmp table
 
2199
  rollup.state= ROLLUP::STATE_INITED;
 
2200
 
 
2201
  /*
 
2202
    Create pointers to the different sum function groups
 
2203
    These are updated by rollup_make_fields()
 
2204
  */
 
2205
  tmp_table_param.group_parts= send_group_parts;
 
2206
 
 
2207
  if (!(rollup.null_items= (Item_null_result**) session->alloc((sizeof(Item*) +
 
2208
                                                sizeof(Item**) +
 
2209
                                                sizeof(List<Item>) +
 
2210
                        ref_pointer_array_size)
 
2211
                        * send_group_parts )))
 
2212
    return 1;
 
2213
 
 
2214
  rollup.fields= (List<Item>*) (rollup.null_items + send_group_parts);
 
2215
  rollup.ref_pointer_arrays= (Item***) (rollup.fields + send_group_parts);
 
2216
  ref_array= (Item**) (rollup.ref_pointer_arrays+send_group_parts);
 
2217
 
 
2218
  /*
 
2219
    Prepare space for field list for the different levels
 
2220
    These will be filled up in rollup_make_fields()
 
2221
  */
 
2222
  for (i= 0 ; i < send_group_parts ; i++)
 
2223
  {
 
2224
    rollup.null_items[i]= new (session->mem_root) Item_null_result();
 
2225
    List<Item> *rollup_fields= &rollup.fields[i];
 
2226
    rollup_fields->empty();
 
2227
    rollup.ref_pointer_arrays[i]= ref_array;
 
2228
    ref_array+= all_fields.elements;
 
2229
  }
 
2230
  for (i= 0 ; i < send_group_parts; i++)
 
2231
  {
 
2232
    for (j=0 ; j < fields_list.elements ; j++)
 
2233
      rollup.fields[i].push_back(rollup.null_items[i]);
 
2234
  }
 
2235
  List_iterator<Item> it(all_fields);
 
2236
  Item *item;
 
2237
  while ((item= it++))
 
2238
  {
 
2239
    order_st *group_tmp;
 
2240
    bool found_in_group= 0;
 
2241
 
 
2242
    for (group_tmp= group_list; group_tmp; group_tmp= group_tmp->next)
 
2243
    {
 
2244
      if (*group_tmp->item == item)
 
2245
      {
 
2246
        item->maybe_null= 1;
 
2247
        found_in_group= 1;
 
2248
        if (item->const_item())
 
2249
        {
 
2250
          /*
 
2251
            For ROLLUP queries each constant item referenced in GROUP BY list
 
2252
            is wrapped up into an Item_func object yielding the same value
 
2253
            as the constant item. The objects of the wrapper class are never
 
2254
            considered as constant items and besides they inherit all
 
2255
            properties of the Item_result_field class.
 
2256
            This wrapping allows us to ensure writing constant items
 
2257
            into temporary tables whenever the result of the ROLLUP
 
2258
            operation has to be written into a temporary table, e.g. when
 
2259
            ROLLUP is used together with DISTINCT in the SELECT list.
 
2260
            Usually when creating temporary tables for a intermidiate
 
2261
            result we do not include fields for constant expressions.
 
2262
          */
 
2263
          Item* new_item= new Item_func_rollup_const(item);
 
2264
          if (!new_item)
 
2265
            return 1;
 
2266
          new_item->fix_fields(session, (Item **) 0);
 
2267
          session->change_item_tree(it.ref(), new_item);
 
2268
          for (order_st *tmp= group_tmp; tmp; tmp= tmp->next)
 
2269
          {
 
2270
            if (*tmp->item == item)
 
2271
              session->change_item_tree(tmp->item, new_item);
 
2272
          }
 
2273
        }
 
2274
      }
 
2275
    }
 
2276
    if (item->type() == Item::FUNC_ITEM && !found_in_group)
 
2277
    {
 
2278
      bool changed= false;
 
2279
      if (change_group_ref(session, (Item_func *) item, group_list, &changed))
 
2280
        return 1;
 
2281
      /*
 
2282
        We have to prevent creation of a field in a temporary table for
 
2283
        an expression that contains GROUP BY attributes.
 
2284
        Marking the expression item as 'with_sum_func' will ensure this.
 
2285
      */
 
2286
      if (changed)
 
2287
        item->with_sum_func= 1;
 
2288
    }
 
2289
  }
 
2290
  return 0;
 
2291
}
 
2292
 
 
2293
/**
 
2294
  Fill up rollup structures with pointers to fields to use.
 
2295
 
 
2296
  Creates copies of item_sum items for each sum level.
 
2297
 
 
2298
  @param fields_arg   List of all fields (hidden and real ones)
 
2299
  @param sel_fields   Pointer to selected fields
 
2300
  @param func     Store here a pointer to all fields
 
2301
 
 
2302
  @retval
 
2303
    0 if ok;
 
2304
    In this case func is pointing to next not used element.
 
2305
  @retval
 
2306
    1    on error
 
2307
*/
 
2308
bool JOIN::rollup_make_fields(List<Item> &fields_arg, List<Item> &sel_fields, Item_sum ***func)
 
2309
{
 
2310
  List_iterator_fast<Item> it(fields_arg);
 
2311
  Item *first_field= sel_fields.head();
 
2312
  uint32_t level;
 
2313
 
 
2314
  /*
 
2315
    Create field lists for the different levels
 
2316
 
 
2317
    The idea here is to have a separate field list for each rollup level to
 
2318
    avoid all runtime checks of which columns should be NULL.
 
2319
 
 
2320
    The list is stored in reverse order to get sum function in such an order
 
2321
    in func that it makes it easy to reset them with init_sum_functions()
 
2322
 
 
2323
    Assuming:  SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
2324
 
 
2325
    rollup.fields[0] will contain list where a,b,c is NULL
 
2326
    rollup.fields[1] will contain list where b,c is NULL
 
2327
    ...
 
2328
    rollup.ref_pointer_array[#] points to fields for rollup.fields[#]
 
2329
    ...
 
2330
    sum_funcs_end[0] points to all sum functions
 
2331
    sum_funcs_end[1] points to all sum functions, except grand totals
 
2332
    ...
 
2333
  */
 
2334
 
 
2335
  for (level=0 ; level < send_group_parts ; level++)
 
2336
  {
 
2337
    uint32_t i;
 
2338
    uint32_t pos= send_group_parts - level -1;
 
2339
    bool real_fields= 0;
 
2340
    Item *item;
 
2341
    List_iterator<Item> new_it(rollup.fields[pos]);
 
2342
    Item **ref_array_start= rollup.ref_pointer_arrays[pos];
 
2343
    order_st *start_group;
 
2344
 
 
2345
    /* Point to first hidden field */
 
2346
    Item **ref_array= ref_array_start + fields_arg.elements-1;
 
2347
 
 
2348
    /* Remember where the sum functions ends for the previous level */
 
2349
    sum_funcs_end[pos+1]= *func;
 
2350
 
 
2351
    /* Find the start of the group for this level */
 
2352
    for (i= 0, start_group= group_list ;i++ < pos ;start_group= start_group->next)
 
2353
    {}
 
2354
 
 
2355
    it.rewind();
 
2356
    while ((item= it++))
 
2357
    {
 
2358
      if (item == first_field)
 
2359
      {
 
2360
        real_fields= 1;       // End of hidden fields
 
2361
        ref_array= ref_array_start;
 
2362
      }
 
2363
 
 
2364
      if (item->type() == Item::SUM_FUNC_ITEM && !item->const_item() &&
 
2365
          (!((Item_sum*) item)->depended_from() ||
 
2366
           ((Item_sum *)item)->depended_from() == select_lex))
 
2367
 
 
2368
      {
 
2369
        /*
 
2370
          This is a top level summary function that must be replaced with
 
2371
          a sum function that is reset for this level.
 
2372
 
 
2373
          NOTE: This code creates an object which is not that nice in a
 
2374
          sub select.  Fortunately it's not common to have rollup in
 
2375
          sub selects.
 
2376
        */
 
2377
        item= item->copy_or_same(session);
 
2378
        ((Item_sum*) item)->make_unique();
 
2379
        *(*func)= (Item_sum*) item;
 
2380
        (*func)++;
 
2381
      }
 
2382
      else
 
2383
      {
 
2384
        /* Check if this is something that is part of this group by */
 
2385
        order_st *group_tmp;
 
2386
        for (group_tmp= start_group, i= pos ;
 
2387
                  group_tmp ; group_tmp= group_tmp->next, i++)
 
2388
        {
 
2389
                if (*group_tmp->item == item)
 
2390
          {
 
2391
            /*
 
2392
              This is an element that is used by the GROUP BY and should be
 
2393
              set to NULL in this level
 
2394
            */
 
2395
                  Item_null_result *null_item= new (session->mem_root) Item_null_result();
 
2396
                  if (!null_item)
 
2397
                    return 1;
 
2398
            item->maybe_null= 1;    // Value will be null sometimes
 
2399
                  null_item->result_field= item->get_tmp_table_field();
 
2400
                  item= null_item;
 
2401
            break;
 
2402
          }
 
2403
        }
 
2404
      }
 
2405
      *ref_array= item;
 
2406
      if (real_fields)
 
2407
      {
 
2408
  (void) new_it++;      // Point to next item
 
2409
  new_it.replace(item);     // Replace previous
 
2410
  ref_array++;
 
2411
      }
 
2412
      else
 
2413
  ref_array--;
 
2414
    }
 
2415
  }
 
2416
  sum_funcs_end[0]= *func;      // Point to last function
 
2417
  return 0;
 
2418
}
 
2419
 
 
2420
/**
 
2421
  Send all rollup levels higher than the current one to the client.
 
2422
 
 
2423
  @b SAMPLE
 
2424
    @code
 
2425
      SELECT a, b, c SUM(b) FROM t1 GROUP BY a,b WITH ROLLUP
 
2426
  @endcode
 
2427
 
 
2428
  @param idx    Level we are on:
 
2429
                        - 0 = Total sum level
 
2430
                        - 1 = First group changed  (a)
 
2431
                        - 2 = Second group changed (a,b)
 
2432
 
 
2433
  @retval
 
2434
    0   ok
 
2435
  @retval
 
2436
    1   If send_data_failed()
 
2437
*/
 
2438
int JOIN::rollup_send_data(uint32_t idx)
 
2439
{
 
2440
  uint32_t i;
 
2441
  for (i= send_group_parts ; i-- > idx ; )
 
2442
  {
 
2443
    /* Get reference pointers to sum functions in place */
 
2444
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2445
     ref_pointer_array_size);
 
2446
    if ((!having || having->val_int()))
 
2447
    {
 
2448
      if (send_records < unit->select_limit_cnt && do_send_rows &&
 
2449
    result->send_data(rollup.fields[i]))
 
2450
  return 1;
 
2451
      send_records++;
 
2452
    }
 
2453
  }
 
2454
  /* Restore ref_pointer_array */
 
2455
  set_items_ref_array(current_ref_pointer_array);
 
2456
  return 0;
 
2457
}
 
2458
 
 
2459
/**
 
2460
  Write all rollup levels higher than the current one to a temp table.
 
2461
 
 
2462
  @b SAMPLE
 
2463
    @code
 
2464
      SELECT a, b, SUM(c) FROM t1 GROUP BY a,b WITH ROLLUP
 
2465
  @endcode
 
2466
 
 
2467
  @param idx                 Level we are on:
 
2468
                               - 0 = Total sum level
 
2469
                               - 1 = First group changed  (a)
 
2470
                               - 2 = Second group changed (a,b)
 
2471
  @param table               reference to temp table
 
2472
 
 
2473
  @retval
 
2474
    0   ok
 
2475
  @retval
 
2476
    1   if write_data_failed()
 
2477
*/
 
2478
int JOIN::rollup_write_data(uint32_t idx, Table *table_arg)
 
2479
{
 
2480
  uint32_t i;
 
2481
  for (i= send_group_parts ; i-- > idx ; )
 
2482
  {
 
2483
    /* Get reference pointers to sum functions in place */
 
2484
    memcpy(ref_pointer_array, rollup.ref_pointer_arrays[i],
 
2485
     ref_pointer_array_size);
 
2486
    if ((!having || having->val_int()))
 
2487
    {
 
2488
      int write_error;
 
2489
      Item *item;
 
2490
      List_iterator_fast<Item> it(rollup.fields[i]);
 
2491
      while ((item= it++))
 
2492
      {
 
2493
        if (item->type() == Item::NULL_ITEM && item->is_result_field())
 
2494
          item->save_in_result_field(1);
 
2495
      }
 
2496
      copy_sum_funcs(sum_funcs_end[i+1], sum_funcs_end[i]);
 
2497
      if ((write_error= table_arg->file->ha_write_row(table_arg->record[0])))
 
2498
      {
 
2499
  if (create_myisam_from_heap(session, table_arg,
 
2500
                                    tmp_table_param.start_recinfo,
 
2501
                                    &tmp_table_param.recinfo,
 
2502
                                    write_error, 0))
 
2503
    return 1;
 
2504
      }
 
2505
    }
 
2506
  }
 
2507
  /* Restore ref_pointer_array */
 
2508
  set_items_ref_array(current_ref_pointer_array);
 
2509
  return 0;
 
2510
}
 
2511
 
 
2512
/**
 
2513
  clear results if there are not rows found for group
 
2514
  (end_send_group/end_write_group)
 
2515
*/
 
2516
void JOIN::clear()
 
2517
{
 
2518
  clear_tables(this);
 
2519
  copy_fields(&tmp_table_param);
 
2520
 
 
2521
  if (sum_funcs)
 
2522
  {
 
2523
    Item_sum *func, **func_ptr= sum_funcs;
 
2524
    while ((func= *(func_ptr++)))
 
2525
      func->clear();
 
2526
  }
 
2527
}
 
2528
 
 
2529
/**
 
2530
  change select_result object of JOIN.
 
2531
 
 
2532
  @param res    new select_result object
 
2533
 
 
2534
  @retval
 
2535
    false   OK
 
2536
  @retval
 
2537
    true    error
 
2538
*/
 
2539
bool JOIN::change_result(select_result *res)
 
2540
{
 
2541
  result= res;
 
2542
  if (result->prepare(fields_list, select_lex->master_unit()))
 
2543
  {
 
2544
    return(true);
 
2545
  }
 
2546
  return(false);
 
2547
}
 
2548
 
 
2549
/**
 
2550
  Give error if we some tables are done with a full join.
 
2551
 
 
2552
  This is used by multi_table_update and multi_table_delete when running
 
2553
  in safe mode.
 
2554
 
 
2555
  @param join           Join condition
 
2556
 
 
2557
  @retval
 
2558
    0   ok
 
2559
  @retval
 
2560
    1   Error (full join used)
 
2561
*/
 
2562
bool error_if_full_join(JOIN *join)
 
2563
{
 
2564
  for (JOIN_TAB *tab= join->join_tab, *end= join->join_tab+join->tables; tab < end; tab++)
 
2565
  {
 
2566
    if (tab->type == JT_ALL && (!tab->select || !tab->select->quick))
 
2567
    {
 
2568
      my_message(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE,
 
2569
                 ER(ER_UPDATE_WITHOUT_KEY_IN_SAFE_MODE), MYF(0));
 
2570
      return(1);
 
2571
    }
 
2572
  }
 
2573
  return(0);
 
2574
}
 
2575
 
 
2576
/**
 
2577
  @brief
 
2578
  
 
2579
  Process one record of the nested loop join.
 
2580
 
 
2581
  @details 
 
2582
 
 
2583
  This function will evaluate parts of WHERE/ON clauses that are
 
2584
  applicable to the partial record on hand and in case of success
 
2585
  submit this record to the next level of the nested loop.
 
2586
*/
 
2587
enum_nested_loop_state evaluate_join_record(JOIN *join, JOIN_TAB *join_tab, int error)
 
2588
{
 
2589
  bool not_used_in_distinct= join_tab->not_used_in_distinct;
 
2590
  ha_rows found_records= join->found_records;
 
2591
  COND *select_cond= join_tab->select_cond;
 
2592
 
 
2593
  if (error > 0 || (join->session->is_error()))     // Fatal error
 
2594
    return NESTED_LOOP_ERROR;
 
2595
  if (error < 0)
 
2596
    return NESTED_LOOP_NO_MORE_ROWS;
 
2597
  if (join->session->killed)                    // Aborted by user
 
2598
  {
 
2599
    join->session->send_kill_message();
 
2600
    return NESTED_LOOP_KILLED;               /* purecov: inspected */
 
2601
  }
 
2602
  if (!select_cond || select_cond->val_int())
 
2603
  {
 
2604
    /*
 
2605
      There is no select condition or the attached pushed down
 
2606
      condition is true => a match is found.
 
2607
    */
 
2608
    bool found= 1;
 
2609
    while (join_tab->first_unmatched && found)
 
2610
    {
 
2611
      /*
 
2612
        The while condition is always false if join_tab is not
 
2613
        the last inner join table of an outer join operation.
 
2614
      */
 
2615
      JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
2616
      /*
 
2617
        Mark that a match for current outer table is found.
 
2618
        This activates push down conditional predicates attached
 
2619
        to the all inner tables of the outer join.
 
2620
      */
 
2621
      first_unmatched->found= 1;
 
2622
      for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
2623
      {
 
2624
        if (tab->table->reginfo.not_exists_optimize)
 
2625
          return NESTED_LOOP_NO_MORE_ROWS;
 
2626
        /* Check all predicates that has just been activated. */
 
2627
        /*
 
2628
          Actually all predicates non-guarded by first_unmatched->found
 
2629
          will be re-evaluated again. It could be fixed, but, probably,
 
2630
          it's not worth doing now.
 
2631
        */
 
2632
        if (tab->select_cond && !tab->select_cond->val_int())
 
2633
        {
 
2634
          /* The condition attached to table tab is false */
 
2635
          if (tab == join_tab)
 
2636
            found= 0;
 
2637
          else
 
2638
          {
 
2639
            /*
 
2640
              Set a return point if rejected predicate is attached
 
2641
              not to the last table of the current nest level.
 
2642
            */
 
2643
            join->return_tab= tab;
 
2644
            return NESTED_LOOP_OK;
 
2645
          }
 
2646
        }
 
2647
      }
 
2648
      /*
 
2649
        Check whether join_tab is not the last inner table
 
2650
        for another embedding outer join.
 
2651
      */
 
2652
      if ((first_unmatched= first_unmatched->first_upper) &&
 
2653
          first_unmatched->last_inner != join_tab)
 
2654
        first_unmatched= 0;
 
2655
      join_tab->first_unmatched= first_unmatched;
 
2656
    }
 
2657
 
 
2658
    JOIN_TAB *return_tab= join->return_tab;
 
2659
    join_tab->found_match= true;
 
2660
    if (join_tab->check_weed_out_table)
 
2661
    {
 
2662
      int res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table);
 
2663
      if (res == -1)
 
2664
        return NESTED_LOOP_ERROR;
 
2665
      if (res == 1)
 
2666
        return NESTED_LOOP_OK;
 
2667
    }
 
2668
    else if (join_tab->do_firstmatch)
 
2669
    {
 
2670
      /*
 
2671
        We should return to the join_tab->do_firstmatch after we have
 
2672
        enumerated all the suffixes for current prefix row combination
 
2673
      */
 
2674
      return_tab= join_tab->do_firstmatch;
 
2675
    }
 
2676
 
 
2677
    /*
 
2678
      It was not just a return to lower loop level when one
 
2679
      of the newly activated predicates is evaluated as false
 
2680
      (See above join->return_tab= tab).
 
2681
    */
 
2682
    join->examined_rows++;
 
2683
    join->session->row_count++;
 
2684
 
 
2685
    if (found)
 
2686
    {
 
2687
      enum enum_nested_loop_state rc;
 
2688
      /* A match from join_tab is found for the current partial join. */
 
2689
      rc= (*join_tab->next_select)(join, join_tab+1, 0);
 
2690
      if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
2691
        return rc;
 
2692
      if (return_tab < join->return_tab)
 
2693
        join->return_tab= return_tab;
 
2694
 
 
2695
      if (join->return_tab < join_tab)
 
2696
        return NESTED_LOOP_OK;
 
2697
      /*
 
2698
        Test if this was a SELECT DISTINCT query on a table that
 
2699
        was not in the field list;  In this case we can abort if
 
2700
        we found a row, as no new rows can be added to the result.
 
2701
      */
 
2702
      if (not_used_in_distinct && found_records != join->found_records)
 
2703
        return NESTED_LOOP_NO_MORE_ROWS;
 
2704
    }
 
2705
    else
 
2706
      join_tab->read_record.file->unlock_row();
 
2707
  }
 
2708
  else
 
2709
  {
 
2710
    /*
 
2711
      The condition pushed down to the table join_tab rejects all rows
 
2712
      with the beginning coinciding with the current partial join.
 
2713
    */
 
2714
    join->examined_rows++;
 
2715
    join->session->row_count++;
 
2716
    join_tab->read_record.file->unlock_row();
 
2717
  }
 
2718
  return NESTED_LOOP_OK;
 
2719
}
 
2720
 
 
2721
/**
 
2722
  @details
 
2723
    Construct a NULL complimented partial join record and feed it to the next
 
2724
    level of the nested loop. This function is used in case we have
 
2725
    an OUTER join and no matching record was found.
 
2726
*/
 
2727
enum_nested_loop_state evaluate_null_complemented_join_record(JOIN *join, JOIN_TAB *join_tab)
 
2728
{
 
2729
  /*
 
2730
    The table join_tab is the first inner table of a outer join operation
 
2731
    and no matches has been found for the current outer row.
 
2732
  */
 
2733
  JOIN_TAB *last_inner_tab= join_tab->last_inner;
 
2734
  /* Cache variables for faster loop */
 
2735
  COND *select_cond;
 
2736
  for ( ; join_tab <= last_inner_tab ; join_tab++)
 
2737
  {
 
2738
    /* Change the the values of guard predicate variables. */
 
2739
    join_tab->found= 1;
 
2740
    join_tab->not_null_compl= 0;
 
2741
    /* The outer row is complemented by nulls for each inner tables */
 
2742
    join_tab->table->restoreRecordAsDefault();  // Make empty record
 
2743
    join_tab->table->mark_as_null_row();       // For group by without error
 
2744
    select_cond= join_tab->select_cond;
 
2745
    /* Check all attached conditions for inner table rows. */
 
2746
    if (select_cond && !select_cond->val_int())
 
2747
      return NESTED_LOOP_OK;
 
2748
  }
 
2749
  join_tab--;
 
2750
  /*
 
2751
    The row complemented by nulls might be the first row
 
2752
    of embedding outer joins.
 
2753
    If so, perform the same actions as in the code
 
2754
    for the first regular outer join row above.
 
2755
  */
 
2756
  for ( ; ; )
 
2757
  {
 
2758
    JOIN_TAB *first_unmatched= join_tab->first_unmatched;
 
2759
    if ((first_unmatched= first_unmatched->first_upper) && first_unmatched->last_inner != join_tab)
 
2760
      first_unmatched= 0;
 
2761
    join_tab->first_unmatched= first_unmatched;
 
2762
    if (! first_unmatched)
 
2763
      break;
 
2764
    first_unmatched->found= 1;
 
2765
    for (JOIN_TAB *tab= first_unmatched; tab <= join_tab; tab++)
 
2766
    {
 
2767
      if (tab->select_cond && !tab->select_cond->val_int())
 
2768
      {
 
2769
        join->return_tab= tab;
 
2770
        return NESTED_LOOP_OK;
 
2771
      }
 
2772
    }
 
2773
  }
 
2774
  /*
 
2775
    The row complemented by nulls satisfies all conditions
 
2776
    attached to inner tables.
 
2777
    Send the row complemented by nulls to be joined with the
 
2778
    remaining tables.
 
2779
  */
 
2780
  return (*join_tab->next_select)(join, join_tab+1, 0);
 
2781
}
 
2782
 
 
2783
enum_nested_loop_state flush_cached_records(JOIN *join, JOIN_TAB *join_tab, bool skip_last)
 
2784
{
 
2785
  enum_nested_loop_state rc= NESTED_LOOP_OK;
 
2786
  int error;
 
2787
  READ_RECORD *info;
 
2788
 
 
2789
  join_tab->table->null_row= 0;
 
2790
  if (!join_tab->cache.records)
 
2791
    return NESTED_LOOP_OK;                      /* Nothing to do */
 
2792
  if (skip_last)
 
2793
    (void) store_record_in_cache(&join_tab->cache); // Must save this for later
 
2794
  if (join_tab->use_quick == 2)
 
2795
  {
 
2796
    if (join_tab->select->quick)
 
2797
    {                                   /* Used quick select last. reset it */
 
2798
      delete join_tab->select->quick;
 
2799
      join_tab->select->quick=0;
 
2800
    }
 
2801
  }
 
2802
  /* read through all records */
 
2803
  if ((error=join_init_read_record(join_tab)))
 
2804
  {
 
2805
    reset_cache_write(&join_tab->cache);
 
2806
    return error < 0 ? NESTED_LOOP_NO_MORE_ROWS: NESTED_LOOP_ERROR;
 
2807
  }
 
2808
 
 
2809
  for (JOIN_TAB *tmp=join->join_tab; tmp != join_tab ; tmp++)
 
2810
  {
 
2811
    tmp->status=tmp->table->status;
 
2812
    tmp->table->status=0;
 
2813
  }
 
2814
 
 
2815
  info= &join_tab->read_record;
 
2816
  do
 
2817
  {
 
2818
    if (join->session->killed)
 
2819
    {
 
2820
      join->session->send_kill_message();
 
2821
      return NESTED_LOOP_KILLED; // Aborted by user /* purecov: inspected */
 
2822
    }
 
2823
    SQL_SELECT *select=join_tab->select;
 
2824
    if (rc == NESTED_LOOP_OK &&
 
2825
        (!join_tab->cache.select || !join_tab->cache.select->skip_record()))
 
2826
    {
 
2827
      uint32_t i;
 
2828
      reset_cache_read(&join_tab->cache);
 
2829
      for (i=(join_tab->cache.records- (skip_last ? 1 : 0)) ; i-- > 0 ;)
 
2830
      {
 
2831
              read_cached_record(join_tab);
 
2832
              if (!select || !select->skip_record())
 
2833
        {
 
2834
          int res= 0;
 
2835
          if (!join_tab->check_weed_out_table ||
 
2836
              !(res= do_sj_dups_weedout(join->session, join_tab->check_weed_out_table)))
 
2837
          {
 
2838
            rc= (join_tab->next_select)(join,join_tab+1,0);
 
2839
            if (rc != NESTED_LOOP_OK && rc != NESTED_LOOP_NO_MORE_ROWS)
 
2840
            {
 
2841
              reset_cache_write(&join_tab->cache);
 
2842
              return rc;
 
2843
            }
 
2844
          }
 
2845
          if (res == -1)
 
2846
            return NESTED_LOOP_ERROR;
 
2847
        }
 
2848
      }
 
2849
    }
 
2850
  } while (!(error=info->read_record(info)));
 
2851
 
 
2852
  if (skip_last)
 
2853
    read_cached_record(join_tab);               // Restore current record
 
2854
  reset_cache_write(&join_tab->cache);
 
2855
  if (error > 0)                                // Fatal error
 
2856
    return NESTED_LOOP_ERROR;                   /* purecov: inspected */
 
2857
  for (JOIN_TAB *tmp2=join->join_tab; tmp2 != join_tab ; tmp2++)
 
2858
    tmp2->table->status=tmp2->status;
 
2859
  return NESTED_LOOP_OK;
 
2860
}
 
2861
 
 
2862
/*****************************************************************************
 
2863
  DESCRIPTION
 
2864
    Functions that end one nested loop iteration. Different functions
 
2865
    are used to support GROUP BY clause and to redirect records
 
2866
    to a table (e.g. in case of SELECT into a temporary table) or to the
 
2867
    network client.
 
2868
 
 
2869
  RETURN VALUES
 
2870
    NESTED_LOOP_OK           - the record has been successfully handled
 
2871
    NESTED_LOOP_ERROR        - a fatal error (like table corruption)
 
2872
                               was detected
 
2873
    NESTED_LOOP_KILLED       - thread shutdown was requested while processing
 
2874
                               the record
 
2875
    NESTED_LOOP_QUERY_LIMIT  - the record has been successfully handled;
 
2876
                               additionally, the nested loop produced the
 
2877
                               number of rows specified in the LIMIT clause
 
2878
                               for the query
 
2879
    NESTED_LOOP_CURSOR_LIMIT - the record has been successfully handled;
 
2880
                               additionally, there is a cursor and the nested
 
2881
                               loop algorithm produced the number of rows
 
2882
                               that is specified for current cursor fetch
 
2883
                               operation.
 
2884
   All return values except NESTED_LOOP_OK abort the nested loop.
 
2885
*****************************************************************************/
 
2886
enum_nested_loop_state end_send(JOIN *join, JOIN_TAB *, bool end_of_records)
 
2887
{
 
2888
  if (! end_of_records)
 
2889
  {
 
2890
    int error;
 
2891
    if (join->having && join->having->val_int() == 0)
 
2892
      return NESTED_LOOP_OK;               // Didn't match having
 
2893
    error= 0;
 
2894
    if (join->do_send_rows)
 
2895
      error=join->result->send_data(*join->fields);
 
2896
    if (error)
 
2897
      return NESTED_LOOP_ERROR; /* purecov: inspected */
 
2898
    if (++join->send_records >= join->unit->select_limit_cnt && join->do_send_rows)
 
2899
    {
 
2900
      if (join->select_options & OPTION_FOUND_ROWS)
 
2901
      {
 
2902
        JOIN_TAB *jt=join->join_tab;
 
2903
        if ((join->tables == 1) && !join->tmp_table && !join->sort_and_group
 
2904
            && !join->send_group_parts && !join->having && !jt->select_cond &&
 
2905
            !(jt->select && jt->select->quick) &&
 
2906
            (jt->table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
2907
                  (jt->ref.key < 0))
 
2908
        {
 
2909
          /* Join over all rows in table;  Return number of found rows */
 
2910
          Table *table= jt->table;
 
2911
 
 
2912
          join->select_options^= OPTION_FOUND_ROWS;
 
2913
          if (table->sort.record_pointers ||
 
2914
              (table->sort.io_cache && my_b_inited(table->sort.io_cache)))
 
2915
          {
 
2916
            /* Using filesort */
 
2917
            join->send_records= table->sort.found_records;
 
2918
          }
 
2919
          else
 
2920
          {
 
2921
            table->file->info(HA_STATUS_VARIABLE);
 
2922
            join->send_records= table->file->stats.records;
 
2923
          }
 
2924
        }
 
2925
        else
 
2926
        {
 
2927
          join->do_send_rows= 0;
 
2928
          if (join->unit->fake_select_lex)
 
2929
            join->unit->fake_select_lex->select_limit= 0;
 
2930
          return NESTED_LOOP_OK;
 
2931
        }
 
2932
      }
 
2933
      return NESTED_LOOP_QUERY_LIMIT;      // Abort nicely
 
2934
    }
 
2935
    else if (join->send_records >= join->fetch_limit)
 
2936
    {
 
2937
      /*
 
2938
        There is a server side cursor and all rows for
 
2939
        this fetch request are sent.
 
2940
      */
 
2941
      return NESTED_LOOP_CURSOR_LIMIT;
 
2942
    }
 
2943
  }
 
2944
 
 
2945
  return NESTED_LOOP_OK;
 
2946
}
 
2947
 
 
2948
enum_nested_loop_state end_write(JOIN *join, JOIN_TAB *, bool end_of_records)
 
2949
{
 
2950
  Table *table= join->tmp_table;
 
2951
 
 
2952
  if (join->session->killed)                    // Aborted by user
 
2953
  {
 
2954
    join->session->send_kill_message();
 
2955
    return NESTED_LOOP_KILLED;             /* purecov: inspected */
 
2956
  }
 
2957
  if (!end_of_records)
 
2958
  {
 
2959
    copy_fields(&join->tmp_table_param);
 
2960
    copy_funcs(join->tmp_table_param.items_to_copy);
 
2961
    if (!join->having || join->having->val_int())
 
2962
    {
 
2963
      int error;
 
2964
      join->found_records++;
 
2965
      if ((error=table->file->ha_write_row(table->record[0])))
 
2966
      {
 
2967
        if (!table->file->is_fatal_error(error, HA_CHECK_DUP))
 
2968
          goto end;
 
2969
        if (create_myisam_from_heap(join->session, table,
 
2970
                                          join->tmp_table_param.start_recinfo,
 
2971
                                          &join->tmp_table_param.recinfo,
 
2972
                  error, 1))
 
2973
          return NESTED_LOOP_ERROR;        // Not a table_is_full error
 
2974
        table->s->uniques= 0;                   // To ensure rows are the same
 
2975
      }
 
2976
      if (++join->send_records >= join->tmp_table_param.end_write_records && join->do_send_rows)
 
2977
      {
 
2978
        if (!(join->select_options & OPTION_FOUND_ROWS))
 
2979
          return NESTED_LOOP_QUERY_LIMIT;
 
2980
        join->do_send_rows= 0;
 
2981
        join->unit->select_limit_cnt= HA_POS_ERROR;
 
2982
        return NESTED_LOOP_OK;
 
2983
      }
 
2984
    }
 
2985
  }
 
2986
end:
 
2987
  return NESTED_LOOP_OK;
 
2988
}
 
2989
 
 
2990
/** Group by searching after group record and updating it if possible. */
 
2991
enum_nested_loop_state end_update(JOIN *join, JOIN_TAB *, bool end_of_records)
 
2992
{
 
2993
  Table *table= join->tmp_table;
 
2994
  order_st *group;
 
2995
  int   error;
 
2996
 
 
2997
  if (end_of_records)
 
2998
    return NESTED_LOOP_OK;
 
2999
  if (join->session->killed)                    // Aborted by user
 
3000
  {
 
3001
    join->session->send_kill_message();
 
3002
    return NESTED_LOOP_KILLED;             /* purecov: inspected */
 
3003
  }
 
3004
 
 
3005
  join->found_records++;
 
3006
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
3007
  /* Make a key of group index */
 
3008
  for (group=table->group ; group ; group=group->next)
 
3009
  {
 
3010
    Item *item= *group->item;
 
3011
    item->save_org_in_field(group->field);
 
3012
    /* Store in the used key if the field was 0 */
 
3013
    if (item->maybe_null)
 
3014
      group->buff[-1]= (char) group->field->is_null();
 
3015
  }
 
3016
  if (!table->file->index_read_map(table->record[1],
 
3017
                                   join->tmp_table_param.group_buff,
 
3018
                                   HA_WHOLE_KEY,
 
3019
                                   HA_READ_KEY_EXACT))
 
3020
  {                                             /* Update old record */
 
3021
    table->restoreRecord();
 
3022
    update_tmptable_sum_func(join->sum_funcs,table);
 
3023
    if ((error= table->file->ha_update_row(table->record[1],
 
3024
                                          table->record[0])))
 
3025
    {
 
3026
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
3027
      return NESTED_LOOP_ERROR;            /* purecov: inspected */
 
3028
    }
 
3029
    return NESTED_LOOP_OK;
 
3030
  }
 
3031
 
 
3032
  /*
 
3033
    Copy null bits from group key to table
 
3034
    We can't copy all data as the key may have different format
 
3035
    as the row data (for example as with VARCHAR keys)
 
3036
  */
 
3037
  KEY_PART_INFO *key_part;
 
3038
  for (group=table->group,key_part=table->key_info[0].key_part;
 
3039
       group ;
 
3040
       group=group->next,key_part++)
 
3041
  {
 
3042
    if (key_part->null_bit)
 
3043
      memcpy(table->record[0]+key_part->offset, group->buff, 1);
 
3044
  }
 
3045
  init_tmptable_sum_functions(join->sum_funcs);
 
3046
  copy_funcs(join->tmp_table_param.items_to_copy);
 
3047
  if ((error=table->file->ha_write_row(table->record[0])))
 
3048
  {
 
3049
    if (create_myisam_from_heap(join->session, table,
 
3050
                                join->tmp_table_param.start_recinfo,
 
3051
                                &join->tmp_table_param.recinfo,
 
3052
                                error, 0))
 
3053
      return NESTED_LOOP_ERROR;            // Not a table_is_full error
 
3054
    /* Change method to update rows */
 
3055
    table->file->ha_index_init(0, 0);
 
3056
    join->join_tab[join->tables-1].next_select= end_unique_update;
 
3057
  }
 
3058
  join->send_records++;
 
3059
  return NESTED_LOOP_OK;
 
3060
}
 
3061
 
 
3062
/** Like end_update, but this is done with unique constraints instead of keys.  */
 
3063
enum_nested_loop_state end_unique_update(JOIN *join, JOIN_TAB *, bool end_of_records)
 
3064
{
 
3065
  Table *table= join->tmp_table;
 
3066
  int   error;
 
3067
 
 
3068
  if (end_of_records)
 
3069
    return NESTED_LOOP_OK;
 
3070
  if (join->session->killed)                    // Aborted by user
 
3071
  {
 
3072
    join->session->send_kill_message();
 
3073
    return NESTED_LOOP_KILLED;             /* purecov: inspected */
 
3074
  }
 
3075
 
 
3076
  init_tmptable_sum_functions(join->sum_funcs);
 
3077
  copy_fields(&join->tmp_table_param);          // Groups are copied twice.
 
3078
  copy_funcs(join->tmp_table_param.items_to_copy);
 
3079
 
 
3080
  if (!(error= table->file->ha_write_row(table->record[0])))
 
3081
    join->send_records++;                       // New group
 
3082
  else
 
3083
  {
 
3084
    if ((int) table->file->get_dup_key(error) < 0)
 
3085
    {
 
3086
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
3087
      return NESTED_LOOP_ERROR;            /* purecov: inspected */
 
3088
    }
 
3089
    if (table->file->rnd_pos(table->record[1],table->file->dup_ref))
 
3090
    {
 
3091
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
3092
      return NESTED_LOOP_ERROR;            /* purecov: inspected */
 
3093
    }
 
3094
    table->restoreRecord();
 
3095
    update_tmptable_sum_func(join->sum_funcs,table);
 
3096
    if ((error= table->file->ha_update_row(table->record[1],
 
3097
                                          table->record[0])))
 
3098
    {
 
3099
      table->file->print_error(error,MYF(0));   /* purecov: inspected */
 
3100
      return NESTED_LOOP_ERROR;            /* purecov: inspected */
 
3101
    }
 
3102
  }
 
3103
  return NESTED_LOOP_OK;
 
3104
}
 
3105
 
 
3106
/**
 
3107
  allocate group fields or take prepared (cached).
 
3108
 
 
3109
  @param main_join   join of current select
 
3110
  @param curr_join   current join (join of current select or temporary copy
 
3111
                     of it)
 
3112
 
 
3113
  @retval
 
3114
    0   ok
 
3115
  @retval
 
3116
    1   failed
 
3117
*/
 
3118
static bool make_group_fields(JOIN *main_join, JOIN *curr_join)
 
3119
{
 
3120
  if (main_join->group_fields_cache.elements)
 
3121
  {
 
3122
    curr_join->group_fields= main_join->group_fields_cache;
 
3123
    curr_join->sort_and_group= 1;
 
3124
  }
 
3125
  else
 
3126
  {
 
3127
    if (alloc_group_fields(curr_join, curr_join->group_list))
 
3128
      return (1);
 
3129
    main_join->group_fields_cache= curr_join->group_fields;
 
3130
  }
 
3131
  return (0);
 
3132
}
 
3133
 
 
3134
/**
 
3135
  calc how big buffer we need for comparing group entries.
 
3136
*/
 
3137
static void calc_group_buffer(JOIN *join,order_st *group)
 
3138
{
 
3139
  uint32_t key_length=0, parts=0, null_parts=0;
 
3140
 
 
3141
  if (group)
 
3142
    join->group= 1;
 
3143
  for (; group ; group=group->next)
 
3144
  {
 
3145
    Item *group_item= *group->item;
 
3146
    Field *field= group_item->get_tmp_table_field();
 
3147
    if (field)
 
3148
    {
 
3149
      enum_field_types type;
 
3150
      if ((type= field->type()) == DRIZZLE_TYPE_BLOB)
 
3151
        key_length+=MAX_BLOB_WIDTH;   // Can't be used as a key
 
3152
      else if (type == DRIZZLE_TYPE_VARCHAR)
 
3153
        key_length+= field->field_length + HA_KEY_BLOB_LENGTH;
 
3154
      else
 
3155
        key_length+= field->pack_length();
 
3156
    }
 
3157
    else
 
3158
    {
 
3159
      switch (group_item->result_type()) {
 
3160
      case REAL_RESULT:
 
3161
        key_length+= sizeof(double);
 
3162
        break;
 
3163
      case INT_RESULT:
 
3164
        key_length+= sizeof(int64_t);
 
3165
        break;
 
3166
      case DECIMAL_RESULT:
 
3167
        key_length+= my_decimal_get_binary_size(group_item->max_length -
 
3168
                                                (group_item->decimals ? 1 : 0),
 
3169
                                                group_item->decimals);
 
3170
        break;
 
3171
      case STRING_RESULT:
 
3172
      {
 
3173
        enum enum_field_types type= group_item->field_type();
 
3174
        /*
 
3175
          As items represented as DATE/TIME fields in the group buffer
 
3176
          have STRING_RESULT result type, we increase the length
 
3177
          by 8 as maximum pack length of such fields.
 
3178
        */
 
3179
        if (type == DRIZZLE_TYPE_DATE ||
 
3180
            type == DRIZZLE_TYPE_DATETIME ||
 
3181
            type == DRIZZLE_TYPE_TIMESTAMP)
 
3182
        {
 
3183
          key_length+= 8;
 
3184
        }
 
3185
        else
 
3186
        {
 
3187
          /*
 
3188
            Group strings are taken as varstrings and require an length field.
 
3189
            A field is not yet created by create_tmp_field()
 
3190
            and the sizes should match up.
 
3191
          */
 
3192
          key_length+= group_item->max_length + HA_KEY_BLOB_LENGTH;
 
3193
        }
 
3194
        break;
 
3195
      }
 
3196
      default:
 
3197
        /* This case should never be choosen */
 
3198
        assert(0);
 
3199
        my_error(ER_OUT_OF_RESOURCES, MYF(ME_FATALERROR));
 
3200
      }
 
3201
    }
 
3202
    parts++;
 
3203
    if (group_item->maybe_null)
 
3204
      null_parts++;
 
3205
  }
 
3206
  join->tmp_table_param.group_length=key_length+null_parts;
 
3207
  join->tmp_table_param.group_parts=parts;
 
3208
  join->tmp_table_param.group_null_parts=null_parts;
 
3209
}
 
3210
 
 
3211
/**
 
3212
  Get a list of buffers for saveing last group.
 
3213
 
 
3214
  Groups are saved in reverse order for easyer check loop.
 
3215
*/
 
3216
static bool alloc_group_fields(JOIN *join,order_st *group)
 
3217
{
 
3218
  if (group)
 
3219
  {
 
3220
    for (; group ; group=group->next)
 
3221
    {
 
3222
      Cached_item *tmp=new_Cached_item(join->session, *group->item, false);
 
3223
      if (!tmp || join->group_fields.push_front(tmp))
 
3224
        return true;
 
3225
    }
 
3226
  }
 
3227
  join->sort_and_group=1;     /* Mark for do_select */
 
3228
  return false;
 
3229
}
 
3230
 
 
3231
/**
 
3232
  @todo
 
3233
  - TODO: this function is here only temporarily until 'greedy_search' is
 
3234
  tested and accepted.
 
3235
 
 
3236
  RETURN VALUES
 
3237
    false       ok
 
3238
    true        Fatal error
 
3239
*/
 
3240
static bool find_best(JOIN *join,table_map rest_tables,uint32_t idx,double record_count, double read_time)
 
3241
{
 
3242
  Session *session= join->session;
 
3243
  if (session->killed)
 
3244
    return(true);
 
3245
  if (!rest_tables)
 
3246
  {
 
3247
    read_time+=record_count/(double) TIME_FOR_COMPARE;
 
3248
    if (join->sort_by_table &&
 
3249
  join->sort_by_table !=
 
3250
  join->positions[join->const_tables].table->table)
 
3251
      read_time+=record_count;      // We have to make a temp table
 
3252
    if (read_time < join->best_read)
 
3253
    {
 
3254
      memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
3255
      join->best_read= read_time - 0.001;
 
3256
    }
 
3257
    return(false);
 
3258
  }
 
3259
  if (read_time+record_count/(double) TIME_FOR_COMPARE >= join->best_read)
 
3260
    return(false);          /* Found better before */
 
3261
 
 
3262
  JOIN_TAB *s;
 
3263
  double best_record_count=DBL_MAX,best_read_time=DBL_MAX;
 
3264
  for (JOIN_TAB **pos=join->best_ref+idx ; (s=*pos) ; pos++)
 
3265
  {
 
3266
    table_map real_table_bit=s->table->map;
 
3267
    if ((rest_tables & real_table_bit) && !(rest_tables & s->dependent) &&
 
3268
        (!idx|| !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
3269
    {
 
3270
      double records, best;
 
3271
      advance_sj_state(rest_tables, s);
 
3272
      best_access_path(join, s, session, rest_tables, idx, record_count,
 
3273
                       read_time);
 
3274
      records= join->positions[idx].records_read;
 
3275
      best= join->positions[idx].read_time;
 
3276
      /*
 
3277
  Go to the next level only if there hasn't been a better key on
 
3278
  this level! This will cut down the search for a lot simple cases!
 
3279
      */
 
3280
      double current_record_count=record_count*records;
 
3281
      double current_read_time=read_time+best;
 
3282
      if (best_record_count > current_record_count ||
 
3283
    best_read_time > current_read_time ||
 
3284
    (idx == join->const_tables && s->table == join->sort_by_table))
 
3285
      {
 
3286
  if (best_record_count >= current_record_count &&
 
3287
      best_read_time >= current_read_time &&
 
3288
      (!(s->key_dependent & rest_tables) || records < 2.0))
 
3289
  {
 
3290
    best_record_count=current_record_count;
 
3291
    best_read_time=current_read_time;
 
3292
  }
 
3293
        std::swap(join->best_ref[idx], *pos);
 
3294
  if (find_best(join,rest_tables & ~real_table_bit,idx+1,
 
3295
                      current_record_count,current_read_time))
 
3296
          return(true);
 
3297
        std::swap(join->best_ref[idx], *pos);
 
3298
      }
 
3299
      restore_prev_nj_state(s);
 
3300
      restore_prev_sj_state(rest_tables, s);
 
3301
      if (join->select_options & SELECT_STRAIGHT_JOIN)
 
3302
  break;        // Don't test all combinations
 
3303
    }
 
3304
  }
 
3305
  return(false);
 
3306
}
 
3307
 
 
3308
static uint32_t cache_record_length(JOIN *join,uint32_t idx)
 
3309
{
 
3310
  uint32_t length=0;
 
3311
  JOIN_TAB **pos,**end;
 
3312
  Session *session=join->session;
 
3313
 
 
3314
  for (pos=join->best_ref+join->const_tables,end=join->best_ref+idx ;
 
3315
       pos != end ;
 
3316
       pos++)
 
3317
  {
 
3318
    JOIN_TAB *join_tab= *pos;
 
3319
    if (!join_tab->used_fieldlength)    /* Not calced yet */
 
3320
      calc_used_field_length(session, join_tab);
 
3321
    length+=join_tab->used_fieldlength;
 
3322
  }
 
3323
  return length;
 
3324
}
 
3325
 
 
3326
/*
 
3327
  Get the number of different row combinations for subset of partial join
 
3328
 
 
3329
  SYNOPSIS
 
3330
    prev_record_reads()
 
3331
      join       The join structure
 
3332
      idx        Number of tables in the partial join order (i.e. the
 
3333
                 partial join order is in join->positions[0..idx-1])
 
3334
      found_ref  Bitmap of tables for which we need to find # of distinct
 
3335
                 row combinations.
 
3336
 
 
3337
  DESCRIPTION
 
3338
    Given a partial join order (in join->positions[0..idx-1]) and a subset of
 
3339
    tables within that join order (specified in found_ref), find out how many
 
3340
    distinct row combinations of subset tables will be in the result of the
 
3341
    partial join order.
 
3342
 
 
3343
    This is used as follows: Suppose we have a table accessed with a ref-based
 
3344
    method. The ref access depends on current rows of tables in found_ref.
 
3345
    We want to count # of different ref accesses. We assume two ref accesses
 
3346
    will be different if at least one of access parameters is different.
 
3347
    Example: consider a query
 
3348
 
 
3349
    SELECT * FROM t1, t2, t3 WHERE t1.key=c1 AND t2.key=c2 AND t3.key=t1.field
 
3350
 
 
3351
    and a join order:
 
3352
      t1,  ref access on t1.key=c1
 
3353
      t2,  ref access on t2.key=c2
 
3354
      t3,  ref access on t3.key=t1.field
 
3355
 
 
3356
    For t1: n_ref_scans = 1, n_distinct_ref_scans = 1
 
3357
    For t2: n_ref_scans = records_read(t1), n_distinct_ref_scans=1
 
3358
    For t3: n_ref_scans = records_read(t1)*records_read(t2)
 
3359
            n_distinct_ref_scans = #records_read(t1)
 
3360
 
 
3361
    The reason for having this function (at least the latest version of it)
 
3362
    is that we need to account for buffering in join execution.
 
3363
 
 
3364
    An edge-case example: if we have a non-first table in join accessed via
 
3365
    ref(const) or ref(param) where there is a small number of different
 
3366
    values of param, then the access will likely hit the disk cache and will
 
3367
    not require any disk seeks.
 
3368
 
 
3369
    The proper solution would be to assume an LRU disk cache of some size,
 
3370
    calculate probability of cache hits, etc. For now we just count
 
3371
    identical ref accesses as one.
 
3372
 
 
3373
  RETURN
 
3374
    Expected number of row combinations
 
3375
*/
 
3376
static double prev_record_reads(JOIN *join, uint32_t idx, table_map found_ref)
 
3377
{
 
3378
  double found=1.0;
 
3379
  POSITION *pos_end= join->positions - 1;
 
3380
  for (POSITION *pos= join->positions + idx - 1; pos != pos_end; pos--)
 
3381
  {
 
3382
    if (pos->table->table->map & found_ref)
 
3383
    {
 
3384
      found_ref|= pos->ref_depend_map;
 
3385
      /*
 
3386
        For the case of "t1 LEFT JOIN t2 ON ..." where t2 is a const table
 
3387
        with no matching row we will get position[t2].records_read==0.
 
3388
        Actually the size of output is one null-complemented row, therefore
 
3389
        we will use value of 1 whenever we get records_read==0.
 
3390
 
 
3391
        Note
 
3392
        - the above case can't occur if inner part of outer join has more
 
3393
          than one table: table with no matches will not be marked as const.
 
3394
 
 
3395
        - Ideally we should add 1 to records_read for every possible null-
 
3396
          complemented row. We're not doing it because: 1. it will require
 
3397
          non-trivial code and add overhead. 2. The value of records_read
 
3398
          is an inprecise estimate and adding 1 (or, in the worst case,
 
3399
          #max_nested_outer_joins=64-1) will not make it any more precise.
 
3400
      */
 
3401
      if (pos->records_read > DBL_EPSILON)
 
3402
        found*= pos->records_read;
 
3403
    }
 
3404
  }
 
3405
  return found;
 
3406
}
 
3407
 
 
3408
/**
 
3409
  Set up join struct according to best position.
 
3410
*/
 
3411
static bool get_best_combination(JOIN *join)
 
3412
{
 
3413
  uint32_t i,tablenr;
 
3414
  table_map used_tables;
 
3415
  JOIN_TAB *join_tab,*j;
 
3416
  KEYUSE *keyuse;
 
3417
  uint32_t table_count;
 
3418
  Session *session=join->session;
 
3419
 
 
3420
  table_count=join->tables;
 
3421
  if (!(join->join_tab=join_tab=
 
3422
  (JOIN_TAB*) session->alloc(sizeof(JOIN_TAB)*table_count)))
 
3423
    return(true);
 
3424
 
 
3425
  join->full_join=0;
 
3426
 
 
3427
  used_tables= OUTER_REF_TABLE_BIT;   // Outer row is already read
 
3428
  for (j=join_tab, tablenr=0 ; tablenr < table_count ; tablenr++,j++)
 
3429
  {
 
3430
    Table *form;
 
3431
    *j= *join->best_positions[tablenr].table;
 
3432
    form=join->table[tablenr]=j->table;
 
3433
    used_tables|= form->map;
 
3434
    form->reginfo.join_tab=j;
 
3435
    if (!*j->on_expr_ref)
 
3436
      form->reginfo.not_exists_optimize=0;  // Only with LEFT JOIN
 
3437
    if (j->type == JT_CONST)
 
3438
      continue;         // Handled in make_join_stat..
 
3439
 
 
3440
    j->ref.key = -1;
 
3441
    j->ref.key_parts=0;
 
3442
 
 
3443
    if (j->type == JT_SYSTEM)
 
3444
      continue;
 
3445
    if (j->keys.none() || !(keyuse= join->best_positions[tablenr].key))
 
3446
    {
 
3447
      j->type=JT_ALL;
 
3448
      if (tablenr != join->const_tables)
 
3449
        join->full_join=1;
 
3450
    }
 
3451
    else if (create_ref_for_key(join, j, keyuse, used_tables))
 
3452
      return(true);                        // Something went wrong
 
3453
  }
 
3454
 
 
3455
  for (i=0 ; i < table_count ; i++)
 
3456
    join->map2table[join->join_tab[i].table->tablenr]=join->join_tab+i;
 
3457
  update_depend_map(join);
 
3458
  return(0);
 
3459
}
 
3460
 
 
3461
/** Save const tables first as used tables. */
 
3462
static void set_position(JOIN *join,uint32_t idx,JOIN_TAB *table,KEYUSE *key)
 
3463
{
 
3464
  join->positions[idx].table= table;
 
3465
  join->positions[idx].key=key;
 
3466
  join->positions[idx].records_read=1.0;  /* This is a const table */
 
3467
  join->positions[idx].ref_depend_map= 0;
 
3468
 
 
3469
  /* Move the const table as down as possible in best_ref */
 
3470
  JOIN_TAB **pos=join->best_ref+idx+1;
 
3471
  JOIN_TAB *next=join->best_ref[idx];
 
3472
  for (;next != table ; pos++)
 
3473
  {
 
3474
    JOIN_TAB *tmp=pos[0];
 
3475
    pos[0]=next;
 
3476
    next=tmp;
 
3477
  }
 
3478
  join->best_ref[idx]=table;
 
3479
}
 
3480
 
 
3481
/**
 
3482
  Selects and invokes a search strategy for an optimal query plan.
 
3483
 
 
3484
  The function checks user-configurable parameters that control the search
 
3485
  strategy for an optimal plan, selects the search method and then invokes
 
3486
  it. Each specific optimization procedure stores the final optimal plan in
 
3487
  the array 'join->best_positions', and the cost of the plan in
 
3488
  'join->best_read'.
 
3489
 
 
3490
  @param join         pointer to the structure providing all context info for
 
3491
                      the query
 
3492
  @param join_tables  set of the tables in the query
 
3493
 
 
3494
  @todo
 
3495
    'MAX_TABLES+2' denotes the old implementation of find_best before
 
3496
    the greedy version. Will be removed when greedy_search is approved.
 
3497
 
 
3498
  @retval
 
3499
    false       ok
 
3500
  @retval
 
3501
    true        Fatal error
 
3502
*/
 
3503
static bool choose_plan(JOIN *join, table_map join_tables)
 
3504
{
 
3505
  uint32_t search_depth= join->session->variables.optimizer_search_depth;
 
3506
  uint32_t prune_level=  join->session->variables.optimizer_prune_level;
 
3507
  bool straight_join= test(join->select_options & SELECT_STRAIGHT_JOIN);
 
3508
 
 
3509
  join->cur_embedding_map= 0;
 
3510
  reset_nj_counters(join->join_list);
 
3511
  /*
 
3512
    if (SELECT_STRAIGHT_JOIN option is set)
 
3513
      reorder tables so dependent tables come after tables they depend
 
3514
      on, otherwise keep tables in the order they were specified in the query
 
3515
    else
 
3516
      Apply heuristic: pre-sort all access plans with respect to the number of
 
3517
      records accessed.
 
3518
  */
 
3519
  my_qsort(join->best_ref + join->const_tables,
 
3520
           join->tables - join->const_tables, sizeof(JOIN_TAB*),
 
3521
           straight_join ? join_tab_cmp_straight : join_tab_cmp);
 
3522
  join->cur_emb_sj_nests= 0;
 
3523
  if (straight_join)
 
3524
  {
 
3525
    optimize_straight_join(join, join_tables);
 
3526
  }
 
3527
  else
 
3528
  {
 
3529
    if (search_depth == MAX_TABLES+2)
 
3530
    { /*
 
3531
        TODO: 'MAX_TABLES+2' denotes the old implementation of find_best before
 
3532
        the greedy version. Will be removed when greedy_search is approved.
 
3533
      */
 
3534
      join->best_read= DBL_MAX;
 
3535
      if (find_best(join, join_tables, join->const_tables, 1.0, 0.0))
 
3536
        return(true);
 
3537
    }
 
3538
    else
 
3539
    {
 
3540
      if (search_depth == 0)
 
3541
        /* Automatically determine a reasonable value for 'search_depth' */
 
3542
        search_depth= determine_search_depth(join);
 
3543
      if (greedy_search(join, join_tables, search_depth, prune_level))
 
3544
        return(true);
 
3545
    }
 
3546
  }
 
3547
 
 
3548
  /*
 
3549
    Store the cost of this query into a user variable
 
3550
    Don't update last_query_cost for statements that are not "flat joins" :
 
3551
    i.e. they have subqueries, unions or call stored procedures.
 
3552
    TODO: calculate a correct cost for a query with subqueries and UNIONs.
 
3553
  */
 
3554
  if (join->session->lex->is_single_level_stmt())
 
3555
    join->session->status_var.last_query_cost= join->best_read;
 
3556
  return(false);
 
3557
}
 
3558
 
 
3559
/**
 
3560
  Find the best access path for an extension of a partial execution
 
3561
  plan and add this path to the plan.
 
3562
 
 
3563
  The function finds the best access path to table 's' from the passed
 
3564
  partial plan where an access path is the general term for any means to
 
3565
  access the data in 's'. An access path may use either an index or a scan,
 
3566
  whichever is cheaper. The input partial plan is passed via the array
 
3567
  'join->positions' of length 'idx'. The chosen access method for 's' and its
 
3568
  cost are stored in 'join->positions[idx]'.
 
3569
 
 
3570
  @param join             pointer to the structure providing all context info
 
3571
                          for the query
 
3572
  @param s                the table to be joined by the function
 
3573
  @param session              thread for the connection that submitted the query
 
3574
  @param remaining_tables set of tables not included into the partial plan yet
 
3575
  @param idx              the length of the partial plan
 
3576
  @param record_count     estimate for the number of records returned by the
 
3577
                          partial plan
 
3578
  @param read_time        the cost of the partial plan
 
3579
 
 
3580
  @return
 
3581
    None
 
3582
*/
 
3583
static void best_access_path(JOIN *join,
 
3584
                             JOIN_TAB *s,
 
3585
                             Session *session,
 
3586
                             table_map remaining_tables,
 
3587
                             uint32_t idx,
 
3588
                             double record_count,
 
3589
                             double)
 
3590
{
 
3591
  KEYUSE *best_key=         0;
 
3592
  uint32_t best_max_key_part=   0;
 
3593
  bool found_constraint= 0;
 
3594
  double best=              DBL_MAX;
 
3595
  double best_time=         DBL_MAX;
 
3596
  double records=           DBL_MAX;
 
3597
  table_map best_ref_depends_map= 0;
 
3598
  double tmp;
 
3599
  ha_rows rec;
 
3600
  uint32_t best_is_sj_inside_out=    0;
 
3601
 
 
3602
  if (s->keyuse)
 
3603
  {                                            /* Use key if possible */
 
3604
    Table *table= s->table;
 
3605
    KEYUSE *keyuse,*start_key=0;
 
3606
    double best_records= DBL_MAX;
 
3607
    uint32_t max_key_part=0;
 
3608
    uint64_t bound_sj_equalities= 0;
 
3609
    bool try_sj_inside_out= false;
 
3610
    /*
 
3611
      Discover the bound equalites. We need to do this, if
 
3612
        1. The next table is an SJ-inner table, and
 
3613
        2. It is the first table from that semijoin, and
 
3614
        3. We're not within a semi-join range (i.e. all semi-joins either have
 
3615
           all or none of their tables in join_table_map), except
 
3616
           s->emb_sj_nest (which we've just entered).
 
3617
        3. All correlation references from this sj-nest are bound
 
3618
    */
 
3619
    if (s->emb_sj_nest &&                                                 // (1)
 
3620
        s->emb_sj_nest->sj_in_exprs < 64 &&
 
3621
        ((remaining_tables & s->emb_sj_nest->sj_inner_tables) ==           // (2)
 
3622
         s->emb_sj_nest->sj_inner_tables) &&                               // (2)
 
3623
        join->cur_emb_sj_nests == s->emb_sj_nest->sj_inner_tables &&       // (3)
 
3624
        !(remaining_tables & s->emb_sj_nest->nested_join->sj_corr_tables)) // (4)
 
3625
    {
 
3626
      /* This table is an InsideOut scan candidate */
 
3627
      bound_sj_equalities= get_bound_sj_equalities(s->emb_sj_nest,
 
3628
                                                   remaining_tables);
 
3629
      try_sj_inside_out= true;
 
3630
    }
 
3631
 
 
3632
    /* Test how we can use keys */
 
3633
    rec= s->records/MATCHING_ROWS_IN_OTHER_TABLE;  // Assumed records/key
 
3634
    for (keyuse=s->keyuse ; keyuse->table == table ;)
 
3635
    {
 
3636
      key_part_map found_part= 0;
 
3637
      table_map found_ref= 0;
 
3638
      uint32_t key= keyuse->key;
 
3639
      KEY *keyinfo= table->key_info+key;
 
3640
      /* Bitmap of keyparts where the ref access is over 'keypart=const': */
 
3641
      key_part_map const_part= 0;
 
3642
      /* The or-null keypart in ref-or-null access: */
 
3643
      key_part_map ref_or_null_part= 0;
 
3644
 
 
3645
      /* Calculate how many key segments of the current key we can use */
 
3646
      start_key= keyuse;
 
3647
      uint64_t handled_sj_equalities=0;
 
3648
      key_part_map sj_insideout_map= 0;
 
3649
 
 
3650
      do /* For each keypart */
 
3651
      {
 
3652
        uint32_t keypart= keyuse->keypart;
 
3653
        table_map best_part_found_ref= 0;
 
3654
        double best_prev_record_reads= DBL_MAX;
 
3655
 
 
3656
        do /* For each way to access the keypart */
 
3657
        {
 
3658
 
 
3659
          /*
 
3660
            if 1. expression doesn't refer to forward tables
 
3661
               2. we won't get two ref-or-null's
 
3662
          */
 
3663
          if (!(remaining_tables & keyuse->used_tables) &&
 
3664
              !(ref_or_null_part && (keyuse->optimize &
 
3665
                                     KEY_OPTIMIZE_REF_OR_NULL)))
 
3666
          {
 
3667
            found_part|= keyuse->keypart_map;
 
3668
            if (!(keyuse->used_tables & ~join->const_table_map))
 
3669
              const_part|= keyuse->keypart_map;
 
3670
 
 
3671
            double tmp2= prev_record_reads(join, idx, (found_ref |
 
3672
                                                      keyuse->used_tables));
 
3673
            if (tmp2 < best_prev_record_reads)
 
3674
            {
 
3675
              best_part_found_ref= keyuse->used_tables & ~join->const_table_map;
 
3676
              best_prev_record_reads= tmp2;
 
3677
            }
 
3678
            if (rec > keyuse->ref_table_rows)
 
3679
              rec= keyuse->ref_table_rows;
 
3680
      /*
 
3681
        If there is one 'key_column IS NULL' expression, we can
 
3682
        use this ref_or_null optimisation of this field
 
3683
      */
 
3684
            if (keyuse->optimize & KEY_OPTIMIZE_REF_OR_NULL)
 
3685
              ref_or_null_part |= keyuse->keypart_map;
 
3686
          }
 
3687
 
 
3688
          if (try_sj_inside_out && keyuse->sj_pred_no != UINT_MAX)
 
3689
          {
 
3690
            if (!(remaining_tables & keyuse->used_tables))
 
3691
              bound_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
3692
            else
 
3693
            {
 
3694
              handled_sj_equalities |= 1UL << keyuse->sj_pred_no;
 
3695
              sj_insideout_map |= ((key_part_map)1) << keyuse->keypart;
 
3696
            }
 
3697
          }
 
3698
 
 
3699
          keyuse++;
 
3700
        } while (keyuse->table == table && keyuse->key == key &&
 
3701
                 keyuse->keypart == keypart);
 
3702
  found_ref|= best_part_found_ref;
 
3703
      } while (keyuse->table == table && keyuse->key == key);
 
3704
 
 
3705
      /*
 
3706
        Assume that that each key matches a proportional part of table.
 
3707
      */
 
3708
      if (!found_part && !handled_sj_equalities)
 
3709
        continue;                               // Nothing usable found
 
3710
 
 
3711
      if (rec < MATCHING_ROWS_IN_OTHER_TABLE)
 
3712
        rec= MATCHING_ROWS_IN_OTHER_TABLE;      // Fix for small tables
 
3713
 
 
3714
      bool sj_inside_out_scan= false;
 
3715
      {
 
3716
        found_constraint= 1;
 
3717
        /*
 
3718
          Check if InsideOut scan is applicable:
 
3719
          1. All IN-equalities are either "bound" or "handled"
 
3720
          2. Index keyparts are
 
3721
             ...
 
3722
        */
 
3723
        if (try_sj_inside_out &&
 
3724
            table->covering_keys.test(key) &&
 
3725
            (handled_sj_equalities | bound_sj_equalities) ==     // (1)
 
3726
            PREV_BITS(uint64_t, s->emb_sj_nest->sj_in_exprs)) // (1)
 
3727
        {
 
3728
          uint32_t n_fixed_parts= max_part_bit(found_part);
 
3729
          if (n_fixed_parts != keyinfo->key_parts &&
 
3730
              (PREV_BITS(uint, n_fixed_parts) | sj_insideout_map) ==
 
3731
               PREV_BITS(uint, keyinfo->key_parts))
 
3732
          {
 
3733
            /*
 
3734
              Not all parts are fixed. Produce bitmap of remaining bits and
 
3735
              check if all of them are covered.
 
3736
            */
 
3737
            sj_inside_out_scan= true;
 
3738
            if (!n_fixed_parts)
 
3739
            {
 
3740
              /*
 
3741
                It's a confluent ref scan.
 
3742
 
 
3743
                That is, all found KEYUSE elements refer to IN-equalities,
 
3744
                and there is really no ref access because there is no
 
3745
                  t.keypart0 = {bound expression}
 
3746
 
 
3747
                Calculate the cost of complete loose index scan.
 
3748
              */
 
3749
              records= (double)s->table->file->stats.records;
 
3750
 
 
3751
              /* The cost is entire index scan cost (divided by 2) */
 
3752
              best_time= s->table->file->index_only_read_time(key, records);
 
3753
 
 
3754
              /* Now figure how many different keys we will get */
 
3755
              ulong rpc;
 
3756
              if ((rpc= keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
3757
                records= records / rpc;
 
3758
              start_key= NULL;
 
3759
            }
 
3760
          }
 
3761
        }
 
3762
 
 
3763
        /*
 
3764
          Check if we found full key
 
3765
        */
 
3766
        if (found_part == PREV_BITS(uint,keyinfo->key_parts) &&
 
3767
            !ref_or_null_part)
 
3768
        {                                         /* use eq key */
 
3769
          max_key_part= UINT32_MAX;
 
3770
          if ((keyinfo->flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME)
 
3771
          {
 
3772
            tmp = prev_record_reads(join, idx, found_ref);
 
3773
            records=1.0;
 
3774
          }
 
3775
          else
 
3776
          {
 
3777
            if (!found_ref)
 
3778
            {                                     /* We found a const key */
 
3779
              /*
 
3780
                ReuseRangeEstimateForRef-1:
 
3781
                We get here if we've found a ref(const) (c_i are constants):
 
3782
                  "(keypart1=c1) AND ... AND (keypartN=cN)"   [ref_const_cond]
 
3783
 
 
3784
                If range optimizer was able to construct a "range"
 
3785
                access on this index, then its condition "quick_cond" was
 
3786
                eqivalent to ref_const_cond (*), and we can re-use E(#rows)
 
3787
                from the range optimizer.
 
3788
 
 
3789
                Proof of (*): By properties of range and ref optimizers
 
3790
                quick_cond will be equal or tighther than ref_const_cond.
 
3791
                ref_const_cond already covers "smallest" possible interval -
 
3792
                a singlepoint interval over all keyparts. Therefore,
 
3793
                quick_cond is equivalent to ref_const_cond (if it was an
 
3794
                empty interval we wouldn't have got here).
 
3795
              */
 
3796
              if (table->quick_keys.test(key))
 
3797
                records= (double) table->quick_rows[key];
 
3798
              else
 
3799
              {
 
3800
                /* quick_range couldn't use key! */
 
3801
                records= (double) s->records/rec;
 
3802
              }
 
3803
            }
 
3804
            else
 
3805
            {
 
3806
              if (!(records=keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
3807
              {                                   /* Prefer longer keys */
 
3808
                records=
 
3809
                  ((double) s->records / (double) rec *
 
3810
                   (1.0 +
 
3811
                    ((double) (table->s->max_key_length-keyinfo->key_length) /
 
3812
                     (double) table->s->max_key_length)));
 
3813
                if (records < 2.0)
 
3814
                  records=2.0;               /* Can't be as good as a unique */
 
3815
              }
 
3816
              /*
 
3817
                ReuseRangeEstimateForRef-2:  We get here if we could not reuse
 
3818
                E(#rows) from range optimizer. Make another try:
 
3819
 
 
3820
                If range optimizer produced E(#rows) for a prefix of the ref
 
3821
                access we're considering, and that E(#rows) is lower then our
 
3822
                current estimate, make an adjustment. The criteria of when we
 
3823
                can make an adjustment is a special case of the criteria used
 
3824
                in ReuseRangeEstimateForRef-3.
 
3825
              */
 
3826
              if (table->quick_keys.test(key) &&
 
3827
                  const_part & (1 << table->quick_key_parts[key]) &&
 
3828
                  table->quick_n_ranges[key] == 1 &&
 
3829
                  records > (double) table->quick_rows[key])
 
3830
              {
 
3831
                records= (double) table->quick_rows[key];
 
3832
              }
 
3833
            }
 
3834
            /* Limit the number of matched rows */
 
3835
            tmp= records;
 
3836
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
3837
            if (table->covering_keys.test(key))
 
3838
            {
 
3839
              /* we can use only index tree */
 
3840
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
3841
            }
 
3842
            else
 
3843
              tmp= record_count*cmin(tmp,s->worst_seeks);
 
3844
          }
 
3845
        }
 
3846
        else
 
3847
        {
 
3848
          /*
 
3849
            Use as much key-parts as possible and a uniq key is better
 
3850
            than a not unique key
 
3851
            Set tmp to (previous record count) * (records / combination)
 
3852
          */
 
3853
          if ((found_part & 1) &&
 
3854
              (!(table->file->index_flags(key, 0, 0) & HA_ONLY_WHOLE_INDEX) ||
 
3855
               found_part == PREV_BITS(uint,keyinfo->key_parts)))
 
3856
          {
 
3857
            max_key_part= max_part_bit(found_part);
 
3858
            /*
 
3859
              ReuseRangeEstimateForRef-3:
 
3860
              We're now considering a ref[or_null] access via
 
3861
              (t.keypart1=e1 AND ... AND t.keypartK=eK) [ OR
 
3862
              (same-as-above but with one cond replaced
 
3863
               with "t.keypart_i IS NULL")]  (**)
 
3864
 
 
3865
              Try re-using E(#rows) from "range" optimizer:
 
3866
              We can do so if "range" optimizer used the same intervals as
 
3867
              in (**). The intervals used by range optimizer may be not
 
3868
              available at this point (as "range" access might have choosen to
 
3869
              create quick select over another index), so we can't compare
 
3870
              them to (**). We'll make indirect judgements instead.
 
3871
              The sufficient conditions for re-use are:
 
3872
              (C1) All e_i in (**) are constants, i.e. found_ref==false. (if
 
3873
                   this is not satisfied we have no way to know which ranges
 
3874
                   will be actually scanned by 'ref' until we execute the
 
3875
                   join)
 
3876
              (C2) max #key parts in 'range' access == K == max_key_part (this
 
3877
                   is apparently a necessary requirement)
 
3878
 
 
3879
              We also have a property that "range optimizer produces equal or
 
3880
              tighter set of scan intervals than ref(const) optimizer". Each
 
3881
              of the intervals in (**) are "tightest possible" intervals when
 
3882
              one limits itself to using keyparts 1..K (which we do in #2).
 
3883
              From here it follows that range access used either one, or
 
3884
              both of the (I1) and (I2) intervals:
 
3885
 
 
3886
               (t.keypart1=c1 AND ... AND t.keypartK=eK)  (I1)
 
3887
               (same-as-above but with one cond replaced
 
3888
                with "t.keypart_i IS NULL")               (I2)
 
3889
 
 
3890
              The remaining part is to exclude the situation where range
 
3891
              optimizer used one interval while we're considering
 
3892
              ref-or-null and looking for estimate for two intervals. This
 
3893
              is done by last limitation:
 
3894
 
 
3895
              (C3) "range optimizer used (have ref_or_null?2:1) intervals"
 
3896
            */
 
3897
            if (table->quick_keys.test(key) && !found_ref &&          //(C1)
 
3898
                table->quick_key_parts[key] == max_key_part &&          //(C2)
 
3899
                table->quick_n_ranges[key] == 1+((ref_or_null_part)?1:0)) //(C3)
 
3900
            {
 
3901
              tmp= records= (double) table->quick_rows[key];
 
3902
            }
 
3903
            else
 
3904
            {
 
3905
              /* Check if we have statistic about the distribution */
 
3906
              if ((records= keyinfo->rec_per_key[max_key_part-1]))
 
3907
              {
 
3908
                /*
 
3909
                  Fix for the case where the index statistics is too
 
3910
                  optimistic: If
 
3911
                  (1) We're considering ref(const) and there is quick select
 
3912
                      on the same index,
 
3913
                  (2) and that quick select uses more keyparts (i.e. it will
 
3914
                      scan equal/smaller interval then this ref(const))
 
3915
                  (3) and E(#rows) for quick select is higher then our
 
3916
                      estimate,
 
3917
                  Then
 
3918
                    We'll use E(#rows) from quick select.
 
3919
 
 
3920
                  Q: Why do we choose to use 'ref'? Won't quick select be
 
3921
                  cheaper in some cases ?
 
3922
                  TODO: figure this out and adjust the plan choice if needed.
 
3923
                */
 
3924
                if (!found_ref && table->quick_keys.test(key) &&    // (1)
 
3925
                    table->quick_key_parts[key] > max_key_part &&     // (2)
 
3926
                    records < (double)table->quick_rows[key])         // (3)
 
3927
                  records= (double)table->quick_rows[key];
 
3928
 
 
3929
                tmp= records;
 
3930
              }
 
3931
              else
 
3932
              {
 
3933
                /*
 
3934
                  Assume that the first key part matches 1% of the file
 
3935
                  and that the whole key matches 10 (duplicates) or 1
 
3936
                  (unique) records.
 
3937
                  Assume also that more key matches proportionally more
 
3938
                  records
 
3939
                  This gives the formula:
 
3940
                  records = (x * (b-a) + a*c-b)/(c-1)
 
3941
 
 
3942
                  b = records matched by whole key
 
3943
                  a = records matched by first key part (1% of all records?)
 
3944
                  c = number of key parts in key
 
3945
                  x = used key parts (1 <= x <= c)
 
3946
                */
 
3947
                double rec_per_key;
 
3948
                if (!(rec_per_key=(double)
 
3949
                      keyinfo->rec_per_key[keyinfo->key_parts-1]))
 
3950
                  rec_per_key=(double) s->records/rec+1;
 
3951
 
 
3952
                if (!s->records)
 
3953
                  tmp = 0;
 
3954
                else if (rec_per_key/(double) s->records >= 0.01)
 
3955
                  tmp = rec_per_key;
 
3956
                else
 
3957
                {
 
3958
                  double a=s->records*0.01;
 
3959
                  if (keyinfo->key_parts > 1)
 
3960
                    tmp= (max_key_part * (rec_per_key - a) +
 
3961
                          a*keyinfo->key_parts - rec_per_key)/
 
3962
                         (keyinfo->key_parts-1);
 
3963
                  else
 
3964
                    tmp= a;
 
3965
                  set_if_bigger(tmp,1.0);
 
3966
                }
 
3967
                records = (uint32_t) tmp;
 
3968
              }
 
3969
 
 
3970
              if (ref_or_null_part)
 
3971
              {
 
3972
                /* We need to do two key searches to find key */
 
3973
                tmp *= 2.0;
 
3974
                records *= 2.0;
 
3975
              }
 
3976
 
 
3977
              /*
 
3978
                ReuseRangeEstimateForRef-4:  We get here if we could not reuse
 
3979
                E(#rows) from range optimizer. Make another try:
 
3980
 
 
3981
                If range optimizer produced E(#rows) for a prefix of the ref
 
3982
                access we're considering, and that E(#rows) is lower then our
 
3983
                current estimate, make the adjustment.
 
3984
 
 
3985
                The decision whether we can re-use the estimate from the range
 
3986
                optimizer is the same as in ReuseRangeEstimateForRef-3,
 
3987
                applied to first table->quick_key_parts[key] key parts.
 
3988
              */
 
3989
              if (table->quick_keys.test(key) &&
 
3990
                  table->quick_key_parts[key] <= max_key_part &&
 
3991
                  const_part & (1 << table->quick_key_parts[key]) &&
 
3992
                  table->quick_n_ranges[key] == 1 + ((ref_or_null_part &
 
3993
                                                     const_part) ? 1 : 0) &&
 
3994
                  records > (double) table->quick_rows[key])
 
3995
              {
 
3996
                tmp= records= (double) table->quick_rows[key];
 
3997
              }
 
3998
            }
 
3999
 
 
4000
            /* Limit the number of matched rows */
 
4001
            set_if_smaller(tmp, (double) session->variables.max_seeks_for_key);
 
4002
            if (table->covering_keys.test(key))
 
4003
            {
 
4004
              /* we can use only index tree */
 
4005
              tmp= record_count * table->file->index_only_read_time(key, tmp);
 
4006
            }
 
4007
            else
 
4008
              tmp= record_count * cmin(tmp,s->worst_seeks);
 
4009
          }
 
4010
          else
 
4011
            tmp= best_time;                    // Do nothing
 
4012
        }
 
4013
 
 
4014
        if (sj_inside_out_scan && !start_key)
 
4015
        {
 
4016
          tmp= tmp/2;
 
4017
          if (records)
 
4018
            records= records/2;
 
4019
        }
 
4020
 
 
4021
      }
 
4022
      if (tmp < best_time - records/(double) TIME_FOR_COMPARE)
 
4023
      {
 
4024
        best_time= tmp + records/(double) TIME_FOR_COMPARE;
 
4025
        best= tmp;
 
4026
        best_records= records;
 
4027
        best_key= start_key;
 
4028
        best_max_key_part= max_key_part;
 
4029
        best_ref_depends_map= found_ref;
 
4030
        best_is_sj_inside_out= sj_inside_out_scan;
 
4031
      }
 
4032
    }
 
4033
    records= best_records;
 
4034
  }
 
4035
 
 
4036
  /*
 
4037
    Don't test table scan if it can't be better.
 
4038
    Prefer key lookup if we would use the same key for scanning.
 
4039
 
 
4040
    Don't do a table scan on InnoDB tables, if we can read the used
 
4041
    parts of the row from any of the used index.
 
4042
    This is because table scans uses index and we would not win
 
4043
    anything by using a table scan.
 
4044
 
 
4045
    A word for word translation of the below if-statement in sergefp's
 
4046
    understanding: we check if we should use table scan if:
 
4047
    (1) The found 'ref' access produces more records than a table scan
 
4048
        (or index scan, or quick select), or 'ref' is more expensive than
 
4049
        any of them.
 
4050
    (2) This doesn't hold: the best way to perform table scan is to to perform
 
4051
        'range' access using index IDX, and the best way to perform 'ref'
 
4052
        access is to use the same index IDX, with the same or more key parts.
 
4053
        (note: it is not clear how this rule is/should be extended to
 
4054
        index_merge quick selects)
 
4055
    (3) See above note about InnoDB.
 
4056
    (4) NOT ("FORCE INDEX(...)" is used for table and there is 'ref' access
 
4057
             path, but there is no quick select)
 
4058
        If the condition in the above brackets holds, then the only possible
 
4059
        "table scan" access method is ALL/index (there is no quick select).
 
4060
        Since we have a 'ref' access path, and FORCE INDEX instructs us to
 
4061
        choose it over ALL/index, there is no need to consider a full table
 
4062
        scan.
 
4063
  */
 
4064
  if ((records >= s->found_records || best > s->read_time) &&            // (1)
 
4065
      !(s->quick && best_key && s->quick->index == best_key->key &&      // (2)
 
4066
        best_max_key_part >= s->table->quick_key_parts[best_key->key]) &&// (2)
 
4067
      !((s->table->file->ha_table_flags() & HA_TABLE_SCAN_ON_INDEX) &&   // (3)
 
4068
        ! s->table->covering_keys.none() && best_key && !s->quick) &&// (3)
 
4069
      !(s->table->force_index && best_key && !s->quick))                 // (4)
 
4070
  {                                             // Check full join
 
4071
    ha_rows rnd_records= s->found_records;
 
4072
    /*
 
4073
      If there is a filtering condition on the table (i.e. ref analyzer found
 
4074
      at least one "table.keyXpartY= exprZ", where exprZ refers only to tables
 
4075
      preceding this table in the join order we're now considering), then
 
4076
      assume that 25% of the rows will be filtered out by this condition.
 
4077
 
 
4078
      This heuristic is supposed to force tables used in exprZ to be before
 
4079
      this table in join order.
 
4080
    */
 
4081
    if (found_constraint)
 
4082
      rnd_records-= rnd_records/4;
 
4083
 
 
4084
    /*
 
4085
      If applicable, get a more accurate estimate. Don't use the two
 
4086
      heuristics at once.
 
4087
    */
 
4088
    if (s->table->quick_condition_rows != s->found_records)
 
4089
      rnd_records= s->table->quick_condition_rows;
 
4090
 
 
4091
    /*
 
4092
      Range optimizer never proposes a RANGE if it isn't better
 
4093
      than FULL: so if RANGE is present, it's always preferred to FULL.
 
4094
      Here we estimate its cost.
 
4095
    */
 
4096
    if (s->quick)
 
4097
    {
 
4098
      /*
 
4099
        For each record we:
 
4100
        - read record range through 'quick'
 
4101
        - skip rows which does not satisfy WHERE constraints
 
4102
        TODO:
 
4103
        We take into account possible use of join cache for ALL/index
 
4104
        access (see first else-branch below), but we don't take it into
 
4105
        account here for range/index_merge access. Find out why this is so.
 
4106
      */
 
4107
      tmp= record_count *
 
4108
        (s->quick->read_time +
 
4109
         (s->found_records - rnd_records)/(double) TIME_FOR_COMPARE);
 
4110
    }
 
4111
    else
 
4112
    {
 
4113
      /* Estimate cost of reading table. */
 
4114
      tmp= s->table->file->scan_time();
 
4115
      if (s->table->map & join->outer_join)     // Can't use join cache
 
4116
      {
 
4117
        /*
 
4118
          For each record we have to:
 
4119
          - read the whole table record
 
4120
          - skip rows which does not satisfy join condition
 
4121
        */
 
4122
        tmp= record_count *
 
4123
          (tmp +
 
4124
           (s->records - rnd_records)/(double) TIME_FOR_COMPARE);
 
4125
      }
 
4126
      else
 
4127
      {
 
4128
        /* We read the table as many times as join buffer becomes full. */
 
4129
        tmp*= (1.0 + floor((double) cache_record_length(join,idx) *
 
4130
                           record_count /
 
4131
                           (double) session->variables.join_buff_size));
 
4132
        /*
 
4133
            We don't make full cartesian product between rows in the scanned
 
4134
           table and existing records because we skip all rows from the
 
4135
           scanned table, which does not satisfy join condition when
 
4136
           we read the table (see flush_cached_records for details). Here we
 
4137
           take into account cost to read and skip these records.
 
4138
        */
 
4139
        tmp+= (s->records - rnd_records)/(double) TIME_FOR_COMPARE;
 
4140
      }
 
4141
    }
 
4142
 
 
4143
    /*
 
4144
      We estimate the cost of evaluating WHERE clause for found records
 
4145
      as record_count * rnd_records / TIME_FOR_COMPARE. This cost plus
 
4146
      tmp give us total cost of using Table SCAN
 
4147
    */
 
4148
    if (best == DBL_MAX ||
 
4149
        (tmp  + record_count/(double) TIME_FOR_COMPARE*rnd_records <
 
4150
         best + record_count/(double) TIME_FOR_COMPARE*records))
 
4151
    {
 
4152
      /*
 
4153
        If the table has a range (s->quick is set) make_join_select()
 
4154
        will ensure that this will be used
 
4155
      */
 
4156
      best= tmp;
 
4157
      records= rows2double(rnd_records);
 
4158
      best_key= 0;
 
4159
      /* range/index_merge/ALL/index access method are "independent", so: */
 
4160
      best_ref_depends_map= 0;
 
4161
      best_is_sj_inside_out= false;
 
4162
    }
 
4163
  }
 
4164
 
 
4165
  /* Update the cost information for the current partial plan */
 
4166
  join->positions[idx].records_read= records;
 
4167
  join->positions[idx].read_time=    best;
 
4168
  join->positions[idx].key=          best_key;
 
4169
  join->positions[idx].table=        s;
 
4170
  join->positions[idx].ref_depend_map= best_ref_depends_map;
 
4171
  join->positions[idx].use_insideout_scan= best_is_sj_inside_out;
 
4172
 
 
4173
  if (!best_key &&
 
4174
      idx == join->const_tables &&
 
4175
      s->table == join->sort_by_table &&
 
4176
      join->unit->select_limit_cnt >= records)
 
4177
    join->sort_by_table= (Table*) 1;  // Must use temporary table
 
4178
 
 
4179
  return;
 
4180
}
 
4181
 
 
4182
/**
 
4183
  Select the best ways to access the tables in a query without reordering them.
 
4184
 
 
4185
    Find the best access paths for each query table and compute their costs
 
4186
    according to their order in the array 'join->best_ref' (thus without
 
4187
    reordering the join tables). The function calls sequentially
 
4188
    'best_access_path' for each table in the query to select the best table
 
4189
    access method. The final optimal plan is stored in the array
 
4190
    'join->best_positions', and the corresponding cost in 'join->best_read'.
 
4191
 
 
4192
  @param join          pointer to the structure providing all context info for
 
4193
                       the query
 
4194
  @param join_tables   set of the tables in the query
 
4195
 
 
4196
  @note
 
4197
    This function can be applied to:
 
4198
    - queries with STRAIGHT_JOIN
 
4199
    - internally to compute the cost of an arbitrary QEP
 
4200
  @par
 
4201
    Thus 'optimize_straight_join' can be used at any stage of the query
 
4202
    optimization process to finalize a QEP as it is.
 
4203
*/
 
4204
static void optimize_straight_join(JOIN *join, table_map join_tables)
 
4205
{
 
4206
  JOIN_TAB *s;
 
4207
  uint32_t idx= join->const_tables;
 
4208
  double    record_count= 1.0;
 
4209
  double    read_time=    0.0;
 
4210
 
 
4211
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
4212
  {
 
4213
    /* Find the best access method from 's' to the current partial plan */
 
4214
    advance_sj_state(join_tables, s);
 
4215
    best_access_path(join, s, join->session, join_tables, idx,
 
4216
                     record_count, read_time);
 
4217
    /* compute the cost of the new plan extended with 's' */
 
4218
    record_count*= join->positions[idx].records_read;
 
4219
    read_time+=    join->positions[idx].read_time;
 
4220
    join_tables&= ~(s->table->map);
 
4221
    ++idx;
 
4222
  }
 
4223
 
 
4224
  read_time+= record_count / (double) TIME_FOR_COMPARE;
 
4225
  if (join->sort_by_table &&
 
4226
      join->sort_by_table != join->positions[join->const_tables].table->table)
 
4227
    read_time+= record_count;  // We have to make a temp table
 
4228
  memcpy(join->best_positions, join->positions, sizeof(POSITION)*idx);
 
4229
  join->best_read= read_time;
 
4230
}
 
4231
 
 
4232
/**
 
4233
  Find a good, possibly optimal, query execution plan (QEP) by a greedy search.
 
4234
 
 
4235
    The search procedure uses a hybrid greedy/exhaustive search with controlled
 
4236
    exhaustiveness. The search is performed in N = card(remaining_tables)
 
4237
    steps. Each step evaluates how promising is each of the unoptimized tables,
 
4238
    selects the most promising table, and extends the current partial QEP with
 
4239
    that table.  Currenly the most 'promising' table is the one with least
 
4240
    expensive extension.\
 
4241
 
 
4242
    There are two extreme cases:
 
4243
    -# When (card(remaining_tables) < search_depth), the estimate finds the
 
4244
    best complete continuation of the partial QEP. This continuation can be
 
4245
    used directly as a result of the search.
 
4246
    -# When (search_depth == 1) the 'best_extension_by_limited_search'
 
4247
    consideres the extension of the current QEP with each of the remaining
 
4248
    unoptimized tables.
 
4249
 
 
4250
    All other cases are in-between these two extremes. Thus the parameter
 
4251
    'search_depth' controlls the exhaustiveness of the search. The higher the
 
4252
    value, the longer the optimizaton time and possibly the better the
 
4253
    resulting plan. The lower the value, the fewer alternative plans are
 
4254
    estimated, but the more likely to get a bad QEP.
 
4255
 
 
4256
    All intermediate and final results of the procedure are stored in 'join':
 
4257
    - join->positions     : modified for every partial QEP that is explored
 
4258
    - join->best_positions: modified for the current best complete QEP
 
4259
    - join->best_read     : modified for the current best complete QEP
 
4260
    - join->best_ref      : might be partially reordered
 
4261
 
 
4262
    The final optimal plan is stored in 'join->best_positions', and its
 
4263
    corresponding cost in 'join->best_read'.
 
4264
 
 
4265
  @note
 
4266
    The following pseudocode describes the algorithm of 'greedy_search':
 
4267
 
 
4268
    @code
 
4269
    procedure greedy_search
 
4270
    input: remaining_tables
 
4271
    output: pplan;
 
4272
    {
 
4273
      pplan = <>;
 
4274
      do {
 
4275
        (t, a) = best_extension(pplan, remaining_tables);
 
4276
        pplan = concat(pplan, (t, a));
 
4277
        remaining_tables = remaining_tables - t;
 
4278
      } while (remaining_tables != {})
 
4279
      return pplan;
 
4280
    }
 
4281
 
 
4282
  @endcode
 
4283
    where 'best_extension' is a placeholder for a procedure that selects the
 
4284
    most "promising" of all tables in 'remaining_tables'.
 
4285
    Currently this estimate is performed by calling
 
4286
    'best_extension_by_limited_search' to evaluate all extensions of the
 
4287
    current QEP of size 'search_depth', thus the complexity of 'greedy_search'
 
4288
    mainly depends on that of 'best_extension_by_limited_search'.
 
4289
 
 
4290
  @par
 
4291
    If 'best_extension()' == 'best_extension_by_limited_search()', then the
 
4292
    worst-case complexity of this algorithm is <=
 
4293
    O(N*N^search_depth/search_depth). When serch_depth >= N, then the
 
4294
    complexity of greedy_search is O(N!).
 
4295
 
 
4296
  @par
 
4297
    In the future, 'greedy_search' might be extended to support other
 
4298
    implementations of 'best_extension', e.g. some simpler quadratic procedure.
 
4299
 
 
4300
  @param join             pointer to the structure providing all context info
 
4301
                          for the query
 
4302
  @param remaining_tables set of tables not included into the partial plan yet
 
4303
  @param search_depth     controlls the exhaustiveness of the search
 
4304
  @param prune_level      the pruning heuristics that should be applied during
 
4305
                          search
 
4306
 
 
4307
  @retval
 
4308
    false       ok
 
4309
  @retval
 
4310
    true        Fatal error
 
4311
*/
 
4312
static bool greedy_search(JOIN      *join,
 
4313
              table_map remaining_tables,
 
4314
              uint32_t      search_depth,
 
4315
              uint32_t      prune_level)
 
4316
{
 
4317
  double    record_count= 1.0;
 
4318
  double    read_time=    0.0;
 
4319
  uint32_t      idx= join->const_tables; // index into 'join->best_ref'
 
4320
  uint32_t      best_idx;
 
4321
  uint32_t      size_remain;    // cardinality of remaining_tables
 
4322
  POSITION  best_pos;
 
4323
  JOIN_TAB  *best_table; // the next plan node to be added to the curr QEP
 
4324
 
 
4325
  /* number of tables that remain to be optimized */
 
4326
  size_remain= my_count_bits(remaining_tables);
 
4327
 
 
4328
  do {
 
4329
    /* Find the extension of the current QEP with the lowest cost */
 
4330
    join->best_read= DBL_MAX;
 
4331
    if (best_extension_by_limited_search(join, remaining_tables, idx, record_count,
 
4332
                                         read_time, search_depth, prune_level))
 
4333
      return(true);
 
4334
 
 
4335
    if (size_remain <= search_depth)
 
4336
    {
 
4337
      /*
 
4338
        'join->best_positions' contains a complete optimal extension of the
 
4339
        current partial QEP.
 
4340
      */
 
4341
      return(false);
 
4342
    }
 
4343
 
 
4344
    /* select the first table in the optimal extension as most promising */
 
4345
    best_pos= join->best_positions[idx];
 
4346
    best_table= best_pos.table;
 
4347
    /*
 
4348
      Each subsequent loop of 'best_extension_by_limited_search' uses
 
4349
      'join->positions' for cost estimates, therefore we have to update its
 
4350
      value.
 
4351
    */
 
4352
    join->positions[idx]= best_pos;
 
4353
 
 
4354
    /* find the position of 'best_table' in 'join->best_ref' */
 
4355
    best_idx= idx;
 
4356
    JOIN_TAB *pos= join->best_ref[best_idx];
 
4357
    while (pos && best_table != pos)
 
4358
      pos= join->best_ref[++best_idx];
 
4359
    assert((pos != NULL)); // should always find 'best_table'
 
4360
    /* move 'best_table' at the first free position in the array of joins */
 
4361
    std::swap(join->best_ref[idx], join->best_ref[best_idx]);
 
4362
 
 
4363
    /* compute the cost of the new plan extended with 'best_table' */
 
4364
    record_count*= join->positions[idx].records_read;
 
4365
    read_time+=    join->positions[idx].read_time;
 
4366
 
 
4367
    remaining_tables&= ~(best_table->table->map);
 
4368
    --size_remain;
 
4369
    ++idx;
 
4370
  } while (true);
 
4371
}
 
4372
 
 
4373
 
 
4374
/**
 
4375
  Find a good, possibly optimal, query execution plan (QEP) by a possibly
 
4376
  exhaustive search.
 
4377
 
 
4378
    The procedure searches for the optimal ordering of the query tables in set
 
4379
    'remaining_tables' of size N, and the corresponding optimal access paths to
 
4380
    each table. The choice of a table order and an access path for each table
 
4381
    constitutes a query execution plan (QEP) that fully specifies how to
 
4382
    execute the query.
 
4383
 
 
4384
    The maximal size of the found plan is controlled by the parameter
 
4385
    'search_depth'. When search_depth == N, the resulting plan is complete and
 
4386
    can be used directly as a QEP. If search_depth < N, the found plan consists
 
4387
    of only some of the query tables. Such "partial" optimal plans are useful
 
4388
    only as input to query optimization procedures, and cannot be used directly
 
4389
    to execute a query.
 
4390
 
 
4391
    The algorithm begins with an empty partial plan stored in 'join->positions'
 
4392
    and a set of N tables - 'remaining_tables'. Each step of the algorithm
 
4393
    evaluates the cost of the partial plan extended by all access plans for
 
4394
    each of the relations in 'remaining_tables', expands the current partial
 
4395
    plan with the access plan that results in lowest cost of the expanded
 
4396
    partial plan, and removes the corresponding relation from
 
4397
    'remaining_tables'. The algorithm continues until it either constructs a
 
4398
    complete optimal plan, or constructs an optimal plartial plan with size =
 
4399
    search_depth.
 
4400
 
 
4401
    The final optimal plan is stored in 'join->best_positions'. The
 
4402
    corresponding cost of the optimal plan is in 'join->best_read'.
 
4403
 
 
4404
  @note
 
4405
    The procedure uses a recursive depth-first search where the depth of the
 
4406
    recursion (and thus the exhaustiveness of the search) is controlled by the
 
4407
    parameter 'search_depth'.
 
4408
 
 
4409
  @note
 
4410
    The pseudocode below describes the algorithm of
 
4411
    'best_extension_by_limited_search'. The worst-case complexity of this
 
4412
    algorithm is O(N*N^search_depth/search_depth). When serch_depth >= N, then
 
4413
    the complexity of greedy_search is O(N!).
 
4414
 
 
4415
    @code
 
4416
    procedure best_extension_by_limited_search(
 
4417
      pplan in,             // in, partial plan of tables-joined-so-far
 
4418
      pplan_cost,           // in, cost of pplan
 
4419
      remaining_tables,     // in, set of tables not referenced in pplan
 
4420
      best_plan_so_far,     // in/out, best plan found so far
 
4421
      best_plan_so_far_cost,// in/out, cost of best_plan_so_far
 
4422
      search_depth)         // in, maximum size of the plans being considered
 
4423
    {
 
4424
      for each table T from remaining_tables
 
4425
      {
 
4426
        // Calculate the cost of using table T as above
 
4427
        cost = complex-series-of-calculations;
 
4428
 
 
4429
        // Add the cost to the cost so far.
 
4430
        pplan_cost+= cost;
 
4431
 
 
4432
        if (pplan_cost >= best_plan_so_far_cost)
 
4433
          // pplan_cost already too great, stop search
 
4434
          continue;
 
4435
 
 
4436
        pplan= expand pplan by best_access_method;
 
4437
        remaining_tables= remaining_tables - table T;
 
4438
        if (remaining_tables is not an empty set
 
4439
            and
 
4440
            search_depth > 1)
 
4441
        {
 
4442
          best_extension_by_limited_search(pplan, pplan_cost,
 
4443
                                           remaining_tables,
 
4444
                                           best_plan_so_far,
 
4445
                                           best_plan_so_far_cost,
 
4446
                                           search_depth - 1);
 
4447
        }
 
4448
        else
 
4449
        {
 
4450
          best_plan_so_far_cost= pplan_cost;
 
4451
          best_plan_so_far= pplan;
 
4452
        }
 
4453
      }
 
4454
    }
 
4455
    @endcode
 
4456
 
 
4457
  @note
 
4458
    When 'best_extension_by_limited_search' is called for the first time,
 
4459
    'join->best_read' must be set to the largest possible value (e.g. DBL_MAX).
 
4460
    The actual implementation provides a way to optionally use pruning
 
4461
    heuristic (controlled by the parameter 'prune_level') to reduce the search
 
4462
    space by skipping some partial plans.
 
4463
 
 
4464
  @note
 
4465
    The parameter 'search_depth' provides control over the recursion
 
4466
    depth, and thus the size of the resulting optimal plan.
 
4467
 
 
4468
  @param join             pointer to the structure providing all context info
 
4469
                          for the query
 
4470
  @param remaining_tables set of tables not included into the partial plan yet
 
4471
  @param idx              length of the partial QEP in 'join->positions';
 
4472
                          since a depth-first search is used, also corresponds
 
4473
                          to the current depth of the search tree;
 
4474
                          also an index in the array 'join->best_ref';
 
4475
  @param record_count     estimate for the number of records returned by the
 
4476
                          best partial plan
 
4477
  @param read_time        the cost of the best partial plan
 
4478
  @param search_depth     maximum depth of the recursion and thus size of the
 
4479
                          found optimal plan
 
4480
                          (0 < search_depth <= join->tables+1).
 
4481
  @param prune_level      pruning heuristics that should be applied during
 
4482
                          optimization
 
4483
                          (values: 0 = EXHAUSTIVE, 1 = PRUNE_BY_TIME_OR_ROWS)
 
4484
 
 
4485
  @retval
 
4486
    false       ok
 
4487
  @retval
 
4488
    true        Fatal error
 
4489
*/
 
4490
static bool best_extension_by_limited_search(JOIN *join,
 
4491
                                             table_map remaining_tables,
 
4492
                                             uint32_t idx,
 
4493
                                             double record_count,
 
4494
                                             double read_time,
 
4495
                                             uint32_t search_depth,
 
4496
                                             uint32_t prune_level)
 
4497
{
 
4498
  Session *session= join->session;
 
4499
  if (session->killed)  // Abort
 
4500
    return(true);
 
4501
 
 
4502
  /*
 
4503
     'join' is a partial plan with lower cost than the best plan so far,
 
4504
     so continue expanding it further with the tables in 'remaining_tables'.
 
4505
  */
 
4506
  JOIN_TAB *s;
 
4507
  double best_record_count= DBL_MAX;
 
4508
  double best_read_time=    DBL_MAX;
 
4509
 
 
4510
  for (JOIN_TAB **pos= join->best_ref + idx ; (s= *pos) ; pos++)
 
4511
  {
 
4512
    table_map real_table_bit= s->table->map;
 
4513
    if ((remaining_tables & real_table_bit) &&
 
4514
        !(remaining_tables & s->dependent) &&
 
4515
        (!idx || !check_interleaving_with_nj(join->positions[idx-1].table, s)))
 
4516
    {
 
4517
      double current_record_count, current_read_time;
 
4518
      advance_sj_state(remaining_tables, s);
 
4519
 
 
4520
      /*
 
4521
        psergey-insideout-todo:
 
4522
          when best_access_path() detects it could do an InsideOut scan or
 
4523
          some other scan, have it return an insideout scan and a flag that
 
4524
          requests to "fork" this loop iteration. (Q: how does that behave
 
4525
          when the depth is insufficient??)
 
4526
      */
 
4527
      /* Find the best access method from 's' to the current partial plan */
 
4528
      best_access_path(join, s, session, remaining_tables, idx,
 
4529
                       record_count, read_time);
 
4530
      /* Compute the cost of extending the plan with 's' */
 
4531
      current_record_count= record_count * join->positions[idx].records_read;
 
4532
      current_read_time=    read_time + join->positions[idx].read_time;
 
4533
 
 
4534
      /* Expand only partial plans with lower cost than the best QEP so far */
 
4535
      if ((current_read_time +
 
4536
           current_record_count / (double) TIME_FOR_COMPARE) >= join->best_read)
 
4537
      {
 
4538
        restore_prev_nj_state(s);
 
4539
        restore_prev_sj_state(remaining_tables, s);
 
4540
        continue;
 
4541
      }
 
4542
 
 
4543
      /*
 
4544
        Prune some less promising partial plans. This heuristic may miss
 
4545
        the optimal QEPs, thus it results in a non-exhaustive search.
 
4546
      */
 
4547
      if (prune_level == 1)
 
4548
      {
 
4549
        if (best_record_count > current_record_count ||
 
4550
            best_read_time > current_read_time ||
 
4551
            (idx == join->const_tables && s->table == join->sort_by_table)) // 's' is the first table in the QEP
 
4552
        {
 
4553
          if (best_record_count >= current_record_count &&
 
4554
              best_read_time >= current_read_time &&
 
4555
              /* TODO: What is the reasoning behind this condition? */
 
4556
              (!(s->key_dependent & remaining_tables) ||
 
4557
               join->positions[idx].records_read < 2.0))
 
4558
          {
 
4559
            best_record_count= current_record_count;
 
4560
            best_read_time=    current_read_time;
 
4561
          }
 
4562
        }
 
4563
        else
 
4564
        {
 
4565
          restore_prev_nj_state(s);
 
4566
          restore_prev_sj_state(remaining_tables, s);
 
4567
          continue;
 
4568
        }
 
4569
      }
 
4570
 
 
4571
      if ( (search_depth > 1) && (remaining_tables & ~real_table_bit) )
 
4572
      { /* Recursively expand the current partial plan */
 
4573
        std::swap(join->best_ref[idx], *pos);
 
4574
        if (best_extension_by_limited_search(join,
 
4575
                                             remaining_tables & ~real_table_bit,
 
4576
                                             idx + 1,
 
4577
                                             current_record_count,
 
4578
                                             current_read_time,
 
4579
                                             search_depth - 1,
 
4580
                                             prune_level))
 
4581
          return(true);
 
4582
        std::swap(join->best_ref[idx], *pos);
 
4583
      }
 
4584
      else
 
4585
      { /*
 
4586
          'join' is either the best partial QEP with 'search_depth' relations,
 
4587
          or the best complete QEP so far, whichever is smaller.
 
4588
        */
 
4589
        current_read_time+= current_record_count / (double) TIME_FOR_COMPARE;
 
4590
        if (join->sort_by_table &&
 
4591
            join->sort_by_table !=
 
4592
            join->positions[join->const_tables].table->table)
 
4593
          /* We have to make a temp table */
 
4594
          current_read_time+= current_record_count;
 
4595
        if ((search_depth == 1) || (current_read_time < join->best_read))
 
4596
        {
 
4597
          memcpy(join->best_positions, join->positions,
 
4598
                 sizeof(POSITION) * (idx + 1));
 
4599
          join->best_read= current_read_time - 0.001;
 
4600
        }
 
4601
      }
 
4602
      restore_prev_nj_state(s);
 
4603
      restore_prev_sj_state(remaining_tables, s);
 
4604
    }
 
4605
  }
 
4606
  return(false);
 
4607
}
 
4608
 
 
4609
/**
 
4610
  Heuristic procedure to automatically guess a reasonable degree of
 
4611
  exhaustiveness for the greedy search procedure.
 
4612
 
 
4613
  The procedure estimates the optimization time and selects a search depth
 
4614
  big enough to result in a near-optimal QEP, that doesn't take too long to
 
4615
  find. If the number of tables in the query exceeds some constant, then
 
4616
  search_depth is set to this constant.
 
4617
 
 
4618
  @param join   pointer to the structure providing all context info for
 
4619
                the query
 
4620
 
 
4621
  @note
 
4622
    This is an extremely simplistic implementation that serves as a stub for a
 
4623
    more advanced analysis of the join. Ideally the search depth should be
 
4624
    determined by learning from previous query optimizations, because it will
 
4625
    depend on the CPU power (and other factors).
 
4626
 
 
4627
  @todo
 
4628
    this value should be determined dynamically, based on statistics:
 
4629
    uint32_t max_tables_for_exhaustive_opt= 7;
 
4630
 
 
4631
  @todo
 
4632
    this value could be determined by some mapping of the form:
 
4633
    depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
4634
 
 
4635
  @return
 
4636
    A positive integer that specifies the search depth (and thus the
 
4637
    exhaustiveness) of the depth-first search algorithm used by
 
4638
    'greedy_search'.
 
4639
*/
 
4640
static uint32_t determine_search_depth(JOIN *join)
 
4641
{
 
4642
  uint32_t table_count=  join->tables - join->const_tables;
 
4643
  uint32_t search_depth;
 
4644
  /* TODO: this value should be determined dynamically, based on statistics: */
 
4645
  uint32_t max_tables_for_exhaustive_opt= 7;
 
4646
 
 
4647
  if (table_count <= max_tables_for_exhaustive_opt)
 
4648
    search_depth= table_count+1; // use exhaustive for small number of tables
 
4649
  else
 
4650
    /*
 
4651
      TODO: this value could be determined by some mapping of the form:
 
4652
      depth : table_count -> [max_tables_for_exhaustive_opt..MAX_EXHAUSTIVE]
 
4653
    */
 
4654
    search_depth= max_tables_for_exhaustive_opt; // use greedy search
 
4655
 
 
4656
  return search_depth;
 
4657
}
 
4658
 
 
4659
static bool make_simple_join(JOIN *join,Table *tmp_table)
 
4660
{
 
4661
  Table **tableptr;
 
4662
  JOIN_TAB *join_tab;
 
4663
 
 
4664
  /*
 
4665
    Reuse Table * and JOIN_TAB if already allocated by a previous call
 
4666
    to this function through JOIN::exec (may happen for sub-queries).
 
4667
  */
 
4668
  if (!join->table_reexec)
 
4669
  {
 
4670
    if (!(join->table_reexec= (Table**) join->session->alloc(sizeof(Table*))))
 
4671
      return(true);                        /* purecov: inspected */
 
4672
    if (join->tmp_join)
 
4673
      join->tmp_join->table_reexec= join->table_reexec;
 
4674
  }
 
4675
  if (!join->join_tab_reexec)
 
4676
  {
 
4677
    if (!(join->join_tab_reexec=
 
4678
          (JOIN_TAB*) join->session->alloc(sizeof(JOIN_TAB))))
 
4679
      return(true);                        /* purecov: inspected */
 
4680
    if (join->tmp_join)
 
4681
      join->tmp_join->join_tab_reexec= join->join_tab_reexec;
 
4682
  }
 
4683
  tableptr= join->table_reexec;
 
4684
  join_tab= join->join_tab_reexec;
 
4685
 
 
4686
  join->join_tab=join_tab;
 
4687
  join->table=tableptr; tableptr[0]=tmp_table;
 
4688
  join->tables=1;
 
4689
  join->const_tables=0;
 
4690
  join->const_table_map=0;
 
4691
  join->tmp_table_param.field_count= join->tmp_table_param.sum_func_count=
 
4692
    join->tmp_table_param.func_count=0;
 
4693
  join->tmp_table_param.copy_field=join->tmp_table_param.copy_field_end=0;
 
4694
  join->first_record=join->sort_and_group=0;
 
4695
  join->send_records=(ha_rows) 0;
 
4696
  join->group=0;
 
4697
  join->row_limit=join->unit->select_limit_cnt;
 
4698
  join->do_send_rows = (join->row_limit) ? 1 : 0;
 
4699
 
 
4700
  join_tab->cache.buff=0;                       /* No caching */
 
4701
  join_tab->table=tmp_table;
 
4702
  join_tab->select=0;
 
4703
  join_tab->select_cond=0;
 
4704
  join_tab->quick=0;
 
4705
  join_tab->type= JT_ALL;                       /* Map through all records */
 
4706
  join_tab->keys.set();                     /* test everything in quick */
 
4707
  join_tab->info=0;
 
4708
  join_tab->on_expr_ref=0;
 
4709
  join_tab->last_inner= 0;
 
4710
  join_tab->first_unmatched= 0;
 
4711
  join_tab->ref.key = -1;
 
4712
  join_tab->not_used_in_distinct=0;
 
4713
  join_tab->read_first_record= join_init_read_record;
 
4714
  join_tab->join=join;
 
4715
  join_tab->ref.key_parts= 0;
 
4716
  join_tab->flush_weedout_table= join_tab->check_weed_out_table= NULL;
 
4717
  join_tab->do_firstmatch= NULL;
 
4718
  memset(&join_tab->read_record, 0, sizeof(join_tab->read_record));
 
4719
  tmp_table->status=0;
 
4720
  tmp_table->null_row=0;
 
4721
  return(false);
 
4722
}
 
4723
 
 
4724
/**
 
4725
  Fill in outer join related info for the execution plan structure.
 
4726
 
 
4727
    For each outer join operation left after simplification of the
 
4728
    original query the function set up the following pointers in the linear
 
4729
    structure join->join_tab representing the selected execution plan.
 
4730
    The first inner table t0 for the operation is set to refer to the last
 
4731
    inner table tk through the field t0->last_inner.
 
4732
    Any inner table ti for the operation are set to refer to the first
 
4733
    inner table ti->first_inner.
 
4734
    The first inner table t0 for the operation is set to refer to the
 
4735
    first inner table of the embedding outer join operation, if there is any,
 
4736
    through the field t0->first_upper.
 
4737
    The on expression for the outer join operation is attached to the
 
4738
    corresponding first inner table through the field t0->on_expr_ref.
 
4739
    Here ti are structures of the JOIN_TAB type.
 
4740
 
 
4741
  EXAMPLE. For the query:
 
4742
  @code
 
4743
        SELECT * FROM t1
 
4744
                      LEFT JOIN
 
4745
                      (t2, t3 LEFT JOIN t4 ON t3.a=t4.a)
 
4746
                      ON (t1.a=t2.a AND t1.b=t3.b)
 
4747
          WHERE t1.c > 5,
 
4748
  @endcode
 
4749
 
 
4750
    given the execution plan with the table order t1,t2,t3,t4
 
4751
    is selected, the following references will be set;
 
4752
    t4->last_inner=[t4], t4->first_inner=[t4], t4->first_upper=[t2]
 
4753
    t2->last_inner=[t4], t2->first_inner=t3->first_inner=[t2],
 
4754
    on expression (t1.a=t2.a AND t1.b=t3.b) will be attached to
 
4755
    *t2->on_expr_ref, while t3.a=t4.a will be attached to *t4->on_expr_ref.
 
4756
 
 
4757
  @param join   reference to the info fully describing the query
 
4758
 
 
4759
  @note
 
4760
    The function assumes that the simplification procedure has been
 
4761
    already applied to the join query (see simplify_joins).
 
4762
    This function can be called only after the execution plan
 
4763
    has been chosen.
 
4764
*/
 
4765
static void make_outerjoin_info(JOIN *join)
 
4766
{
 
4767
  for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
4768
  {
 
4769
    JOIN_TAB *tab=join->join_tab+i;
 
4770
    Table *table=tab->table;
 
4771
    TableList *tbl= table->pos_in_table_list;
 
4772
    TableList *embedding= tbl->embedding;
 
4773
 
 
4774
    if (tbl->outer_join)
 
4775
    {
 
4776
      /*
 
4777
        Table tab is the only one inner table for outer join.
 
4778
        (Like table t4 for the table reference t3 LEFT JOIN t4 ON t3.a=t4.a
 
4779
        is in the query above.)
 
4780
      */
 
4781
      tab->last_inner= tab->first_inner= tab;
 
4782
      tab->on_expr_ref= &tbl->on_expr;
 
4783
      tab->cond_equal= tbl->cond_equal;
 
4784
      if (embedding)
 
4785
        tab->first_upper= embedding->nested_join->first_nested;
 
4786
    }
 
4787
    for ( ; embedding ; embedding= embedding->embedding)
 
4788
    {
 
4789
      /* Ignore sj-nests: */
 
4790
      if (!embedding->on_expr)
 
4791
        continue;
 
4792
      nested_join_st *nested_join= embedding->nested_join;
 
4793
      if (!nested_join->counter_)
 
4794
      {
 
4795
        /*
 
4796
          Table tab is the first inner table for nested_join.
 
4797
          Save reference to it in the nested join structure.
 
4798
        */
 
4799
        nested_join->first_nested= tab;
 
4800
        tab->on_expr_ref= &embedding->on_expr;
 
4801
        tab->cond_equal= tbl->cond_equal;
 
4802
        if (embedding->embedding)
 
4803
          tab->first_upper= embedding->embedding->nested_join->first_nested;
 
4804
      }
 
4805
      if (!tab->first_inner)
 
4806
        tab->first_inner= nested_join->first_nested;
 
4807
      if (++nested_join->counter_ < nested_join->join_list.elements)
 
4808
        break;
 
4809
      /* Table tab is the last inner table for nested join. */
 
4810
      nested_join->first_nested->last_inner= tab;
 
4811
    }
 
4812
  }
 
4813
  return;
 
4814
}
 
4815
 
 
4816
static bool make_join_select(JOIN *join,SQL_SELECT *select,COND *cond)
 
4817
{
 
4818
  Session *session= join->session;
 
4819
  if (select)
 
4820
  {
 
4821
    add_not_null_conds(join);
 
4822
    table_map used_tables;
 
4823
    if (cond)                /* Because of QUICK_GROUP_MIN_MAX_SELECT */
 
4824
    {                        /* there may be a select without a cond. */
 
4825
      if (join->tables > 1)
 
4826
        cond->update_used_tables();             // Tablenr may have changed
 
4827
      if (join->const_tables == join->tables &&
 
4828
          session->lex->current_select->master_unit() ==
 
4829
          &session->lex->unit)          // not upper level SELECT
 
4830
        join->const_table_map|=RAND_TABLE_BIT;
 
4831
      {                                         // Check const tables
 
4832
        COND *const_cond=
 
4833
          make_cond_for_table(cond,
 
4834
                              join->const_table_map,
 
4835
                              (table_map) 0, 1);
 
4836
        for (JOIN_TAB *tab= join->join_tab+join->const_tables;
 
4837
             tab < join->join_tab+join->tables ; tab++)
 
4838
        {
 
4839
          if (*tab->on_expr_ref)
 
4840
          {
 
4841
            JOIN_TAB *cond_tab= tab->first_inner;
 
4842
            COND *tmp= make_cond_for_table(*tab->on_expr_ref,
 
4843
                                           join->const_table_map,
 
4844
                                           (  table_map) 0, 0);
 
4845
            if (!tmp)
 
4846
              continue;
 
4847
            tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
4848
            if (!tmp)
 
4849
              return(1);
 
4850
            tmp->quick_fix_field();
 
4851
            cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
4852
                                    new Item_cond_and(cond_tab->select_cond,
 
4853
                                                      tmp);
 
4854
            if (!cond_tab->select_cond)
 
4855
              return(1);
 
4856
            cond_tab->select_cond->quick_fix_field();
 
4857
          }
 
4858
        }
 
4859
        if (const_cond && !const_cond->val_int())
 
4860
        {
 
4861
          return(1);     // Impossible const condition
 
4862
        }
 
4863
      }
 
4864
    }
 
4865
    used_tables=((select->const_tables=join->const_table_map) |
 
4866
                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
4867
    for (uint32_t i=join->const_tables ; i < join->tables ; i++)
 
4868
    {
 
4869
      JOIN_TAB *tab=join->join_tab+i;
 
4870
      /*
 
4871
        first_inner is the X in queries like:
 
4872
        SELECT * FROM t1 LEFT OUTER JOIN (t2 JOIN t3) ON X
 
4873
      */
 
4874
      JOIN_TAB *first_inner_tab= tab->first_inner;
 
4875
      table_map current_map= tab->table->map;
 
4876
      bool use_quick_range=0;
 
4877
      COND *tmp;
 
4878
 
 
4879
      /*
 
4880
        Following force including random expression in last table condition.
 
4881
        It solve problem with select like SELECT * FROM t1 WHERE rand() > 0.5
 
4882
      */
 
4883
      if (i == join->tables-1)
 
4884
        current_map|= OUTER_REF_TABLE_BIT | RAND_TABLE_BIT;
 
4885
      used_tables|=current_map;
 
4886
 
 
4887
      if (tab->type == JT_REF && tab->quick &&
 
4888
          (uint32_t) tab->ref.key == tab->quick->index &&
 
4889
          tab->ref.key_length < tab->quick->max_used_key_length)
 
4890
      {
 
4891
        /* Range uses longer key;  Use this instead of ref on key */
 
4892
        tab->type=JT_ALL;
 
4893
        use_quick_range=1;
 
4894
        tab->use_quick=1;
 
4895
        tab->ref.key= -1;
 
4896
        tab->ref.key_parts=0;           // Don't use ref key.
 
4897
        join->best_positions[i].records_read= rows2double(tab->quick->records);
 
4898
        /*
 
4899
          We will use join cache here : prevent sorting of the first
 
4900
          table only and sort at the end.
 
4901
        */
 
4902
        if (i != join->const_tables && join->tables > join->const_tables + 1)
 
4903
          join->full_join= 1;
 
4904
      }
 
4905
 
 
4906
      tmp= NULL;
 
4907
      if (cond)
 
4908
        tmp= make_cond_for_table(cond,used_tables,current_map, 0);
 
4909
      if (cond && !tmp && tab->quick)
 
4910
      {                                         // Outer join
 
4911
        if (tab->type != JT_ALL)
 
4912
        {
 
4913
          /*
 
4914
            Don't use the quick method
 
4915
            We come here in the case where we have 'key=constant' and
 
4916
            the test is removed by make_cond_for_table()
 
4917
          */
 
4918
          delete tab->quick;
 
4919
          tab->quick= 0;
 
4920
        }
 
4921
        else
 
4922
        {
 
4923
          /*
 
4924
            Hack to handle the case where we only refer to a table
 
4925
            in the ON part of an OUTER JOIN. In this case we want the code
 
4926
            below to check if we should use 'quick' instead.
 
4927
          */
 
4928
          tmp= new Item_int((int64_t) 1,1);     // Always true
 
4929
        }
 
4930
 
 
4931
      }
 
4932
      if (tmp || !cond || tab->type == JT_REF || tab->type == JT_REF_OR_NULL ||
 
4933
          tab->type == JT_EQ_REF)
 
4934
      {
 
4935
        SQL_SELECT *sel= tab->select= ((SQL_SELECT*)
 
4936
                                       session->memdup((unsigned char*) select,
 
4937
                                                   sizeof(*select)));
 
4938
        if (!sel)
 
4939
          return(1);                    // End of memory
 
4940
        /*
 
4941
          If tab is an inner table of an outer join operation,
 
4942
          add a match guard to the pushed down predicate.
 
4943
          The guard will turn the predicate on only after
 
4944
          the first match for outer tables is encountered.
 
4945
        */
 
4946
        if (cond && tmp)
 
4947
        {
 
4948
          /*
 
4949
            Because of QUICK_GROUP_MIN_MAX_SELECT there may be a select without
 
4950
            a cond, so neutralize the hack above.
 
4951
          */
 
4952
          if (!(tmp= add_found_match_trig_cond(first_inner_tab, tmp, 0)))
 
4953
            return(1);
 
4954
          tab->select_cond=sel->cond=tmp;
 
4955
          /* Push condition to storage engine if this is enabled
 
4956
             and the condition is not guarded */
 
4957
          tab->table->file->pushed_cond= NULL;
 
4958
          if (session->variables.engine_condition_pushdown)
 
4959
          {
 
4960
            COND *push_cond=
 
4961
              make_cond_for_table(tmp, current_map, current_map, 0);
 
4962
            if (push_cond)
 
4963
            {
 
4964
              /* Push condition to handler */
 
4965
              if (!tab->table->file->cond_push(push_cond))
 
4966
                tab->table->file->pushed_cond= push_cond;
 
4967
            }
 
4968
          }
 
4969
        }
 
4970
        else
 
4971
          tab->select_cond= sel->cond= NULL;
 
4972
 
 
4973
        sel->head=tab->table;
 
4974
        if (tab->quick)
 
4975
        {
 
4976
          /* Use quick key read if it's a constant and it's not used
 
4977
             with key reading */
 
4978
          if (tab->needed_reg.none() && tab->type != JT_EQ_REF
 
4979
              && (tab->type != JT_REF || (uint32_t) tab->ref.key == tab->quick->index))
 
4980
          {
 
4981
            sel->quick=tab->quick;              // Use value from get_quick_...
 
4982
            sel->quick_keys.reset();
 
4983
            sel->needed_reg.reset();
 
4984
          }
 
4985
          else
 
4986
          {
 
4987
            delete tab->quick;
 
4988
          }
 
4989
          tab->quick=0;
 
4990
        }
 
4991
        uint32_t ref_key=(uint32_t) sel->head->reginfo.join_tab->ref.key+1;
 
4992
        if (i == join->const_tables && ref_key)
 
4993
        {
 
4994
          if (tab->const_keys.any() &&
 
4995
              tab->table->reginfo.impossible_range)
 
4996
            return(1);
 
4997
        }
 
4998
        else if (tab->type == JT_ALL && ! use_quick_range)
 
4999
        {
 
5000
          if (tab->const_keys.any() &&
 
5001
              tab->table->reginfo.impossible_range)
 
5002
            return(1);                          // Impossible range
 
5003
          /*
 
5004
            We plan to scan all rows.
 
5005
            Check again if we should use an index.
 
5006
            We could have used an column from a previous table in
 
5007
            the index if we are using limit and this is the first table
 
5008
          */
 
5009
 
 
5010
          if ((cond && (!((tab->keys & tab->const_keys) == tab->keys) && i > 0)) ||
 
5011
              (!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)))
 
5012
          {
 
5013
            /* Join with outer join condition */
 
5014
            COND *orig_cond=sel->cond;
 
5015
            sel->cond= and_conds(sel->cond, *tab->on_expr_ref);
 
5016
 
 
5017
            /*
 
5018
              We can't call sel->cond->fix_fields,
 
5019
              as it will break tab->on_expr if it's AND condition
 
5020
              (fix_fields currently removes extra AND/OR levels).
 
5021
              Yet attributes of the just built condition are not needed.
 
5022
              Thus we call sel->cond->quick_fix_field for safety.
 
5023
            */
 
5024
            if (sel->cond && !sel->cond->fixed)
 
5025
              sel->cond->quick_fix_field();
 
5026
 
 
5027
            if (sel->test_quick_select(session, tab->keys,
 
5028
                                       used_tables & ~ current_map,
 
5029
                                       (join->select_options &
 
5030
                                        OPTION_FOUND_ROWS ?
 
5031
                                        HA_POS_ERROR :
 
5032
                                        join->unit->select_limit_cnt), 0,
 
5033
                                        false) < 0)
 
5034
            {
 
5035
              /*
 
5036
                Before reporting "Impossible WHERE" for the whole query
 
5037
                we have to check isn't it only "impossible ON" instead
 
5038
              */
 
5039
              sel->cond=orig_cond;
 
5040
              if (!*tab->on_expr_ref ||
 
5041
                  sel->test_quick_select(session, tab->keys,
 
5042
                                         used_tables & ~ current_map,
 
5043
                                         (join->select_options &
 
5044
                                          OPTION_FOUND_ROWS ?
 
5045
                                          HA_POS_ERROR :
 
5046
                                          join->unit->select_limit_cnt),0,
 
5047
                                          false) < 0)
 
5048
                return(1);                      // Impossible WHERE
 
5049
            }
 
5050
            else
 
5051
              sel->cond=orig_cond;
 
5052
 
 
5053
            /* Fix for EXPLAIN */
 
5054
            if (sel->quick)
 
5055
              join->best_positions[i].records_read= (double)sel->quick->records;
 
5056
          }
 
5057
          else
 
5058
          {
 
5059
            sel->needed_reg=tab->needed_reg;
 
5060
            sel->quick_keys.reset();
 
5061
          }
 
5062
          if (!((tab->checked_keys & sel->quick_keys) == sel->quick_keys) ||
 
5063
              !((tab->checked_keys & sel->needed_reg) == sel->needed_reg))
 
5064
          {
 
5065
            tab->keys= sel->quick_keys;
 
5066
            tab->keys|= sel->needed_reg;
 
5067
            tab->use_quick= (!sel->needed_reg.none() &&
 
5068
                             (select->quick_keys.none() ||
 
5069
                              (select->quick &&
 
5070
                               (select->quick->records >= 100L)))) ?
 
5071
              2 : 1;
 
5072
            sel->read_tables= used_tables & ~current_map;
 
5073
          }
 
5074
          if (i != join->const_tables && tab->use_quick != 2)
 
5075
          {                                     /* Read with cache */
 
5076
            if (cond &&
 
5077
                (tmp=make_cond_for_table(cond,
 
5078
                                         join->const_table_map |
 
5079
                                         current_map,
 
5080
                                         current_map, 0)))
 
5081
            {
 
5082
              tab->cache.select=(SQL_SELECT*)
 
5083
                session->memdup((unsigned char*) sel, sizeof(SQL_SELECT));
 
5084
              tab->cache.select->cond=tmp;
 
5085
              tab->cache.select->read_tables=join->const_table_map;
 
5086
            }
 
5087
          }
 
5088
        }
 
5089
      }
 
5090
 
 
5091
      /*
 
5092
        Push down conditions from all on expressions.
 
5093
        Each of these conditions are guarded by a variable
 
5094
        that turns if off just before null complemented row for
 
5095
        outer joins is formed. Thus, the condition from an
 
5096
        'on expression' are guaranteed not to be checked for
 
5097
        the null complemented row.
 
5098
      */
 
5099
 
 
5100
      /* First push down constant conditions from on expressions */
 
5101
      for (JOIN_TAB *join_tab= join->join_tab+join->const_tables;
 
5102
           join_tab < join->join_tab+join->tables ; join_tab++)
 
5103
      {
 
5104
        if (*join_tab->on_expr_ref)
 
5105
        {
 
5106
          JOIN_TAB *cond_tab= join_tab->first_inner;
 
5107
          tmp= make_cond_for_table(*join_tab->on_expr_ref,
 
5108
                                   join->const_table_map,
 
5109
                                   (table_map) 0, 0);
 
5110
          if (!tmp)
 
5111
            continue;
 
5112
          tmp= new Item_func_trig_cond(tmp, &cond_tab->not_null_compl);
 
5113
          if (!tmp)
 
5114
            return(1);
 
5115
          tmp->quick_fix_field();
 
5116
          cond_tab->select_cond= !cond_tab->select_cond ? tmp :
 
5117
                                    new Item_cond_and(cond_tab->select_cond,tmp);
 
5118
          if (!cond_tab->select_cond)
 
5119
            return(1);
 
5120
          cond_tab->select_cond->quick_fix_field();
 
5121
        }
 
5122
      }
 
5123
 
 
5124
      /* Push down non-constant conditions from on expressions */
 
5125
      JOIN_TAB *last_tab= tab;
 
5126
      while (first_inner_tab && first_inner_tab->last_inner == last_tab)
 
5127
      {
 
5128
        /*
 
5129
          Table tab is the last inner table of an outer join.
 
5130
          An on expression is always attached to it.
 
5131
        */
 
5132
        COND *on_expr= *first_inner_tab->on_expr_ref;
 
5133
 
 
5134
        table_map used_tables2= (join->const_table_map |
 
5135
                                 OUTER_REF_TABLE_BIT | RAND_TABLE_BIT);
 
5136
        for (tab= join->join_tab+join->const_tables; tab <= last_tab ; tab++)
 
5137
        {
 
5138
          current_map= tab->table->map;
 
5139
          used_tables2|= current_map;
 
5140
          COND *tmp_cond= make_cond_for_table(on_expr, used_tables2,
 
5141
                                              current_map, 0);
 
5142
          if (tmp_cond)
 
5143
          {
 
5144
            JOIN_TAB *cond_tab= tab < first_inner_tab ? first_inner_tab : tab;
 
5145
            /*
 
5146
              First add the guards for match variables of
 
5147
              all embedding outer join operations.
 
5148
            */
 
5149
            if (!(tmp_cond= add_found_match_trig_cond(cond_tab->first_inner,
 
5150
                                                     tmp_cond,
 
5151
                                                     first_inner_tab)))
 
5152
              return(1);
 
5153
            /*
 
5154
              Now add the guard turning the predicate off for
 
5155
              the null complemented row.
 
5156
            */
 
5157
            tmp_cond= new Item_func_trig_cond(tmp_cond,
 
5158
                                              &first_inner_tab->
 
5159
                                              not_null_compl);
 
5160
            if (tmp_cond)
 
5161
              tmp_cond->quick_fix_field();
 
5162
            /* Add the predicate to other pushed down predicates */
 
5163
            cond_tab->select_cond= !cond_tab->select_cond ? tmp_cond :
 
5164
                                  new Item_cond_and(cond_tab->select_cond,
 
5165
                                                    tmp_cond);
 
5166
            if (!cond_tab->select_cond)
 
5167
              return(1);
 
5168
            cond_tab->select_cond->quick_fix_field();
 
5169
          }
 
5170
        }
 
5171
        first_inner_tab= first_inner_tab->first_upper;
 
5172
      }
 
5173
    }
 
5174
  }
 
5175
  return(0);
 
5176
}
 
5177
 
 
5178
/*
 
5179
  Plan refinement stage: do various set ups for the executioner
 
5180
 
 
5181
  SYNOPSIS
 
5182
    make_join_readinfo()
 
5183
      join           Join being processed
 
5184
      options        Join's options (checking for SELECT_DESCRIBE,
 
5185
                     SELECT_NO_JOIN_CACHE)
 
5186
      no_jbuf_after  Don't use join buffering after table with this number.
 
5187
 
 
5188
  DESCRIPTION
 
5189
    Plan refinement stage: do various set ups for the executioner
 
5190
      - set up use of join buffering
 
5191
      - push index conditions
 
5192
      - increment counters
 
5193
      - etc
 
5194
 
 
5195
  RETURN
 
5196
    false - OK
 
5197
    true  - Out of memory
 
5198
*/
 
5199
static bool make_join_readinfo(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
5200
{
 
5201
  uint32_t i;
 
5202
  bool statistics= test(!(join->select_options & SELECT_DESCRIBE));
 
5203
  bool sorted= 1;
 
5204
 
 
5205
  for (i=join->const_tables ; i < join->tables ; i++)
 
5206
  {
 
5207
    JOIN_TAB *tab=join->join_tab+i;
 
5208
    Table *table=tab->table;
 
5209
    bool using_join_cache;
 
5210
    tab->read_record.table= table;
 
5211
    tab->read_record.file=table->file;
 
5212
    tab->next_select=sub_select;                /* normal select */
 
5213
    /*
 
5214
      TODO: don't always instruct first table's ref/range access method to
 
5215
      produce sorted output.
 
5216
    */
 
5217
    tab->sorted= sorted;
 
5218
    sorted= 0;                                  // only first must be sorted
 
5219
    if (tab->insideout_match_tab)
 
5220
    {
 
5221
      if (!(tab->insideout_buf= (unsigned char*)join->session->alloc(tab->table->key_info
 
5222
                                                         [tab->index].
 
5223
                                                         key_length)))
 
5224
        return true;
 
5225
    }
 
5226
    switch (tab->type) {
 
5227
    case JT_SYSTEM:                             // Only happens with left join
 
5228
      table->status=STATUS_NO_RECORD;
 
5229
      tab->read_first_record= join_read_system;
 
5230
      tab->read_record.read_record= join_no_more_records;
 
5231
      break;
 
5232
    case JT_CONST:                              // Only happens with left join
 
5233
      table->status=STATUS_NO_RECORD;
 
5234
      tab->read_first_record= join_read_const;
 
5235
      tab->read_record.read_record= join_no_more_records;
 
5236
      if (table->covering_keys.test(tab->ref.key) &&
 
5237
          !table->no_keyread)
 
5238
      {
 
5239
        table->key_read=1;
 
5240
        table->file->extra(HA_EXTRA_KEYREAD);
 
5241
      }
 
5242
      break;
 
5243
    case JT_EQ_REF:
 
5244
      table->status=STATUS_NO_RECORD;
 
5245
      if (tab->select)
 
5246
      {
 
5247
        delete tab->select->quick;
 
5248
        tab->select->quick=0;
 
5249
      }
 
5250
      delete tab->quick;
 
5251
      tab->quick=0;
 
5252
      tab->read_first_record= join_read_key;
 
5253
      tab->read_record.read_record= join_no_more_records;
 
5254
      if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
 
5255
      {
 
5256
        table->key_read=1;
 
5257
        table->file->extra(HA_EXTRA_KEYREAD);
 
5258
      }
 
5259
      else
 
5260
        push_index_cond(tab, tab->ref.key, true);
 
5261
      break;
 
5262
    case JT_REF_OR_NULL:
 
5263
    case JT_REF:
 
5264
      table->status=STATUS_NO_RECORD;
 
5265
      if (tab->select)
 
5266
      {
 
5267
        delete tab->select->quick;
 
5268
        tab->select->quick=0;
 
5269
      }
 
5270
      delete tab->quick;
 
5271
      tab->quick=0;
 
5272
      if (table->covering_keys.test(tab->ref.key) && !table->no_keyread)
 
5273
      {
 
5274
        table->key_read=1;
 
5275
        table->file->extra(HA_EXTRA_KEYREAD);
 
5276
      }
 
5277
      else
 
5278
        push_index_cond(tab, tab->ref.key, true);
 
5279
      if (tab->type == JT_REF)
 
5280
      {
 
5281
        tab->read_first_record= join_read_always_key;
 
5282
        tab->read_record.read_record= tab->insideout_match_tab?
 
5283
           join_read_next_same_diff : join_read_next_same;
 
5284
      }
 
5285
      else
 
5286
      {
 
5287
        tab->read_first_record= join_read_always_key_or_null;
 
5288
        tab->read_record.read_record= join_read_next_same_or_null;
 
5289
      }
 
5290
      break;
 
5291
    case JT_ALL:
 
5292
      /*
 
5293
        If previous table use cache
 
5294
        If the incoming data set is already sorted don't use cache.
 
5295
      */
 
5296
      table->status=STATUS_NO_RECORD;
 
5297
      using_join_cache= false;
 
5298
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
5299
          tab->use_quick != 2 && !tab->first_inner && i <= no_jbuf_after &&
 
5300
          !tab->insideout_match_tab)
 
5301
      {
 
5302
        if ((options & SELECT_DESCRIBE) ||
 
5303
            !join_init_cache(join->session,join->join_tab+join->const_tables,
 
5304
                i-join->const_tables))
 
5305
        {
 
5306
                using_join_cache= true;
 
5307
          tab[-1].next_select=sub_select_cache; /* Patch previous */
 
5308
        }
 
5309
      }
 
5310
      /* These init changes read_record */
 
5311
      if (tab->use_quick == 2)
 
5312
      {
 
5313
        join->session->server_status|=SERVER_QUERY_NO_GOOD_INDEX_USED;
 
5314
        tab->read_first_record= join_init_quick_read_record;
 
5315
        if (statistics)
 
5316
          status_var_increment(join->session->status_var.select_range_check_count);
 
5317
      }
 
5318
      else
 
5319
      {
 
5320
        tab->read_first_record= join_init_read_record;
 
5321
        if (i == join->const_tables)
 
5322
        {
 
5323
          if (tab->select && tab->select->quick)
 
5324
          {
 
5325
            if (statistics)
 
5326
              status_var_increment(join->session->status_var.select_range_count);
 
5327
          }
 
5328
          else
 
5329
          {
 
5330
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
5331
            if (statistics)
 
5332
              status_var_increment(join->session->status_var.select_scan_count);
 
5333
          }
 
5334
        }
 
5335
        else
 
5336
        {
 
5337
          if (tab->select && tab->select->quick)
 
5338
          {
 
5339
            if (statistics)
 
5340
              status_var_increment(join->session->status_var.select_full_range_join_count);
 
5341
          }
 
5342
          else
 
5343
          {
 
5344
            join->session->server_status|=SERVER_QUERY_NO_INDEX_USED;
 
5345
            if (statistics)
 
5346
              status_var_increment(join->session->status_var.select_full_join_count);
 
5347
          }
 
5348
        }
 
5349
        if (!table->no_keyread)
 
5350
        {
 
5351
          if (tab->select && tab->select->quick &&
 
5352
                    tab->select->quick->index != MAX_KEY && //not index_merge
 
5353
              table->covering_keys.test(tab->select->quick->index))
 
5354
          {
 
5355
            table->key_read=1;
 
5356
            table->file->extra(HA_EXTRA_KEYREAD);
 
5357
          }
 
5358
          else if (!table->covering_keys.none() &&
 
5359
            !(tab->select && tab->select->quick))
 
5360
          {                                     // Only read index tree
 
5361
                  if (!tab->insideout_match_tab)
 
5362
                  {
 
5363
                    /*
 
5364
                      See bug #26447: "Using the clustered index for a table scan
 
5365
                      is always faster than using a secondary index".
 
5366
                    */
 
5367
                    if (table->s->primary_key != MAX_KEY &&
 
5368
                        table->file->primary_key_is_clustered())
 
5369
                      tab->index= table->s->primary_key;
 
5370
                    else
 
5371
                      tab->index= table->find_shortest_key(&table->covering_keys);
 
5372
                  }
 
5373
            tab->read_first_record= join_read_first;
 
5374
            tab->type=JT_NEXT;          // Read with index_first / index_next
 
5375
          }
 
5376
        }
 
5377
        if (tab->select && tab->select->quick &&
 
5378
            tab->select->quick->index != MAX_KEY && ! tab->table->key_read)
 
5379
          push_index_cond(tab, tab->select->quick->index, !using_join_cache);
 
5380
      }
 
5381
      break;
 
5382
    default:
 
5383
      break;                                    /* purecov: deadcode */
 
5384
    case JT_UNKNOWN:
 
5385
    case JT_MAYBE_REF:
 
5386
      abort();                                  /* purecov: deadcode */
 
5387
    }
 
5388
  }
 
5389
  join->join_tab[join->tables-1].next_select=0; /* Set by do_select */
 
5390
  return(false);
 
5391
}
 
5392
 
 
5393
/** Update the dependency map for the tables. */
 
5394
static void update_depend_map(JOIN *join)
 
5395
{
 
5396
  JOIN_TAB *join_tab=join->join_tab, *end=join_tab+join->tables;
 
5397
 
 
5398
  for (; join_tab != end ; join_tab++)
 
5399
  {
 
5400
    TABLE_REF *ref= &join_tab->ref;
 
5401
    table_map depend_map=0;
 
5402
    Item **item=ref->items;
 
5403
    uint32_t i;
 
5404
    for (i=0 ; i < ref->key_parts ; i++,item++)
 
5405
      depend_map|=(*item)->used_tables();
 
5406
    ref->depend_map=depend_map & ~OUTER_REF_TABLE_BIT;
 
5407
    depend_map&= ~OUTER_REF_TABLE_BIT;
 
5408
    for (JOIN_TAB **tab=join->map2table; depend_map; tab++,depend_map>>=1 )
 
5409
    {
 
5410
      if (depend_map & 1)
 
5411
        ref->depend_map|=(*tab)->ref.depend_map;
 
5412
    }
 
5413
  }
 
5414
}
 
5415
 
 
5416
/** Update the dependency map for the sort order. */
 
5417
static void update_depend_map(JOIN *join, order_st *order)
 
5418
{
 
5419
  for (; order ; order=order->next)
 
5420
  {
 
5421
    table_map depend_map;
 
5422
    order->item[0]->update_used_tables();
 
5423
    order->depend_map=depend_map=order->item[0]->used_tables();
 
5424
    // Not item_sum(), RAND() and no reference to table outside of sub select
 
5425
    if (!(order->depend_map & (OUTER_REF_TABLE_BIT | RAND_TABLE_BIT))
 
5426
        && !order->item[0]->with_sum_func)
 
5427
    {
 
5428
      for (JOIN_TAB **tab=join->map2table; depend_map; tab++, depend_map>>=1)
 
5429
      {
 
5430
        if (depend_map & 1)
 
5431
          order->depend_map|=(*tab)->ref.depend_map;
 
5432
      }
 
5433
    }
 
5434
  }
 
5435
}
 
5436
 
 
5437
/**
 
5438
  Remove all constants and check if order_st only contains simple
 
5439
  expressions.
 
5440
 
 
5441
  simple_order is set to 1 if sort_order only uses fields from head table
 
5442
  and the head table is not a LEFT JOIN table.
 
5443
 
 
5444
  @param join                   Join handler
 
5445
  @param first_order            List of SORT or GROUP order
 
5446
  @param cond                   WHERE statement
 
5447
  @param change_list            Set to 1 if we should remove things from list.
 
5448
                               If this is not set, then only simple_order is
 
5449
                               calculated.
 
5450
  @param simple_order           Set to 1 if we are only using simple expressions
 
5451
 
 
5452
  @return
 
5453
    Returns new sort order
 
5454
*/
 
5455
static order_st *remove_constants(JOIN *join,order_st *first_order, COND *cond, bool change_list, bool *simple_order)
 
5456
{
 
5457
  if (join->tables == join->const_tables)
 
5458
    return change_list ? 0 : first_order;               // No need to sort
 
5459
 
 
5460
  order_st *order,**prev_ptr;
 
5461
  table_map first_table= join->join_tab[join->const_tables].table->map;
 
5462
  table_map not_const_tables= ~join->const_table_map;
 
5463
  table_map ref;
 
5464
 
 
5465
  prev_ptr= &first_order;
 
5466
  *simple_order= *join->join_tab[join->const_tables].on_expr_ref ? 0 : 1;
 
5467
 
 
5468
  /* NOTE: A variable of not_const_tables ^ first_table; breaks gcc 2.7 */
 
5469
 
 
5470
  update_depend_map(join, first_order);
 
5471
  for (order=first_order; order ; order=order->next)
 
5472
  {
 
5473
    table_map order_tables=order->item[0]->used_tables();
 
5474
    if (order->item[0]->with_sum_func)
 
5475
      *simple_order=0;                          // Must do a temp table to sort
 
5476
    else if (!(order_tables & not_const_tables))
 
5477
    {
 
5478
      if (order->item[0]->with_subselect)
 
5479
        order->item[0]->val_str(&order->item[0]->str_value);
 
5480
      continue;                                 // skip const item
 
5481
    }
 
5482
    else
 
5483
    {
 
5484
      if (order_tables & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT))
 
5485
        *simple_order=0;
 
5486
      else
 
5487
      {
 
5488
        Item *comp_item=0;
 
5489
        if (cond && const_expression_in_where(cond,order->item[0], &comp_item))
 
5490
        {
 
5491
          continue;
 
5492
        }
 
5493
        if ((ref=order_tables & (not_const_tables ^ first_table)))
 
5494
        {
 
5495
          if (!(order_tables & first_table) &&
 
5496
                    only_eq_ref_tables(join,first_order, ref))
 
5497
          {
 
5498
            continue;
 
5499
          }
 
5500
          *simple_order=0;                      // Must do a temp table to sort
 
5501
        }
 
5502
      }
 
5503
    }
 
5504
    if (change_list)
 
5505
      *prev_ptr= order;                         // use this entry
 
5506
    prev_ptr= &order->next;
 
5507
  }
 
5508
  if (change_list)
 
5509
    *prev_ptr=0;
 
5510
  if (prev_ptr == &first_order)                 // Nothing to sort/group
 
5511
    *simple_order=1;
 
5512
  return(first_order);
 
5513
}
 
5514
 
 
5515
static int return_zero_rows(JOIN *join,
 
5516
                            select_result *result,
 
5517
                            TableList *tables,
 
5518
                                        List<Item> &fields,
 
5519
                            bool send_row,
 
5520
                            uint64_t select_options,
 
5521
                            const char *info,
 
5522
                            Item *having)
 
5523
{
 
5524
  if (select_options & SELECT_DESCRIBE)
 
5525
  {
 
5526
    select_describe(join, false, false, false, info);
 
5527
    return(0);
 
5528
  }
 
5529
 
 
5530
  join->join_free();
 
5531
 
 
5532
  if (send_row)
 
5533
  {
 
5534
    for (TableList *table= tables; table; table= table->next_leaf)
 
5535
      table->table->mark_as_null_row();         // All fields are NULL
 
5536
    if (having && having->val_int() == 0)
 
5537
      send_row=0;
 
5538
  }
 
5539
  if (!(result->send_fields(fields, Protocol::SEND_NUM_ROWS | Protocol::SEND_EOF)))
 
5540
  {
 
5541
    if (send_row)
 
5542
    {
 
5543
      List_iterator_fast<Item> it(fields);
 
5544
      Item *item;
 
5545
      while ((item= it++))
 
5546
        item->no_rows_in_result();
 
5547
      result->send_data(fields);
 
5548
    }
 
5549
    result->send_eof();                         // Should be safe
 
5550
  }
 
5551
  /* Update results for FOUND_ROWS */
 
5552
  join->session->limit_found_rows= join->session->examined_row_count= 0;
 
5553
  return(0);
 
5554
}
 
5555
 
 
5556
/**
 
5557
  Simplify joins replacing outer joins by inner joins whenever it's
 
5558
  possible.
 
5559
 
 
5560
    The function, during a retrieval of join_list,  eliminates those
 
5561
    outer joins that can be converted into inner join, possibly nested.
 
5562
    It also moves the on expressions for the converted outer joins
 
5563
    and from inner joins to conds.
 
5564
    The function also calculates some attributes for nested joins:
 
5565
    - used_tables
 
5566
    - not_null_tables
 
5567
    - dep_tables.
 
5568
    - on_expr_dep_tables
 
5569
    The first two attributes are used to test whether an outer join can
 
5570
    be substituted for an inner join. The third attribute represents the
 
5571
    relation 'to be dependent on' for tables. If table t2 is dependent
 
5572
    on table t1, then in any evaluated execution plan table access to
 
5573
    table t2 must precede access to table t2. This relation is used also
 
5574
    to check whether the query contains  invalid cross-references.
 
5575
    The forth attribute is an auxiliary one and is used to calculate
 
5576
    dep_tables.
 
5577
    As the attribute dep_tables qualifies possibles orders of tables in the
 
5578
    execution plan, the dependencies required by the straight join
 
5579
    modifiers are reflected in this attribute as well.
 
5580
    The function also removes all braces that can be removed from the join
 
5581
    expression without changing its meaning.
 
5582
 
 
5583
  @note
 
5584
    An outer join can be replaced by an inner join if the where condition
 
5585
    or the on expression for an embedding nested join contains a conjunctive
 
5586
    predicate rejecting null values for some attribute of the inner tables.
 
5587
 
 
5588
    E.g. in the query:
 
5589
    @code
 
5590
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
5591
    @endcode
 
5592
    the predicate t2.b < 5 rejects nulls.
 
5593
    The query is converted first to:
 
5594
    @code
 
5595
      SELECT * FROM t1 INNER JOIN t2 ON t2.a=t1.a WHERE t2.b < 5
 
5596
    @endcode
 
5597
    then to the equivalent form:
 
5598
    @code
 
5599
      SELECT * FROM t1, t2 ON t2.a=t1.a WHERE t2.b < 5 AND t2.a=t1.a
 
5600
    @endcode
 
5601
 
 
5602
 
 
5603
    Similarly the following query:
 
5604
    @code
 
5605
      SELECT * from t1 LEFT JOIN (t2, t3) ON t2.a=t1.a t3.b=t1.b
 
5606
        WHERE t2.c < 5
 
5607
    @endcode
 
5608
    is converted to:
 
5609
    @code
 
5610
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a t3.b=t1.b
 
5611
 
 
5612
    @endcode
 
5613
 
 
5614
    One conversion might trigger another:
 
5615
    @code
 
5616
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a
 
5617
                       LEFT JOIN t3 ON t3.b=t2.b
 
5618
        WHERE t3 IS NOT NULL =>
 
5619
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t1.a, t3
 
5620
        WHERE t3 IS NOT NULL AND t3.b=t2.b =>
 
5621
      SELECT * FROM t1, t2, t3
 
5622
        WHERE t3 IS NOT NULL AND t3.b=t2.b AND t2.a=t1.a
 
5623
  @endcode
 
5624
 
 
5625
    The function removes all unnecessary braces from the expression
 
5626
    produced by the conversions.
 
5627
    E.g.
 
5628
    @code
 
5629
      SELECT * FROM t1, (t2, t3) WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
5630
    @endcode
 
5631
    finally is converted to:
 
5632
    @code
 
5633
      SELECT * FROM t1, t2, t3 WHERE t2.c < 5 AND t2.a=t1.a AND t3.b=t1.b
 
5634
 
 
5635
    @endcode
 
5636
 
 
5637
 
 
5638
    It also will remove braces from the following queries:
 
5639
    @code
 
5640
      SELECT * from (t1 LEFT JOIN t2 ON t2.a=t1.a) LEFT JOIN t3 ON t3.b=t2.b
 
5641
      SELECT * from (t1, (t2,t3)) WHERE t1.a=t2.a AND t2.b=t3.b.
 
5642
    @endcode
 
5643
 
 
5644
    The benefit of this simplification procedure is that it might return
 
5645
    a query for which the optimizer can evaluate execution plan with more
 
5646
    join orders. With a left join operation the optimizer does not
 
5647
    consider any plan where one of the inner tables is before some of outer
 
5648
    tables.
 
5649
 
 
5650
  IMPLEMENTATION
 
5651
    The function is implemented by a recursive procedure.  On the recursive
 
5652
    ascent all attributes are calculated, all outer joins that can be
 
5653
    converted are replaced and then all unnecessary braces are removed.
 
5654
    As join list contains join tables in the reverse order sequential
 
5655
    elimination of outer joins does not require extra recursive calls.
 
5656
 
 
5657
  SEMI-JOIN NOTES
 
5658
    Remove all semi-joins that have are within another semi-join (i.e. have
 
5659
    an "ancestor" semi-join nest)
 
5660
 
 
5661
  EXAMPLES
 
5662
    Here is an example of a join query with invalid cross references:
 
5663
    @code
 
5664
      SELECT * FROM t1 LEFT JOIN t2 ON t2.a=t3.a LEFT JOIN t3 ON t3.b=t1.b
 
5665
    @endcode
 
5666
 
 
5667
  @param join        reference to the query info
 
5668
  @param join_list   list representation of the join to be converted
 
5669
  @param conds       conditions to add on expressions for converted joins
 
5670
  @param top         true <=> conds is the where condition
 
5671
 
 
5672
  @return
 
5673
    - The new condition, if success
 
5674
    - 0, otherwise
 
5675
*/
 
5676
static COND *simplify_joins(JOIN *join, List<TableList> *join_list, COND *conds, bool top, bool in_sj)
 
5677
{
 
5678
  TableList *table;
 
5679
  nested_join_st *nested_join;
 
5680
  TableList *prev_table= 0;
 
5681
  List_iterator<TableList> li(*join_list);
 
5682
 
 
5683
  /*
 
5684
    Try to simplify join operations from join_list.
 
5685
    The most outer join operation is checked for conversion first.
 
5686
  */
 
5687
  while ((table= li++))
 
5688
  {
 
5689
    table_map used_tables;
 
5690
    table_map not_null_tables= (table_map) 0;
 
5691
 
 
5692
    if ((nested_join= table->nested_join))
 
5693
    {
 
5694
      /*
 
5695
         If the element of join_list is a nested join apply
 
5696
         the procedure to its nested join list first.
 
5697
      */
 
5698
      if (table->on_expr)
 
5699
      {
 
5700
        Item *expr= table->on_expr;
 
5701
        /*
 
5702
           If an on expression E is attached to the table,
 
5703
           check all null rejected predicates in this expression.
 
5704
           If such a predicate over an attribute belonging to
 
5705
           an inner table of an embedded outer join is found,
 
5706
           the outer join is converted to an inner join and
 
5707
           the corresponding on expression is added to E.
 
5708
              */
 
5709
        expr= simplify_joins(join, &nested_join->join_list,
 
5710
                             expr, false, in_sj || table->sj_on_expr);
 
5711
 
 
5712
        if (!table->prep_on_expr || expr != table->on_expr)
 
5713
        {
 
5714
          assert(expr);
 
5715
 
 
5716
          table->on_expr= expr;
 
5717
          table->prep_on_expr= expr->copy_andor_structure(join->session);
 
5718
        }
 
5719
      }
 
5720
      nested_join->used_tables= (table_map) 0;
 
5721
      nested_join->not_null_tables=(table_map) 0;
 
5722
      conds= simplify_joins(join, &nested_join->join_list, conds, top, in_sj || table->sj_on_expr);
 
5723
      used_tables= nested_join->used_tables;
 
5724
      not_null_tables= nested_join->not_null_tables;
 
5725
    }
 
5726
    else
 
5727
    {
 
5728
      if (!table->prep_on_expr)
 
5729
        table->prep_on_expr= table->on_expr;
 
5730
      used_tables= table->table->map;
 
5731
      if (conds)
 
5732
        not_null_tables= conds->not_null_tables();
 
5733
    }
 
5734
 
 
5735
    if (table->embedding)
 
5736
    {
 
5737
      table->embedding->nested_join->used_tables|= used_tables;
 
5738
      table->embedding->nested_join->not_null_tables|= not_null_tables;
 
5739
    }
 
5740
 
 
5741
    if (!table->outer_join || (used_tables & not_null_tables))
 
5742
    {
 
5743
      /*
 
5744
        For some of the inner tables there are conjunctive predicates
 
5745
        that reject nulls => the outer join can be replaced by an inner join.
 
5746
      */
 
5747
      table->outer_join= 0;
 
5748
      if (table->on_expr)
 
5749
      {
 
5750
        /* Add ON expression to the WHERE or upper-level ON condition. */
 
5751
        if (conds)
 
5752
        {
 
5753
          conds= and_conds(conds, table->on_expr);
 
5754
          conds->top_level_item();
 
5755
          /* conds is always a new item as both cond and on_expr existed */
 
5756
          assert(!conds->fixed);
 
5757
          conds->fix_fields(join->session, &conds);
 
5758
        }
 
5759
        else
 
5760
          conds= table->on_expr;
 
5761
        table->prep_on_expr= table->on_expr= 0;
 
5762
      }
 
5763
    }
 
5764
 
 
5765
    if (!top)
 
5766
      continue;
 
5767
 
 
5768
    /*
 
5769
      Only inner tables of non-convertible outer joins
 
5770
      remain with on_expr.
 
5771
    */
 
5772
    if (table->on_expr)
 
5773
    {
 
5774
      table->dep_tables|= table->on_expr->used_tables();
 
5775
      if (table->embedding)
 
5776
      {
 
5777
        table->dep_tables&= ~table->embedding->nested_join->used_tables;
 
5778
        /*
 
5779
           Embedding table depends on tables used
 
5780
           in embedded on expressions.
 
5781
        */
 
5782
        table->embedding->on_expr_dep_tables|= table->on_expr->used_tables();
 
5783
      }
 
5784
      else
 
5785
        table->dep_tables&= ~table->table->map;
 
5786
    }
 
5787
 
 
5788
    if (prev_table)
 
5789
    {
 
5790
      /* The order of tables is reverse: prev_table follows table */
 
5791
      if (prev_table->straight)
 
5792
        prev_table->dep_tables|= used_tables;
 
5793
      if (prev_table->on_expr)
 
5794
      {
 
5795
        prev_table->dep_tables|= table->on_expr_dep_tables;
 
5796
        table_map prev_used_tables= prev_table->nested_join ?
 
5797
                                    prev_table->nested_join->used_tables :
 
5798
                                    prev_table->table->map;
 
5799
        /*
 
5800
          If on expression contains only references to inner tables
 
5801
          we still make the inner tables dependent on the outer tables.
 
5802
          It would be enough to set dependency only on one outer table
 
5803
          for them. Yet this is really a rare case.
 
5804
              */
 
5805
        if (!(prev_table->on_expr->used_tables() & ~prev_used_tables))
 
5806
          prev_table->dep_tables|= used_tables;
 
5807
      }
 
5808
    }
 
5809
    prev_table= table;
 
5810
  }
 
5811
 
 
5812
  /*
 
5813
    Flatten nested joins that can be flattened.
 
5814
    no ON expression and not a semi-join => can be flattened.
 
5815
  */
 
5816
  li.rewind();
 
5817
  while ((table= li++))
 
5818
  {
 
5819
    nested_join= table->nested_join;
 
5820
    if (table->sj_on_expr && !in_sj)
 
5821
    {
 
5822
       /*
 
5823
         If this is a semi-join that is not contained within another semi-join,
 
5824
         leave it intact (otherwise it is flattened)
 
5825
       */
 
5826
      join->select_lex->sj_nests.push_back(table);
 
5827
    }
 
5828
    else if (nested_join && !table->on_expr)
 
5829
    {
 
5830
      TableList *tbl;
 
5831
      List_iterator<TableList> it(nested_join->join_list);
 
5832
      while ((tbl= it++))
 
5833
      {
 
5834
        tbl->embedding= table->embedding;
 
5835
        tbl->join_list= table->join_list;
 
5836
      }
 
5837
      li.replace(nested_join->join_list);
 
5838
    }
 
5839
  }
 
5840
  return(conds);
 
5841
}
 
5842
 
 
5843
static int remove_duplicates(JOIN *join, Table *entry,List<Item> &fields, Item *having)
 
5844
{
 
5845
  int error;
 
5846
  uint32_t reclength,offset;
 
5847
  uint32_t field_count;
 
5848
  Session *session= join->session;
 
5849
 
 
5850
  entry->reginfo.lock_type=TL_WRITE;
 
5851
 
 
5852
  /* Calculate how many saved fields there is in list */
 
5853
  field_count=0;
 
5854
  List_iterator<Item> it(fields);
 
5855
  Item *item;
 
5856
  while ((item=it++))
 
5857
  {
 
5858
    if (item->get_tmp_table_field() && ! item->const_item())
 
5859
      field_count++;
 
5860
  }
 
5861
 
 
5862
  if (!field_count && !(join->select_options & OPTION_FOUND_ROWS) && !having)
 
5863
  {                    // only const items with no OPTION_FOUND_ROWS
 
5864
    join->unit->select_limit_cnt= 1;            // Only send first row
 
5865
    return(0);
 
5866
  }
 
5867
  Field **first_field=entry->field+entry->s->fields - field_count;
 
5868
  offset= (field_count ?
 
5869
           entry->field[entry->s->fields - field_count]->
 
5870
           offset(entry->record[0]) : 0);
 
5871
  reclength= entry->s->reclength-offset;
 
5872
 
 
5873
  free_io_cache(entry);                         // Safety
 
5874
  entry->file->info(HA_STATUS_VARIABLE);
 
5875
  if (entry->s->db_type() == heap_engine ||
 
5876
      (!entry->s->blob_fields &&
 
5877
       ((ALIGN_SIZE(reclength) + HASH_OVERHEAD) * entry->file->stats.records <
 
5878
        session->variables.sortbuff_size)))
 
5879
    error= remove_dup_with_hash_index(join->session, entry,
 
5880
                                     field_count, first_field,
 
5881
                                     reclength, having);
 
5882
  else
 
5883
    error= remove_dup_with_compare(join->session, entry, first_field, offset,
 
5884
                                  having);
 
5885
 
 
5886
  free_blobs(first_field);
 
5887
  return(error);
 
5888
}
 
5889
 
 
5890
/**
 
5891
  Function to setup clauses without sum functions.
 
5892
*/
 
5893
static int setup_without_group(Session *session, 
 
5894
                               Item **ref_pointer_array,
 
5895
                               TableList *tables,
 
5896
                               TableList *,
 
5897
                               List<Item> &fields,
 
5898
                               List<Item> &all_fields,
 
5899
                               COND **conds,
 
5900
                               order_st *order,
 
5901
                               order_st *group,
 
5902
                               bool *hidden_group_fields)
 
5903
{
 
5904
  int res;
 
5905
  nesting_map save_allow_sum_func=session->lex->allow_sum_func ;
 
5906
 
 
5907
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5908
  res= setup_conds(session, tables, conds);
 
5909
 
 
5910
  session->lex->allow_sum_func|= 1 << session->lex->current_select->nest_level;
 
5911
  res= res || setup_order(session, ref_pointer_array, tables, fields, all_fields,
 
5912
                          order);
 
5913
  session->lex->allow_sum_func&= ~(1 << session->lex->current_select->nest_level);
 
5914
  res= res || setup_group(session, ref_pointer_array, tables, fields, all_fields,
 
5915
                          group, hidden_group_fields);
 
5916
  session->lex->allow_sum_func= save_allow_sum_func;
 
5917
  return(res);
 
5918
}
 
5919
 
 
5920
/**
 
5921
  Calculate the best possible join and initialize the join structure.
 
5922
 
 
5923
  @retval
 
5924
    0   ok
 
5925
  @retval
 
5926
    1   Fatal error
 
5927
*/
 
5928
static bool make_join_statistics(JOIN *join, TableList *tables, COND *conds, DYNAMIC_ARRAY *keyuse_array)
 
5929
{
 
5930
  int error;
 
5931
  Table *table;
 
5932
  uint32_t i,table_count,const_count,key;
 
5933
  table_map found_const_table_map, all_table_map, found_ref, refs;
 
5934
  key_map const_ref, eq_part;
 
5935
  Table **table_vector;
 
5936
  JOIN_TAB *stat,*stat_end,*s,**stat_ref;
 
5937
  KEYUSE *keyuse,*start_keyuse;
 
5938
  table_map outer_join=0;
 
5939
  SARGABLE_PARAM *sargables= 0;
 
5940
  JOIN_TAB *stat_vector[MAX_TABLES+1];
 
5941
 
 
5942
  table_count=join->tables;
 
5943
  stat=(JOIN_TAB*) join->session->calloc(sizeof(JOIN_TAB)*table_count);
 
5944
  stat_ref=(JOIN_TAB**) join->session->alloc(sizeof(JOIN_TAB*)*MAX_TABLES);
 
5945
  table_vector=(Table**) join->session->alloc(sizeof(Table*)*(table_count*2));
 
5946
  if (!stat || !stat_ref || !table_vector)
 
5947
    return(1);                          // Eom /* purecov: inspected */
 
5948
 
 
5949
  join->best_ref=stat_vector;
 
5950
 
 
5951
  stat_end=stat+table_count;
 
5952
  found_const_table_map= all_table_map=0;
 
5953
  const_count=0;
 
5954
 
 
5955
  for (s= stat, i= 0;
 
5956
       tables;
 
5957
       s++, tables= tables->next_leaf, i++)
 
5958
  {
 
5959
    TableList *embedding= tables->embedding;
 
5960
    stat_vector[i]=s;
 
5961
    s->keys.reset();
 
5962
    s->const_keys.reset();
 
5963
    s->checked_keys.reset();
 
5964
    s->needed_reg.reset();
 
5965
    table_vector[i]=s->table=table=tables->table;
 
5966
    table->pos_in_table_list= tables;
 
5967
    error= table->file->info(HA_STATUS_VARIABLE | HA_STATUS_NO_LOCK);
 
5968
    if(error)
 
5969
    {
 
5970
        table->file->print_error(error, MYF(0));
 
5971
        return(1);
 
5972
    }
 
5973
    table->quick_keys.reset();
 
5974
    table->reginfo.join_tab=s;
 
5975
    table->reginfo.not_exists_optimize=0;
 
5976
    memset(table->const_key_parts, 0,
 
5977
           sizeof(key_part_map)*table->s->keys);
 
5978
    all_table_map|= table->map;
 
5979
    s->join=join;
 
5980
    s->info=0;                                  // For describe
 
5981
 
 
5982
    s->dependent= tables->dep_tables;
 
5983
    s->key_dependent= 0;
 
5984
    if (tables->schema_table)
 
5985
      table->file->stats.records= 2;
 
5986
    table->quick_condition_rows= table->file->stats.records;
 
5987
 
 
5988
    s->on_expr_ref= &tables->on_expr;
 
5989
    if (*s->on_expr_ref)
 
5990
    {
 
5991
      /* s is the only inner table of an outer join */
 
5992
      if (!table->file->stats.records && !embedding)
 
5993
      {                                         // Empty table
 
5994
        s->dependent= 0;                        // Ignore LEFT JOIN depend.
 
5995
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
5996
        continue;
 
5997
      }
 
5998
      outer_join|= table->map;
 
5999
      s->embedding_map= 0;
 
6000
      for (;embedding; embedding= embedding->embedding)
 
6001
        s->embedding_map|= embedding->nested_join->nj_map;
 
6002
      continue;
 
6003
    }
 
6004
    if (embedding && !(embedding->sj_on_expr && ! embedding->embedding))
 
6005
    {
 
6006
      /* s belongs to a nested join, maybe to several embedded joins */
 
6007
      s->embedding_map= 0;
 
6008
      do
 
6009
      {
 
6010
        nested_join_st *nested_join= embedding->nested_join;
 
6011
        s->embedding_map|=nested_join->nj_map;
 
6012
        s->dependent|= embedding->dep_tables;
 
6013
        embedding= embedding->embedding;
 
6014
        outer_join|= nested_join->used_tables;
 
6015
      }
 
6016
      while (embedding);
 
6017
      continue;
 
6018
    }
 
6019
    if ((table->file->stats.records <= 1) && !s->dependent &&
 
6020
              (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) && 
 
6021
        !join->no_const_tables)
 
6022
    {
 
6023
      set_position(join,const_count++,s,(KEYUSE*) 0);
 
6024
    }
 
6025
  }
 
6026
  stat_vector[i]=0;
 
6027
  join->outer_join=outer_join;
 
6028
 
 
6029
  if (join->outer_join)
 
6030
  {
 
6031
    /*
 
6032
       Build transitive closure for relation 'to be dependent on'.
 
6033
       This will speed up the plan search for many cases with outer joins,
 
6034
       as well as allow us to catch illegal cross references/
 
6035
       Warshall's algorithm is used to build the transitive closure.
 
6036
       As we use bitmaps to represent the relation the complexity
 
6037
       of the algorithm is O((number of tables)^2).
 
6038
    */
 
6039
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
6040
    {
 
6041
      for (uint32_t j= 0 ; j < table_count ; j++)
 
6042
      {
 
6043
        table= stat[j].table;
 
6044
        if (s->dependent & table->map)
 
6045
          s->dependent |= table->reginfo.join_tab->dependent;
 
6046
      }
 
6047
      if (s->dependent)
 
6048
        s->table->maybe_null= 1;
 
6049
    }
 
6050
    /* Catch illegal cross references for outer joins */
 
6051
    for (i= 0, s= stat ; i < table_count ; i++, s++)
 
6052
    {
 
6053
      if (s->dependent & s->table->map)
 
6054
      {
 
6055
        join->tables=0;                 // Don't use join->table
 
6056
        my_message(ER_WRONG_OUTER_JOIN, ER(ER_WRONG_OUTER_JOIN), MYF(0));
 
6057
        return(1);
 
6058
      }
 
6059
      s->key_dependent= s->dependent;
 
6060
    }
 
6061
  }
 
6062
 
 
6063
  if (conds || outer_join)
 
6064
    if (update_ref_and_keys(join->session, keyuse_array, stat, join->tables,
 
6065
                            conds, join->cond_equal,
 
6066
                            ~outer_join, join->select_lex, &sargables))
 
6067
      return(1);
 
6068
 
 
6069
  /* Read tables with 0 or 1 rows (system tables) */
 
6070
  join->const_table_map= 0;
 
6071
 
 
6072
  for (POSITION *p_pos=join->positions, *p_end=p_pos+const_count;
 
6073
       p_pos < p_end ;
 
6074
       p_pos++)
 
6075
  {
 
6076
    int tmp;
 
6077
    s= p_pos->table;
 
6078
    s->type=JT_SYSTEM;
 
6079
    join->const_table_map|=s->table->map;
 
6080
    if ((tmp=join_read_const_table(s, p_pos)))
 
6081
    {
 
6082
      if (tmp > 0)
 
6083
        return(1);                      // Fatal error
 
6084
    }
 
6085
    else
 
6086
      found_const_table_map|= s->table->map;
 
6087
  }
 
6088
 
 
6089
  /* loop until no more const tables are found */
 
6090
  int ref_changed;
 
6091
  do
 
6092
  {
 
6093
  more_const_tables_found:
 
6094
    ref_changed = 0;
 
6095
    found_ref=0;
 
6096
 
 
6097
    /*
 
6098
      We only have to loop from stat_vector + const_count as
 
6099
      set_position() will move all const_tables first in stat_vector
 
6100
    */
 
6101
 
 
6102
    for (JOIN_TAB **pos=stat_vector+const_count ; (s= *pos) ; pos++)
 
6103
    {
 
6104
      table=s->table;
 
6105
 
 
6106
      /*
 
6107
        If equi-join condition by a key is null rejecting and after a
 
6108
        substitution of a const table the key value happens to be null
 
6109
        then we can state that there are no matches for this equi-join.
 
6110
      */
 
6111
      if ((keyuse= s->keyuse) && *s->on_expr_ref && !s->embedding_map)
 
6112
      {
 
6113
        /*
 
6114
          When performing an outer join operation if there are no matching rows
 
6115
          for the single row of the outer table all the inner tables are to be
 
6116
          null complemented and thus considered as constant tables.
 
6117
          Here we apply this consideration to the case of outer join operations
 
6118
          with a single inner table only because the case with nested tables
 
6119
          would require a more thorough analysis.
 
6120
          TODO. Apply single row substitution to null complemented inner tables
 
6121
          for nested outer join operations.
 
6122
        */
 
6123
        while (keyuse->table == table)
 
6124
        {
 
6125
          if (!(keyuse->val->used_tables() & ~join->const_table_map) &&
 
6126
              keyuse->val->is_null() && keyuse->null_rejecting)
 
6127
          {
 
6128
            s->type= JT_CONST;
 
6129
            table->mark_as_null_row();
 
6130
            found_const_table_map|= table->map;
 
6131
            join->const_table_map|= table->map;
 
6132
            set_position(join,const_count++,s,(KEYUSE*) 0);
 
6133
            goto more_const_tables_found;
 
6134
           }
 
6135
          keyuse++;
 
6136
        }
 
6137
      }
 
6138
 
 
6139
      if (s->dependent)                         // If dependent on some table
 
6140
      {
 
6141
        // All dep. must be constants
 
6142
        if (s->dependent & ~(found_const_table_map))
 
6143
          continue;
 
6144
        if (table->file->stats.records <= 1L &&
 
6145
            (table->file->ha_table_flags() & HA_STATS_RECORDS_IS_EXACT) &&
 
6146
                  !table->pos_in_table_list->embedding)
 
6147
        {                                       // system table
 
6148
          int tmp= 0;
 
6149
          s->type=JT_SYSTEM;
 
6150
          join->const_table_map|=table->map;
 
6151
          set_position(join,const_count++,s,(KEYUSE*) 0);
 
6152
          if ((tmp= join_read_const_table(s, join->positions+const_count-1)))
 
6153
          {
 
6154
            if (tmp > 0)
 
6155
              return(1);                        // Fatal error
 
6156
          }
 
6157
          else
 
6158
            found_const_table_map|= table->map;
 
6159
          continue;
 
6160
        }
 
6161
      }
 
6162
      /* check if table can be read by key or table only uses const refs */
 
6163
      if ((keyuse=s->keyuse))
 
6164
      {
 
6165
        s->type= JT_REF;
 
6166
        while (keyuse->table == table)
 
6167
        {
 
6168
          start_keyuse=keyuse;
 
6169
          key=keyuse->key;
 
6170
          s->keys.set(key);               // QQ: remove this ?
 
6171
 
 
6172
          refs=0;
 
6173
                const_ref.reset();
 
6174
          eq_part.reset();
 
6175
          do
 
6176
          {
 
6177
            if (keyuse->val->type() != Item::NULL_ITEM && !keyuse->optimize)
 
6178
            {
 
6179
              if (!((~found_const_table_map) & keyuse->used_tables))
 
6180
                const_ref.set(keyuse->keypart);
 
6181
              else
 
6182
                refs|=keyuse->used_tables;
 
6183
              eq_part.set(keyuse->keypart);
 
6184
            }
 
6185
            keyuse++;
 
6186
          } while (keyuse->table == table && keyuse->key == key);
 
6187
 
 
6188
          if (is_keymap_prefix(eq_part, table->key_info[key].key_parts) &&
 
6189
              !table->pos_in_table_list->embedding)
 
6190
          {
 
6191
            if ((table->key_info[key].flags & (HA_NOSAME)) == HA_NOSAME)
 
6192
            {
 
6193
              if (const_ref == eq_part)
 
6194
              {                                 // Found everything for ref.
 
6195
                int tmp;
 
6196
                ref_changed = 1;
 
6197
                s->type= JT_CONST;
 
6198
                join->const_table_map|= table->map;
 
6199
                set_position(join,const_count++,s,start_keyuse);
 
6200
                if (create_ref_for_key(join, s, start_keyuse, found_const_table_map))
 
6201
                  return(1);
 
6202
                if ((tmp=join_read_const_table(s, join->positions+const_count-1)))
 
6203
                {
 
6204
                  if (tmp > 0)
 
6205
                    return(1);                  // Fatal error
 
6206
                }
 
6207
                else
 
6208
                  found_const_table_map|= table->map;
 
6209
                break;
 
6210
              }
 
6211
              else
 
6212
                found_ref|= refs;      // Table is const if all refs are const
 
6213
            }
 
6214
            else if (const_ref == eq_part)
 
6215
              s->const_keys.set(key);
 
6216
          }
 
6217
        }
 
6218
      }
 
6219
    }
 
6220
  } while (join->const_table_map & found_ref && ref_changed);
 
6221
 
 
6222
  /*
 
6223
    Update info on indexes that can be used for search lookups as
 
6224
    reading const tables may has added new sargable predicates.
 
6225
  */
 
6226
  if (const_count && sargables)
 
6227
  {
 
6228
    for( ; sargables->field ; sargables++)
 
6229
    {
 
6230
      Field *field= sargables->field;
 
6231
      JOIN_TAB *join_tab= field->table->reginfo.join_tab;
 
6232
      key_map possible_keys= field->key_start;
 
6233
      possible_keys&= field->table->keys_in_use_for_query;
 
6234
      bool is_const= 1;
 
6235
      for (uint32_t j=0; j < sargables->num_values; j++)
 
6236
        is_const&= sargables->arg_value[j]->const_item();
 
6237
      if (is_const)
 
6238
        join_tab[0].const_keys|= possible_keys;
 
6239
    }
 
6240
  }
 
6241
 
 
6242
  if (pull_out_semijoin_tables(join))
 
6243
    return(true);
 
6244
 
 
6245
  /* Calc how many (possible) matched records in each table */
 
6246
 
 
6247
  for (s=stat ; s < stat_end ; s++)
 
6248
  {
 
6249
    if (s->type == JT_SYSTEM || s->type == JT_CONST)
 
6250
    {
 
6251
      /* Only one matching row */
 
6252
      s->found_records=s->records=s->read_time=1; s->worst_seeks=1.0;
 
6253
      continue;
 
6254
    }
 
6255
    /* Approximate found rows and time to read them */
 
6256
    s->found_records=s->records=s->table->file->stats.records;
 
6257
    s->read_time=(ha_rows) s->table->file->scan_time();
 
6258
 
 
6259
    /*
 
6260
      Set a max range of how many seeks we can expect when using keys
 
6261
      This is can't be to high as otherwise we are likely to use
 
6262
      table scan.
 
6263
    */
 
6264
    s->worst_seeks= cmin((double) s->found_records / 10,
 
6265
                        (double) s->read_time*3);
 
6266
    if (s->worst_seeks < 2.0)                   // Fix for small tables
 
6267
      s->worst_seeks=2.0;
 
6268
 
 
6269
    /*
 
6270
      Add to stat->const_keys those indexes for which all group fields or
 
6271
      all select distinct fields participate in one index.
 
6272
    */
 
6273
    add_group_and_distinct_keys(join, s);
 
6274
 
 
6275
    if (s->const_keys.any() &&
 
6276
        !s->table->pos_in_table_list->embedding)
 
6277
    {
 
6278
      ha_rows records;
 
6279
      SQL_SELECT *select;
 
6280
      select= make_select(s->table, found_const_table_map, found_const_table_map, *s->on_expr_ref ? *s->on_expr_ref : conds, 1, &error);
 
6281
      if (! select)
 
6282
        return(1);
 
6283
      records= get_quick_record_count(join->session, select, s->table, &s->const_keys, join->row_limit);
 
6284
      s->quick=select->quick;
 
6285
      s->needed_reg=select->needed_reg;
 
6286
      select->quick=0;
 
6287
      if (records == 0 && s->table->reginfo.impossible_range)
 
6288
      {
 
6289
        /*
 
6290
          Impossible WHERE or ON expression
 
6291
          In case of ON, we mark that the we match one empty NULL row.
 
6292
          In case of WHERE, don't set found_const_table_map to get the
 
6293
          caller to abort with a zero row result.
 
6294
        */
 
6295
        join->const_table_map|= s->table->map;
 
6296
        set_position(join,const_count++,s,(KEYUSE*) 0);
 
6297
        s->type= JT_CONST;
 
6298
        if (*s->on_expr_ref)
 
6299
        {
 
6300
          /* Generate empty row */
 
6301
          s->info= "Impossible ON condition";
 
6302
          found_const_table_map|= s->table->map;
 
6303
          s->type= JT_CONST;
 
6304
          s->table->mark_as_null_row();         // All fields are NULL
 
6305
        }
 
6306
      }
 
6307
      if (records != HA_POS_ERROR)
 
6308
      {
 
6309
        s->found_records=records;
 
6310
        s->read_time= (ha_rows) (s->quick ? s->quick->read_time : 0.0);
 
6311
      }
 
6312
      delete select;
 
6313
    }
 
6314
  }
 
6315
 
 
6316
  join->join_tab=stat;
 
6317
  join->map2table=stat_ref;
 
6318
  join->table= join->all_tables=table_vector;
 
6319
  join->const_tables=const_count;
 
6320
  join->found_const_table_map=found_const_table_map;
 
6321
 
 
6322
  /* Find an optimal join order of the non-constant tables. */
 
6323
  if (join->const_tables != join->tables)
 
6324
  {
 
6325
    optimize_keyuse(join, keyuse_array);
 
6326
    if (choose_plan(join, all_table_map & ~join->const_table_map))
 
6327
      return(true);
 
6328
  }
 
6329
  else
 
6330
  {
 
6331
    memcpy(join->best_positions, join->positions, sizeof(POSITION)*join->const_tables);
 
6332
    join->best_read= 1.0;
 
6333
  }
 
6334
  /* Generate an execution plan from the found optimal join order. */
 
6335
  return (join->session->killed || get_best_combination(join));
 
6336
}
 
6337
 
 
6338
/**
 
6339
  Assign each nested join structure a bit in nested_join_map.
 
6340
 
 
6341
    Assign each nested join structure (except "confluent" ones - those that
 
6342
    embed only one element) a bit in nested_join_map.
 
6343
 
 
6344
  @param join          Join being processed
 
6345
  @param join_list     List of tables
 
6346
  @param first_unused  Number of first unused bit in nested_join_map before the
 
6347
                       call
 
6348
 
 
6349
  @note
 
6350
    This function is called after simplify_joins(), when there are no
 
6351
    redundant nested joins, #non_confluent_nested_joins <= #tables_in_join so
 
6352
    we will not run out of bits in nested_join_map.
 
6353
 
 
6354
  @return
 
6355
    First unused bit in nested_join_map after the call.
 
6356
*/
 
6357
static uint32_t build_bitmap_for_nested_joins(List<TableList> *join_list, uint32_t first_unused)
 
6358
{
 
6359
  List_iterator<TableList> li(*join_list);
 
6360
  TableList *table;
 
6361
  while ((table= li++))
 
6362
  {
 
6363
    nested_join_st *nested_join;
 
6364
    if ((nested_join= table->nested_join))
 
6365
    {
 
6366
      /*
 
6367
        It is guaranteed by simplify_joins() function that a nested join
 
6368
        that has only one child is either
 
6369
         - a single-table view (the child is the underlying table), or
 
6370
         - a single-table semi-join nest
 
6371
 
 
6372
        We don't assign bits to such sj-nests because
 
6373
        1. it is redundant (a "sequence" of one table cannot be interleaved
 
6374
            with anything)
 
6375
        2. we could run out bits in nested_join_map otherwise.
 
6376
      */
 
6377
      if (nested_join->join_list.elements != 1)
 
6378
      {
 
6379
        /* Don't assign bits to sj-nests */
 
6380
        if (table->on_expr)
 
6381
          nested_join->nj_map= (nested_join_map) 1 << first_unused++;
 
6382
        first_unused= build_bitmap_for_nested_joins(&nested_join->join_list,
 
6383
                                                    first_unused);
 
6384
      }
 
6385
    }
 
6386
  }
 
6387
  return(first_unused);
 
6388
}
 
6389
 
 
6390
 
 
6391
/**
 
6392
  Return table number if there is only one table in sort order
 
6393
  and group and order is compatible, else return 0.
 
6394
*/
 
6395
static Table *get_sort_by_table(order_st *a,order_st *b,TableList *tables)
 
6396
{
 
6397
  table_map map= (table_map) 0;
 
6398
 
 
6399
  if (!a)
 
6400
    a= b;                                       // Only one need to be given
 
6401
  else if (!b)
 
6402
    b= a;
 
6403
 
 
6404
  for (; a && b; a=a->next,b=b->next)
 
6405
  {
 
6406
    if (!(*a->item)->eq(*b->item,1))
 
6407
      return (Table *) NULL;
 
6408
    map|= a->item[0]->used_tables();
 
6409
  }
 
6410
  if (!map || (map & (RAND_TABLE_BIT | OUTER_REF_TABLE_BIT)))
 
6411
    return (Table *) NULL;
 
6412
 
 
6413
  for (; !(map & tables->table->map); tables= tables->next_leaf) {};
 
6414
  if (map != tables->table->map)
 
6415
    return (Table *) NULL;                              // More than one table
 
6416
  return tables->table;
 
6417
}
 
6418
 
 
6419
/**
 
6420
  Set nested_join_st::counter=0 in all nested joins in passed list.
 
6421
 
 
6422
    Recursively set nested_join_st::counter=0 for all nested joins contained in
 
6423
    the passed join_list.
 
6424
 
 
6425
  @param join_list  List of nested joins to process. It may also contain base
 
6426
                    tables which will be ignored.
 
6427
*/
 
6428
static void reset_nj_counters(List<TableList> *join_list)
 
6429
{
 
6430
  List_iterator<TableList> li(*join_list);
 
6431
  TableList *table;
 
6432
  while ((table= li++))
 
6433
  {
 
6434
    nested_join_st *nested_join;
 
6435
    if ((nested_join= table->nested_join))
 
6436
    {
 
6437
      nested_join->counter_= 0;
 
6438
      reset_nj_counters(&nested_join->join_list);
 
6439
    }
 
6440
  }
 
6441
  return;
 
6442
}
 
6443
 
 
6444
/**
 
6445
  Return 1 if second is a subpart of first argument.
 
6446
 
 
6447
  If first parts has different direction, change it to second part
 
6448
  (group is sorted like order)
 
6449
*/
 
6450
static bool test_if_subpart(order_st *a,order_st *b)
 
6451
{
 
6452
  for (; a && b; a=a->next,b=b->next)
 
6453
  {
 
6454
    if ((*a->item)->eq(*b->item,1))
 
6455
      a->asc=b->asc;
 
6456
    else
 
6457
      return 0;
 
6458
  }
 
6459
  return test(!b);
 
6460
}
 
6461
 
 
6462
/**
 
6463
  Nested joins perspective: Remove the last table from the join order.
 
6464
 
 
6465
    Remove the last table from the partial join order and update the nested
 
6466
    joins counters and join->cur_embedding_map. It is ok to call this
 
6467
    function for the first table in join order (for which
 
6468
    check_interleaving_with_nj has not been called)
 
6469
 
 
6470
  @param last  join table to remove, it is assumed to be the last in current
 
6471
               partial join order.
 
6472
*/
 
6473
static void restore_prev_nj_state(JOIN_TAB *last)
 
6474
{
 
6475
  TableList *last_emb= last->table->pos_in_table_list->embedding;
 
6476
  JOIN *join= last->join;
 
6477
  while (last_emb)
 
6478
  {
 
6479
    if (last_emb->on_expr)
 
6480
    {
 
6481
      if (!(--last_emb->nested_join->counter_))
 
6482
        join->cur_embedding_map&= ~last_emb->nested_join->nj_map;
 
6483
      else if (last_emb->nested_join->join_list.elements-1 ==
 
6484
               last_emb->nested_join->counter_)
 
6485
        join->cur_embedding_map|= last_emb->nested_join->nj_map;
 
6486
      else
 
6487
        break;
 
6488
    }
 
6489
    last_emb= last_emb->embedding;
 
6490
  }
 
6491
}
 
6492
 
 
6493
/**
 
6494
  Determine if the set is already ordered for order_st BY, so it can
 
6495
  disable join cache because it will change the ordering of the results.
 
6496
  Code handles sort table that is at any location (not only first after
 
6497
  the const tables) despite the fact that it's currently prohibited.
 
6498
  We must disable join cache if the first non-const table alone is
 
6499
  ordered. If there is a temp table the ordering is done as a last
 
6500
  operation and doesn't prevent join cache usage.
 
6501
*/
 
6502
static uint32_t make_join_orderinfo(JOIN *join)
 
6503
{
 
6504
  uint32_t i;
 
6505
  if (join->need_tmp)
 
6506
    return join->tables;
 
6507
 
 
6508
  for (i=join->const_tables ; i < join->tables ; i++)
 
6509
  {
 
6510
    JOIN_TAB *tab= join->join_tab+i;
 
6511
    Table *table= tab->table;
 
6512
    if ((table == join->sort_by_table &&
 
6513
        (!join->order || join->skip_sort_order)) ||
 
6514
        (join->sort_by_table == (Table *) 1 &&  i != join->const_tables))
 
6515
    {
 
6516
      break;
 
6517
    }
 
6518
  }
 
6519
  return i;
 
6520
}
 
6521
 
 
6522
/**
 
6523
  Setup the strategies to eliminate semi-join duplicates.
 
6524
 
 
6525
  SYNOPSIS
 
6526
    setup_semijoin_dups_elimination()
 
6527
      join           Join to process
 
6528
      options        Join options (needed to see if join buffering will be
 
6529
                     used or not)
 
6530
      no_jbuf_after  Another bit of information re where join buffering will
 
6531
                     be used.
 
6532
 
 
6533
  DESCRIPTION
 
6534
    Setup the strategies to eliminate semi-join duplicates. ATM there are 3
 
6535
    strategies:
 
6536
 
 
6537
    1. DuplicateWeedout (use of temptable to remove duplicates based on rowids
 
6538
                         of row combinations)
 
6539
    2. FirstMatch (pick only the 1st matching row combination of inner tables)
 
6540
    3. InsideOut (scanning the sj-inner table in a way that groups duplicates
 
6541
                  together and picking the 1st one)
 
6542
 
 
6543
    The join order has "duplicate-generating ranges", and every range is
 
6544
    served by one strategy or a combination of FirstMatch with with some
 
6545
    other strategy.
 
6546
 
 
6547
    "Duplicate-generating range" is defined as a range within the join order
 
6548
    that contains all of the inner tables of a semi-join. All ranges must be
 
6549
    disjoint, if tables of several semi-joins are interleaved, then the ranges
 
6550
    are joined together, which is equivalent to converting
 
6551
      SELECT ... WHERE oe1 IN (SELECT ie1 ...) AND oe2 IN (SELECT ie2 )
 
6552
    to
 
6553
      SELECT ... WHERE (oe1, oe2) IN (SELECT ie1, ie2 ... ...)
 
6554
    .
 
6555
 
 
6556
    Applicability conditions are as follows:
 
6557
 
 
6558
    DuplicateWeedout strategy
 
6559
    ~~~~~~~~~~~~~~~~~~~~~~~~~
 
6560
 
 
6561
      (ot|nt)*  [ it ((it|ot|nt)* (it|ot))]  (nt)*
 
6562
      +------+  +=========================+  +---+
 
6563
        (1)                 (2)               (3)
 
6564
 
 
6565
       (1) - Prefix of OuterTables (those that participate in
 
6566
             IN-equality and/or are correlated with subquery) and outer
 
6567
             Noncorrelated Tables.
 
6568
       (2) - The handled range. The range starts with the first sj-inner
 
6569
             table, and covers all sj-inner and outer tables
 
6570
             Within the range,  Inner, Outer, outer Noncorrelated tables
 
6571
             may follow in any order.
 
6572
       (3) - The suffix of outer Noncorrelated tables.
 
6573
 
 
6574
    FirstMatch strategy
 
6575
    ~~~~~~~~~~~~~~~~~~~
 
6576
 
 
6577
      (ot|nt)*  [ it ((it|nt)* it) ]  (nt)*
 
6578
      +------+  +==================+  +---+
 
6579
        (1)             (2)          (3)
 
6580
 
 
6581
      (1) - Prefix of outer and non-correlated tables
 
6582
      (2) - The handled range, which may contain only inner and
 
6583
            non-correlated tables.
 
6584
      (3) - The suffix of outer Noncorrelated tables.
 
6585
 
 
6586
    InsideOut strategy
 
6587
    ~~~~~~~~~~~~~~~~~~
 
6588
 
 
6589
     (ot|ct|nt) [ insideout_tbl (ot|nt|it)* it ]  (ot|nt)*
 
6590
     +--------+   +===========+ +=============+   +------+
 
6591
        (1)           (2)          (3)              (4)
 
6592
 
 
6593
      (1) - Prefix that may contain any outer tables. The prefix must contain
 
6594
            all the non-trivially correlated outer tables. (non-trivially means
 
6595
            that the correlation is not just through the IN-equality).
 
6596
 
 
6597
      (2) - Inner table for which the InsideOut scan is performed.
 
6598
 
 
6599
      (3) - The remainder of the duplicate-generating range. It is served by
 
6600
            application of FirstMatch strategy, with the exception that
 
6601
            outer IN-correlated tables are considered to be non-correlated.
 
6602
 
 
6603
      (4) - THe suffix of outer and outer non-correlated tables.
 
6604
 
 
6605
    If several strategies are applicable, their relative priorities are:
 
6606
      1. InsideOut
 
6607
      2. FirstMatch
 
6608
      3. DuplicateWeedout
 
6609
 
 
6610
    This function walks over the join order and sets up the strategies by
 
6611
    setting appropriate members in join_tab structures.
 
6612
 
 
6613
  RETURN
 
6614
    false  OK
 
6615
    true   Out of memory error
 
6616
*/
 
6617
static int setup_semijoin_dups_elimination(JOIN *join, uint64_t options, uint32_t no_jbuf_after)
 
6618
{
 
6619
  table_map cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
6620
  struct {
 
6621
    /*
 
6622
      0 - invalid (EOF marker),
 
6623
      1 - InsideOut,
 
6624
      2 - Temptable (maybe confluent),
 
6625
      3 - Temptable with join buffering
 
6626
    */
 
6627
    uint32_t strategy;
 
6628
    uint32_t start_idx; /* Left range bound */
 
6629
    uint32_t end_idx;   /* Right range bound */
 
6630
    /*
 
6631
      For Temptable strategy: Bitmap of all outer and correlated tables from
 
6632
      all involved join nests.
 
6633
    */
 
6634
    table_map outer_tables;
 
6635
  } dups_ranges [MAX_TABLES];
 
6636
 
 
6637
  TableList *emb_insideout_nest= NULL;
 
6638
  table_map emb_sj_map= 0;  /* A bitmap of sj-nests (that is, their sj-inner
 
6639
                               tables) whose ranges we're in */
 
6640
  table_map emb_outer_tables= 0; /* sj-outer tables for those sj-nests */
 
6641
  table_map range_start_map= 0; /* table_map at current range start */
 
6642
  bool dealing_with_jbuf= false; /* true <=> table within cur range uses join buf */
 
6643
  int cur_range= 0;
 
6644
  uint32_t i;
 
6645
 
 
6646
  /*
 
6647
    First pass: locate the duplicate-generating ranges and pick the strategies.
 
6648
  */
 
6649
  for (i=join->const_tables ; i < join->tables ; i++)
 
6650
  {
 
6651
    JOIN_TAB *tab=join->join_tab+i;
 
6652
    Table *table=tab->table;
 
6653
    cur_map |= table->map;
 
6654
 
 
6655
    if (tab->emb_sj_nest) // Encountered an sj-inner table
 
6656
    {
 
6657
      if (!emb_sj_map)
 
6658
      {
 
6659
        dups_ranges[cur_range].start_idx= i;
 
6660
        range_start_map= cur_map & ~table->map;
 
6661
        /*
 
6662
          Remember if this is a possible start of range that is covered by
 
6663
          the InsideOut strategy (the reason that it is not covered could
 
6664
          be that it overlaps with anther semi-join's range. we don't
 
6665
          support InsideOut for joined ranges)
 
6666
        */
 
6667
        if (join->best_positions[i].use_insideout_scan)
 
6668
          emb_insideout_nest= tab->emb_sj_nest;
 
6669
      }
 
6670
 
 
6671
      emb_sj_map |= tab->emb_sj_nest->sj_inner_tables;
 
6672
      emb_outer_tables |= tab->emb_sj_nest->nested_join->sj_depends_on;
 
6673
 
 
6674
      if (tab->emb_sj_nest != emb_insideout_nest)
 
6675
      {
 
6676
        /*
 
6677
          Two different semi-joins interleave. This cannot be handled by
 
6678
          InsideOut strategy.
 
6679
        */
 
6680
        emb_insideout_nest= NULL;
 
6681
      }
 
6682
    }
 
6683
 
 
6684
    if (emb_sj_map) /* We're in duplicate-generating range */
 
6685
    {
 
6686
      if (i != join->const_tables && !(options & SELECT_NO_JOIN_CACHE) &&
 
6687
          tab->type == JT_ALL && tab->use_quick != 2 && !tab->first_inner &&
 
6688
          i <= no_jbuf_after && !dealing_with_jbuf)
 
6689
      {
 
6690
        /*
 
6691
          This table uses join buffering, which makes use of FirstMatch or
 
6692
          InsideOut strategies impossible for the current and (we assume)
 
6693
          preceding duplicate-producing ranges.
 
6694
          That is, for the join order:
 
6695
 
 
6696
              x x [ x  x]  x  [x x x]  x  [x x X*  x] x
 
6697
                  |     |     |     |          | \
 
6698
                  +-----+     +-----+          |  join buffering use
 
6699
                     r1          r2         we're here
 
6700
 
 
6701
          we'll have to remove r1 and r2 and use duplicate-elimination
 
6702
          strategy that spans all the tables, starting from the very 1st
 
6703
          one.
 
6704
        */
 
6705
        dealing_with_jbuf= true;
 
6706
        emb_insideout_nest= false;
 
6707
 
 
6708
        /*
 
6709
          Absorb all preceding duplicate-eliminating ranges. Their strategies
 
6710
          do not matter:
 
6711
        */
 
6712
        for (int prev_range= 0; prev_range < cur_range; prev_range++)
 
6713
        {
 
6714
          dups_ranges[cur_range].outer_tables |=
 
6715
            dups_ranges[prev_range].outer_tables;
 
6716
        }
 
6717
        dups_ranges[0].start_idx= 0; /* Will need to start from the 1st table */
 
6718
        dups_ranges[0].outer_tables= dups_ranges[cur_range].outer_tables;
 
6719
        cur_range=  0;
 
6720
      }
 
6721
 
 
6722
      /*
 
6723
        Check if we are at the end of duplicate-producing range. We are if
 
6724
 
 
6725
        1. It's an InsideOut range (which presumes all correlated tables are
 
6726
           in the prefix), and all inner tables are in the join order prefix,
 
6727
           or
 
6728
        2. It's a DuplicateElimination range (possibly covering several
 
6729
           SJ-nests), and all inner, outer, and correlated tables of all
 
6730
           sj-nests are in the join order prefix.
 
6731
      */
 
6732
      bool end_of_range= false;
 
6733
      if (emb_insideout_nest &&
 
6734
          bitmap_covers(cur_map, emb_insideout_nest->sj_inner_tables))
 
6735
      {
 
6736
        /* Save that this range is handled with InsideOut: */
 
6737
        dups_ranges[cur_range].strategy= 1;
 
6738
        end_of_range= true;
 
6739
      }
 
6740
      else if (bitmap_covers(cur_map, emb_outer_tables | emb_sj_map))
 
6741
      {
 
6742
        /*
 
6743
          This is a complete range to be handled with either DuplicateWeedout
 
6744
          or FirstMatch
 
6745
        */
 
6746
        dups_ranges[cur_range].strategy= dealing_with_jbuf? 3 : 2;
 
6747
        /*
 
6748
          This will hold tables from within the range that need to be put
 
6749
          into the join buffer before we can use the FirstMatch on its tail.
 
6750
        */
 
6751
        dups_ranges[cur_range].outer_tables= emb_outer_tables &
 
6752
                                             ~range_start_map;
 
6753
        end_of_range= true;
 
6754
      }
 
6755
 
 
6756
      if (end_of_range)
 
6757
      {
 
6758
        dups_ranges[cur_range].end_idx= i+1;
 
6759
        emb_sj_map= emb_outer_tables= 0;
 
6760
        emb_insideout_nest= NULL;
 
6761
        dealing_with_jbuf= false;
 
6762
        dups_ranges[++cur_range].strategy= 0;
 
6763
      }
 
6764
    }
 
6765
  }
 
6766
 
 
6767
  Session *session= join->session;
 
6768
  SJ_TMP_TABLE **next_sjtbl_ptr= &join->sj_tmp_tables;
 
6769
  /*
 
6770
    Second pass: setup the chosen strategies
 
6771
  */
 
6772
  for (int j= 0; j < cur_range; j++)
 
6773
  {
 
6774
    JOIN_TAB *tab=join->join_tab + dups_ranges[j].start_idx;
 
6775
    JOIN_TAB *jump_to;
 
6776
    if (dups_ranges[j].strategy == 1)  // InsideOut strategy
 
6777
    {
 
6778
      tab->insideout_match_tab= join->join_tab + dups_ranges[j].end_idx - 1;
 
6779
      jump_to= tab++;
 
6780
    }
 
6781
    else // DuplicateWeedout strategy
 
6782
    {
 
6783
      SJ_TMP_TABLE::TAB sjtabs[MAX_TABLES];
 
6784
      table_map weed_cur_map= join->const_table_map | PSEUDO_TABLE_BITS;
 
6785
      uint32_t jt_rowid_offset= 0; // # tuple bytes are already occupied (w/o NULL bytes)
 
6786
      uint32_t jt_null_bits= 0;    // # null bits in tuple bytes
 
6787
      SJ_TMP_TABLE::TAB *last_tab= sjtabs;
 
6788
      uint32_t rowid_keep_flags= JOIN_TAB::CALL_POSITION | JOIN_TAB::KEEP_ROWID;
 
6789
      JOIN_TAB *last_outer_tab= tab - 1;
 
6790
      /*
 
6791
        Walk through the range and remember
 
6792
         - tables that need their rowids to be put into temptable
 
6793
         - the last outer table
 
6794
      */
 
6795
      for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
6796
      {
 
6797
        if (sj_table_is_included(join, tab))
 
6798
        {
 
6799
          last_tab->join_tab= tab;
 
6800
          last_tab->rowid_offset= jt_rowid_offset;
 
6801
          jt_rowid_offset += tab->table->file->ref_length;
 
6802
          if (tab->table->maybe_null)
 
6803
          {
 
6804
            last_tab->null_byte= jt_null_bits / 8;
 
6805
            last_tab->null_bit= jt_null_bits++;
 
6806
          }
 
6807
          last_tab++;
 
6808
          tab->table->prepare_for_position();
 
6809
          tab->rowid_keep_flags= rowid_keep_flags;
 
6810
        }
 
6811
        weed_cur_map |= tab->table->map;
 
6812
        if (!tab->emb_sj_nest && bitmap_covers(weed_cur_map,
 
6813
                                               dups_ranges[j].outer_tables))
 
6814
          last_outer_tab= tab;
 
6815
      }
 
6816
 
 
6817
      if (jt_rowid_offset) /* Temptable has at least one rowid */
 
6818
      {
 
6819
        SJ_TMP_TABLE *sjtbl;
 
6820
        uint32_t tabs_size= (last_tab - sjtabs) * sizeof(SJ_TMP_TABLE::TAB);
 
6821
        if (!(sjtbl= (SJ_TMP_TABLE*)session->alloc(sizeof(SJ_TMP_TABLE))) ||
 
6822
            !(sjtbl->tabs= (SJ_TMP_TABLE::TAB*) session->alloc(tabs_size)))
 
6823
          return(true);
 
6824
        memcpy(sjtbl->tabs, sjtabs, tabs_size);
 
6825
        sjtbl->tabs_end= sjtbl->tabs + (last_tab - sjtabs);
 
6826
        sjtbl->rowid_len= jt_rowid_offset;
 
6827
        sjtbl->null_bits= jt_null_bits;
 
6828
        sjtbl->null_bytes= (jt_null_bits + 7)/8;
 
6829
 
 
6830
        *next_sjtbl_ptr= sjtbl;
 
6831
        next_sjtbl_ptr= &(sjtbl->next);
 
6832
        sjtbl->next= NULL;
 
6833
 
 
6834
        sjtbl->tmp_table=
 
6835
          create_duplicate_weedout_tmp_table(session,
 
6836
                                             sjtbl->rowid_len +
 
6837
                                             sjtbl->null_bytes,
 
6838
                                             sjtbl);
 
6839
 
 
6840
        join->join_tab[dups_ranges[j].start_idx].flush_weedout_table= sjtbl;
 
6841
        join->join_tab[dups_ranges[j].end_idx - 1].check_weed_out_table= sjtbl;
 
6842
      }
 
6843
      tab= last_outer_tab + 1;
 
6844
      jump_to= last_outer_tab;
 
6845
    }
 
6846
 
 
6847
    /* Create the FirstMatch tail */
 
6848
    for (; tab < join->join_tab + dups_ranges[j].end_idx; tab++)
 
6849
    {
 
6850
      if (tab->emb_sj_nest)
 
6851
        tab->do_firstmatch= jump_to;
 
6852
      else
 
6853
        jump_to= tab;
 
6854
    }
 
6855
  }
 
6856
  return(false);
 
6857
}
 
6858
 
 
6859
static void cleanup_sj_tmp_tables(JOIN *join)
 
6860
{
 
6861
  for (SJ_TMP_TABLE *sj_tbl= join->sj_tmp_tables; sj_tbl;
 
6862
       sj_tbl= sj_tbl->next)
 
6863
  {
 
6864
    if (sj_tbl->tmp_table)
 
6865
    {
 
6866
      sj_tbl->tmp_table->free_tmp_table(join->session);
 
6867
    }
 
6868
  }
 
6869
  join->sj_tmp_tables= NULL;
 
6870
}
 
6871
 
 
6872
/**
 
6873
  Create a condition for a const reference and add this to the
 
6874
  currenct select for the table.
 
6875
*/
 
6876
static bool add_ref_to_table_cond(Session *session, JOIN_TAB *join_tab)
 
6877
{
 
6878
  if (!join_tab->ref.key_parts)
 
6879
    return(false);
 
6880
 
 
6881
  Item_cond_and *cond=new Item_cond_and();
 
6882
  Table *table=join_tab->table;
 
6883
  int error;
 
6884
  if (!cond)
 
6885
    return(true);
 
6886
 
 
6887
  for (uint32_t i=0 ; i < join_tab->ref.key_parts ; i++)
 
6888
  {
 
6889
    Field *field=table->field[table->key_info[join_tab->ref.key].key_part[i].
 
6890
                              fieldnr-1];
 
6891
    Item *value=join_tab->ref.items[i];
 
6892
    cond->add(new Item_func_equal(new Item_field(field), value));
 
6893
  }
 
6894
  if (session->is_fatal_error)
 
6895
    return(true);
 
6896
 
 
6897
  if (!cond->fixed)
 
6898
    cond->fix_fields(session, (Item**)&cond);
 
6899
  if (join_tab->select)
 
6900
  {
 
6901
    error=(int) cond->add(join_tab->select->cond);
 
6902
    join_tab->select_cond=join_tab->select->cond=cond;
 
6903
  }
 
6904
  else if ((join_tab->select= make_select(join_tab->table, 0, 0, cond, 0,
 
6905
                                          &error)))
 
6906
    join_tab->select_cond=cond;
 
6907
 
 
6908
  return(error ? true : false);
 
6909
}
 
6910
 
 
6911
/**
 
6912
   @brief Replaces an expression destructively inside the expression tree of
 
6913
   the WHERE clase.
 
6914
 
 
6915
   @note Because of current requirements for semijoin flattening, we do not
 
6916
   need to recurse here, hence this function will only examine the top-level
 
6917
   AND conditions. (see JOIN::prepare, comment above the line
 
6918
   'if (do_materialize)'
 
6919
 
 
6920
   @param join The top-level query.
 
6921
   @param old_cond The expression to be replaced.
 
6922
   @param new_cond The expression to be substituted.
 
6923
   @param do_fix_fields If true, Item::fix_fields(Session*, Item**) is called for
 
6924
   the new expression.
 
6925
   @return <code>true</code> if there was an error, <code>false</code> if
 
6926
   successful.
 
6927
*/
 
6928
static bool replace_where_subcondition(JOIN *join, Item *old_cond,
 
6929
                                       Item *new_cond, bool do_fix_fields)
 
6930
{
 
6931
  if (join->conds == old_cond) {
 
6932
    join->conds= new_cond;
 
6933
    if (do_fix_fields)
 
6934
      new_cond->fix_fields(join->session, &join->conds);
 
6935
    return false;
 
6936
  }
 
6937
 
 
6938
  if (join->conds->type() == Item::COND_ITEM) {
 
6939
    List_iterator<Item> li(*((Item_cond*)join->conds)->argument_list());
 
6940
    Item *item;
 
6941
    while ((item= li++))
 
6942
      if (item == old_cond)
 
6943
      {
 
6944
        li.replace(new_cond);
 
6945
        if (do_fix_fields)
 
6946
          new_cond->fix_fields(join->session, li.ref());
 
6947
        return false;
 
6948
      }
 
6949
  }
 
6950
 
 
6951
  return true;
 
6952
}
 
6953
 
 
6954
/*
 
6955
  Pull tables out of semi-join nests, if possible
 
6956
 
 
6957
  SYNOPSIS
 
6958
    pull_out_semijoin_tables()
 
6959
      join  The join where to do the semi-join flattening
 
6960
 
 
6961
  DESCRIPTION
 
6962
    Try to pull tables out of semi-join nests.
 
6963
 
 
6964
    PRECONDITIONS
 
6965
    When this function is called, the join may have several semi-join nests
 
6966
    (possibly within different semi-join nests), but it is guaranteed that
 
6967
    one semi-join nest does not contain another.
 
6968
 
 
6969
    ACTION
 
6970
    A table can be pulled out of the semi-join nest if
 
6971
     - It is a constant table
 
6972
     - It is accessed
 
6973
 
 
6974
    POSTCONDITIONS
 
6975
     * Pulled out tables have JOIN_TAB::emb_sj_nest == NULL (like the outer
 
6976
       tables)
 
6977
     * Tables that were not pulled out have JOIN_TAB::emb_sj_nest.
 
6978
     * Semi-join nests TableList::sj_inner_tables
 
6979
 
 
6980
    This operation is (and should be) performed at each PS execution since
 
6981
    tables may become/cease to be constant across PS reexecutions.
 
6982
 
 
6983
  RETURN
 
6984
    0 - OK
 
6985
    1 - Out of memory error
 
6986
*/
 
6987
static int pull_out_semijoin_tables(JOIN *join)
 
6988
{
 
6989
  TableList *sj_nest;
 
6990
  List_iterator<TableList> sj_list_it(join->select_lex->sj_nests);
 
6991
 
 
6992
  /* Try pulling out of the each of the semi-joins */
 
6993
  while ((sj_nest= sj_list_it++))
 
6994
  {
 
6995
    /* Action #1: Mark the constant tables to be pulled out */
 
6996
    table_map pulled_tables= 0;
 
6997
 
 
6998
    List_iterator<TableList> child_li(sj_nest->nested_join->join_list);
 
6999
    TableList *tbl;
 
7000
    while ((tbl= child_li++))
 
7001
    {
 
7002
      if (tbl->table)
 
7003
      {
 
7004
        tbl->table->reginfo.join_tab->emb_sj_nest= sj_nest;
 
7005
        if (tbl->table->map & join->const_table_map)
 
7006
        {
 
7007
          pulled_tables |= tbl->table->map;
 
7008
        }
 
7009
      }
 
7010
    }
 
7011
 
 
7012
    /*
 
7013
      Action #2: Find which tables we can pull out based on
 
7014
      update_ref_and_keys() data. Note that pulling one table out can allow
 
7015
      us to pull out some other tables too.
 
7016
    */
 
7017
    bool pulled_a_table;
 
7018
    do
 
7019
    {
 
7020
      pulled_a_table= false;
 
7021
      child_li.rewind();
 
7022
      while ((tbl= child_li++))
 
7023
      {
 
7024
        if (tbl->table && !(pulled_tables & tbl->table->map))
 
7025
        {
 
7026
          if (find_eq_ref_candidate(tbl->table,
 
7027
                                    sj_nest->nested_join->used_tables &
 
7028
                                    ~pulled_tables))
 
7029
          {
 
7030
            pulled_a_table= true;
 
7031
            pulled_tables |= tbl->table->map;
 
7032
          }
 
7033
        }
 
7034
      }
 
7035
    } while (pulled_a_table);
 
7036
 
 
7037
    child_li.rewind();
 
7038
    if ((sj_nest)->nested_join->used_tables == pulled_tables)
 
7039
    {
 
7040
      (sj_nest)->sj_inner_tables= 0;
 
7041
      while ((tbl= child_li++))
 
7042
      {
 
7043
        if (tbl->table)
 
7044
          tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
7045
      }
 
7046
    }
 
7047
    else
 
7048
    {
 
7049
      /* Record the bitmap of inner tables, mark the inner tables */
 
7050
      table_map inner_tables=(sj_nest)->nested_join->used_tables &
 
7051
                             ~pulled_tables;
 
7052
      (sj_nest)->sj_inner_tables= inner_tables;
 
7053
      while ((tbl= child_li++))
 
7054
      {
 
7055
        if (tbl->table)
 
7056
        {
 
7057
          if (inner_tables & tbl->table->map)
 
7058
            tbl->table->reginfo.join_tab->emb_sj_nest= (sj_nest);
 
7059
          else
 
7060
            tbl->table->reginfo.join_tab->emb_sj_nest= NULL;
 
7061
        }
 
7062
      }
 
7063
    }
 
7064
  }
 
7065
  return(0);
 
7066
}
 
7067
 
 
7068
/*
 
7069
  SemiJoinDuplicateElimination: Weed out duplicate row combinations
 
7070
 
 
7071
  SYNPOSIS
 
7072
    do_sj_dups_weedout()
 
7073
 
 
7074
  RETURN
 
7075
    -1  Error
 
7076
    1   The row combination is a duplicate (discard it)
 
7077
    0   The row combination is not a duplicate (continue)
 
7078
*/
 
7079
static int do_sj_dups_weedout(Session *session, SJ_TMP_TABLE *sjtbl)
 
7080
{
 
7081
  int error;
 
7082
  SJ_TMP_TABLE::TAB *tab= sjtbl->tabs;
 
7083
  SJ_TMP_TABLE::TAB *tab_end= sjtbl->tabs_end;
 
7084
  unsigned char *ptr= sjtbl->tmp_table->record[0] + 1;
 
7085
  unsigned char *nulls_ptr= ptr;
 
7086
 
 
7087
  /* Put the the rowids tuple into table->record[0]: */
 
7088
 
 
7089
  // 1. Store the length
 
7090
  if (((Field_varstring*)(sjtbl->tmp_table->field[0]))->length_bytes == 1)
 
7091
  {
 
7092
    *ptr= (unsigned char)(sjtbl->rowid_len + sjtbl->null_bytes);
 
7093
    ptr++;
 
7094
  }
 
7095
  else
 
7096
  {
 
7097
    int2store(ptr, sjtbl->rowid_len + sjtbl->null_bytes);
 
7098
    ptr += 2;
 
7099
  }
 
7100
 
 
7101
  // 2. Zero the null bytes
 
7102
  if (sjtbl->null_bytes)
 
7103
  {
 
7104
    memset(ptr, 0, sjtbl->null_bytes);
 
7105
    ptr += sjtbl->null_bytes;
 
7106
  }
 
7107
 
 
7108
  // 3. Put the rowids
 
7109
  for (uint32_t i=0; tab != tab_end; tab++, i++)
 
7110
  {
 
7111
    handler *h= tab->join_tab->table->file;
 
7112
    if (tab->join_tab->table->maybe_null && tab->join_tab->table->null_row)
 
7113
    {
 
7114
      /* It's a NULL-complemented row */
 
7115
      *(nulls_ptr + tab->null_byte) |= tab->null_bit;
 
7116
      memset(ptr + tab->rowid_offset, 0, h->ref_length);
 
7117
    }
 
7118
    else
 
7119
    {
 
7120
      /* Copy the rowid value */
 
7121
      if (tab->join_tab->rowid_keep_flags & JOIN_TAB::CALL_POSITION)
 
7122
        h->position(tab->join_tab->table->record[0]);
 
7123
      memcpy(ptr + tab->rowid_offset, h->ref, h->ref_length);
 
7124
    }
 
7125
  }
 
7126
 
 
7127
  error= sjtbl->tmp_table->file->ha_write_row(sjtbl->tmp_table->record[0]);
 
7128
  if (error)
 
7129
  {
 
7130
    /* create_myisam_from_heap will generate error if needed */
 
7131
    if (sjtbl->tmp_table->file->is_fatal_error(error, HA_CHECK_DUP) &&
 
7132
        create_myisam_from_heap(session, sjtbl->tmp_table, sjtbl->start_recinfo,
 
7133
                                &sjtbl->recinfo, error, 1))
 
7134
      return -1;
 
7135
    //return (error == HA_ERR_FOUND_DUPP_KEY || error== HA_ERR_FOUND_DUPP_UNIQUE) ? 1: -1;
 
7136
    return 1;
 
7137
  }
 
7138
  return 0;
 
7139
}
 
7140
 
 
7141
static void free_blobs(Field **ptr)
 
7142
{
 
7143
  for (; *ptr ; ptr++)
 
7144
  {
 
7145
    if ((*ptr)->flags & BLOB_FLAG)
 
7146
      ((Field_blob *) (*ptr))->free();
 
7147
  }
 
7148
}
 
7149
 
 
7150
static bool bitmap_covers(const table_map x, const table_map y)
 
7151
{
 
7152
  return !test(y & ~x);
 
7153
}
 
7154
 
 
7155
/*
 
7156
  Check if the table's rowid is included in the temptable
 
7157
 
 
7158
  SYNOPSIS
 
7159
    sj_table_is_included()
 
7160
      join      The join
 
7161
      join_tab  The table to be checked
 
7162
 
 
7163
  DESCRIPTION
 
7164
    SemiJoinDuplicateElimination: check the table's rowid should be included
 
7165
    in the temptable. This is so if
 
7166
 
 
7167
    1. The table is not embedded within some semi-join nest
 
7168
    2. The has been pulled out of a semi-join nest, or
 
7169
 
 
7170
    3. The table is functionally dependent on some previous table
 
7171
 
 
7172
    [4. This is also true for constant tables that can't be
 
7173
        NULL-complemented but this function is not called for such tables]
 
7174
 
 
7175
  RETURN
 
7176
    true  - Include table's rowid
 
7177
    false - Don't
 
7178
*/
 
7179
static bool sj_table_is_included(JOIN *join, JOIN_TAB *join_tab)
 
7180
{
 
7181
  if (join_tab->emb_sj_nest)
 
7182
    return false;
 
7183
 
 
7184
  /* Check if this table is functionally dependent on the tables that
 
7185
     are within the same outer join nest
 
7186
  */
 
7187
  TableList *embedding= join_tab->table->pos_in_table_list->embedding;
 
7188
  if (join_tab->type == JT_EQ_REF)
 
7189
  {
 
7190
    Table_map_iterator it(join_tab->ref.depend_map & ~PSEUDO_TABLE_BITS);
 
7191
    uint32_t idx;
 
7192
    while ((idx= it.next_bit())!=Table_map_iterator::BITMAP_END)
 
7193
    {
 
7194
      JOIN_TAB *ref_tab= join->join_tab + idx;
 
7195
      if (embedding == ref_tab->table->pos_in_table_list->embedding)
 
7196
        return true;
 
7197
    }
 
7198
    /* Ok, functionally dependent */
 
7199
    return false;
 
7200
  }
 
7201
  /* Not functionally dependent => need to include*/
 
7202
  return true;
 
7203
}
 
7204
 
 
7205
/**
 
7206
  @} (end of group Query_Optimizer)
 
7207
*/