MySQL  8.0.18
Source Code Documentation
sql_select.h
Go to the documentation of this file.
1 #ifndef SQL_SELECT_INCLUDED
2 #define SQL_SELECT_INCLUDED
3 
4 /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
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, version 2.0,
8  as published by the Free Software Foundation.
9 
10  This program is also distributed with certain software (including
11  but not limited to OpenSSL) that is licensed under separate terms,
12  as designated in a particular file or component or in included license
13  documentation. The authors of MySQL hereby grant you an additional
14  permission to link the program and your derivative works with the
15  separately licensed software that they have included with MySQL.
16 
17  This program is distributed in the hope that it will be useful,
18  but WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20  GNU General Public License, version 2.0, for more details.
21 
22  You should have received a copy of the GNU General Public License
23  along with this program; if not, write to the Free Software
24  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
25 
26 /**
27  @file sql/sql_select.h
28 */
29 
30 #include <limits.h>
31 #include <stddef.h>
32 #include <sys/types.h>
33 #include <functional>
34 
35 #include "my_alloc.h"
36 #include "my_base.h"
37 #include "my_bitmap.h"
38 #include "my_dbug.h"
39 #include "my_inttypes.h"
40 #include "my_sqlcommand.h"
41 #include "my_table_map.h"
42 #include "sql/field.h"
43 #include "sql/item.h"
44 #include "sql/item_cmpfunc.h" // Item_cond_and
45 #include "sql/opt_costmodel.h"
46 #include "sql/sql_bitmap.h"
47 #include "sql/sql_class.h" // THD
48 #include "sql/sql_cmd_dml.h" // Sql_cmd_dml
49 #include "sql/sql_const.h"
50 #include "sql/sql_executor.h"
51 #include "sql/sql_lex.h"
52 #include "sql/sql_opt_exec_shared.h" // join_type
53 #include "sql/system_variables.h"
54 #include "sql/table.h"
55 
56 class Item_func;
57 class JOIN_TAB;
58 class KEY;
59 class QEP_TAB;
60 class Query_result;
61 class Temp_table_param;
62 template <class T>
63 class List;
64 
66 
67 class Sql_cmd_select : public Sql_cmd_dml {
68  public:
69  explicit Sql_cmd_select(Query_result *result_arg) : Sql_cmd_dml() {
70  result = result_arg;
71  }
72 
73  virtual enum_sql_command sql_command_code() const override {
74  return SQLCOM_SELECT;
75  }
76 
77  virtual bool is_data_change_stmt() const override { return false; }
78 
79  virtual bool accept(THD *thd, Select_lex_visitor *visitor) override {
80  return thd->lex->unit->accept(visitor);
81  }
82 
84 
85  protected:
86  virtual bool precheck(THD *thd) override;
87 
88  virtual bool prepare_inner(THD *thd) override;
89 };
90 
91 /**
92  Returns a constant of type 'type' with the 'A' lowest-weight bits set.
93  Example: LOWER_BITS(uint, 3) == 7.
94  Requirement: A < sizeof(type) * 8.
95 */
96 #define LOWER_BITS(type, A) ((type)(((type)1 << (A)) - 1))
97 
98 /* Values in optimize */
99 #define KEY_OPTIMIZE_EXISTS 1
100 #define KEY_OPTIMIZE_REF_OR_NULL 2
101 #define FT_KEYPART (MAX_REF_PARTS + 10)
102 
103 /**
104  A Key_use represents an equality predicate of the form (table.column = val),
105  where the column is indexed by @c keypart in @c key and @c val is either a
106  constant, a column from other table, or an expression over column(s) from
107  other table(s). If @c val is not a constant, then the Key_use specifies an
108  equi-join predicate, and @c table must be put in the join plan after all
109  tables in @c used_tables.
110 
111  At an abstract level a Key_use instance can be viewed as a directed arc
112  of an equi-join graph, where the arc originates from the table(s)
113  containing the column(s) that produce the values used for index lookup
114  into @c table, and the arc points into @c table.
115 
116  For instance, assuming there is only an index t3(c), the query
117 
118  @code
119  SELECT * FROM t1, t2, t3
120  WHERE t1.a = t3.c AND
121  t2.b = t3.c;
122  @endcode
123 
124  would generate two arcs (instances of Key_use)
125 
126  @code
127  t1-- a ->- c --.
128  |
129  V
130  t3
131  ^
132  |
133  t2-- b ->- c --'
134  @endcode
135 
136  If there were indexes t1(a), and t2(b), then the equi-join graph
137  would have two additional arcs "c->a" and "c->b" recording the fact
138  that it is possible to perform lookup in either direction.
139 
140  @code
141  t1-- a ->- c --. ,-- c -<- b --- t2
142  ^ | | ^
143  | | | |
144  `-- a -<- c - v v-- c ->- b ----'
145  t3
146  @endcode
147 
148  The query
149 
150  @code
151  SELECT * FROM t1, t2, t3 WHERE t1.a + t2.b = t3.c;
152  @endcode
153 
154  can be viewed as a graph with one "multi-source" arc:
155 
156  @code
157  t1-- a ---
158  |
159  >-- c --> t3
160  |
161  t2-- b ---
162  @endcode
163 
164  The graph of all equi-join conditions usable for index lookup is
165  stored as an ordered sequence of Key_use elements in
166  JOIN::keyuse_array. See sort_keyuse() for details on the
167  ordering. Each JOIN_TAB::keyuse points to the first array element
168  with the same table.
169 */
170 class Key_use {
171  public:
172  // We need the default constructor for unit testing.
174  : table_ref(NULL),
175  val(NULL),
176  used_tables(0),
177  key(0),
178  keypart(0),
179  optimize(0),
180  keypart_map(0),
181  ref_table_rows(0),
183  cond_guard(NULL),
184  sj_pred_no(UINT_MAX),
185  bound_keyparts(0),
186  fanout(0.0),
187  read_cost(0.0) {}
188 
189  Key_use(TABLE_LIST *table_ref_arg, Item *val_arg, table_map used_tables_arg,
190  uint key_arg, uint keypart_arg, uint optimize_arg,
191  key_part_map keypart_map_arg, ha_rows ref_table_rows_arg,
192  bool null_rejecting_arg, bool *cond_guard_arg, uint sj_pred_no_arg)
193  : table_ref(table_ref_arg),
194  val(val_arg),
195  used_tables(used_tables_arg),
196  key(key_arg),
197  keypart(keypart_arg),
198  optimize(optimize_arg),
199  keypart_map(keypart_map_arg),
200  ref_table_rows(ref_table_rows_arg),
201  null_rejecting(null_rejecting_arg),
202  cond_guard(cond_guard_arg),
203  sj_pred_no(sj_pred_no_arg),
204  bound_keyparts(0),
205  fanout(0.0),
206  read_cost(0.0) {}
207 
208  TABLE_LIST *table_ref; ///< table owning the index
209 
210  /**
211  Value used for lookup into @c key. It may be an Item_field, a
212  constant or any other expression. If @c val contains a field from
213  another table, then we have a join condition, and the table(s) of
214  the field(s) in @c val should be before @c table in the join plan.
215  */
217 
218  /**
219  All tables used in @c val, that is all tables that provide bindings
220  for the expression @c val. These tables must be in the plan before
221  executing the equi-join described by a Key_use.
222  */
224  uint key; ///< number of index
225  uint keypart; ///< used part of the index
226  uint optimize; ///< 0, or KEY_OPTIMIZE_*
227  key_part_map keypart_map; ///< like keypart, but as a bitmap
228  ha_rows ref_table_rows; ///< Estimate of how many rows for a key value
229  /**
230  If true, the comparison this value was created from will not be
231  satisfied if val has NULL 'value'.
232  Not used if the index is fulltext (such index cannot be used for
233  equalities).
234  */
236  /**
237  !NULL - This Key_use was created from an equality that was wrapped into
238  an Item_func_trig_cond. This means the equality (and validity of
239  this Key_use element) can be turned on and off. The on/off state
240  is indicted by the pointed value:
241  *cond_guard == true @<=@> equality condition is on
242  *cond_guard == false @<=@> equality condition is off
243 
244  NULL - Otherwise (the source equality can't be turned off)
245 
246  Not used if the index is fulltext (such index cannot be used for
247  equalities).
248  */
249  bool *cond_guard;
250  /**
251  0..63 @<=@> This was created from semi-join IN-equality # sj_pred_no.
252  UINT_MAX Otherwise
253 
254  Not used if the index is fulltext (such index cannot be used for
255  semijoin).
256 
257  @see get_semi_join_select_list_index()
258  */
260 
261  /*
262  The three members below are different from the rest of Key_use: they are
263  set only by Optimize_table_order, and they change with the currently
264  considered join prefix.
265  */
266 
267  /**
268  The key columns which are equal to expressions depending only of earlier
269  tables of the current join prefix.
270  This information is stored only in the first Key_use of the index.
271  */
273 
274  /**
275  Fanout of the ref access path for this index, in the current join
276  prefix.
277  This information is stored only in the first Key_use of the index.
278  */
279  double fanout;
280 
281  /**
282  Cost of the ref access path for the current join prefix, i.e. the
283  cost of using ref access once multiplied by estimated number of
284  partial rows from tables earlier in the join sequence.
285  read_cost does NOT include cost of processing rows on the
286  server side (row_evaluate_cost).
287 
288  Example: If the cost of ref access on this index is 5, and the
289  estimated number of partial rows from earlier tables is 10,
290  read_cost=50.
291 
292  This information is stored only in the first Key_use of the index.
293  */
294  double read_cost;
295 };
296 
297 /// @returns join type according to quick select type used
299 
300 class JOIN;
301 
302 #define SJ_OPT_NONE 0
303 #define SJ_OPT_DUPS_WEEDOUT 1
304 #define SJ_OPT_LOOSE_SCAN 2
305 #define SJ_OPT_FIRST_MATCH 3
306 #define SJ_OPT_MATERIALIZE_LOOKUP 4
307 #define SJ_OPT_MATERIALIZE_SCAN 5
308 
309 inline bool sj_is_materialize_strategy(uint strategy) {
310  return strategy >= SJ_OPT_MATERIALIZE_LOOKUP;
311 }
312 
313 /**
314  Bits describing quick select type
315 */
317 
318 /**
319  A position of table within a join order. This structure is primarily used
320  as a part of @c join->positions and @c join->best_positions arrays.
321 
322  One POSITION element contains information about:
323  - Which table is accessed
324  - Which access method was chosen
325  = Its cost and \#of output records
326  - Semi-join strategy choice. Note that there are two different
327  representation formats:
328  1. The one used during join optimization
329  2. The one used at plan refinement/code generation stage.
330  We call fix_semijoin_strategies_for_picked_join_order() to switch
331  between #1 and #2. See that function's comment for more details.
332 
333  - Semi-join optimization state. When we're running join optimization,
334  we main a state for every semi-join strategy which are various
335  variables that tell us if/at which point we could consider applying the
336  strategy.
337  The variables are really a function of join prefix but they are too
338  expensive to re-caclulate for every join prefix we consider, so we
339  maintain current state in join->positions[\#tables_in_prefix]. See
340  advance_sj_state() for details.
341 
342  This class has to stay a POD, because it is memcpy'd in many places.
343 */
344 
345 struct POSITION {
346  /**
347  The number of rows that will be fetched by the chosen access
348  method per each row combination of previous tables. That is:
349 
350  rows_fetched = selectivity(access_condition) * cardinality(table)
351 
352  where 'access_condition' is whatever condition was chosen for
353  index access, depending on the access method ('ref', 'range',
354  etc.)
355 
356  @note For index/table scans, rows_fetched may be less than
357  the number of rows in the table because the cost of evaluating
358  constant conditions is included in the scan cost, and the number
359  of rows produced by these scans is the estimated number of rows
360  that pass the constant conditions. @see
361  Optimize_table_order::calculate_scan_cost() . But this is only during
362  planning; make_join_readinfo() simplifies it for EXPLAIN.
363  */
364  double rows_fetched;
365 
366  /**
367  Cost of accessing the table in course of the entire complete join
368  execution, i.e. cost of one access method use (e.g. 'range' or
369  'ref' scan ) multiplied by estimated number of rows from tables
370  earlier in the join sequence.
371 
372  read_cost does NOT include cost of processing rows within the
373  executor (row_evaluate_cost).
374  */
375  double read_cost;
376 
377  /**
378  The fraction of the 'rows_fetched' rows that will pass the table
379  conditions that were NOT used by the access method. If, e.g.,
380 
381  "SELECT ... WHERE t1.colx = 4 and t1.coly @> 5"
382 
383  is resolved by ref access on t1.colx, filter_effect will be the
384  fraction of rows that will pass the "t1.coly @> 5" predicate. The
385  valid range is 0..1, where 0.0 means that no rows will pass the
386  table conditions and 1.0 means that all rows will pass.
387 
388  It is used to calculate how many row combinations will be joined
389  with the next table, @see prefix_rowcount below.
390 
391  @note With condition filtering enabled, it is possible to get
392  a fanout = rows_fetched * filter_effect that is less than 1.0.
393  Consider, e.g., a join between t1 and t2:
394 
395  "SELECT ... WHERE t1.col1=t2.colx and t2.coly OP @<something@>"
396 
397  where t1 is a prefix table and the optimizer currently calculates
398  the cost of adding t2 to the join. Assume that the chosen access
399  method on t2 is a 'ref' access on 'colx' that is estimated to
400  produce 2 rows per row from t1 (i.e., rows_fetched = 2). It will
401  in this case be perfectly fine to calculate a filtering effect
402  @<0.5 (resulting in "rows_fetched * filter_effect @< 1.0") from the
403  predicate "t2.coly OP @<something@>". If so, the number of row
404  combinations from (t1,t2) is lower than the prefix_rowcount of t1.
405 
406  The above is just an example of how the fanout of a table can
407  become less than one. It can happen for any access method.
408  */
410 
411  /**
412  prefix_rowcount and prefix_cost form a stack of partial join
413  order costs and output sizes
414 
415  prefix_rowcount: The number of row combinations that will be
416  joined to the next table in the join sequence.
417 
418  For a joined table it is calculated as
419  prefix_rowcount =
420  last_table.prefix_rowcount * rows_fetched * filter_effect
421 
422  @see filter_effect
423 
424  For a semijoined table it may be less than this formula due to
425  duplicate elimination.
426  */
428  double prefix_cost;
429 
431 
432  /**
433  NULL - 'index' or 'range' or 'index_merge' or 'ALL' access is used.
434  Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
435  */
437 
438  /** If ref-based access is used: bitmap of tables this table depends on */
441 
442  /**
443  Current optimization state: Semi-join strategy to be used for this
444  and preceding join tables.
445 
446  Join optimizer sets this for the *last* join_tab in the
447  duplicate-generating range. That is, in order to interpret this field,
448  one needs to traverse join->[best_]positions array from right to left.
449  When you see a join table with sj_strategy!= SJ_OPT_NONE, some other
450  field (depending on the strategy) tells how many preceding positions
451  this applies to. The values of covered_preceding_positions->sj_strategy
452  must be ignored.
453  */
455  /**
456  Valid only after fix_semijoin_strategies_for_picked_join_order() call:
457  if sj_strategy!=SJ_OPT_NONE, this is the number of subsequent tables that
458  are covered by the specified semi-join strategy
459  */
461 
462  /**
463  Bitmap of semi-join inner tables that are in the join prefix and for
464  which there's no provision yet for how to eliminate semi-join duplicates
465  which they produce.
466  */
468 
469  /* LooseScan strategy members */
470 
471  /* The first (i.e. driving) table we're doing loose scan for */
473  /*
474  Tables that need to be in the prefix before we can calculate the cost
475  of using LooseScan strategy.
476  */
478 
479  /*
480  keyno - Planning to do LooseScan on this key. If keyuse is NULL then
481  this is a full index scan, otherwise this is a ref+loosescan
482  scan (and keyno matches the KEUSE's)
483  MAX_KEY - Not doing a LooseScan
484  */
485  uint loosescan_key; // final (one for strategy instance )
486  uint loosescan_parts; /* Number of keyparts to be kept distinct */
487 
488  /* FirstMatch strategy */
489  /*
490  Index of the first inner table that we intend to handle with this
491  strategy
492  */
494  /*
495  Tables that were not in the join prefix when we've started considering
496  FirstMatch strategy.
497  */
499  /*
500  Tables that need to be in the prefix before we can calculate the cost
501  of using FirstMatch strategy.
502  */
504 
505  /* Duplicate Weedout strategy */
506  /* The first table that the strategy will need to handle */
508  /*
509  Tables that we will need to have in the prefix to do the weedout step
510  (all inner and all outer that the involved semi-joins are correlated with)
511  */
513 
514  /* SJ-Materialization-Scan strategy */
515  /* The last inner table (valid once we're after it) */
517  /*
518  Tables that we need to have in the prefix to calculate the correct cost.
519  Basically, we need all inner tables and outer tables mentioned in the
520  semi-join's ON expression so we can correctly account for fanout.
521  */
523 
524  /**
525  Even if the query has no semijoin, two sj-related members are read and
526  must thus have been set, by this function.
527  */
528  void no_semijoin() {
531  }
532  /**
533  Set complete estimated cost and produced rowcount for the prefix of tables
534  up to and including this table, in the join plan.
535 
536  @param cost Estimated cost
537  @param rowcount Estimated row count
538  */
539  void set_prefix_cost(double cost, double rowcount) {
540  prefix_cost = cost;
541  prefix_rowcount = rowcount;
542  }
543  /**
544  Set complete estimated cost and produced rowcount for the prefix of tables
545  up to and including this table, calculated from the cost of the previous
546  stage, the fanout of the current stage and the cost to process a row at
547  the current stage.
548 
549  @param idx Index of position object within array, if zero there is no
550  "previous" stage that can be added.
551  @param cm Cost model that provides the actual calculation
552  */
554  if (idx == 0) {
557  } else {
559  prefix_cost = (this - 1)->prefix_cost + read_cost +
561  }
563  }
564 };
565 
566 /**
567  Use this in a function which depends on best_ref listing tables in the
568  final join order. If 'tables==0', one is not expected to consult best_ref
569  cells, and best_ref may not even have been allocated.
570 */
571 #define ASSERT_BEST_REF_IN_JOIN_ORDER(join) \
572  do { \
573  DBUG_ASSERT(join->tables == 0 || (join->best_ref && !join->join_tab)); \
574  } while (0)
575 
576 /**
577  Query optimization plan node.
578 
579  Specifies:
580 
581  - a table access operation on the table specified by this node, and
582 
583  - a join between the result of the set of previous plan nodes and
584  this plan node.
585 */
586 class JOIN_TAB : public QEP_shared_owner {
587  public:
588  JOIN_TAB();
589 
590  void set_table(TABLE *t) {
591  if (t) t->reginfo.join_tab = this;
592  m_qs->set_table(t);
593  }
594 
595  /// Sets the pointer to the join condition of TABLE_LIST
598  }
599 
600  /// @returns join condition
601  Item *join_cond() const { return *m_join_cond_ref; }
602 
603  /**
604  Sets join condition
605  @note this also changes TABLE_LIST::m_join_cond.
606  */
607  void set_join_cond(Item *cond) { *m_join_cond_ref = cond; }
608 
609  /// Set the combined condition for a table (may be performed several times)
610  void set_condition(Item *to) {
611  if (condition() != to) {
612  m_qs->set_condition(to);
613  // Condition changes, so some indexes may become usable or not:
615  }
616  }
617 
620  Key_use *keyuse() const { return m_keyuse; }
621  void set_keyuse(Key_use *k) { m_keyuse = k; }
622 
623  TABLE_LIST *table_ref; /**< points to table reference */
624 
625  private:
626  Key_use *m_keyuse; /**< pointer to first used key */
627 
628  /**
629  Pointer to the associated join condition:
630 
631  - if this is a table with position==NULL (e.g. internal sort/group
632  temporary table), pointer is NULL
633 
634  - otherwise, pointer is the address of some TABLE_LIST::m_join_cond.
635  Thus, the pointee is the same as TABLE_LIST::m_join_cond (changing one
636  changes the other; thus, optimizations made on the second are reflected
637  in SELECT_LEX::print_table_array() which uses the first one).
638  */
640 
641  public:
642  COND_EQUAL *cond_equal; /**< multiple equalities for the on expression*/
643 
644  /**
645  The maximum value for the cost of seek operations for key lookup
646  during ref access. The cost model for ref access assumes every key
647  lookup will cause reading a block from disk. With many key lookups
648  into the same table, most of the blocks will soon be in a memory
649  buffer. As a consequence, there will in most cases be an upper
650  limit on the number of actual disk accesses the ref access will
651  cause. This variable is used for storing a maximum cost estimate
652  for the disk accesses for ref access. It is used for limiting the
653  cost estimate for ref access to a more realistic value than
654  assuming every key lookup causes a random disk access. Without
655  having this upper limit for the cost of ref access, table scan
656  would be more likely to be chosen for cases where ref access
657  performs better.
658  */
659  double worst_seeks;
660  /** Keys with constant part. Subset of keys. */
662  Key_map checked_keys; /**< Keys checked */
663  /**
664  Keys which can be used for skip scan access. We store it
665  separately from tab->const_keys & join_tab->keys() to avoid
666  unnecessary printing of the prossible keys in EXPLAIN output
667  as some of these keys can be marked not usable for skip scan later.
668  More strict check for prossible keys is performed in
669  get_best_skip_scan() function.
670  */
673 
674  /**
675  Used to avoid repeated range analysis for the same key in
676  test_if_skip_sort_order(). This would otherwise happen if the best
677  range access plan found for a key is turned down.
678  quick_order_tested is cleared every time the select condition for
679  this JOIN_TAB changes since a new condition may give another plan
680  and cost from range analysis.
681  */
683 
684  /*
685  Number of records that will be scanned (yes scanned, not returned) by the
686  best 'independent' access method, i.e. table scan or QUICK_*_SELECT)
687  */
689  /*
690  Cost of accessing the table using "ALL" or range/index_merge access
691  method (but not 'index' for some reason), i.e. this matches method which
692  E(#records) is in found_records.
693  */
694  double read_time;
695  /**
696  The set of tables that this table depends on. Used for outer join and
697  straight join dependencies.
698  */
700  /**
701  The set of tables that are referenced by key from this table.
702  */
704 
705  public:
708 
709  /**
710  Join buffering strategy.
711  After optimization it contains chosen join buffering strategy (if any).
712  */
714 
715  /* SemiJoinDuplicateElimination variables: */
716  /*
717  Embedding SJ-nest (may be not the direct parent), or NULL if none.
718  It is the closest semijoin or antijoin nest.
719  This variable holds the result of table pullout.
720  */
722 
723  /* NestedOuterJoins: Bitmap of nested joins this table is part of */
725 
726  /** Flags from SE's MRR implementation, to be used by JOIN_CACHE */
728 
729  /** true <=> AM will scan backward */
731 
732  /** Clean up associated table after query execution, including resources */
733  void cleanup();
734 
735  /// @returns semijoin strategy for this table.
736  uint get_sj_strategy() const;
737 
738  private:
739  JOIN_TAB(const JOIN_TAB &); // not defined
740  JOIN_TAB &operator=(const JOIN_TAB &); // not defined
741 };
742 
744  : QEP_shared_owner(),
745  table_ref(NULL),
746  m_keyuse(NULL),
747  m_join_cond_ref(NULL),
748  cond_equal(NULL),
749  worst_seeks(0.0),
750  const_keys(),
751  checked_keys(),
752  skip_scan_keys(),
753  needed_reg(),
754  quick_order_tested(),
755  found_records(0),
756  read_time(0),
757  dependent(0),
758  key_dependent(0),
759  used_fieldlength(0),
760  use_quick(QS_NONE),
761  m_use_join_cache(0),
762  emb_sj_nest(NULL),
763  embedding_map(0),
764  join_cache_flags(0),
765  reversed_access(false) {}
766 
767 /**
768  "Less than" comparison function object used to compare two JOIN_TAB
769  objects based on a number of factors in this order:
770 
771  - table before another table that depends on it (straight join,
772  outer join etc), then
773  - table before another table that depends on it to use a key
774  as access method, then
775  - table with smallest number of records first, then
776  - the table with lowest-value pointer (i.e., the one located
777  in the lowest memory address) first.
778 
779  @param jt1 first JOIN_TAB object
780  @param jt2 second JOIN_TAB object
781 
782  @note The order relation implemented by Join_tab_compare_default is not
783  transitive, i.e. it is possible to choose a, b and c such that
784  (a @< b) && (b @< c) but (c @< a). This is the case in the
785  following example:
786 
787  a: dependent = @<none@> found_records = 3
788  b: dependent = @<none@> found_records = 4
789  c: dependent = b found_records = 2
790 
791  a @< b: because a has fewer records
792  b @< c: because c depends on b (e.g outer join dependency)
793  c @< a: because c has fewer records
794 
795  This implies that the result of a sort using the relation
796  implemented by Join_tab_compare_default () depends on the order in
797  which elements are compared, i.e. the result is
798  implementation-specific.
799 
800  @return
801  true if jt1 is smaller than jt2, false otherwise
802 */
804  public:
805  bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2) {
806  // Sorting distinct tables, so a table should not be compared with itself
807  DBUG_ASSERT(jt1 != jt2);
808 
809  if (jt1->dependent & jt2->table_ref->map()) return false;
810  if (jt2->dependent & jt1->table_ref->map()) return true;
811 
812  const bool jt1_keydep_jt2 = jt1->key_dependent & jt2->table_ref->map();
813  const bool jt2_keydep_jt1 = jt2->key_dependent & jt1->table_ref->map();
814 
815  if (jt1_keydep_jt2 && !jt2_keydep_jt1) return false;
816  if (jt2_keydep_jt1 && !jt1_keydep_jt2) return true;
817 
818  if (jt1->found_records > jt2->found_records) return false;
819  if (jt1->found_records < jt2->found_records) return true;
820 
821  return jt1 < jt2;
822  }
823 };
824 
825 /**
826  "Less than" comparison function object used to compare two JOIN_TAB
827  objects that are joined using STRAIGHT JOIN. For STRAIGHT JOINs,
828  the join order is dictated by the relative order of the tables in the
829  query which is reflected in JOIN_TAB::dependent. Table size and key
830  dependencies are ignored here.
831 */
833  public:
834  bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2) {
835  // Sorting distinct tables, so a table should not be compared with itself
836  DBUG_ASSERT(jt1 != jt2);
837 
838  /*
839  We don't do subquery flattening if the parent or child select has
840  STRAIGHT_JOIN modifier. It is complicated to implement and the semantics
841  is hardly useful.
842  */
843  DBUG_ASSERT(!jt1->emb_sj_nest);
844  DBUG_ASSERT(!jt2->emb_sj_nest);
845 
846  if (jt1->dependent & jt2->table_ref->map()) return false;
847  if (jt2->dependent & jt1->table_ref->map()) return true;
848 
849  return jt1 < jt2;
850  }
851 };
852 
853 /*
854  Same as Join_tab_compare_default but tables from within the given
855  semi-join nest go first. Used when optimizing semi-join
856  materialization nests.
857 */
859  private:
861 
862  public:
864 
865  bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2) {
866  // Sorting distinct tables, so a table should not be compared with itself
867  DBUG_ASSERT(jt1 != jt2);
868 
869  if (jt1->emb_sj_nest == emb_nest && jt2->emb_sj_nest != emb_nest)
870  return true;
871  if (jt1->emb_sj_nest != emb_nest && jt2->emb_sj_nest == emb_nest)
872  return false;
873 
875  return cmp(jt1, jt2);
876  }
877 };
878 
879 /* Extern functions in sql_select.cc */
880 void count_field_types(SELECT_LEX *select_lex, Temp_table_param *param,
881  List<Item> &fields, bool reset_with_sum_func,
882  bool save_sum_fields);
883 uint find_shortest_key(TABLE *table, const Key_map *usable_keys);
884 
885 /* functions from opt_sum.cc */
886 bool simple_pred(Item_func *func_item, Item **args, bool *inv_order);
887 
889  AGGR_COMPLETE, // All aggregates were evaluated
890  AGGR_REGULAR, // Aggregates not fully evaluated, regular execution required
891  AGGR_DELAYED, // Aggregates not fully evaluated, execute with ha_records()
892  AGGR_EMPTY // Source tables empty, aggregates are NULL or 0 (for COUNT)
893 };
894 
895 bool optimize_aggregated_query(THD *thd, SELECT_LEX *select,
896  List<Item> &all_fields, Item *conds,
897  aggregate_evaluated *decision);
898 
899 /* from sql_delete.cc, used by opt_range.cc */
900 extern "C" int refpos_order_cmp(const void *arg, const void *a, const void *b);
901 
902 /** class to copying an field/item to a key struct */
903 
904 class store_key {
905  public:
906  bool null_key; /* true <=> the value of the key has a null part */
908  store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
909  : null_key(0), null_ptr(null), err(0) {
910  if (field_arg->type() == MYSQL_TYPE_BLOB ||
911  field_arg->type() == MYSQL_TYPE_GEOMETRY) {
912  /*
913  Key segments are always packed with a 2 byte length prefix.
914  See mi_rkey for details.
915  */
916  to_field = new (thd->mem_root) Field_varstring(
917  ptr, length, 2, null, 1, Field::NONE, field_arg->field_name,
918  field_arg->table->s, field_arg->charset());
919  to_field->init(field_arg->table);
920  } else
921  to_field = field_arg->new_key_field(thd->mem_root, field_arg->table, ptr,
922  null, 1);
923  }
924  virtual ~store_key() {} /** Not actually needed */
925  virtual const char *name() const = 0;
926 
927  /**
928  @brief sets ignore truncation warnings mode and calls the real copy method
929 
930  @details this function makes sure truncation warnings when preparing the
931  key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
932  */
935  THD *thd = to_field->table->in_use;
936  enum_check_fields saved_check_for_truncated_fields =
940 
942 
943  result = copy_inner();
944 
945  thd->check_for_truncated_fields = saved_check_for_truncated_fields;
946  thd->variables.sql_mode = sql_mode;
947 
948  return result;
949  }
950 
951  protected:
952  Field *to_field; // Store data here
955 
956  virtual enum store_key_result copy_inner() = 0;
957 };
958 
959 class store_key_field : public store_key {
961  const char *field_name;
962 
963  public:
964  store_key_field(THD *thd, Field *to_field_arg, uchar *ptr,
965  uchar *null_ptr_arg, uint length, Field *from_field,
966  const char *name_arg)
967  : store_key(thd, to_field_arg, ptr,
968  null_ptr_arg ? null_ptr_arg
969  : from_field->maybe_null() ? &err : (uchar *)0,
970  length),
971  field_name(name_arg) {
972  if (to_field) {
973  copy_field.set(to_field, from_field, 0);
974  }
975  }
976  const char *name() const { return field_name; }
977 
978  protected:
980  TABLE *table = copy_field.to_field()->table;
981  my_bitmap_map *old_map = dbug_tmp_use_all_columns(table, table->write_set);
983  dbug_tmp_restore_column_map(table->write_set, old_map);
985  return err != 0 ? STORE_KEY_FATAL : STORE_KEY_OK;
986  }
987 };
988 
989 class store_key_item : public store_key {
990  protected:
992 
993  public:
994  store_key_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg,
995  uint length, Item *item_arg)
996  : store_key(thd, to_field_arg, ptr,
997  null_ptr_arg ? null_ptr_arg
998  : item_arg->maybe_null ? &err : (uchar *)0,
999  length),
1000  item(item_arg) {}
1001  const char *name() const override { return "func"; }
1002 
1003  protected:
1004  enum store_key_result copy_inner() override;
1005 };
1006 
1007 /*
1008  Class used for unique constraint implementation by subselect_hash_sj_engine.
1009  It uses store_key_item implementation to do actual copying, but after
1010  that, copy_inner calculates hash of each key part for unique constraint.
1011 */
1012 
1014  protected:
1016 
1017  public:
1018  store_key_hash_item(THD *thd, Field *to_field_arg, uchar *ptr,
1019  uchar *null_ptr_arg, uint length, Item *item_arg,
1020  ulonglong *hash_arg)
1021  : store_key_item(thd, to_field_arg, ptr, null_ptr_arg, length, item_arg),
1022  hash(hash_arg) {}
1023  const char *name() const { return "func"; }
1024 
1025  protected:
1027 };
1028 
1029 /*
1030  Class used for indexes over JSON expressions. The value to lookup is
1031  obtained from val_json() method and then converted according to field's
1032  result type and saved. This allows proper handling of temporal values.
1033 */
1035  /// Whether the key is constant.
1036  const bool m_const_key{false};
1037  /// Whether the key was already copied.
1038  bool m_inited{false};
1039 
1040  public:
1041  store_key_json_item(THD *thd, Field *to_field_arg, uchar *ptr,
1042  uchar *null_ptr_arg, uint length, Item *item_arg,
1043  bool const_key_arg)
1044  : store_key_item(thd, to_field_arg, ptr, null_ptr_arg, length, item_arg),
1045  m_const_key(const_key_arg) {}
1046 
1047  const char *name() const override { return m_const_key ? "const" : "func"; }
1048 
1049  protected:
1050  enum store_key_result copy_inner() override;
1051 };
1052 
1054  bool inited;
1055 
1056  public:
1057  store_key_const_item(THD *thd, Field *to_field_arg, uchar *ptr,
1058  uchar *null_ptr_arg, uint length, Item *item_arg)
1059  : store_key_item(thd, to_field_arg, ptr, null_ptr_arg, length, item_arg),
1060  inited(0) {}
1061  static const char static_name[]; ///< used out of this class
1062  const char *name() const { return static_name; }
1063 
1064  protected:
1066  if (!inited) {
1067  inited = 1;
1069  if (res && !err) err = res;
1070  }
1071  return (err > 2 ? STORE_KEY_FATAL : (store_key_result)err);
1072  }
1073 };
1074 
1076 bool handle_query(THD *thd, LEX *lex, Query_result *result,
1077  ulonglong added_options, ulonglong removed_options);
1078 
1079 // Statement timeout function(s)
1080 bool set_statement_timer(THD *thd);
1081 void reset_statement_timer(THD *thd);
1082 
1083 void free_underlaid_joins(THD *thd, SELECT_LEX *select);
1084 
1085 void calc_used_field_length(TABLE *table, bool needs_rowid, uint *p_used_fields,
1086  uint *p_used_fieldlength, uint *p_used_blobs,
1087  bool *p_used_null_fields,
1088  bool *p_used_uneven_bit_fields);
1089 
1091 bool const_expression_in_where(Item *cond, Item *comp_item,
1092  Field *comp_field = NULL,
1093  Item **const_item = NULL);
1094 bool test_if_subpart(ORDER *a, ORDER *b);
1095 void calc_group_buffer(JOIN *join, ORDER *group);
1096 bool make_join_readinfo(JOIN *join, uint no_jbuf_after);
1097 bool create_ref_for_key(JOIN *join, JOIN_TAB *j, Key_use *org_keyuse,
1098  table_map used_tables);
1099 bool types_allow_materialization(Item *outer, Item *inner);
1100 bool and_conditions(Item **e1, Item *e2);
1101 
1102 /**
1103  Create a AND item of two existing items.
1104  A new Item_cond_and item is created with the two supplied items as
1105  arguments.
1106 
1107  @note About handling of null pointers as arguments: if the first
1108  argument is a null pointer, then the item given as second argument is
1109  returned (no new Item_cond_and item is created). The second argument
1110  must not be a null pointer.
1111 
1112  @param cond the first argument to the new AND condition
1113  @param item the second argument to the new AND condtion
1114 
1115  @return the new AND item
1116 */
1117 static inline Item *and_items(Item *cond, Item *item) {
1118  DBUG_ASSERT(item != NULL);
1119  return (cond ? (new Item_cond_and(cond, item)) : item);
1120 }
1121 
1122 /// A variant of the above, guaranteed to return Item_bool_func.
1123 static inline Item_bool_func *and_items(Item *cond, Item_bool_func *item) {
1124  DBUG_ASSERT(item != NULL);
1125  return (cond ? (new Item_cond_and(cond, item)) : item);
1126 }
1127 
1128 uint actual_key_parts(const KEY *key_info);
1129 
1130 class ORDER_with_src;
1131 
1133  bool *need_sort, bool *reverse);
1135  uint *used_key_parts, bool *skip_quick);
1137  TABLE *table, Key_map usable_keys, int key,
1138  ha_rows select_limit, int *new_key,
1139  int *new_key_direction, ha_rows *new_select_limit,
1140  uint *new_used_key_parts = NULL,
1141  uint *saved_best_key_parts = NULL);
1142 /**
1143  Calculate properties of ref key: key length, number of used key parts,
1144  dependency map, possibility of null.
1145 
1146  @param keyuse Array of keys to consider
1147  @param tab join_tab to calculate ref parameters for
1148  @param key number of the key to use
1149  @param used_tables tables read prior to this table
1150  @param [out] chosen_keyuses when given, this function will fill array with
1151  chosen keyuses
1152  @param [out] length_out calculated length of the ref
1153  @param [out] keyparts_out calculated number of used keyparts
1154  @param [out] dep_map when given, calculated dependency map
1155  @param [out] maybe_null when given, calculated maybe_null property
1156 */
1157 
1158 void calc_length_and_keyparts(Key_use *keyuse, JOIN_TAB *tab, const uint key,
1159  table_map used_tables, Key_use **chosen_keyuses,
1160  uint *length_out, uint *keyparts_out,
1161  table_map *dep_map, bool *maybe_null);
1162 
1163 /**
1164  Set up the support structures (NULL bits, row offsets, etc.) for a semijoin
1165  duplicate weedout table. The object is allocated on the given THD's MEM_ROOT.
1166 
1167  @param thd the THD to allocate the object on
1168  @param join the JOIN that will own the temporary table (ie., has the
1169  responsibility to destroy it after use)
1170  @param first_tab first table in row key (inclusive)
1171  @param last_tab last table in row key (exclusive)
1172  */
1174  SJ_TMP_TABLE::TAB *first_tab,
1175  SJ_TMP_TABLE::TAB *last_tab);
1176 
1177 #endif /* SQL_SELECT_INCLUDED */
static my_bitmap_map * dbug_tmp_use_all_columns(TABLE *table, MY_BITMAP *bitmap)
Definition: table.h:3667
Join_tab_compare_embedded_first(const TABLE_LIST *nest)
Definition: sql_select.h:863
const bool m_const_key
Whether the key is constant.
Definition: sql_select.h:1036
store_key_json_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Item *item_arg, bool const_key_arg)
Definition: sql_select.h:1041
bool * cond_guard
!NULL - This Key_use was created from an equality that was wrapped into an Item_func_trig_cond.
Definition: sql_select.h:249
static int cmp(Bigint *a, Bigint *b)
Definition: dtoa.cc:1074
Definition: result.h:33
Field * to_field
Definition: sql_select.h:952
unsigned long long int ulonglong
Definition: my_inttypes.h:55
Definition: sql_select.h:316
Item ** m_join_cond_ref
Pointer to the associated join condition:
Definition: sql_select.h:639
table_map dependent
The set of tables that this table depends on.
Definition: sql_select.h:699
void set(Field *to, Field *from, bool save)
Definition: field_conv.cc:617
unsigned char uchar
Definition: my_inttypes.h:51
Definition: sql_optimizer.h:177
virtual enum store_key_result copy_inner()=0
void set_table(TABLE *t)
Definition: sql_select.h:590
bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2)
Definition: sql_select.h:805
enum store_key_result copy()
sets ignore truncation warnings mode and calls the real copy method
Definition: sql_select.h:933
Sql_cmd_select(Query_result *result_arg)
Definition: sql_select.h:69
bool and_conditions(Item **e1, Item *e2)
Extend e1 by AND&#39;ing e2 to the condition e1 points to.
Definition: sql_select.cc:2253
bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2)
Definition: sql_select.h:865
void set_keyuse(Key_use *k)
Definition: sql_select.h:621
bool is_null(ptrdiff_t row_offset=0) const
Check whether the full table&#39;s row is NULL or the Field has value NULL.
Definition: field.h:1293
double row_evaluate_cost(double rows) const
Cost of processing a number of records and evaluating the query condition on the records.
Definition: opt_costmodel.h:83
ulonglong table_map
Definition: my_table_map.h:32
RangeReverse< Range > reverse(Range &x)
Iterate over a range in reverse.
Definition: utilities.h:135
TABLE * table
Definition: field.h:796
Definition: sql_lex.h:3192
void init_join_cond_ref(TABLE_LIST *tl)
Sets the pointer to the join condition of TABLE_LIST.
Definition: sql_select.h:596
bool accept(Select_lex_visitor *visitor)
Definition: sql_lex.cc:3335
uint first_dupsweedout_table
Definition: sql_select.h:507
Order clause list element.
Definition: table.h:276
uint actual_key_parts(const KEY *key_info)
Returns number of key parts depending on OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS flag.
Definition: sql_select.cc:5243
bool null_rejecting
If true, the comparison this value was created from will not be satisfied if val has NULL &#39;value&#39;...
Definition: sql_select.h:235
uint sj_pred_no
0..63 <=> This was created from semi-join IN-equality # sj_pred_no.
Definition: sql_select.h:259
Definition: sql_select.h:907
struct result result
Definition: result.h:37
virtual ~store_key()
Definition: sql_select.h:924
"Less than" comparison function object used to compare two JOIN_TAB objects that are joined using STR...
Definition: sql_select.h:832
double fanout
Fanout of the ref access path for this index, in the current join prefix.
Definition: sql_select.h:279
Constitutes a mapping from columns of tables in the from clause to aggregated columns.
Definition: field.h:4945
File containing constants that can be used throughout the server.
ulong key_part_map
Definition: my_base.h:998
uchar * null_ptr
Definition: sql_select.h:953
TABLE_LIST * table_ref
points to table reference
Definition: sql_select.h:623
Some integer typedefs for easier portability.
bool simple_pred(Item_func *func_item, Item **args, bool *inv_order)
Test if the predicate compares a field with constants.
Definition: opt_sum.cc:629
bool types_allow_materialization(Item *outer, Item *inner)
Check if two items are compatible wrt.
Definition: sql_select.cc:1004
Definition: sql_bitmap.h:135
ha_rows found_records
Definition: sql_select.h:688
Definition: sql_select.h:1053
Owner of a QEP_shared; parent of JOIN_TAB and QEP_TAB.
Definition: sql_opt_exec_shared.h:463
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:971
bool const_expression_in_where(Item *cond, Item *comp_item, Field *comp_field=NULL, Item **const_item=NULL)
Test if a field or an item is equal to a constant value in WHERE.
Definition: sql_select.cc:3406
Definition: field.h:700
enum store_key_result copy_inner()
Definition: sql_select.h:1065
Definition: sql_select.h:1034
JOIN_TAB * table
Definition: sql_select.h:430
Definition: item_func.h:82
Abstract base class for traversing the SELECT_LEX tree.
Definition: select_lex_visitor.h:39
void set_prefix_cost(double cost, double rowcount)
Set complete estimated cost and produced rowcount for the prefix of tables up to and including this t...
Definition: sql_select.h:539
struct System_variables variables
Definition: sql_class.h:957
Key_use * m_keyuse
pointer to first used key
Definition: sql_select.h:626
void set_prefix_join_cost(uint idx, const Cost_model_server *cm)
Set complete estimated cost and produced rowcount for the prefix of tables up to and including this t...
Definition: sql_select.h:553
enum_sql_command
Definition: my_sqlcommand.h:45
Definition: mysql_lex_string.h:39
table_map key_dependent
The set of tables that are referenced by key from this table.
Definition: sql_select.h:703
uint get_sj_strategy() const
Definition: sql_optimizer.cc:1129
uint optimize
0, or KEY_OPTIMIZE_*
Definition: sql_select.h:226
Definition: query_result.h:48
Item * condition() const
Definition: sql_opt_exec_shared.h:502
Key_map skip_scan_keys
Keys which can be used for skip scan access.
Definition: sql_select.h:671
std::string join(Container cont, const std::string &delim)
join elements of an container into a string seperated by a delimiter.
Definition: string.h:144
table_map first_firstmatch_rtbl
Definition: sql_select.h:498
double prefix_cost
Definition: sql_select.h:428
class JOIN_TAB * join_tab
Definition: table.h:1593
join_type calc_join_type(int quick_type)
Definition: sql_select.cc:5266
table_map firstmatch_need_tables
Definition: sql_select.h:503
#define SJ_OPT_MATERIALIZE_LOOKUP
Definition: sql_select.h:306
table_map dups_producing_tables
Bitmap of semi-join inner tables that are in the join prefix and for which there&#39;s no provision yet f...
Definition: sql_select.h:467
Definition: field.h:3714
Item ** join_cond_optim_ref()
Definition: table.h:2631
A Key_use represents an equality predicate of the form (table.column = val), where the column is inde...
Definition: sql_select.h:170
virtual bool prepare_inner(THD *thd) override
Prepare a SELECT statement.
Definition: sql_select.cc:534
void clear_all()
Definition: sql_bitmap.h:161
Definition: sql_select.h:67
THD * in_use
Definition: table.h:1329
ulonglong * hash
Definition: sql_select.h:1015
Definition: field_types.h:85
JOIN_TAB & operator=(const JOIN_TAB &)
Field * to_field()
Definition: field.h:5031
const char * field_name
Definition: sql_select.h:961
Key_map needed_reg
Definition: sql_select.h:672
ORDER * simple_remove_const(ORDER *order, Item *where)
Filter out ORDER items those are equal to constants in WHERE.
Definition: sql_select.cc:3325
void calc_group_buffer(JOIN *join, ORDER *group)
calc how big buffer we need for comparing group entries.
Definition: sql_select.cc:3579
Common types of the Optimizer, used by optimization and execution.
Definition: table.h:1301
uint loosescan_parts
Definition: sql_select.h:486
store_key_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Item *item_arg)
Definition: sql_select.h:994
const char * name() const override
Not actually needed.
Definition: sql_select.h:1047
void invoke_do_copy(Copy_field *f)
Definition: field_conv.cc:568
store_key_const_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Item *item_arg)
Definition: sql_select.h:1057
static const char static_name[]
used out of this class
Definition: sql_select.h:1061
void calc_length_and_keyparts(Key_use *keyuse, JOIN_TAB *tab, const uint key, table_map used_tables, Key_use **chosen_keyuses, uint *length_out, uint *keyparts_out, table_map *dep_map, bool *maybe_null)
Calculate properties of ref key: key length, number of used key parts, dependency map...
Definition: sql_select.cc:1891
store_key_hash_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Item *item_arg, ulonglong *hash_arg)
Definition: sql_select.h:1018
class to copying an field/item to a key struct
Definition: sql_select.h:904
bool optimize_aggregated_query(THD *thd, SELECT_LEX *select, List< Item > &all_fields, Item *conds, aggregate_evaluated *decision)
Substitute constants for some COUNT(), MIN() and MAX() functions in an aggregated (implicitly grouped...
Definition: opt_sum.cc:275
Definition: sql_select.h:316
This file includes constants used by all storage engines.
Definition: sql_select.h:889
Definition: item_cmpfunc.h:279
const TABLE_LIST * emb_nest
Definition: sql_select.h:860
uchar err
Definition: sql_select.h:954
#define DBUG_ASSERT(A)
Definition: my_dbug.h:197
table_map used_tables
All tables used in val, that is all tables that provide bindings for the expression val...
Definition: sql_select.h:223
void set_join_cond(Item *cond)
Sets join condition.
Definition: sql_select.h:607
void cleanup()
Clean up associated table after query execution, including resources.
Definition: sql_select.cc:3090
uint sj_strategy
Current optimization state: Semi-join strategy to be used for this and preceding join tables...
Definition: sql_select.h:454
Definition: sql_select.h:316
TABLE_LIST * emb_sj_nest
Definition: sql_select.h:721
void set_condition(Item *c)
Definition: sql_opt_exec_shared.h:284
Definition: sql_select.h:892
static Item * and_items(Item *cond, Item *item)
Create a AND item of two existing items.
Definition: sql_select.h:1117
sql_mode_t sql_mode
which non-standard SQL behaviour should be enabled
Definition: system_variables.h:202
virtual bool precheck(THD *thd) override
Perform an authorization precheck for a SELECT statement.
Definition: sql_select.cc:947
int refpos_order_cmp(const void *arg, const void *a, const void *b)
Definition: sql_delete.cc:823
void no_semijoin()
Even if the query has no semijoin, two sj-related members are read and must thus have been set...
Definition: sql_select.h:528
int test_if_order_by_key(ORDER_with_src *order, TABLE *table, uint idx, uint *used_key_parts, bool *skip_quick)
Test if one can use the key to resolve ordering.
Definition: sql_optimizer.cc:1504
Definition: aggregate_check.h:523
virtual Field * new_key_field(MEM_ROOT *root, TABLE *new_table, uchar *new_ptr, uchar *new_null_ptr, uint new_null_bit) const
Definition: field.cc:2183
float filter_effect
The fraction of the &#39;rows_fetched&#39; rows that will pass the table conditions that were NOT used by the...
Definition: sql_select.h:409
TABLE_SHARE * s
Definition: table.h:1302
bool error_if_full_join(JOIN *join)
Give error if we some tables are done with a full join.
Definition: sql_select.cc:3061
Definition: sql_select.h:989
virtual const CHARSET_INFO * charset() const
Definition: field.h:1623
Classes for query execution.
Key_map const_keys
Keys with constant part.
Definition: sql_select.h:661
key_part_map keypart_map
like keypart, but as a bitmap
Definition: sql_select.h:227
Definition: sql_select.h:907
void reset_statement_timer(THD *thd)
Deactivate the timer associated with the statement that was executed.
Definition: sql_select.cc:310
bool make_join_readinfo(JOIN *join, uint no_jbuf_after)
Plan refinement stage: do various setup things for the executor.
Definition: sql_select.cc:2848
MY_BITMAP * write_set
Definition: table.h:1424
Definition: item.h:668
unsigned int uint
Definition: uca-dump.cc:29
#define final(a, b, c)
Definition: hash.c:109
ha_rows ref_table_rows
Estimate of how many rows for a key value.
Definition: sql_select.h:228
uint join_cache_flags
Flags from SE&#39;s MRR implementation, to be used by JOIN_CACHE.
Definition: sql_select.h:727
uint keypart
used part of the index
Definition: sql_select.h:225
ORDER * order
ORDER expression that we are wrapping with this class.
Definition: sql_optimizer.h:117
bool sj_is_materialize_strategy(uint strategy)
Definition: sql_select.h:309
const char * name() const override
Not actually needed.
Definition: sql_select.h:1001
bool handle_query(THD *thd, LEX *lex, Query_result *result, ulonglong added_options, ulonglong removed_options)
Handle data manipulation query which is not represented by Sql_cmd_dml class.
Definition: sql_select.cc:151
Definition: sql_executor.h:115
Definition: sql_select.h:891
ulonglong nested_join_map
Definition: nested_join.h:36
uint find_shortest_key(TABLE *table, const Key_map *usable_keys)
Find shortest key suitable for full table scan.
Definition: sql_optimizer.cc:1653
store_key_field(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Field *from_field, const char *name_arg)
Definition: sql_select.h:964
bool operator()(const JOIN_TAB *jt1, const JOIN_TAB *jt2)
Definition: sql_select.h:834
enum_check_fields
Definition: field.h:174
const char * field_name
Definition: field.h:798
Item * item
Definition: sql_select.h:991
static char * where
Definition: mysqldump.cc:127
store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
Definition: sql_select.h:908
table_map sjm_scan_need_tables
Definition: sql_select.h:522
Definition: sql_executor.h:406
void set_use_join_cache(uint u)
Definition: sql_select.h:619
static const char * key
Definition: suite_stubs.c:14
JOIN_TAB()
Definition: sql_select.h:743
double rows_fetched
The number of rows that will be fetched by the chosen access method per each row combination of previ...
Definition: sql_select.h:364
LEX * lex
Definition: sql_class.h:838
bool null_key
Definition: sql_select.h:906
bool inited
Definition: sql_select.h:1054
MEM_ROOT * mem_root
Definition: sql_class.h:238
Copy_field copy_field
Definition: sql_select.h:960
Definition: sql_select.h:907
Definition: sql_select.h:1013
nested_join_map embedding_map
Definition: sql_select.h:724
Definition: sql_select.h:890
uint n_sj_tables
Valid only after fix_semijoin_strategies_for_picked_join_order() call: if sj_strategy!=SJ_OPT_NONE, this is the number of subsequent tables that are covered by the specified semi-join strategy.
Definition: sql_select.h:460
Definition: field.h:175
store_key_result
Definition: sql_select.h:907
table_map dupsweedout_tables
Definition: sql_select.h:512
A position of table within a join order.
Definition: sql_select.h:345
bool reversed_access
true <=> AM will scan backward
Definition: sql_select.h:730
Query optimization plan node.
Definition: sql_select.h:586
#define SJ_OPT_NONE
Definition: sql_select.h:302
#define MODE_NO_ZERO_IN_DATE
Definition: system_variables.h:120
virtual void init(TABLE *table_arg)
Definition: field.cc:10231
table_map ref_depend_map
If ref-based access is used: bitmap of tables this table depends on.
Definition: sql_select.h:439
quick_type
Bits describing quick select type.
Definition: sql_select.h:316
Item * join_cond() const
Definition: sql_select.h:601
const char * name() const
Not actually needed.
Definition: sql_select.h:976
Wrapper for ORDER* pointer to trace origins of ORDER list.
Definition: sql_optimizer.h:102
"Less than" comparison function object used to compare two JOIN_TAB objects based on a number of fact...
Definition: sql_select.h:803
Definition: sql_executor.h:108
virtual const char * name() const =0
Not actually needed.
enum store_key_result copy_inner() override
Definition: sql_select.cc:2222
enum store_key_result copy_inner() override
Definition: sql_select.cc:2163
QEP_shared * m_qs
Definition: sql_opt_exec_shared.h:550
void set_condition(Item *to)
Set the combined condition for a table (may be performed several times)
Definition: sql_select.h:610
Definition: key.h:111
double prefix_rowcount
prefix_rowcount and prefix_cost form a stack of partial join order costs and output sizes ...
Definition: sql_select.h:427
Definition: sql_select.h:959
Definition: item_cmpfunc.h:2440
TABLE_LIST * table_ref
table owning the index
Definition: sql_select.h:208
void count_field_types(SELECT_LEX *select_lex, Temp_table_param *param, List< Item > &fields, bool reset_with_sum_func, bool save_sum_fields)
Update TMP_TABLE_PARAM with count of the different type of fields.
Definition: sql_select.cc:3471
uint key
number of index
Definition: sql_select.h:224
uint sjm_scan_last_inner
Definition: sql_select.h:516
enum store_key_result copy_inner()
Definition: sql_select.cc:2157
Key_map checked_keys
Keys checked.
Definition: sql_select.h:662
enum store_key_result copy_inner()
Definition: sql_select.h:979
void calc_used_field_length(TABLE *table, bool needs_rowid, uint *p_used_fields, uint *p_used_fieldlength, uint *p_used_blobs, bool *p_used_null_fields, bool *p_used_uneven_bit_fields)
Find how much space the prevous read not const tables takes in cache.
Definition: sql_select.cc:1783
ulonglong sql_mode_t
Definition: dd_event.h:36
Key_use * key
NULL - &#39;index&#39; or &#39;range&#39; or &#39;index_merge&#39; or &#39;ALL&#39; access is used.
Definition: sql_select.h:436
uint32 my_bitmap_map
Definition: my_bitmap.h:40
Definition: field.h:823
bool set_statement_timer(THD *thd)
Set the time until the currently running statement is aborted.
Definition: sql_select.cc:283
#define NULL
Definition: types.h:55
Definition: sql_cmd_dml.h:32
table_map loosescan_need_tables
Definition: sql_select.h:477
const char * name() const
Not actually needed.
Definition: sql_select.h:1023
double read_time
Definition: sql_select.h:694
aggregate_evaluated
Definition: sql_select.h:888
uint get_index_for_order(ORDER_with_src *order, QEP_TAB *tab, ha_rows limit, bool *need_sort, bool *reverse)
Find a key to apply single table UPDATE/DELETE by a given ORDER.
Definition: sql_select.cc:5148
static ulong select_limit
Definition: mysql.cc:194
Definition: my_sqlcommand.h:46
Definition: field_types.h:88
uint use_join_cache() const
Definition: sql_select.h:618
uint first_loosescan_table
Definition: sql_select.h:472
table_map map() const
Return table map derived from table number.
Definition: table.h:3479
bool create_ref_for_key(JOIN *join, JOIN_TAB *j, Key_use *org_keyuse, table_map used_tables)
Setup a ref access for looking up rows via an index (a key).
Definition: sql_select.cc:1955
const MYSQL_LEX_CSTRING * eligible_secondary_storage_engine() const override
Is this statement of a type and on a form that makes it eligible for execution in a secondary storage...
Definition: sql_select.cc:484
void free_underlaid_joins(THD *thd, SELECT_LEX *select)
Free joins of subselect of this select.
Definition: sql_select.cc:3746
Item * val
Value used for lookup into key.
Definition: sql_select.h:216
double worst_seeks
The maximum value for the cost of seek operations for key lookup during ref access.
Definition: sql_select.h:659
uint first_firstmatch_table
Definition: sql_select.h:493
SELECT_LEX_UNIT * unit
Outer-most query expression.
Definition: sql_lex.h:3195
static const char * sql_mode
Definition: mysqlslap.cc:193
struct TABLE::@119 reginfo
const char * name() const
Not actually needed.
Definition: sql_select.h:1062
virtual enum_field_types type() const =0
Key_use * keyuse() const
Definition: sql_select.h:620
Definition: table.h:2468
virtual enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_select.h:73
key_part_map bound_keyparts
The key columns which are equal to expressions depending only of earlier tables of the current join p...
Definition: sql_select.h:272
Key_use(TABLE_LIST *table_ref_arg, Item *val_arg, table_map used_tables_arg, uint key_arg, uint keypart_arg, uint optimize_arg, key_part_map keypart_map_arg, ha_rows ref_table_rows_arg, bool null_rejecting_arg, bool *cond_guard_arg, uint sj_pred_no_arg)
Definition: sql_select.h:189
void set_table(TABLE *t)
Definition: sql_opt_exec_shared.h:263
Definition: sql_select.h:858
virtual bool is_data_change_stmt() const override
Definition: sql_select.h:77
enum quick_type use_quick
Definition: sql_select.h:707
enum_check_fields check_for_truncated_fields
Definition: sql_class.h:2192
bool test_if_subpart(ORDER *a, ORDER *b)
Return 1 if second is a subpart of first argument.
Definition: sql_select.cc:3557
#define MODE_NO_ZERO_DATE
Definition: system_variables.h:121
double read_cost
Cost of accessing the table in course of the entire complete join execution, i.e. ...
Definition: sql_select.h:375
virtual bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_select.h:79
Key_use()
Definition: sql_select.h:173
bool m_inited
Whether the key was already copied.
Definition: sql_select.h:1038
ulonglong nested_join_map
Definition: sql_select.h:63
bool test_if_cheaper_ordering(const JOIN_TAB *tab, ORDER_with_src *order, TABLE *table, Key_map usable_keys, int key, ha_rows select_limit, int *new_key, int *new_key_direction, ha_rows *new_select_limit, uint *new_used_key_parts=NULL, uint *saved_best_key_parts=NULL)
Find a cheaper access key than a given key.
Definition: sql_select.cc:4914
#define false
Definition: config_static.h:43
double read_cost
Cost of the ref access path for the current join prefix, i.e.
Definition: sql_select.h:294
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:75
join_type
Definition: sql_opt_exec_shared.h:169
uint used_fieldlength
Definition: sql_select.h:706
SJ_TMP_TABLE * create_sj_tmp_table(THD *thd, JOIN *join, SJ_TMP_TABLE::TAB *first_tab, SJ_TMP_TABLE::TAB *last_tab)
Set up the support structures (NULL bits, row offsets, etc.) for a semijoin duplicate weedout table...
Definition: sql_select.cc:1074
API for getting cost estimates for server operations that are not directly related to a table object...
Definition: opt_costmodel.h:41
static void dbug_tmp_restore_column_map(MY_BITMAP *bitmap, my_bitmap_map *old)
Definition: table.h:3677
bool use_join_buffer
Definition: sql_select.h:440
Definition: item_cmpfunc.h:2461
uint m_use_join_cache
Join buffering strategy.
Definition: sql_select.h:713
my_off_t ha_rows
Definition: my_base.h:1130
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:70
uint loosescan_key
Definition: sql_select.h:485
COND_EQUAL * cond_equal
multiple equalities for the on expression
Definition: sql_select.h:642
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:778
Key_map quick_order_tested
Used to avoid repeated range analysis for the same key in test_if_skip_sort_order().
Definition: sql_select.h:682