MySQL  8.0.26
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, 2021, Oracle and/or its affiliates.
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 <assert.h>
31 #include <sys/types.h>
32 
33 #include <climits>
34 
35 #include "my_base.h"
36 
37 #include "my_inttypes.h"
38 #include "my_sqlcommand.h"
39 #include "my_table_map.h"
40 #include "sql/field.h" // Copy_field
41 #include "sql/item_cmpfunc.h" // Item_cond_and
42 #include "sql/opt_costmodel.h"
43 #include "sql/sql_bitmap.h"
44 #include "sql/sql_cmd_dml.h" // Sql_cmd_dml
45 #include "sql/sql_const.h"
46 #include "sql/sql_opt_exec_shared.h" // join_type
47 
48 class Item;
49 class Item_func;
50 class JOIN_TAB;
51 class KEY;
52 class QEP_TAB;
53 class Query_result;
54 class Query_block;
55 class Select_lex_visitor;
56 class SJ_TMP_TABLE;
57 class Temp_table_param;
58 class THD;
59 template <class T>
60 class List;
61 struct LEX;
62 struct MYSQL_LEX_CSTRING;
63 struct ORDER;
64 struct SJ_TMP_TABLE_TAB;
65 struct TABLE;
66 struct TABLE_LIST;
67 
69 
70 class Sql_cmd_select : public Sql_cmd_dml {
71  public:
72  explicit Sql_cmd_select(Query_result *result_arg) : Sql_cmd_dml() {
73  result = result_arg;
74  }
75 
76  enum_sql_command sql_command_code() const override { return SQLCOM_SELECT; }
77 
78  bool is_data_change_stmt() const override { return false; }
79 
80  bool accept(THD *thd, Select_lex_visitor *visitor) override;
81 
83 
84  protected:
85  bool may_use_cursor() const override { return true; }
86  bool precheck(THD *thd) override;
87  bool check_privileges(THD *thd) override;
88  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(nullptr),
175  val(nullptr),
176  used_tables(0),
177  key(0),
178  keypart(0),
179  optimize(0),
180  keypart_map(0),
181  ref_table_rows(0),
182  null_rejecting(false),
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  Value used for lookup into @c key. It may be an Item_field, a
211  constant or any other expression. If @c val contains a field from
212  another table, then we have a join condition, and the table(s) of
213  the field(s) in @c val should be before @c table in the join plan.
214  */
216  /**
217  All tables used in @c val, that is all tables that provide bindings
218  for the expression @c val. These tables must be in the plan before
219  executing the equi-join described by a Key_use.
220  For all expressions except for the MATCH function, this is the same
221  as val->used_tables().
222  For the MATCH function, val is the actual MATCH function, and used_tables
223  is the set of tables used in the AGAINST argument.
224  */
226  uint key; ///< number of index
227  uint keypart; ///< used part of the index
228  uint optimize; ///< 0, or KEY_OPTIMIZE_*
229  key_part_map keypart_map; ///< like keypart, but as a bitmap
230  ha_rows ref_table_rows; ///< Estimate of how many rows for a key value
231  /**
232  If true, the comparison this value was created from will not be
233  satisfied if val has NULL 'value' (unless KEY_OPTIMIZE_REF_OR_NULL is set).
234  Not used if the index is fulltext (such index cannot be used for
235  equalities).
236  */
238  /**
239  !NULL - This Key_use was created from an equality that was wrapped into
240  an Item_func_trig_cond. This means the equality (and validity of
241  this Key_use element) can be turned on and off. The on/off state
242  is indicted by the pointed value:
243  *cond_guard == true @<=@> equality condition is on
244  *cond_guard == false @<=@> equality condition is off
245 
246  NULL - Otherwise (the source equality can't be turned off)
247 
248  Not used if the index is fulltext (such index cannot be used for
249  equalities).
250  */
251  bool *cond_guard;
252  /**
253  0..63 @<=@> This was created from semi-join IN-equality # sj_pred_no.
254  UINT_MAX Otherwise
255 
256  Not used if the index is fulltext (such index cannot be used for
257  semijoin).
258 
259  @see get_semi_join_select_list_index()
260  */
262 
263  /*
264  The three members below are different from the rest of Key_use: they are
265  set only by Optimize_table_order, and they change with the currently
266  considered join prefix.
267  */
268 
269  /**
270  The key columns which are equal to expressions depending only of earlier
271  tables of the current join prefix.
272  This information is stored only in the first Key_use of the index.
273  */
275 
276  /**
277  Fanout of the ref access path for this index, in the current join
278  prefix.
279  This information is stored only in the first Key_use of the index.
280  */
281  double fanout;
282 
283  /**
284  Cost of the ref access path for the current join prefix, i.e. the
285  cost of using ref access once multiplied by estimated number of
286  partial rows from tables earlier in the join sequence.
287  read_cost does NOT include cost of processing rows on the
288  server side (row_evaluate_cost).
289 
290  Example: If the cost of ref access on this index is 5, and the
291  estimated number of partial rows from earlier tables is 10,
292  read_cost=50.
293 
294  This information is stored only in the first Key_use of the index.
295  */
296  double read_cost;
297 };
298 
299 /// @returns join type according to quick select type used
301 
302 class JOIN;
303 
304 #define SJ_OPT_NONE 0
305 #define SJ_OPT_DUPS_WEEDOUT 1
306 #define SJ_OPT_LOOSE_SCAN 2
307 #define SJ_OPT_FIRST_MATCH 3
308 #define SJ_OPT_MATERIALIZE_LOOKUP 4
309 #define SJ_OPT_MATERIALIZE_SCAN 5
310 
311 inline bool sj_is_materialize_strategy(uint strategy) {
312  return strategy >= SJ_OPT_MATERIALIZE_LOOKUP;
313 }
314 
315 /**
316  Bits describing quick select type
317 */
319 
320 /**
321  A position of table within a join order. This structure is primarily used
322  as a part of @c join->positions and @c join->best_positions arrays.
323 
324  One POSITION element contains information about:
325  - Which table is accessed
326  - Which access method was chosen
327  = Its cost and \#of output records
328  - Semi-join strategy choice. Note that there are two different
329  representation formats:
330  1. The one used during join optimization
331  2. The one used at plan refinement/code generation stage.
332  We call fix_semijoin_strategies_for_picked_join_order() to switch
333  between #1 and #2. See that function's comment for more details.
334 
335  - Semi-join optimization state. When we're running join optimization,
336  we main a state for every semi-join strategy which are various
337  variables that tell us if/at which point we could consider applying the
338  strategy.
339  The variables are really a function of join prefix but they are too
340  expensive to re-caclulate for every join prefix we consider, so we
341  maintain current state in join->positions[\#tables_in_prefix]. See
342  advance_sj_state() for details.
343 
344  This class has to stay a POD, because it is memcpy'd in many places.
345 */
346 
347 struct POSITION {
348  /**
349  The number of rows that will be fetched by the chosen access
350  method per each row combination of previous tables. That is:
351 
352  rows_fetched = selectivity(access_condition) * cardinality(table)
353 
354  where 'access_condition' is whatever condition was chosen for
355  index access, depending on the access method ('ref', 'range',
356  etc.)
357 
358  @note For index/table scans, rows_fetched may be less than
359  the number of rows in the table because the cost of evaluating
360  constant conditions is included in the scan cost, and the number
361  of rows produced by these scans is the estimated number of rows
362  that pass the constant conditions. @see
363  Optimize_table_order::calculate_scan_cost() . But this is only during
364  planning; make_join_readinfo() simplifies it for EXPLAIN.
365  */
366  double rows_fetched;
367 
368  /**
369  Cost of accessing the table in course of the entire complete join
370  execution, i.e. cost of one access method use (e.g. 'range' or
371  'ref' scan ) multiplied by estimated number of rows from tables
372  earlier in the join sequence.
373 
374  read_cost does NOT include cost of processing rows within the
375  executor (row_evaluate_cost).
376  */
377  double read_cost;
378 
379  /**
380  The fraction of the 'rows_fetched' rows that will pass the table
381  conditions that were NOT used by the access method. If, e.g.,
382 
383  "SELECT ... WHERE t1.colx = 4 and t1.coly @> 5"
384 
385  is resolved by ref access on t1.colx, filter_effect will be the
386  fraction of rows that will pass the "t1.coly @> 5" predicate. The
387  valid range is 0..1, where 0.0 means that no rows will pass the
388  table conditions and 1.0 means that all rows will pass.
389 
390  It is used to calculate how many row combinations will be joined
391  with the next table, @see prefix_rowcount below.
392 
393  @note With condition filtering enabled, it is possible to get
394  a fanout = rows_fetched * filter_effect that is less than 1.0.
395  Consider, e.g., a join between t1 and t2:
396 
397  "SELECT ... WHERE t1.col1=t2.colx and t2.coly OP @<something@>"
398 
399  where t1 is a prefix table and the optimizer currently calculates
400  the cost of adding t2 to the join. Assume that the chosen access
401  method on t2 is a 'ref' access on 'colx' that is estimated to
402  produce 2 rows per row from t1 (i.e., rows_fetched = 2). It will
403  in this case be perfectly fine to calculate a filtering effect
404  @<0.5 (resulting in "rows_fetched * filter_effect @< 1.0") from the
405  predicate "t2.coly OP @<something@>". If so, the number of row
406  combinations from (t1,t2) is lower than the prefix_rowcount of t1.
407 
408  The above is just an example of how the fanout of a table can
409  become less than one. It can happen for any access method.
410  */
412 
413  /**
414  prefix_rowcount and prefix_cost form a stack of partial join
415  order costs and output sizes
416 
417  prefix_rowcount: The number of row combinations that will be
418  joined to the next table in the join sequence.
419 
420  For a joined table it is calculated as
421  prefix_rowcount =
422  last_table.prefix_rowcount * rows_fetched * filter_effect
423 
424  @see filter_effect
425 
426  For a semijoined table it may be less than this formula due to
427  duplicate elimination.
428  */
430  double prefix_cost;
431 
433 
434  /**
435  NULL - 'index' or 'range' or 'index_merge' or 'ALL' access is used.
436  Other - [eq_]ref[_or_null] access is used. Pointer to {t.keypart1 = expr}
437  */
439 
440  /** If ref-based access is used: bitmap of tables this table depends on */
443 
444  /**
445  Current optimization state: Semi-join strategy to be used for this
446  and preceding join tables.
447 
448  Join optimizer sets this for the *last* join_tab in the
449  duplicate-generating range. That is, in order to interpret this field,
450  one needs to traverse join->[best_]positions array from right to left.
451  When you see a join table with sj_strategy!= SJ_OPT_NONE, some other
452  field (depending on the strategy) tells how many preceding positions
453  this applies to. The values of covered_preceding_positions->sj_strategy
454  must be ignored.
455  */
457  /**
458  Valid only after fix_semijoin_strategies_for_picked_join_order() call:
459  if sj_strategy!=SJ_OPT_NONE, this is the number of subsequent tables that
460  are covered by the specified semi-join strategy
461  */
463 
464  /**
465  Bitmap of semi-join inner tables that are in the join prefix and for
466  which there's no provision yet for how to eliminate semi-join duplicates
467  which they produce.
468  */
470 
471  /* LooseScan strategy members */
472 
473  /* The first (i.e. driving) table we're doing loose scan for */
475  /*
476  Tables that need to be in the prefix before we can calculate the cost
477  of using LooseScan strategy.
478  */
480 
481  /*
482  keyno - Planning to do LooseScan on this key. If keyuse is NULL then
483  this is a full index scan, otherwise this is a ref+loosescan
484  scan (and keyno matches the KEUSE's)
485  MAX_KEY - Not doing a LooseScan
486  */
487  uint loosescan_key; // final (one for strategy instance )
488  uint loosescan_parts; /* Number of keyparts to be kept distinct */
489 
490  /* FirstMatch strategy */
491  /*
492  Index of the first inner table that we intend to handle with this
493  strategy
494  */
496  /**
497  Value of Optimize_table_order::cur_embedding_map after this table has
498  been added to the plan. Used to constrain FirstMatch table orders.
499  */
501  /*
502  Tables that were not in the join prefix when we've started considering
503  FirstMatch strategy.
504  */
506  /*
507  Tables that need to be in the prefix before we can calculate the cost
508  of using FirstMatch strategy.
509  */
511 
512  /* Duplicate Weedout strategy */
513  /* The first table that the strategy will need to handle */
515  /*
516  Tables that we will need to have in the prefix to do the weedout step
517  (all inner and all outer that the involved semi-joins are correlated with)
518  */
520 
521  /* SJ-Materialization-Scan strategy */
522  /* The last inner table (valid once we're after it) */
524  /*
525  Tables that we need to have in the prefix to calculate the correct cost.
526  Basically, we need all inner tables and outer tables mentioned in the
527  semi-join's ON expression so we can correctly account for fanout.
528  */
530 
531  /**
532  Even if the query has no semijoin, two sj-related members are read and
533  must thus have been set, by this function.
534  */
535  void no_semijoin() {
538  }
539  /**
540  Set complete estimated cost and produced rowcount for the prefix of tables
541  up to and including this table, in the join plan.
542 
543  @param cost Estimated cost
544  @param rowcount Estimated row count
545  */
546  void set_prefix_cost(double cost, double rowcount) {
547  prefix_cost = cost;
548  prefix_rowcount = rowcount;
549  }
550  /**
551  Set complete estimated cost and produced rowcount for the prefix of tables
552  up to and including this table, calculated from the cost of the previous
553  stage, the fanout of the current stage and the cost to process a row at
554  the current stage.
555 
556  @param idx Index of position object within array, if zero there is no
557  "previous" stage that can be added.
558  @param cm Cost model that provides the actual calculation
559  */
561  if (idx == 0) {
564  } else {
566  prefix_cost = (this - 1)->prefix_cost + read_cost +
568  }
570  }
571 
573 
575 
576  private:
577  /**
578  The lateral dependendencies of 'table' and all subsequent JOIN_TABs
579  in the join plan.
580  */
582 };
583 
584 /**
585  Query optimization plan node.
586 
587  Specifies:
588 
589  - a table access operation on the table specified by this node, and
590 
591  - a join between the result of the set of previous plan nodes and
592  this plan node.
593 */
594 class JOIN_TAB : public QEP_shared_owner {
595  public:
596  JOIN_TAB();
597 
598  void set_table(TABLE *t);
599 
600  /// Sets the pointer to the join condition of TABLE_LIST
601  void init_join_cond_ref(TABLE_LIST *tl);
602 
603  /// @returns join condition
604  Item *join_cond() const { return *m_join_cond_ref; }
605 
606  /**
607  Sets join condition
608  @note this also changes TABLE_LIST::m_join_cond.
609  */
610  void set_join_cond(Item *cond) { *m_join_cond_ref = cond; }
611 
612  /// Set the combined condition for a table (may be performed several times)
613  void set_condition(Item *to) {
614  if (condition() != to) {
615  m_qs->set_condition(to);
616  // Condition changes, so some indexes may become usable or not:
618  }
619  }
620 
623  Key_use *keyuse() const { return m_keyuse; }
624  void set_keyuse(Key_use *k) { m_keyuse = k; }
625 
626  TABLE_LIST *table_ref; /**< points to table reference */
627 
628  private:
629  Key_use *m_keyuse; /**< pointer to first used key */
630 
631  /**
632  Pointer to the associated join condition:
633 
634  - if this is a table with position==NULL (e.g. internal sort/group
635  temporary table), pointer is NULL
636 
637  - otherwise, pointer is the address of some TABLE_LIST::m_join_cond.
638  Thus, the pointee is the same as TABLE_LIST::m_join_cond (changing one
639  changes the other; thus, optimizations made on the second are reflected
640  in Query_block::print_table_array() which uses the first one).
641  */
643 
644  public:
645  COND_EQUAL *cond_equal; /**< multiple equalities for the on expression*/
646 
647  /**
648  The maximum value for the cost of seek operations for key lookup
649  during ref access. The cost model for ref access assumes every key
650  lookup will cause reading a block from disk. With many key lookups
651  into the same table, most of the blocks will soon be in a memory
652  buffer. As a consequence, there will in most cases be an upper
653  limit on the number of actual disk accesses the ref access will
654  cause. This variable is used for storing a maximum cost estimate
655  for the disk accesses for ref access. It is used for limiting the
656  cost estimate for ref access to a more realistic value than
657  assuming every key lookup causes a random disk access. Without
658  having this upper limit for the cost of ref access, table scan
659  would be more likely to be chosen for cases where ref access
660  performs better.
661  */
662  double worst_seeks;
663  /** Keys with constant part. Subset of keys. */
665  Key_map checked_keys; /**< Keys checked */
666  /**
667  Keys which can be used for skip scan access. We store it
668  separately from tab->const_keys & join_tab->keys() to avoid
669  unnecessary printing of the prossible keys in EXPLAIN output
670  as some of these keys can be marked not usable for skip scan later.
671  More strict check for prossible keys is performed in
672  get_best_skip_scan() function.
673  */
676 
677  /**
678  Used to avoid repeated range analysis for the same key in
679  test_if_skip_sort_order(). This would otherwise happen if the best
680  range access plan found for a key is turned down.
681  quick_order_tested is cleared every time the select condition for
682  this JOIN_TAB changes since a new condition may give another plan
683  and cost from range analysis.
684  */
686 
687  /*
688  Number of records that will be scanned (yes scanned, not returned) by the
689  best 'independent' access method, i.e. table scan or QUICK_*_SELECT)
690  */
692  /*
693  Cost of accessing the table using "ALL" or range/index_merge access
694  method (but not 'index' for some reason), i.e. this matches method which
695  E(#records) is in found_records.
696  */
697  double read_time;
698  /**
699  The set of tables that this table depends on. Used for outer join and
700  straight join dependencies.
701  */
703  /**
704  The set of tables that are referenced by key from this table.
705  */
707 
708  public:
710  enum quick_type use_quick;
711 
712  /**
713  Join buffering strategy.
714  After optimization it contains chosen join buffering strategy (if any).
715  */
717 
718  /* SemiJoinDuplicateElimination variables: */
719  /*
720  Embedding SJ-nest (may be not the direct parent), or NULL if none.
721  It is the closest semijoin or antijoin nest.
722  This variable holds the result of table pullout.
723  */
725 
726  /* NestedOuterJoins: Bitmap of nested joins this table is part of */
728 
729  /** Flags from SE's MRR implementation, to be used by JOIN_CACHE */
731 
732  /** true <=> AM will scan backward */
734 
735  /** Clean up associated table after query execution, including resources */
736  void cleanup();
737 
738  /// @returns semijoin strategy for this table.
739  uint get_sj_strategy() const;
740 
741  private:
742  JOIN_TAB(const JOIN_TAB &); // not defined
743  JOIN_TAB &operator=(const JOIN_TAB &); // not defined
744 };
745 
747  : QEP_shared_owner(),
748  table_ref(nullptr),
749  m_keyuse(nullptr),
750  m_join_cond_ref(nullptr),
751  cond_equal(nullptr),
752  worst_seeks(0.0),
753  const_keys(),
754  checked_keys(),
755  skip_scan_keys(),
756  needed_reg(),
757  quick_order_tested(),
758  found_records(0),
759  read_time(0),
760  dependent(0),
761  key_dependent(0),
762  used_fieldlength(0),
763  use_quick(QS_NONE),
764  m_use_join_cache(0),
765  emb_sj_nest(nullptr),
766  embedding_map(0),
767  join_cache_flags(0),
768  reversed_access(false) {}
769 
770 /* Extern functions in sql_select.cc */
771 void count_field_types(const Query_block *query_block, Temp_table_param *param,
772  const mem_root_deque<Item *> &fields,
773  bool reset_with_sum_func, bool save_sum_fields);
774 uint find_shortest_key(TABLE *table, const Key_map *usable_keys);
775 
776 /* functions from opt_sum.cc */
777 bool is_simple_predicate(Item_func *func_item, Item **args, bool *inv_order);
778 
780  AGGR_COMPLETE, // All aggregates were evaluated
781  AGGR_REGULAR, // Aggregates not fully evaluated, regular execution required
782  AGGR_DELAYED, // Aggregates not fully evaluated, execute with ha_records()
783  AGGR_EMPTY // Source tables empty, aggregates are NULL or 0 (for COUNT)
784 };
785 
786 bool optimize_aggregated_query(THD *thd, Query_block *select,
787  const mem_root_deque<Item *> &all_fields,
788  Item *conds, aggregate_evaluated *decision);
789 
790 /* from sql_delete.cc, used by opt_range.cc */
791 extern "C" int refpos_order_cmp(const void *arg, const void *a, const void *b);
792 
793 /// The name of store_key instances that represent constant items.
794 extern const char *STORE_KEY_CONST_NAME;
795 
796 /// Check privileges for all columns referenced from join expression
798 
799 /// Check privileges for all columns referenced from an expression list
801  ulong privileges);
802 
803 /** class to copying an field/item to a key struct */
804 
805 class store_key {
806  public:
807  bool null_key{false}; /* true <=> the value of the key has a null part */
809  store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length);
810  virtual ~store_key() = default;
811  virtual const char *name() const = 0;
812 
813  /**
814  @brief sets ignore truncation warnings mode and calls the real copy method
815 
816  @details this function makes sure truncation warnings when preparing the
817  key buffers don't end up as errors (because of an enclosing INSERT/UPDATE).
818  */
820 
821  protected:
822  Field *to_field; // Store data here
823 
824  virtual enum store_key_result copy_inner() = 0;
825 };
826 
827 class store_key_field final : public store_key {
829  const char *m_field_name;
830 
831  public:
832  store_key_field(THD *thd, Field *to_field_arg, uchar *ptr,
833  uchar *null_ptr_arg, uint length, Field *from_field,
834  const char *name_arg);
835 
836  const char *name() const override { return m_field_name; }
837 
838  // Change the source field to be another field. Used only by
839  // CreateBKAIterator, when rewriting multi-equalities used in ref access.
840  void replace_from_field(Field *from_field);
841 
842  protected:
843  enum store_key_result copy_inner() override;
844 };
845 class store_key_item : public store_key {
846  protected:
848 
849  public:
850  store_key_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg,
851  uint length, Item *item_arg);
852  const char *name() const override { return "func"; }
853 
854  protected:
855  enum store_key_result copy_inner() override;
856 };
857 
858 /*
859  Class used for unique constraint implementation by subselect_hash_sj_engine.
860  It uses store_key_item implementation to do actual copying, but after
861  that, copy_inner calculates hash of each key part for unique constraint.
862 */
863 
864 class store_key_hash_item final : public store_key_item {
866 
867  public:
868  store_key_hash_item(THD *thd, Field *to_field_arg, uchar *ptr,
869  uchar *null_ptr_arg, uint length, Item *item_arg,
870  ulonglong *hash_arg)
871  : store_key_item(thd, to_field_arg, ptr, null_ptr_arg, length, item_arg),
872  hash(hash_arg) {}
873  const char *name() const override { return "func"; }
874 
875  protected:
876  enum store_key_result copy_inner() override;
877 };
878 
880 
881 // Statement timeout function(s)
882 bool set_statement_timer(THD *thd);
883 void reset_statement_timer(THD *thd);
884 
885 void free_underlaid_joins(THD *thd, Query_block *select);
886 
887 void calc_used_field_length(TABLE *table, bool needs_rowid,
888  uint *p_used_fieldlength);
889 
891 bool check_field_is_const(Item *cond, const Item *order_item,
892  const Field *order_field = nullptr,
893  Item **const_item = nullptr);
894 bool test_if_subpart(ORDER *a, ORDER *b);
895 void calc_group_buffer(JOIN *join, ORDER *group);
896 bool make_join_readinfo(JOIN *join, uint no_jbuf_after);
897 bool create_ref_for_key(JOIN *join, JOIN_TAB *j, Key_use *org_keyuse,
898  table_map used_tables);
899 bool types_allow_materialization(Item *outer, Item *inner);
900 bool and_conditions(Item **e1, Item *e2);
901 
902 /**
903  Create a AND item of two existing items.
904  A new Item_cond_and item is created with the two supplied items as
905  arguments.
906 
907  @note About handling of null pointers as arguments: if the first
908  argument is a null pointer, then the item given as second argument is
909  returned (no new Item_cond_and item is created). The second argument
910  must not be a null pointer.
911 
912  @param cond the first argument to the new AND condition
913  @param item the second argument to the new AND condtion
914 
915  @return the new AND item
916 */
917 static inline Item *and_items(Item *cond, Item *item) {
918  assert(item != nullptr);
919  return (cond ? (new Item_cond_and(cond, item)) : item);
920 }
921 
922 /// A variant of the above, guaranteed to return Item_bool_func.
923 static inline Item_bool_func *and_items(Item *cond, Item_bool_func *item) {
924  assert(item != nullptr);
925  return (cond ? (new Item_cond_and(cond, item)) : item);
926 }
927 
928 uint actual_key_parts(const KEY *key_info);
929 
930 class ORDER_with_src;
931 
933  bool *need_sort, bool *reverse);
935  uint *used_key_parts, bool *skip_quick);
937  TABLE *table, Key_map usable_keys, int key,
938  ha_rows select_limit, int *new_key,
939  int *new_key_direction, ha_rows *new_select_limit,
940  uint *new_used_key_parts = nullptr,
941  uint *saved_best_key_parts = nullptr);
942 /**
943  Calculate properties of ref key: key length, number of used key parts,
944  dependency map, possibility of null.
945 
946  @param keyuse Array of keys to consider
947  @param tab join_tab to calculate ref parameters for
948  @param key number of the key to use
949  @param used_tables tables read prior to this table
950  @param [out] chosen_keyuses when given, this function will fill array with
951  chosen keyuses
952  @param [out] length_out calculated length of the ref
953  @param [out] keyparts_out calculated number of used keyparts
954  @param [out] dep_map when given, calculated dependency map
955  @param [out] maybe_null when given, calculated maybe_null property
956 */
957 
958 void calc_length_and_keyparts(Key_use *keyuse, JOIN_TAB *tab, const uint key,
959  table_map used_tables, Key_use **chosen_keyuses,
960  uint *length_out, uint *keyparts_out,
961  table_map *dep_map, bool *maybe_null);
962 
963 /**
964  Initialize the given TABLE_REF; setting basic fields and allocating memory
965  for arrays. Call init_ref_part() for each keypart (index field) that is to
966  take part in the ref lookup.
967  */
968 bool init_ref(THD *thd, unsigned keyparts, unsigned length, unsigned keyno,
969  TABLE_REF *ref);
970 
971 /**
972  Initialize a given keypart in the table ref. In particular, sets up the
973  right function pointer to copy the value from “val” into the ref at
974  execution time (or copies the value right now, if it is constant).
975  */
976 bool init_ref_part(THD *thd, unsigned part_no, Item *val, bool *cond_guard,
977  bool null_rejecting, table_map const_tables,
978  table_map used_tables, bool nullable,
979  const KEY_PART_INFO *key_part_info, uchar *key_buff,
980  TABLE_REF *ref);
981 
982 /**
983  Set up the support structures (NULL bits, row offsets, etc.) for a semijoin
984  duplicate weedout table. The object is allocated on the given THD's MEM_ROOT.
985 
986  @param thd the THD to allocate the object on
987  @param join the JOIN that will own the temporary table (ie., has the
988  responsibility to destroy it after use)
989  @param first_tab first table in row key (inclusive)
990  @param last_tab last table in row key (exclusive)
991  */
993  SJ_TMP_TABLE_TAB *first_tab,
994  SJ_TMP_TABLE_TAB *last_tab);
995 
996 /**
997  Returns key flags depending on
998  OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS flag.
999 
1000  @param key_info pointer to KEY structure
1001 
1002  @return key flags.
1003  */
1004 uint actual_key_flags(const KEY *key_info);
1005 
1006 /**
1007  Check if equality can be used to remove sub-clause of GROUP BY/ORDER BY
1008 
1009  @param func comparison operator (= or <=>)
1010  @param v variable comparison operand (validated to be equal to
1011  ordering expression)
1012  @param c other comparison operand (likely to be a constant)
1013 
1014  @returns whether equality determines uniqueness
1015 
1016  Checks if an equality predicate can be used to remove a GROUP BY/ORDER BY
1017  sub-clause when it is known to be true for exactly one distinct value
1018  (e.g. "expr" == "const").
1019  Arguments must be of the same type because e.g. "string_field" = "int_const"
1020  may match more than one distinct value from the column.
1021  */
1023  const Item *v, const Item *c);
1024 
1025 /**
1026  Check whether equality between two items is exact, ie., there are no implicit
1027  casts involved. This is especially important for GROUP BY/ORDER BY, as it
1028  means they can be treated interchangeably. The primary difference between this
1029  and equality_determines_uniqueness() is that item2 does not need to be
1030  a constant (which makes it stricter in other aspects).
1031  */
1033  const Item *item1, const Item *item2);
1034 
1035 /**
1036  Validates a query that uses the secondary engine
1037 
1038  No validations are done if query has not been prepared against the secondary
1039  engine.
1040 
1041  @param lex Parse tree descriptor.
1042 
1043  @return True if error, false otherwise.
1044 */
1045 bool validate_use_secondary_engine(const LEX *lex);
1046 
1047 /**
1048  Perform query optimizations that are specific to a secondary storage
1049  engine.
1050 
1051  @param thd the current session
1052  @return true on error, false on success
1053 */
1054 bool optimize_secondary_engine(THD *thd);
1055 
1056 /**
1057  Calculates the cost of executing a statement, including all its
1058  subqueries and stores it in thd->m_current_query_cost.
1059 
1060  @param lex the statement
1061 */
1062 void accumulate_statement_cost(const LEX *lex);
1063 
1064 #endif /* SQL_SELECT_INCLUDED */
Definition: sql_bitmap.h:137
void clear_all()
Definition: sql_bitmap.h:163
Definition: item_cmpfunc.h:2550
Definition: field.h:4611
API for getting cost estimates for server operations that are not directly related to a table object.
Definition: opt_costmodel.h:41
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:84
Definition: field.h:591
Definition: item_cmpfunc.h:292
Definition: item_cmpfunc.h:2571
Item_func_comparison is a class for comparison functions that take two arguments and return a boolean...
Definition: item_cmpfunc.h:677
Definition: item_func.h:93
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:801
Query optimization plan node.
Definition: sql_select.h:594
Key_map needed_reg
Definition: sql_select.h:675
enum quick_type use_quick
Definition: sql_select.h:710
uint m_use_join_cache
Join buffering strategy.
Definition: sql_select.h:716
void set_condition(Item *to)
Set the combined condition for a table (may be performed several times)
Definition: sql_select.h:613
JOIN_TAB(const JOIN_TAB &)
void set_keyuse(Key_use *k)
Definition: sql_select.h:624
double read_time
Definition: sql_select.h:697
Key_use * keyuse() const
Definition: sql_select.h:623
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:685
TABLE_LIST * table_ref
points to table reference
Definition: sql_select.h:626
Key_map const_keys
Keys with constant part.
Definition: sql_select.h:664
table_map dependent
The set of tables that this table depends on.
Definition: sql_select.h:702
void set_use_join_cache(uint u)
Definition: sql_select.h:622
Key_map skip_scan_keys
Keys which can be used for skip scan access.
Definition: sql_select.h:674
Item * join_cond() const
Definition: sql_select.h:604
bool reversed_access
true <=> AM will scan backward
Definition: sql_select.h:733
TABLE_LIST * emb_sj_nest
Definition: sql_select.h:724
void set_join_cond(Item *cond)
Sets join condition.
Definition: sql_select.h:610
table_map key_dependent
The set of tables that are referenced by key from this table.
Definition: sql_select.h:706
double worst_seeks
The maximum value for the cost of seek operations for key lookup during ref access.
Definition: sql_select.h:662
uint join_cache_flags
Flags from SE's MRR implementation, to be used by JOIN_CACHE.
Definition: sql_select.h:730
COND_EQUAL * cond_equal
multiple equalities for the on expression
Definition: sql_select.h:645
ha_rows found_records
Definition: sql_select.h:691
JOIN_TAB()
Definition: sql_select.h:746
Key_map checked_keys
Keys checked.
Definition: sql_select.h:665
Item ** m_join_cond_ref
Pointer to the associated join condition:
Definition: sql_select.h:642
Key_use * m_keyuse
pointer to first used key
Definition: sql_select.h:629
nested_join_map embedding_map
Definition: sql_select.h:727
uint use_join_cache() const
Definition: sql_select.h:621
JOIN_TAB & operator=(const JOIN_TAB &)
uint used_fieldlength
Definition: sql_select.h:709
Definition: sql_optimizer.h:125
Definition: key.h:56
Definition: key.h:112
A Key_use represents an equality predicate of the form (table.column = val), where the column is inde...
Definition: sql_select.h:170
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:274
Item * val
Value used for lookup into key.
Definition: sql_select.h:215
double read_cost
Cost of the ref access path for the current join prefix, i.e.
Definition: sql_select.h:296
uint optimize
0, or KEY_OPTIMIZE_*
Definition: sql_select.h:228
key_part_map keypart_map
like keypart, but as a bitmap
Definition: sql_select.h:229
ha_rows ref_table_rows
Estimate of how many rows for a key value.
Definition: sql_select.h:230
uint sj_pred_no
0..63 <=> This was created from semi-join IN-equality # sj_pred_no.
Definition: sql_select.h:261
uint keypart
used part of the index
Definition: sql_select.h:227
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
uint key
number of index
Definition: sql_select.h:226
TABLE_LIST * table_ref
table owning the index
Definition: sql_select.h:208
bool null_rejecting
If true, the comparison this value was created from will not be satisfied if val has NULL 'value' (un...
Definition: sql_select.h:237
Key_use()
Definition: sql_select.h:173
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:251
table_map used_tables
All tables used in val, that is all tables that provide bindings for the expression val.
Definition: sql_select.h:225
double fanout
Fanout of the ref access path for this index, in the current join prefix.
Definition: sql_select.h:281
Definition: sql_list.h:431
Wrapper for ORDER* pointer to trace origins of ORDER list.
Definition: sql_optimizer.h:95
ORDER * order
ORDER expression that we are wrapping with this class.
Definition: sql_optimizer.h:97
Definition: sql_executor.h:260
Owner of a QEP_shared; parent of JOIN_TAB and QEP_TAB.
Definition: sql_opt_exec_shared.h:470
Item * condition() const
Definition: sql_opt_exec_shared.h:515
QEP_shared * m_qs
Definition: sql_opt_exec_shared.h:563
void set_condition(Item *c)
Definition: sql_opt_exec_shared.h:291
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1107
Definition: query_result.h:55
Definition: sql_executor.h:102
Abstract base class for traversing the Query_block tree.
Definition: select_lex_visitor.h:39
Definition: sql_cmd_dml.h:34
Definition: sql_select.h:70
Sql_cmd_select(Query_result *result_arg)
Definition: sql_select.h:72
enum_sql_command sql_command_code() const override
Return the command code for this statement.
Definition: sql_select.h:76
bool may_use_cursor() const override
Definition: sql_select.h:85
bool is_data_change_stmt() const override
Definition: sql_select.h:78
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:821
Object containing parameters used when creating and using temporary tables.
Definition: temp_table_param.h:98
Definition: sql_select.h:827
Copy_field m_copy_field
Definition: sql_select.h:828
const char * m_field_name
Definition: sql_select.h:829
const char * name() const override
Definition: sql_select.h:836
Definition: sql_select.h:864
const char * name() const override
Definition: sql_select.h:873
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:868
ulonglong * hash
Definition: sql_select.h:865
Definition: sql_select.h:845
Item * item
Definition: sql_select.h:847
const char * name() const override
Definition: sql_select.h:852
class to copying an field/item to a key struct
Definition: sql_select.h:805
virtual const char * name() const =0
Field * to_field
Definition: sql_select.h:822
virtual ~store_key()=default
virtual enum store_key_result copy_inner()=0
bool null_key
Definition: sql_select.h:807
store_key_result
Definition: sql_select.h:808
@ STORE_KEY_OK
Definition: sql_select.h:808
@ STORE_KEY_FATAL
Definition: sql_select.h:808
@ STORE_KEY_CONV
Definition: sql_select.h:808
Dialog Client Authentication nullptr
Definition: dialog.cc:353
enum store_key_result copy_inner() override
Definition: sql_select.cc:2331
void replace_from_field(Field *from_field)
Definition: sql_select.cc:2340
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=nullptr, uint *saved_best_key_parts=nullptr)
Find a cheaper access key than a given key.
Definition: sql_select.cc:4894
bool accept(THD *thd, Select_lex_visitor *visitor) override
Definition: sql_select.cc:417
enum store_key_result copy_inner() override
Definition: sql_select.cc:2466
void free_underlaid_joins(THD *thd, Query_block *select)
Free joins of subselect of this select.
Definition: sql_select.cc:4014
bool make_join_readinfo(JOIN *join, uint no_jbuf_after)
Plan refinement stage: do various setup things for the executor.
Definition: sql_select.cc:3105
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:1127
void reset_statement_timer(THD *thd)
Deactivate the timer associated with the statement that was executed.
Definition: sql_select.cc:224
const char * STORE_KEY_CONST_NAME
The name of store_key instances that represent constant items.
Definition: sql_select.cc:5248
void accumulate_statement_cost(const LEX *lex)
Calculates the cost of executing a statement, including all its subqueries and stores it in thd->m_cu...
Definition: sql_select.cc:642
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:2198
void count_field_types(const Query_block *query_block, Temp_table_param *param, const mem_root_deque< 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:3769
bool types_allow_materialization(Item *outer, Item *inner)
Check if two items are compatible wrt.
Definition: sql_select.cc:1028
bool test_if_subpart(ORDER *a, ORDER *b)
Return 1 if second is a subpart of first argument.
Definition: sql_select.cc:3835
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:1687
store_key(THD *thd, Field *field_arg, uchar *ptr, uchar *null, uint length)
Definition: sql_select.cc:2431
void init_join_cond_ref(TABLE_LIST *tl)
Sets the pointer to the join condition of TABLE_LIST.
Definition: sql_select.cc:3350
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:2057
bool check_privileges_for_join(THD *thd, mem_root_deque< TABLE_LIST * > *tables)
Check privileges for all columns referenced from join expression.
Definition: sql_select.cc:1889
store_key_result copy()
sets ignore truncation warnings mode and calls the real copy method
Definition: sql_select.cc:2448
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:5225
uint find_shortest_key(TABLE *table, const Key_map *usable_keys)
Find shortest key suitable for full table scan.
Definition: sql_optimizer.cc:1841
bool set_statement_timer(THD *thd)
Set the time until the currently running statement is aborted.
Definition: sql_select.cc:197
bool precheck(THD *thd) override
Perform an authorization precheck for an unprepared SELECT statement.
Definition: sql_select.cc:864
bool equality_determines_uniqueness(const Item_func_comparison *func, const Item *v, const Item *c)
Check if equality can be used to remove sub-clause of GROUP BY/ORDER BY.
Definition: sql_select.cc:3635
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:421
bool optimize_secondary_engine(THD *thd)
Perform query optimizations that are specific to a secondary storage engine.
Definition: sql_select.cc:719
bool check_field_is_const(Item *cond, const Item *order_item, const Field *order_field=nullptr, Item **const_item=nullptr)
Check if a field is equal to a constant value in a condition.
Definition: sql_select.cc:3706
void set_table(TABLE *t)
Definition: sql_select.cc:3345
store_key_item(THD *thd, Field *to_field_arg, uchar *ptr, uchar *null_ptr_arg, uint length, Item *item_arg)
Definition: sql_select.cc:2533
void calc_group_buffer(JOIN *join, ORDER *group)
calc how big buffer we need for comparing group entries.
Definition: sql_select.cc:3857
join_type calc_join_type(int quick_type)
Definition: sql_select.cc:5239
bool check_privileges_for_list(THD *thd, const mem_root_deque< Item * > &items, ulong privileges)
Check privileges for all columns referenced from an expression list.
Definition: sql_select.cc:1916
uint actual_key_flags(const KEY *key_info)
Returns key flags depending on OPTIMIZER_SWITCH_USE_INDEX_EXTENSIONS flag.
Definition: sql_select.cc:5232
uint get_sj_strategy() const
Definition: sql_optimizer.cc:1282
bool check_privileges(THD *thd) override
Perform an authorization check for a prepared SELECT statement.
Definition: sql_select.cc:905
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:5130
bool and_conditions(Item **e1, Item *e2)
Extend e1 by AND'ing e2 to the condition e1 points to.
Definition: sql_select.cc:2576
bool init_ref(THD *thd, unsigned keyparts, unsigned length, unsigned keyno, TABLE_REF *ref)
Initialize the given TABLE_REF; setting basic fields and allocating memory for arrays.
Definition: sql_select.cc:2096
bool prepare_inner(THD *thd) override
Prepare a SELECT statement.
Definition: sql_select.cc:430
ORDER * simple_remove_const(ORDER *order, Item *where)
Filter out ORDER BY items that are equal to constants in WHERE condition.
Definition: sql_select.cc:3619
bool validate_use_secondary_engine(const LEX *lex)
Validates a query that uses the secondary engine.
Definition: sql_select.cc:272
void cleanup()
Clean up associated table after query execution, including resources.
Definition: sql_select.cc:3358
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.cc:2317
bool init_ref_part(THD *thd, unsigned part_no, Item *val, bool *cond_guard, bool null_rejecting, table_map const_tables, table_map used_tables, bool nullable, const KEY_PART_INFO *key_part_info, uchar *key_buff, TABLE_REF *ref)
Initialize a given keypart in the table ref.
Definition: sql_select.cc:2116
bool error_if_full_join(JOIN *join)
Give error if we some tables are done with a full join.
Definition: sql_select.cc:3320
enum store_key_result copy_inner() override
Definition: sql_select.cc:2543
bool equality_has_no_implicit_casts(const Item_func_comparison *func, const Item *item1, const Item *item2)
Check whether equality between two items is exact, ie., there are no implicit casts involved.
Definition: sql_select.cc:3653
void calc_used_field_length(TABLE *table, bool needs_rowid, uint *p_used_fieldlength)
Find how much space the prevous read not const tables takes in cache.
Definition: sql_select.cc:1956
static uchar key[100]
Definition: mi_test2.cc:58
This file includes constants used by all storage engines.
ulong key_part_map
Definition: my_base.h:1006
my_off_t ha_rows
Definition: my_base.h:1138
Some integer typedefs for easier portability.
unsigned long long int ulonglong
Definition: my_inttypes.h:55
unsigned char uchar
Definition: my_inttypes.h:51
enum_sql_command
Definition: my_sqlcommand.h:45
@ SQLCOM_SELECT
Definition: my_sqlcommand.h:46
uint64_t table_map
Definition: my_table_map.h:29
static ulong select_limit
Definition: mysql.cc:201
static char * where
Definition: mysqldump.cc:135
PT & ref(PT *tp)
Definition: tablespace_impl.cc:358
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
RangeReverse< Range > reverse(Range &x)
Iterate over a range in reverse.
Definition: utilities.h:135
std::string join(Container cont, const std::string &delim)
join elements of an container into a string separated by a delimiter.
Definition: string.h:144
ulonglong nested_join_map
Definition: nested_join.h:36
File containing constants that can be used throughout the server.
Common types of the Optimizer, used by optimization and execution.
join_type
Definition: sql_opt_exec_shared.h:178
bool sj_is_materialize_strategy(uint strategy)
Definition: sql_select.h:311
#define SJ_OPT_MATERIALIZE_LOOKUP
Definition: sql_select.h:308
aggregate_evaluated
Definition: sql_select.h:779
@ AGGR_COMPLETE
Definition: sql_select.h:780
@ AGGR_EMPTY
Definition: sql_select.h:783
@ AGGR_REGULAR
Definition: sql_select.h:781
@ AGGR_DELAYED
Definition: sql_select.h:782
#define SJ_OPT_NONE
Definition: sql_select.h:304
bool optimize_aggregated_query(THD *thd, Query_block *select, const mem_root_deque< 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:274
quick_type
Bits describing quick select type.
Definition: sql_select.h:318
@ QS_DYNAMIC_RANGE
Definition: sql_select.h:318
@ QS_NONE
Definition: sql_select.h:318
@ QS_RANGE
Definition: sql_select.h:318
ulonglong nested_join_map
Definition: sql_select.h:66
static Item * and_items(Item *cond, Item *item)
Create a AND item of two existing items.
Definition: sql_select.h:917
int refpos_order_cmp(const void *arg, const void *a, const void *b)
Definition: sql_delete.cc:854
bool is_simple_predicate(Item_func *func_item, Item **args, bool *inv_order)
Test if the predicate compares a field with constants.
Definition: opt_sum.cc:625
The LEX object currently serves three different purposes:
Definition: sql_lex.h:3638
Definition: mysql_lex_string.h:39
Definition: table.h:279
A position of table within a join order.
Definition: sql_select.h:347
table_map m_suffix_lateral_deps
The lateral dependendencies of 'table' and all subsequent JOIN_TABs in the join plan.
Definition: sql_select.h:581
uint first_loosescan_table
Definition: sql_select.h:474
uint sjm_scan_last_inner
Definition: sql_select.h:523
float filter_effect
The fraction of the 'rows_fetched' rows that will pass the table conditions that were NOT used by the...
Definition: sql_select.h:411
table_map loosescan_need_tables
Definition: sql_select.h:479
double read_cost
Cost of accessing the table in course of the entire complete join execution, i.e.
Definition: sql_select.h:377
JOIN_TAB * table
Definition: sql_select.h:432
uint first_dupsweedout_table
Definition: sql_select.h:514
table_map sjm_scan_need_tables
Definition: sql_select.h:529
bool use_join_buffer
Definition: sql_select.h:442
nested_join_map cur_embedding_map
Value of Optimize_table_order::cur_embedding_map after this table has been added to the plan.
Definition: sql_select.h:500
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:546
table_map ref_depend_map
If ref-based access is used: bitmap of tables this table depends on
Definition: sql_select.h:441
table_map dupsweedout_tables
Definition: sql_select.h:519
uint sj_strategy
Current optimization state: Semi-join strategy to be used for this and preceding join tables.
Definition: sql_select.h:456
table_map get_suffix_lateral_deps() const
Definition: sql_select.h:574
table_map firstmatch_need_tables
Definition: sql_select.h:510
uint n_sj_tables
Valid only after fix_semijoin_strategies_for_picked_join_order() call: if sj_strategy!...
Definition: sql_select.h:462
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:535
void set_suffix_lateral_deps(table_map deps)
Definition: sql_select.h:572
table_map dups_producing_tables
Bitmap of semi-join inner tables that are in the join prefix and for which there's no provision yet f...
Definition: sql_select.h:469
Key_use * key
NULL - 'index' or 'range' or 'index_merge' or 'ALL' access is used.
Definition: sql_select.h:438
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:560
uint loosescan_parts
Definition: sql_select.h:488
double prefix_cost
Definition: sql_select.h:430
table_map first_firstmatch_rtbl
Definition: sql_select.h:505
uint loosescan_key
Definition: sql_select.h:487
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:366
uint first_firstmatch_table
Definition: sql_select.h:495
double prefix_rowcount
prefix_rowcount and prefix_cost form a stack of partial join order costs and output sizes
Definition: sql_select.h:429
Definition: sql_executor.h:78
Definition: table.h:2612
Definition: sql_opt_exec_shared.h:59
Definition: table.h:1396
Definition: result.h:29
unsigned int uint
Definition: uca-dump.cc:29