~drizzle-trunk/drizzle/development

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