MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
opt_range.cc
1 /* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
2 
3  This program is free software; you can redistribute it and/or modify
4  it under the terms of the GNU General Public License as published by
5  the Free Software Foundation; version 2 of the License.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  GNU General Public License for more details.
11 
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software
14  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
15 
16 /*
17  TODO:
18  Fix that MAYBE_KEY are stored in the tree so that we can detect use
19  of full hash keys for queries like:
20 
21  select s.id, kws.keyword_id from sites as s,kws where s.id=kws.site_id and kws.keyword_id in (204,205);
22 
23 */
24 
25 /*
26  This file contains:
27 
28  RangeAnalysisModule
29  A module that accepts a condition, index (or partitioning) description,
30  and builds lists of intervals (in index/partitioning space), such that
31  all possible records that match the condition are contained within the
32  intervals.
33  The entry point for the range analysis module is get_mm_tree()
34  (mm=min_max) function.
35 
36  The lists are returned in form of complicated structure of interlinked
37  SEL_TREE/SEL_IMERGE/SEL_ARG objects.
38  See quick_range_seq_next, find_used_partitions for examples of how to walk
39  this structure.
40  All direct "users" of this module are located within this file, too.
41 
42 
43  PartitionPruningModule
44  A module that accepts a partitioned table, condition, and finds which
45  partitions we will need to use in query execution. Search down for
46  "PartitionPruningModule" for description.
47  The module has single entry point - prune_partitions() function.
48 
49 
50  Range/index_merge/groupby-minmax optimizer module
51  A module that accepts a table, condition, and returns
52  - a QUICK_*_SELECT object that can be used to retrieve rows that match
53  the specified condition, or a "no records will match the condition"
54  statement.
55 
56  The module entry points are
57  test_quick_select()
58  get_quick_select_for_ref()
59 
60 
61  Record retrieval code for range/index_merge/groupby-min-max.
62  Implementations of QUICK_*_SELECT classes.
63 
64  KeyTupleFormat
65  ~~~~~~~~~~~~~~
66  The code in this file (and elsewhere) makes operations on key value tuples.
67  Those tuples are stored in the following format:
68 
69  The tuple is a sequence of key part values. The length of key part value
70  depends only on its type (and not depends on the what value is stored)
71 
72  KeyTuple: keypart1-data, keypart2-data, ...
73 
74  The value of each keypart is stored in the following format:
75 
76  keypart_data: [isnull_byte] keypart-value-bytes
77 
78  If a keypart may have a NULL value (key_part->field->real_maybe_null() can
79  be used to check this), then the first byte is a NULL indicator with the
80  following valid values:
81  1 - keypart has NULL value.
82  0 - keypart has non-NULL value.
83 
84  <questionable-statement> If isnull_byte==1 (NULL value), then the following
85  keypart->length bytes must be 0.
86  </questionable-statement>
87 
88  keypart-value-bytes holds the value. Its format depends on the field type.
89  The length of keypart-value-bytes may or may not depend on the value being
90  stored. The default is that length is static and equal to
91  KEY_PART_INFO::length.
92 
93  Key parts with (key_part_flag & HA_BLOB_PART) have length depending of the
94  value:
95 
96  keypart-value-bytes: value_length value_bytes
97 
98  The value_length part itself occupies HA_KEY_BLOB_LENGTH=2 bytes.
99 
100  See key_copy() and key_restore() for code to move data between index tuple
101  and table record
102 
103  CAUTION: the above description is only sergefp's understanding of the
104  subject and may omit some details.
105 */
106 
107 #include "sql_priv.h"
108 #include "key.h" // is_key_used, key_copy, key_cmp, key_restore
109 #include "sql_parse.h" // check_stack_overrun
110 #include "sql_partition.h" // get_part_id_func, PARTITION_ITERATOR,
111  // struct partition_info, NOT_A_PARTITION_ID
112 #include "sql_base.h" // free_io_cache
113 #include "records.h" // init_read_record, end_read_record
114 #include <m_ctype.h>
115 #include "sql_select.h"
116 #include "opt_trace.h"
117 #include "filesort.h" // filesort_free_buffers
118 #include "sql_optimizer.h" // is_indexed_agg_distinct,field_time_cmp_date
119 
120 using std::min;
121 using std::max;
122 
123 /*
124  Convert double value to #rows. Currently this does floor(), and we
125  might consider using round() instead.
126 */
127 #define double2rows(x) ((ha_rows)(x))
128 
129 static int sel_cmp(Field *f,uchar *a,uchar *b,uint8 a_flag,uint8 b_flag);
130 
131 static uchar is_null_string[2]= {1,0};
132 
133 class RANGE_OPT_PARAM;
134 /*
135  A construction block of the SEL_ARG-graph.
136 
137  The following description only covers graphs of SEL_ARG objects with
138  sel_arg->type==KEY_RANGE:
139 
140  One SEL_ARG object represents an "elementary interval" in form
141 
142  min_value <=? table.keypartX <=? max_value
143 
144  The interval is a non-empty interval of any kind: with[out] minimum/maximum
145  bound, [half]open/closed, single-point interval, etc.
146 
147  1. SEL_ARG GRAPH STRUCTURE
148 
149  SEL_ARG objects are linked together in a graph. The meaning of the graph
150  is better demostrated by an example:
151 
152  tree->keys[i]
153  |
154  | $ $
155  | part=1 $ part=2 $ part=3
156  | $ $
157  | +-------+ $ +-------+ $ +--------+
158  | | kp1<1 |--$-->| kp2=5 |--$-->| kp3=10 |
159  | +-------+ $ +-------+ $ +--------+
160  | | $ $ |
161  | | $ $ +--------+
162  | | $ $ | kp3=12 |
163  | | $ $ +--------+
164  | +-------+ $ $
165  \->| kp1=2 |--$--------------$-+
166  +-------+ $ $ | +--------+
167  | $ $ ==>| kp3=11 |
168  +-------+ $ $ | +--------+
169  | kp1=3 |--$--------------$-+ |
170  +-------+ $ $ +--------+
171  | $ $ | kp3=14 |
172  ... $ $ +--------+
173 
174  The entire graph is partitioned into "interval lists".
175 
176  An interval list is a sequence of ordered disjoint intervals over
177  the same key part. SEL_ARG are linked via "next" and "prev" pointers
178  with NULL as sentinel.
179 
180  In the example pic, there are 4 interval lists:
181  "kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
182  The vertical lines represent SEL_ARG::next/prev pointers.
183 
184  Additionally, all intervals in the list form a red-black (RB) tree,
185  linked via left/right/parent pointers with null_element as sentinel. The
186  red-black tree root SEL_ARG object will be further called "root of the
187  interval list".
188 
189  A red-black tree with 7 SEL_ARGs will look similar to what is shown
190  below. Left/right/parent pointers are shown while next pointers go from a
191  node with number X to the node with number X+1 (and prev in the
192  opposite direction):
193 
194  Root
195  +---+
196  | 4 |
197  +---+
198  left/ \ right
199  __/ \__
200  / \
201  +---+ +---+
202  | 2 | | 6 |
203  +---+ +---+
204  left / \ right left / \ right
205  | | | |
206  +---+ +---+ +---+ +---+
207  | 1 | | 3 | | 5 | | 7 |
208  +---+ +---+ +---+ +---+
209 
210  In this tree,
211  * node1->prev == node7->next == NULL
212  * node1->left == node1->right ==
213  node3->left == ... node7->right == &null_element
214 
215  In an interval list, each member X may have SEL_ARG::next_key_part pointer
216  pointing to the root of another interval list Y. The pointed interval list
217  must cover a key part with greater number (i.e. Y->part > X->part).
218 
219  In the example pic, the next_key_part pointers are represented by
220  horisontal lines.
221 
222  2. SEL_ARG GRAPH SEMANTICS
223 
224  It represents a condition in a special form (we don't have a name for it ATM)
225  The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
226 
227  For example, the picture represents the condition in form:
228  (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR
229  (kp1=2 AND (kp3=11 OR kp3=14)) OR
230  (kp1=3 AND (kp3=11 OR kp3=14))
231 
232  In red-black tree form:
233 
234  +-------+ +--------+
235  | kp1=2 |.................| kp3=14 |
236  +-------+ +--------+
237  / \ /
238  +---------+ +-------+ +--------+
239  | kp1 < 1 | | kp1=3 | | kp3=11 |
240  +---------+ +-------+ +--------+
241  . .
242  ...... .......
243  . .
244  +-------+ +--------+
245  | kp2=5 | | kp3=14 |
246  +-------+ +--------+
247  . /
248  . +--------+
249  (root of R-B tree | kp3=11 |
250  for "kp3={10|12}") +--------+
251 
252 
253  Where / and \ denote left and right pointers and ... denotes
254  next_key_part pointers to the root of the R-B tree of intervals for
255  consecutive key parts.
256 
257  3. SEL_ARG GRAPH USE
258 
259  Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
260  Then walk the SEL_ARG graph and get a list of dijsoint ordered key
261  intervals (i.e. intervals in form
262 
263  (constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)
264 
265  Those intervals can be used to access the index. The uses are in:
266  - check_quick_select() - Walk the SEL_ARG graph and find an estimate of
267  how many table records are contained within all
268  intervals.
269  - get_quick_select() - Walk the SEL_ARG, materialize the key intervals,
270  and create QUICK_RANGE_SELECT object that will
271  read records within these intervals.
272 
273  4. SPACE COMPLEXITY NOTES
274 
275  SEL_ARG graph is a representation of an ordered disjoint sequence of
276  intervals over the ordered set of index tuple values.
277 
278  For multi-part keys, one can construct a WHERE expression such that its
279  list of intervals will be of combinatorial size. Here is an example:
280 
281  (keypart1 IN (1,2, ..., n1)) AND
282  (keypart2 IN (1,2, ..., n2)) AND
283  (keypart3 IN (1,2, ..., n3))
284 
285  For this WHERE clause the list of intervals will have n1*n2*n3 intervals
286  of form
287 
288  (keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
289 
290  SEL_ARG graph structure aims to reduce the amount of required space by
291  "sharing" the elementary intervals when possible (the pic at the
292  beginning of this comment has examples of such sharing). The sharing may
293  prevent combinatorial blowup:
294 
295  There are WHERE clauses that have combinatorial-size interval lists but
296  will be represented by a compact SEL_ARG graph.
297  Example:
298  (keypartN IN (1,2, ..., n1)) AND
299  ...
300  (keypart2 IN (1,2, ..., n2)) AND
301  (keypart1 IN (1,2, ..., n3))
302 
303  but not in all cases:
304 
305  - There are WHERE clauses that do have a compact SEL_ARG-graph
306  representation but get_mm_tree() and its callees will construct a
307  graph of combinatorial size.
308  Example:
309  (keypart1 IN (1,2, ..., n1)) AND
310  (keypart2 IN (1,2, ..., n2)) AND
311  ...
312  (keypartN IN (1,2, ..., n3))
313 
314  - There are WHERE clauses for which the minimal possible SEL_ARG graph
315  representation will have combinatorial size.
316  Example:
317  By induction: Let's take any interval on some keypart in the middle:
318 
319  kp15=c0
320 
321  Then let's AND it with this interval 'structure' from preceding and
322  following keyparts:
323 
324  (kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
325 
326  We will obtain this SEL_ARG graph:
327 
328  kp14 $ kp15 $ kp16
329  $ $
330  +---------+ $ +---------+ $ +---------+
331  | kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
332  +---------+ $ +---------+ $ +---------+
333  | $ $
334  +---------+ $ +---------+ $
335  | kp14=c2 |--$-->| kp15=c0 | $
336  +---------+ $ +---------+ $
337  $ $
338 
339  Note that we had to duplicate "kp15=c0" and there was no way to avoid
340  that.
341  The induction step: AND the obtained expression with another "wrapping"
342  expression like (*).
343  When the process ends because of the limit on max. number of keyparts
344  we'll have:
345 
346  WHERE clause length is O(3*#max_keyparts)
347  SEL_ARG graph size is O(2^(#max_keyparts/2))
348 
349  (it is also possible to construct a case where instead of 2 in 2^n we
350  have a bigger constant, e.g. 4, and get a graph with 4^(31/2)= 2^31
351  nodes)
352 
353  We avoid consuming too much memory by setting a limit on the number of
354  SEL_ARG object we can construct during one range analysis invocation.
355 */
356 
357 class SEL_ARG :public Sql_alloc
358 {
359 public:
360  uint8 min_flag,max_flag,maybe_flag;
361  uint8 part; // Which key part
362  uint8 maybe_null;
363  /*
364  Number of children of this element in the RB-tree, plus 1 for this
365  element itself.
366  */
367  uint16 elements;
368  /*
369  Valid only for elements which are RB-tree roots: Number of times this
370  RB-tree is referred to (it is referred by SEL_ARG::next_key_part or by
371  SEL_TREE::keys[i] or by a temporary SEL_ARG* variable)
372  */
373  ulong use_count;
374 
375  Field *field;
376  uchar *min_value,*max_value; // Pointer to range
377 
378  /*
379  eq_tree(), first(), last() etc require that left == right == NULL
380  if the type is MAYBE_KEY. Todo: fix this so SEL_ARGs without R-B
381  children are handled consistently. See related WL#5894.
382  */
383  SEL_ARG *left,*right; /* R-B tree children */
384  SEL_ARG *next,*prev; /* Links for bi-directional interval list */
385  SEL_ARG *parent; /* R-B tree parent */
386  /*
387  R-B tree root of intervals covering keyparts consecutive to this
388  SEL_ARG. See documentation of SEL_ARG GRAPH semantics for details.
389  */
390  SEL_ARG *next_key_part;
391  enum leaf_color { BLACK,RED } color;
392 
402  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, MAYBE_KEY, KEY_RANGE } type;
403 
404  enum { MAX_SEL_ARGS = 16000 };
405 
406  SEL_ARG() {}
407  SEL_ARG(SEL_ARG &);
408  SEL_ARG(Field *,const uchar *, const uchar *);
409  SEL_ARG(Field *field, uint8 part, uchar *min_value, uchar *max_value,
410  uint8 min_flag, uint8 max_flag, uint8 maybe_flag);
411  /*
412  Used to construct MAYBE_KEY and IMPOSSIBLE SEL_ARGs. left and
413  right is NULL, so this ctor must not be used to create other
414  SEL_ARG types. See todo for left/right pointers.
415  */
416  SEL_ARG(enum Type type_arg)
417  :min_flag(0),elements(1),use_count(1),left(NULL),right(NULL),
418  next_key_part(0), color(BLACK), type(type_arg)
419  {
420  DBUG_ASSERT(type_arg == MAYBE_KEY || type_arg == IMPOSSIBLE);
421  }
422  inline bool is_same(SEL_ARG *arg)
423  {
424  if (type != arg->type || part != arg->part)
425  return 0;
426  if (type != KEY_RANGE)
427  return 1;
428  return cmp_min_to_min(arg) == 0 && cmp_max_to_max(arg) == 0;
429  }
430  inline void merge_flags(SEL_ARG *arg) { maybe_flag|=arg->maybe_flag; }
431  inline void maybe_smaller() { maybe_flag=1; }
432  /* Return true iff it's a single-point null interval */
433  inline bool is_null_interval() { return maybe_null && max_value[0] == 1; }
434  inline int cmp_min_to_min(SEL_ARG* arg)
435  {
436  return sel_cmp(field,min_value, arg->min_value, min_flag, arg->min_flag);
437  }
438  inline int cmp_min_to_max(SEL_ARG* arg)
439  {
440  return sel_cmp(field,min_value, arg->max_value, min_flag, arg->max_flag);
441  }
442  inline int cmp_max_to_max(SEL_ARG* arg)
443  {
444  return sel_cmp(field,max_value, arg->max_value, max_flag, arg->max_flag);
445  }
446  inline int cmp_max_to_min(SEL_ARG* arg)
447  {
448  return sel_cmp(field,max_value, arg->min_value, max_flag, arg->min_flag);
449  }
450  SEL_ARG *clone_and(SEL_ARG* arg)
451  { // Get overlapping range
452  uchar *new_min,*new_max;
453  uint8 flag_min,flag_max;
454  if (cmp_min_to_min(arg) >= 0)
455  {
456  new_min=min_value; flag_min=min_flag;
457  }
458  else
459  {
460  new_min=arg->min_value; flag_min=arg->min_flag; /* purecov: deadcode */
461  }
462  if (cmp_max_to_max(arg) <= 0)
463  {
464  new_max=max_value; flag_max=max_flag;
465  }
466  else
467  {
468  new_max=arg->max_value; flag_max=arg->max_flag;
469  }
470  return new SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
471  test(maybe_flag && arg->maybe_flag));
472  }
473  SEL_ARG *clone_first(SEL_ARG *arg)
474  { // min <= X < arg->min
475  return new SEL_ARG(field,part, min_value, arg->min_value,
476  min_flag, arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX,
477  maybe_flag | arg->maybe_flag);
478  }
479  SEL_ARG *clone_last(SEL_ARG *arg)
480  { // min <= X <= key_max
481  return new SEL_ARG(field, part, min_value, arg->max_value,
482  min_flag, arg->max_flag, maybe_flag | arg->maybe_flag);
483  }
484  SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
485 
486  bool copy_min(SEL_ARG* arg)
487  { // Get overlapping range
488  if (cmp_min_to_min(arg) > 0)
489  {
490  min_value=arg->min_value; min_flag=arg->min_flag;
491  if ((max_flag & NO_MAX_RANGE) && (min_flag & NO_MIN_RANGE))
492  return 1; // Full range
493  }
494  maybe_flag|=arg->maybe_flag;
495  return 0;
496  }
497  bool copy_max(SEL_ARG* arg)
498  { // Get overlapping range
499  if (cmp_max_to_max(arg) <= 0)
500  {
501  max_value=arg->max_value; max_flag=arg->max_flag;
502  if ((max_flag & NO_MAX_RANGE) && (min_flag & NO_MIN_RANGE))
503  return 1; // Full range
504  }
505  maybe_flag|=arg->maybe_flag;
506  return 0;
507  }
508 
509  void copy_min_to_min(SEL_ARG *arg)
510  {
511  min_value=arg->min_value; min_flag=arg->min_flag;
512  }
513  void copy_min_to_max(SEL_ARG *arg)
514  {
515  max_value=arg->min_value;
516  max_flag=arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
517  }
518  void copy_max_to_min(SEL_ARG *arg)
519  {
520  min_value=arg->max_value;
521  min_flag=arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
522  }
523  /* returns a number of keypart values (0 or 1) appended to the key buffer */
524  int store_min(uint length, uchar **min_key,uint min_key_flag)
525  {
526  /* "(kp1 > c1) AND (kp2 OP c2) AND ..." -> (kp1 > c1) */
527  if ((min_flag & GEOM_FLAG) ||
528  (!(min_flag & NO_MIN_RANGE) &&
529  !(min_key_flag & (NO_MIN_RANGE | NEAR_MIN))))
530  {
531  if (maybe_null && *min_value)
532  {
533  **min_key=1;
534  memset(*min_key+1, 0, length-1);
535  }
536  else
537  memcpy(*min_key,min_value,length);
538  (*min_key)+= length;
539  return 1;
540  }
541  return 0;
542  }
543  /* returns a number of keypart values (0 or 1) appended to the key buffer */
544  int store_max(uint length, uchar **max_key, uint max_key_flag)
545  {
546  if (!(max_flag & NO_MAX_RANGE) &&
547  !(max_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
548  {
549  if (maybe_null && *max_value)
550  {
551  **max_key=1;
552  memset(*max_key+1, 0, length-1);
553  }
554  else
555  memcpy(*max_key,max_value,length);
556  (*max_key)+= length;
557  return 1;
558  }
559  return 0;
560  }
561 
562  /*
563  Returns a number of keypart values appended to the key buffer
564  for min key and max key. This function is used by both Range
565  Analysis and Partition pruning. For partition pruning we have
566  to ensure that we don't store also subpartition fields. Thus
567  we have to stop at the last partition part and not step into
568  the subpartition fields. For Range Analysis we set last_part
569  to MAX_KEY which we should never reach.
570  */
571  int store_min_key(KEY_PART *key,
572  uchar **range_key,
573  uint *range_key_flag,
574  uint last_part)
575  {
576  SEL_ARG *key_tree= first();
577  uint res= key_tree->store_min(key[key_tree->part].store_length,
578  range_key, *range_key_flag);
579  *range_key_flag|= key_tree->min_flag;
580 
581  if (key_tree->next_key_part &&
582  key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
583  key_tree->part != last_part &&
584  key_tree->next_key_part->part == key_tree->part+1 &&
585  !(*range_key_flag & (NO_MIN_RANGE | NEAR_MIN)))
586  res+= key_tree->next_key_part->store_min_key(key,
587  range_key,
588  range_key_flag,
589  last_part);
590  return res;
591  }
592 
593  /* returns a number of keypart values appended to the key buffer */
594  int store_max_key(KEY_PART *key,
595  uchar **range_key,
596  uint *range_key_flag,
597  uint last_part)
598  {
599  SEL_ARG *key_tree= last();
600  uint res=key_tree->store_max(key[key_tree->part].store_length,
601  range_key, *range_key_flag);
602  (*range_key_flag)|= key_tree->max_flag;
603  if (key_tree->next_key_part &&
604  key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
605  key_tree->part != last_part &&
606  key_tree->next_key_part->part == key_tree->part+1 &&
607  !(*range_key_flag & (NO_MAX_RANGE | NEAR_MAX)))
608  res+= key_tree->next_key_part->store_max_key(key,
609  range_key,
610  range_key_flag,
611  last_part);
612  return res;
613  }
614 
615  SEL_ARG *insert(SEL_ARG *key);
616  SEL_ARG *tree_delete(SEL_ARG *key);
617  SEL_ARG *find_range(SEL_ARG *key);
618  SEL_ARG *rb_insert(SEL_ARG *leaf);
619  friend SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key, SEL_ARG *par);
620 #ifndef DBUG_OFF
621  friend int test_rb_tree(SEL_ARG *element,SEL_ARG *parent);
622  void test_use_count(SEL_ARG *root);
623 #endif
624  SEL_ARG *first();
625  SEL_ARG *last();
626  void make_root();
627  inline bool simple_key()
628  {
629  return !next_key_part && elements == 1;
630  }
631  void increment_use_count(long count)
632  {
633  if (next_key_part)
634  {
635  next_key_part->use_count+=count;
636  for (SEL_ARG *pos=next_key_part->first(); pos ; pos=pos->next)
637  if (pos->next_key_part)
638  pos->increment_use_count(count);
639  }
640  }
641  void free_tree()
642  {
643  for (SEL_ARG *pos=first(); pos ; pos=pos->next)
644  if (pos->next_key_part)
645  {
646  pos->next_key_part->use_count--;
647  pos->next_key_part->free_tree();
648  }
649  }
650 
651  inline SEL_ARG **parent_ptr()
652  {
653  return parent->left == this ? &parent->left : &parent->right;
654  }
655 
656 
657  /*
658  Check if this SEL_ARG object represents a single-point interval
659 
660  SYNOPSIS
661  is_singlepoint()
662 
663  DESCRIPTION
664  Check if this SEL_ARG object (not tree) represents a single-point
665  interval, i.e. if it represents a "keypart = const" or
666  "keypart IS NULL".
667 
668  RETURN
669  TRUE This SEL_ARG object represents a singlepoint interval
670  FALSE Otherwise
671  */
672 
673  bool is_singlepoint() const
674  {
675  /*
676  Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field)
677  flags, and the same for right edge.
678  */
679  if (min_flag || max_flag)
680  return FALSE;
681  uchar *min_val= min_value;
682  uchar *max_val= max_value;
683 
684  if (maybe_null)
685  {
686  /* First byte is a NULL value indicator */
687  if (*min_val != *max_val)
688  return FALSE;
689 
690  if (*min_val)
691  return TRUE; /* This "x IS NULL" */
692  min_val++;
693  max_val++;
694  }
695  return !field->key_cmp(min_val, max_val);
696  }
697  SEL_ARG *clone_tree(RANGE_OPT_PARAM *param);
698 };
699 
700 class SEL_IMERGE;
701 
702 
703 class SEL_TREE :public Sql_alloc
704 {
705 public:
729  enum Type { IMPOSSIBLE, ALWAYS, MAYBE, KEY, KEY_SMALLER } type;
730  SEL_TREE(enum Type type_arg) :type(type_arg) {}
731  SEL_TREE() :type(KEY)
732  {
733  memset(keys, 0, sizeof(keys));
734  }
735  SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param);
736  /*
737  Possible ways to read rows using a single index because the
738  conditions of the query consists of single-index conjunctions:
739 
740  (ranges_for_idx_1) AND (ranges_for_idx_2) AND ...
741 
742  The SEL_ARG graph for each non-NULL element in keys[] may consist
743  of many single-index ranges (disjunctions), so ranges_for_idx_1
744  may e.g. be:
745 
746  "idx_field1 = 1 OR (idx_field1 > 5 AND idx_field2 = 10)"
747 
748  assuming that index1 is a composite index covering
749  (idx_field1,...,idx_field2,..)
750 
751  Index merge intersection intersects ranges on SEL_ARGs from two or
752  more indexes.
753 
754  Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
755  keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
756  merit in range analyzer functions (e.g. get_mm_parts) returning a
757  pointer to such SEL_TREE instead of NULL)
758  */
759  SEL_ARG *keys[MAX_KEY];
760  key_map keys_map; /* bitmask of non-NULL elements in keys */
761 
762  /*
763  Possible ways to read rows using Index merge (sort) union.
764 
765  Each element in 'merges' consists of multi-index disjunctions,
766  which means that Index merge (sort) union must be applied to read
767  rows. The nodes in the 'merges' list forms a conjunction of such
768  multi-index disjunctions.
769 
770  The list is non-empty only if type==KEY.
771  */
772  List<SEL_IMERGE> merges;
773 
774  /* The members below are filled/used only after get_mm_tree is done */
775  key_map ror_scans_map; /* bitmask of ROR scan-able elements in keys */
776  uint n_ror_scans; /* number of set bits in ror_scans_map */
777 
778  struct st_ror_scan_info **ror_scans; /* list of ROR key scans */
779  struct st_ror_scan_info **ror_scans_end; /* last ROR scan */
780  /* Note that #records for each key scan is stored in table->quick_rows */
781 };
782 
784 {
785 public:
786  THD *thd; /* Current thread handle */
787  TABLE *table; /* Table being analyzed */
788  Item *cond; /* Used inside get_mm_tree(). */
789  table_map prev_tables;
790  table_map read_tables;
791  table_map current_table; /* Bit of the table being analyzed */
792 
793  /* Array of parts of all keys for which range analysis is performed */
794  KEY_PART *key_parts;
795  KEY_PART *key_parts_end;
796  MEM_ROOT *mem_root; /* Memory that will be freed when range analysis completes */
797  MEM_ROOT *old_root; /* Memory that will last until the query end */
798  /*
799  Number of indexes used in range analysis (In SEL_TREE::keys only first
800  #keys elements are not empty)
801  */
802  uint keys;
803 
804  /*
805  If true, the index descriptions describe real indexes (and it is ok to
806  call field->optimize_range(real_keynr[...], ...).
807  Otherwise index description describes fake indexes, like a partitioning
808  expression.
809  */
810  bool using_real_indexes;
811 
812  /*
813  Aggressively remove "scans" that do not have conditions on first
814  keyparts. Such scans are usable when doing partition pruning but not
815  regular range optimization.
816  */
817  bool remove_jump_scans;
818 
819  /*
820  used_key_no -> table_key_no translation table. Only makes sense if
821  using_real_indexes==TRUE
822  */
823  uint real_keynr[MAX_KEY];
824 
825  /*
826  Used to store 'current key tuples', in both range analysis and
827  partitioning (list) analysis
828  */
829  uchar min_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH],
830  max_key[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];
831 
832  /* Number of SEL_ARG objects allocated by SEL_ARG::clone_tree operations */
833  uint alloced_sel_args;
834  bool force_default_mrr;
841 
842  bool statement_should_be_aborted() const
843  {
844  return
845  thd->is_fatal_error ||
846  thd->is_error() ||
847  alloced_sel_args > SEL_ARG::MAX_SEL_ARGS;
848  }
849 
850 };
851 
852 class PARAM : public RANGE_OPT_PARAM
853 {
854 public:
855  KEY_PART *key[MAX_KEY]; /* First key parts of keys used in the query */
856  longlong baseflag;
857  uint max_key_part;
858  /* Number of ranges in the last checked tree->key */
859  uint range_count;
860 
861  bool quick; // Don't calulate possible keys
862 
863  uint fields_bitmap_size;
864  MY_BITMAP needed_fields; /* bitmask of fields needed by the query */
865  MY_BITMAP tmp_covered_fields;
866 
867  key_map *needed_reg; /* ptr to SQL_SELECT::needed_reg */
868 
869  uint *imerge_cost_buff; /* buffer for index_merge cost estimates */
870  uint imerge_cost_buff_size; /* size of the buffer */
871 
872  /* TRUE if last checked tree->key can be used for ROR-scan */
873  bool is_ror_scan;
874  /* Number of ranges in the last checked tree->key */
875  uint n_ranges;
876 
877  /*
878  The sort order the range access method must be able
879  to provide. Three-value logic: asc/desc/don't care
880  */
881  ORDER::enum_order order_direction;
882 };
883 
884 class TABLE_READ_PLAN;
885  class TRP_RANGE;
886  class TRP_ROR_INTERSECT;
887  class TRP_ROR_UNION;
888  class TRP_INDEX_MERGE;
889  class TRP_GROUP_MIN_MAX;
890 
891 struct st_ror_scan_info;
892 
893 static SEL_TREE * get_mm_parts(RANGE_OPT_PARAM *param,
894  Item_func *cond_func,Field *field,
895  Item_func::Functype type,Item *value,
896  Item_result cmp_type);
897 static SEL_ARG *get_mm_leaf(RANGE_OPT_PARAM *param,Item *cond_func,Field *field,
898  KEY_PART *key_part,
899  Item_func::Functype type,Item *value);
900 static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,Item *cond);
901 
902 static bool is_key_scan_ror(PARAM *param, uint keynr, uint nparts);
903 static ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
904  SEL_ARG *tree, bool update_tbl_stats,
905  uint *mrr_flags, uint *bufsize,
906  Cost_estimate *cost);
907 QUICK_RANGE_SELECT *get_quick_select(PARAM *param,uint index,
908  SEL_ARG *key_tree, uint mrr_flags,
909  uint mrr_buf_size, MEM_ROOT *alloc);
910 static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
911  bool index_read_must_be_used,
912  bool update_tbl_stats,
913  double read_time);
914 static
915 TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
916  double read_time);
917 static
918 TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
919  double read_time);
920 static
921 TRP_GROUP_MIN_MAX *get_best_group_min_max(PARAM *param, SEL_TREE *tree,
922  double read_time);
923 #ifndef DBUG_OFF
924 static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
925  const char *msg);
926 static void print_ror_scans_arr(TABLE *table, const char *msg,
927  struct st_ror_scan_info **start,
928  struct st_ror_scan_info **end);
929 static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg);
930 #endif
931 
932 static void append_range_all_keyparts(Opt_trace_array *range_trace,
933  String *range_string,
934  String *range_so_far,
935  SEL_ARG *keypart_root,
936  const KEY_PART_INFO *key_parts);
937 static inline void dbug_print_tree(const char *tree_name,
938  SEL_TREE *tree,
939  const RANGE_OPT_PARAM *param);
940 
941 void append_range(String *out,
942  const KEY_PART_INFO *key_parts,
943  const uchar *min_key, const uchar *max_key,
944  const uint flag);
945 
946 static SEL_TREE *tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
947 static SEL_TREE *tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2);
948 static SEL_ARG *sel_add(SEL_ARG *key1,SEL_ARG *key2);
949 static SEL_ARG *key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2);
950 static SEL_ARG *key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
951  uint clone_flag);
952 static bool get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1);
953 bool get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
954  SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
955  uchar *max_key,uint max_key_flag);
956 static bool eq_tree(SEL_ARG* a,SEL_ARG *b);
957 static bool eq_ranges_exceeds_limit(SEL_ARG *keypart_root, uint* count,
958  uint limit);
959 
960 static SEL_ARG null_element(SEL_ARG::IMPOSSIBLE);
961 static bool null_part_in_key(KEY_PART *key_part, const uchar *key,
962  uint length);
963 bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM* param);
964 
965 
966 /*
967  SEL_IMERGE is a list of possible ways to do index merge, i.e. it is
968  a condition in the following form:
969  (t_1||t_2||...||t_N) && (next)
970 
971  where all t_i are SEL_TREEs, next is another SEL_IMERGE and no pair
972  (t_i,t_j) contains SEL_ARGS for the same index.
973 
974  SEL_TREE contained in SEL_IMERGE always has merges=NULL.
975 
976  This class relies on memory manager to do the cleanup.
977 */
978 
979 class SEL_IMERGE : public Sql_alloc
980 {
981  enum { PREALLOCED_TREES= 10};
982 public:
983  SEL_TREE *trees_prealloced[PREALLOCED_TREES];
984  SEL_TREE **trees; /* trees used to do index_merge */
985  SEL_TREE **trees_next; /* last of these trees */
986  SEL_TREE **trees_end; /* end of allocated space */
987 
988  SEL_ARG ***best_keys; /* best keys to read in SEL_TREEs */
989 
990  SEL_IMERGE() :
991  trees(&trees_prealloced[0]),
992  trees_next(trees),
993  trees_end(trees + PREALLOCED_TREES)
994  {}
995  SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param);
996  int or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree);
997  int or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree);
998  int or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge);
999 };
1000 
1001 
1002 /*
1003  Add SEL_TREE to this index_merge without any checks,
1004 
1005  NOTES
1006  This function implements the following:
1007  (x_1||...||x_N) || t = (x_1||...||x_N||t), where x_i, t are SEL_TREEs
1008 
1009  RETURN
1010  0 - OK
1011  -1 - Out of memory.
1012 */
1013 
1014 int SEL_IMERGE::or_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree)
1015 {
1016  if (trees_next == trees_end)
1017  {
1018  const int realloc_ratio= 2; /* Double size for next round */
1019  uint old_elements= (trees_end - trees);
1020  uint old_size= sizeof(SEL_TREE**) * old_elements;
1021  uint new_size= old_size * realloc_ratio;
1022  SEL_TREE **new_trees;
1023  if (!(new_trees= (SEL_TREE**)alloc_root(param->mem_root, new_size)))
1024  return -1;
1025  memcpy(new_trees, trees, old_size);
1026  trees= new_trees;
1027  trees_next= trees + old_elements;
1028  trees_end= trees + old_elements * realloc_ratio;
1029  }
1030  *(trees_next++)= tree;
1031  return 0;
1032 }
1033 
1034 
1035 /*
1036  Perform OR operation on this SEL_IMERGE and supplied SEL_TREE new_tree,
1037  combining new_tree with one of the trees in this SEL_IMERGE if they both
1038  have SEL_ARGs for the same key.
1039 
1040  SYNOPSIS
1041  or_sel_tree_with_checks()
1042  param PARAM from SQL_SELECT::test_quick_select
1043  new_tree SEL_TREE with type KEY or KEY_SMALLER.
1044 
1045  NOTES
1046  This does the following:
1047  (t_1||...||t_k)||new_tree =
1048  either
1049  = (t_1||...||t_k||new_tree)
1050  or
1051  = (t_1||....||(t_j|| new_tree)||...||t_k),
1052 
1053  where t_i, y are SEL_TREEs.
1054  new_tree is combined with the first t_j it has a SEL_ARG on common
1055  key with. As a consequence of this, choice of keys to do index_merge
1056  read may depend on the order of conditions in WHERE part of the query.
1057 
1058  RETURN
1059  0 OK
1060  1 One of the trees was combined with new_tree to SEL_TREE::ALWAYS,
1061  and (*this) should be discarded.
1062  -1 An error occurred.
1063 */
1064 
1065 int SEL_IMERGE::or_sel_tree_with_checks(RANGE_OPT_PARAM *param, SEL_TREE *new_tree)
1066 {
1067  for (SEL_TREE** tree = trees;
1068  tree != trees_next;
1069  tree++)
1070  {
1071  if (sel_trees_can_be_ored(*tree, new_tree, param))
1072  {
1073  *tree = tree_or(param, *tree, new_tree);
1074  if (!*tree)
1075  return 1;
1076  if (((*tree)->type == SEL_TREE::MAYBE) ||
1077  ((*tree)->type == SEL_TREE::ALWAYS))
1078  return 1;
1079  /* SEL_TREE::IMPOSSIBLE is impossible here */
1080  return 0;
1081  }
1082  }
1083 
1084  /* New tree cannot be combined with any of existing trees. */
1085  return or_sel_tree(param, new_tree);
1086 }
1087 
1088 
1089 /*
1090  Perform OR operation on this index_merge and supplied index_merge list.
1091 
1092  RETURN
1093  0 - OK
1094  1 - One of conditions in result is always TRUE and this SEL_IMERGE
1095  should be discarded.
1096  -1 - An error occurred
1097 */
1098 
1099 int SEL_IMERGE::or_sel_imerge_with_checks(RANGE_OPT_PARAM *param, SEL_IMERGE* imerge)
1100 {
1101  for (SEL_TREE** tree= imerge->trees;
1102  tree != imerge->trees_next;
1103  tree++)
1104  {
1105  if (or_sel_tree_with_checks(param, *tree))
1106  return 1;
1107  }
1108  return 0;
1109 }
1110 
1111 
1112 SEL_TREE::SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param): Sql_alloc()
1113 {
1114  keys_map= arg->keys_map;
1115  type= arg->type;
1116  for (uint idx= 0; idx < MAX_KEY; idx++)
1117  {
1118  if ((keys[idx]= arg->keys[idx]))
1119  {
1120  keys[idx]->use_count++;
1121  keys[idx]->increment_use_count(1);
1122  }
1123  }
1124 
1125  List_iterator<SEL_IMERGE> it(arg->merges);
1126  for (SEL_IMERGE *el= it++; el; el= it++)
1127  {
1128  SEL_IMERGE *merge= new SEL_IMERGE(el, param);
1129  if (!merge || merge->trees == merge->trees_next)
1130  {
1131  merges.empty();
1132  return;
1133  }
1134  merges.push_back (merge);
1135  }
1136 }
1137 
1138 
1139 SEL_IMERGE::SEL_IMERGE (SEL_IMERGE *arg, RANGE_OPT_PARAM *param) : Sql_alloc()
1140 {
1141  uint elements= (arg->trees_end - arg->trees);
1142  if (elements > PREALLOCED_TREES)
1143  {
1144  uint size= elements * sizeof (SEL_TREE **);
1145  if (!(trees= (SEL_TREE **)alloc_root(param->mem_root, size)))
1146  goto mem_err;
1147  }
1148  else
1149  trees= &trees_prealloced[0];
1150 
1151  trees_next= trees;
1152  trees_end= trees + elements;
1153 
1154  for (SEL_TREE **tree = trees, **arg_tree= arg->trees; tree < trees_end;
1155  tree++, arg_tree++)
1156  {
1157  if (!(*tree= new SEL_TREE(*arg_tree, param)))
1158  goto mem_err;
1159  }
1160 
1161  return;
1162 
1163 mem_err:
1164  trees= &trees_prealloced[0];
1165  trees_next= trees;
1166  trees_end= trees;
1167 }
1168 
1169 
1170 /*
1171  Perform AND operation on two index_merge lists and store result in *im1.
1172 */
1173 
1174 inline void imerge_list_and_list(List<SEL_IMERGE> *im1, List<SEL_IMERGE> *im2)
1175 {
1176  im1->concat(im2);
1177 }
1178 
1179 
1180 /*
1181  Perform OR operation on 2 index_merge lists, storing result in first list.
1182 
1183  NOTES
1184  The following conversion is implemented:
1185  (a_1 &&...&& a_N)||(b_1 &&...&& b_K) = AND_i,j(a_i || b_j) =>
1186  => (a_1||b_1).
1187 
1188  i.e. all conjuncts except the first one are currently dropped.
1189  This is done to avoid producing N*K ways to do index_merge.
1190 
1191  If (a_1||b_1) produce a condition that is always TRUE, NULL is returned
1192  and index_merge is discarded (while it is actually possible to try
1193  harder).
1194 
1195  As a consequence of this, choice of keys to do index_merge read may depend
1196  on the order of conditions in WHERE part of the query.
1197 
1198  RETURN
1199  0 OK, result is stored in *im1
1200  other Error, both passed lists are unusable
1201 */
1202 
1203 int imerge_list_or_list(RANGE_OPT_PARAM *param,
1204  List<SEL_IMERGE> *im1,
1205  List<SEL_IMERGE> *im2)
1206 {
1207  SEL_IMERGE *imerge= im1->head();
1208  im1->empty();
1209  im1->push_back(imerge);
1210 
1211  return imerge->or_sel_imerge_with_checks(param, im2->head());
1212 }
1213 
1214 
1215 /*
1216  Perform OR operation on index_merge list and key tree.
1217 
1218  RETURN
1219  false OK, result is stored in *im1.
1220  true Error
1221 */
1222 
1223 static bool imerge_list_or_tree(RANGE_OPT_PARAM *param,
1224  List<SEL_IMERGE> *im1,
1225  SEL_TREE *tree)
1226 {
1227  DBUG_ENTER("imerge_list_or_tree");
1228  SEL_IMERGE *imerge;
1229  List_iterator<SEL_IMERGE> it(*im1);
1230 
1231  uint remaining_trees= im1->elements;
1232  while ((imerge= it++))
1233  {
1234  SEL_TREE *or_tree;
1235  /*
1236  Need to make a copy of 'tree' for all but the last OR operation
1237  because or_sel_tree_with_checks() may change it.
1238  */
1239  if (--remaining_trees == 0)
1240  or_tree= tree;
1241  else
1242  {
1243  or_tree= new SEL_TREE (tree, param);
1244  if (!or_tree)
1245  DBUG_RETURN(true);
1246  if (or_tree->keys_map.is_clear_all() && or_tree->merges.is_empty())
1247  DBUG_RETURN(false);
1248  }
1249 
1250  int result_or= imerge->or_sel_tree_with_checks(param, or_tree);
1251  if (result_or == 1)
1252  it.remove();
1253  else if (result_or == -1)
1254  DBUG_RETURN(true);
1255  }
1256  DBUG_ASSERT(remaining_trees == 0);
1257  DBUG_RETURN(im1->is_empty());
1258 }
1259 
1260 
1261 /***************************************************************************
1262 ** Basic functions for SQL_SELECT and QUICK_RANGE_SELECT
1263 ***************************************************************************/
1264 
1265  /* make a select from mysql info
1266  Error is set as following:
1267  0 = ok
1268  1 = Got some error (out of memory?)
1269  */
1270 
1271 SQL_SELECT *make_select(TABLE *head, table_map const_tables,
1272  table_map read_tables, Item *conds,
1273  bool allow_null_cond,
1274  int *error)
1275 {
1276  SQL_SELECT *select;
1277  DBUG_ENTER("make_select");
1278 
1279  *error=0;
1280 
1281  if (!conds && !allow_null_cond)
1282  DBUG_RETURN(0);
1283  if (!(select= new SQL_SELECT))
1284  {
1285  *error= 1; // out of memory
1286  DBUG_RETURN(0); /* purecov: inspected */
1287  }
1288  select->read_tables=read_tables;
1289  select->const_tables=const_tables;
1290  select->head=head;
1291  select->cond=conds;
1292 
1293  if (head->sort.io_cache)
1294  {
1295  select->file= *head->sort.io_cache;
1296  select->records=(ha_rows) (select->file.end_of_file/
1297  head->file->ref_length);
1298  my_free(head->sort.io_cache);
1299  head->sort.io_cache=0;
1300  }
1301  DBUG_RETURN(select);
1302 }
1303 
1304 
1305 SQL_SELECT::SQL_SELECT() :
1306  quick(0), cond(0), icp_cond(0),
1307  free_cond(0), traced_before(false)
1308 {
1309  my_b_clear(&file);
1310 }
1311 
1312 
1313 void SQL_SELECT::cleanup()
1314 {
1315  set_quick(NULL);
1316  if (free_cond)
1317  {
1318  free_cond=0;
1319  delete cond;
1320  cond= 0;
1321  }
1322  close_cached_file(&file);
1323  traced_before= false;
1324 }
1325 
1326 
1327 SQL_SELECT::~SQL_SELECT()
1328 {
1329  cleanup();
1330 }
1331 
1332 #undef index // Fix for Unixware 7
1333 
1334 QUICK_SELECT_I::QUICK_SELECT_I()
1335  :max_used_key_length(0),
1336  used_key_parts(0)
1337 {}
1338 
1339 QUICK_RANGE_SELECT::QUICK_RANGE_SELECT(THD *thd, TABLE *table, uint key_nr,
1340  bool no_alloc, MEM_ROOT *parent_alloc,
1341  bool *create_error)
1342  :free_file(0), cur_range(NULL), last_range(0),
1343  mrr_flags(0), mrr_buf_size(0), mrr_buf_desc(NULL),
1344  dont_free(0)
1345 {
1346  my_bitmap_map *bitmap;
1347  DBUG_ENTER("QUICK_RANGE_SELECT::QUICK_RANGE_SELECT");
1348 
1349  in_ror_merged_scan= 0;
1350  index= key_nr;
1351  head= table;
1352  key_part_info= head->key_info[index].key_part;
1353  my_init_dynamic_array(&ranges, sizeof(QUICK_RANGE*), 16, 16);
1354 
1355  /* 'thd' is not accessible in QUICK_RANGE_SELECT::reset(). */
1356  mrr_buf_size= thd->variables.read_rnd_buff_size;
1357 
1358  if (!no_alloc && !parent_alloc)
1359  {
1360  // Allocates everything through the internal memroot
1361  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1362  thd->mem_root= &alloc;
1363  }
1364  else
1365  memset(&alloc, 0, sizeof(alloc));
1366  file= head->file;
1367  record= head->record[0];
1368 
1369  /* Allocate a bitmap for used columns (Q: why not on MEM_ROOT?) */
1370  if (!(bitmap= (my_bitmap_map*) my_malloc(head->s->column_bitmap_size,
1371  MYF(MY_WME))))
1372  {
1373  column_bitmap.bitmap= 0;
1374  *create_error= 1;
1375  }
1376  else
1377  bitmap_init(&column_bitmap, bitmap, head->s->fields, FALSE);
1378  DBUG_VOID_RETURN;
1379 }
1380 
1381 
1382 void QUICK_RANGE_SELECT::need_sorted_output()
1383 {
1384  mrr_flags |= HA_MRR_SORTED;
1385 }
1386 
1387 
1388 int QUICK_RANGE_SELECT::init()
1389 {
1390  DBUG_ENTER("QUICK_RANGE_SELECT::init");
1391 
1392  if (file->inited)
1393  file->ha_index_or_rnd_end();
1394  DBUG_RETURN(FALSE);
1395 }
1396 
1397 
1398 void QUICK_RANGE_SELECT::range_end()
1399 {
1400  if (file->inited)
1401  file->ha_index_or_rnd_end();
1402 }
1403 
1404 
1405 QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT()
1406 {
1407  DBUG_ENTER("QUICK_RANGE_SELECT::~QUICK_RANGE_SELECT");
1408  if (!dont_free)
1409  {
1410  /* file is NULL for CPK scan on covering ROR-intersection */
1411  if (file)
1412  {
1413  range_end();
1414  if (free_file)
1415  {
1416  DBUG_PRINT("info", ("Freeing separate handler %p (free: %d)", file,
1417  free_file));
1418  file->ha_external_lock(current_thd, F_UNLCK);
1419  file->ha_close();
1420  delete file;
1421  }
1422  }
1423  delete_dynamic(&ranges); /* ranges are allocated in alloc */
1424  free_root(&alloc,MYF(0));
1425  my_free(column_bitmap.bitmap);
1426  }
1427  my_free(mrr_buf_desc);
1428  DBUG_VOID_RETURN;
1429 }
1430 
1431 
1432 QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT(THD *thd_param,
1433  TABLE *table)
1434  :unique(NULL), pk_quick_select(NULL), thd(thd_param)
1435 {
1436  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::QUICK_INDEX_MERGE_SELECT");
1437  index= MAX_KEY;
1438  head= table;
1439  memset(&read_record, 0, sizeof(read_record));
1440  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1441  DBUG_VOID_RETURN;
1442 }
1443 
1444 int QUICK_INDEX_MERGE_SELECT::init()
1445 {
1446  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::init");
1447  DBUG_RETURN(0);
1448 }
1449 
1450 int QUICK_INDEX_MERGE_SELECT::reset()
1451 {
1452  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::reset");
1453  const int retval= read_keys_and_merge();
1454  DBUG_RETURN(retval);
1455 }
1456 
1457 bool
1458 QUICK_INDEX_MERGE_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick_sel_range)
1459 {
1460  /*
1461  Save quick_select that does scan on clustered primary key as it will be
1462  processed separately.
1463  */
1464  if (head->file->primary_key_is_clustered() &&
1465  quick_sel_range->index == head->s->primary_key)
1466  pk_quick_select= quick_sel_range;
1467  else
1468  return quick_selects.push_back(quick_sel_range);
1469  return 0;
1470 }
1471 
1472 QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT()
1473 {
1474  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
1475  QUICK_RANGE_SELECT* quick;
1476  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::~QUICK_INDEX_MERGE_SELECT");
1477  delete unique;
1478  quick_it.rewind();
1479  while ((quick= quick_it++))
1480  quick->file= NULL;
1481  quick_selects.delete_elements();
1482  delete pk_quick_select;
1483  /* It's ok to call the next two even if they are already deinitialized */
1484  end_read_record(&read_record);
1485  free_io_cache(head);
1486  free_root(&alloc,MYF(0));
1487  DBUG_VOID_RETURN;
1488 }
1489 
1490 
1491 QUICK_ROR_INTERSECT_SELECT::QUICK_ROR_INTERSECT_SELECT(THD *thd_param,
1492  TABLE *table,
1493  bool retrieve_full_rows,
1494  MEM_ROOT *parent_alloc)
1495  : cpk_quick(NULL), thd(thd_param), need_to_fetch_row(retrieve_full_rows),
1496  scans_inited(FALSE)
1497 {
1498  index= MAX_KEY;
1499  head= table;
1500  record= head->record[0];
1501  if (!parent_alloc)
1502  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1503  else
1504  memset(&alloc, 0, sizeof(MEM_ROOT));
1505  last_rowid= (uchar*) alloc_root(parent_alloc? parent_alloc : &alloc,
1506  head->file->ref_length);
1507 }
1508 
1509 
1510 /*
1511  Do post-constructor initialization.
1512  SYNOPSIS
1513  QUICK_ROR_INTERSECT_SELECT::init()
1514 
1515  RETURN
1516  0 OK
1517  other Error code
1518 */
1519 
1520 int QUICK_ROR_INTERSECT_SELECT::init()
1521 {
1522  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init");
1523  /* Check if last_rowid was successfully allocated in ctor */
1524  DBUG_RETURN(!last_rowid);
1525 }
1526 
1527 
1528 /*
1529  Initialize this quick select to be a ROR-merged scan.
1530 
1531  SYNOPSIS
1532  QUICK_RANGE_SELECT::init_ror_merged_scan()
1533  reuse_handler If TRUE, use head->file, otherwise create a separate
1534  handler object
1535 
1536  NOTES
1537  This function creates and prepares for subsequent use a separate handler
1538  object if it can't reuse head->file. The reason for this is that during
1539  ROR-merge several key scans are performed simultaneously, and a single
1540  handler is only capable of preserving context of a single key scan.
1541 
1542  In ROR-merge the quick select doing merge does full records retrieval,
1543  merged quick selects read only keys.
1544 
1545  RETURN
1546  0 ROR child scan initialized, ok to use.
1547  1 error
1548 */
1549 
1550 int QUICK_RANGE_SELECT::init_ror_merged_scan(bool reuse_handler)
1551 {
1552  handler *save_file= file, *org_file;
1553  THD *thd;
1554  MY_BITMAP * const save_read_set= head->read_set;
1555  MY_BITMAP * const save_write_set= head->write_set;
1556  DBUG_ENTER("QUICK_RANGE_SELECT::init_ror_merged_scan");
1557 
1558  in_ror_merged_scan= 1;
1559  if (reuse_handler)
1560  {
1561  DBUG_PRINT("info", ("Reusing handler %p", file));
1562  if (init() || reset())
1563  {
1564  DBUG_RETURN(1);
1565  }
1566  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1567  goto end;
1568  }
1569 
1570  /* Create a separate handler object for this quick select */
1571  if (free_file)
1572  {
1573  /* already have own 'handler' object. */
1574  DBUG_RETURN(0);
1575  }
1576 
1577  thd= head->in_use;
1578  if (!(file= head->file->clone(head->s->normalized_path.str, thd->mem_root)))
1579  {
1580  /*
1581  Manually set the error flag. Note: there seems to be quite a few
1582  places where a failure could cause the server to "hang" the client by
1583  sending no response to a query. ATM those are not real errors because
1584  the storage engine calls in question happen to never fail with the
1585  existing storage engines.
1586  */
1587  my_error(ER_OUT_OF_RESOURCES, MYF(0)); /* purecov: inspected */
1588  /* Caller will free the memory */
1589  goto failure; /* purecov: inspected */
1590  }
1591 
1592  head->column_bitmaps_set(&column_bitmap, &column_bitmap);
1593 
1594  if (file->ha_external_lock(thd, F_RDLCK))
1595  goto failure;
1596 
1597  if (init() || reset())
1598  {
1599  file->ha_external_lock(thd, F_UNLCK);
1600  file->ha_close();
1601  goto failure;
1602  }
1603  free_file= TRUE;
1604  last_rowid= file->ref;
1605 
1606 end:
1607  /*
1608  We are only going to read key fields and call position() on 'file'
1609  The following sets head->tmp_set to only use this key and then updates
1610  head->read_set and head->write_set to use this bitmap.
1611  The now bitmap is stored in 'column_bitmap' which is used in ::get_next()
1612  */
1613  org_file= head->file;
1614  head->file= file;
1615  /* We don't have to set 'head->keyread' here as the 'file' is unique */
1616  if (!head->no_keyread)
1617  head->mark_columns_used_by_index(index);
1618  head->prepare_for_position();
1619  head->file= org_file;
1620  bitmap_copy(&column_bitmap, head->read_set);
1621 
1622  /*
1623  We have prepared a column_bitmap which get_next() will use. To do this we
1624  used TABLE::read_set/write_set as playground; restore them to their
1625  original value to not pollute other scans.
1626  */
1627  head->column_bitmaps_set(save_read_set, save_write_set);
1628 
1629  DBUG_RETURN(0);
1630 
1631 failure:
1632  head->column_bitmaps_set(save_read_set, save_write_set);
1633  delete file;
1634  file= save_file;
1635  DBUG_RETURN(1);
1636 }
1637 
1638 
1639 /*
1640  Initialize this quick select to be a part of a ROR-merged scan.
1641  SYNOPSIS
1642  QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan()
1643  reuse_handler If TRUE, use head->file, otherwise create separate
1644  handler object.
1645  RETURN
1646  0 OK
1647  other error code
1648 */
1649 int QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan(bool reuse_handler)
1650 {
1651  int error;
1652  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
1653  QUICK_RANGE_SELECT* quick;
1654  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::init_ror_merged_scan");
1655 
1656  /* Initialize all merged "children" quick selects */
1657  DBUG_ASSERT(!need_to_fetch_row || reuse_handler);
1658  if (!need_to_fetch_row && reuse_handler)
1659  {
1660  quick= quick_it++;
1661  /*
1662  There is no use of this->file. Use it for the first of merged range
1663  selects.
1664  */
1665  int error= quick->init_ror_merged_scan(TRUE);
1666  if (error)
1667  DBUG_RETURN(error);
1668  quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
1669  }
1670  while ((quick= quick_it++))
1671  {
1672 #ifndef DBUG_OFF
1673  const MY_BITMAP * const save_read_set= quick->head->read_set;
1674  const MY_BITMAP * const save_write_set= quick->head->write_set;
1675 #endif
1676  if ((error= quick->init_ror_merged_scan(FALSE)))
1677  DBUG_RETURN(error);
1678  quick->file->extra(HA_EXTRA_KEYREAD_PRESERVE_FIELDS);
1679  // Sets are shared by all members of "quick_selects" so must not change
1680  DBUG_ASSERT(quick->head->read_set == save_read_set);
1681  DBUG_ASSERT(quick->head->write_set == save_write_set);
1682  /* All merged scans share the same record buffer in intersection. */
1683  quick->record= head->record[0];
1684  }
1685 
1686  /* Prepare for ha_rnd_pos calls if needed. */
1687  if (need_to_fetch_row && (error= head->file->ha_rnd_init(false)))
1688  {
1689  DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
1690  DBUG_RETURN(error);
1691  }
1692  DBUG_RETURN(0);
1693 }
1694 
1695 
1696 /*
1697  Initialize quick select for row retrieval.
1698  SYNOPSIS
1699  reset()
1700  RETURN
1701  0 OK
1702  other Error code
1703 */
1704 
1705 int QUICK_ROR_INTERSECT_SELECT::reset()
1706 {
1707  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::reset");
1708  if (!scans_inited && init_ror_merged_scan(TRUE))
1709  DBUG_RETURN(1);
1710  scans_inited= TRUE;
1711  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
1712  QUICK_RANGE_SELECT *quick;
1713  while ((quick= it++))
1714  quick->reset();
1715  DBUG_RETURN(0);
1716 }
1717 
1718 
1719 /*
1720  Add a merged quick select to this ROR-intersection quick select.
1721 
1722  SYNOPSIS
1723  QUICK_ROR_INTERSECT_SELECT::push_quick_back()
1724  quick Quick select to be added. The quick select must return
1725  rows in rowid order.
1726  NOTES
1727  This call can only be made before init() is called.
1728 
1729  RETURN
1730  FALSE OK
1731  TRUE Out of memory.
1732 */
1733 
1734 bool
1735 QUICK_ROR_INTERSECT_SELECT::push_quick_back(QUICK_RANGE_SELECT *quick)
1736 {
1737  return quick_selects.push_back(quick);
1738 }
1739 
1740 QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT()
1741 {
1742  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::~QUICK_ROR_INTERSECT_SELECT");
1743  quick_selects.delete_elements();
1744  delete cpk_quick;
1745  free_root(&alloc,MYF(0));
1746  if (need_to_fetch_row && head->file->inited)
1747  head->file->ha_rnd_end();
1748  DBUG_VOID_RETURN;
1749 }
1750 
1751 
1752 QUICK_ROR_UNION_SELECT::QUICK_ROR_UNION_SELECT(THD *thd_param,
1753  TABLE *table)
1754  : thd(thd_param), scans_inited(FALSE)
1755 {
1756  index= MAX_KEY;
1757  head= table;
1758  rowid_length= table->file->ref_length;
1759  record= head->record[0];
1760  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
1761  thd_param->mem_root= &alloc;
1762 }
1763 
1764 
1765 /*
1766  Comparison function to be used QUICK_ROR_UNION_SELECT::queue priority
1767  queue.
1768 
1769  SYNPOSIS
1770  QUICK_ROR_UNION_SELECT_queue_cmp()
1771  arg Pointer to QUICK_ROR_UNION_SELECT
1772  val1 First merged select
1773  val2 Second merged select
1774 */
1775 
1776 C_MODE_START
1777 
1778 static int QUICK_ROR_UNION_SELECT_queue_cmp(void *arg, uchar *val1, uchar *val2)
1779 {
1781  return self->head->file->cmp_ref(((QUICK_SELECT_I*)val1)->last_rowid,
1782  ((QUICK_SELECT_I*)val2)->last_rowid);
1783 }
1784 
1785 C_MODE_END
1786 
1787 
1788 /*
1789  Do post-constructor initialization.
1790  SYNOPSIS
1791  QUICK_ROR_UNION_SELECT::init()
1792 
1793  RETURN
1794  0 OK
1795  other Error code
1796 */
1797 
1798 int QUICK_ROR_UNION_SELECT::init()
1799 {
1800  DBUG_ENTER("QUICK_ROR_UNION_SELECT::init");
1801  if (init_queue(&queue, quick_selects.elements, 0,
1802  FALSE , QUICK_ROR_UNION_SELECT_queue_cmp,
1803  (void*) this))
1804  {
1805  memset(&queue, 0, sizeof(QUEUE));
1806  DBUG_RETURN(1);
1807  }
1808 
1809  if (!(cur_rowid= (uchar*) alloc_root(&alloc, 2*head->file->ref_length)))
1810  DBUG_RETURN(1);
1811  prev_rowid= cur_rowid + head->file->ref_length;
1812  DBUG_RETURN(0);
1813 }
1814 
1815 
1816 /*
1817  Initialize quick select for row retrieval.
1818  SYNOPSIS
1819  reset()
1820 
1821  RETURN
1822  0 OK
1823  other Error code
1824 */
1825 
1826 int QUICK_ROR_UNION_SELECT::reset()
1827 {
1828  QUICK_SELECT_I *quick;
1829  int error;
1830  DBUG_ENTER("QUICK_ROR_UNION_SELECT::reset");
1831  have_prev_rowid= FALSE;
1832  if (!scans_inited)
1833  {
1834  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
1835  while ((quick= it++))
1836  {
1837  if (quick->init_ror_merged_scan(FALSE))
1838  DBUG_RETURN(1);
1839  }
1840  scans_inited= TRUE;
1841  }
1842  queue_remove_all(&queue);
1843  /*
1844  Initialize scans for merged quick selects and put all merged quick
1845  selects into the queue.
1846  */
1847  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
1848  while ((quick= it++))
1849  {
1850  if ((error= quick->reset()))
1851  DBUG_RETURN(error);
1852  if ((error= quick->get_next()))
1853  {
1854  if (error == HA_ERR_END_OF_FILE)
1855  continue;
1856  DBUG_RETURN(error);
1857  }
1858  quick->save_last_pos();
1859  queue_insert(&queue, (uchar*)quick);
1860  }
1861 
1862  /* Prepare for ha_rnd_pos calls. */
1863  if (head->file->inited && (error= head->file->ha_rnd_end()))
1864  {
1865  DBUG_PRINT("error", ("ROR index_merge rnd_end call failed"));
1866  DBUG_RETURN(error);
1867  }
1868  if ((error= head->file->ha_rnd_init(false)))
1869  {
1870  DBUG_PRINT("error", ("ROR index_merge rnd_init call failed"));
1871  DBUG_RETURN(error);
1872  }
1873 
1874  DBUG_RETURN(0);
1875 }
1876 
1877 
1878 bool
1879 QUICK_ROR_UNION_SELECT::push_quick_back(QUICK_SELECT_I *quick_sel_range)
1880 {
1881  return quick_selects.push_back(quick_sel_range);
1882 }
1883 
1884 QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT()
1885 {
1886  DBUG_ENTER("QUICK_ROR_UNION_SELECT::~QUICK_ROR_UNION_SELECT");
1887  delete_queue(&queue);
1888  quick_selects.delete_elements();
1889  if (head->file->inited)
1890  head->file->ha_rnd_end();
1891  free_root(&alloc,MYF(0));
1892  DBUG_VOID_RETURN;
1893 }
1894 
1895 
1896 QUICK_RANGE::QUICK_RANGE()
1897  :min_key(0),max_key(0),min_length(0),max_length(0),
1898  flag(NO_MIN_RANGE | NO_MAX_RANGE),
1899  min_keypart_map(0), max_keypart_map(0)
1900 {}
1901 
1902 QUICK_RANGE::QUICK_RANGE(const uchar *min_key_arg, uint min_length_arg,
1903  key_part_map min_keypart_map_arg,
1904  const uchar *max_key_arg, uint max_length_arg,
1905  key_part_map max_keypart_map_arg,
1906  uint flag_arg)
1907  : min_key(NULL),
1908  max_key(NULL),
1909  min_length((uint16) min_length_arg),
1910  max_length((uint16) max_length_arg),
1911  flag((uint16) flag_arg),
1912  min_keypart_map(min_keypart_map_arg),
1913  max_keypart_map(max_keypart_map_arg)
1914 {
1915  min_key= static_cast<uchar*>(sql_memdup(min_key_arg, min_length_arg + 1));
1916  max_key= static_cast<uchar*>(sql_memdup(max_key_arg, max_length_arg + 1));
1917  // If we get is_null_string as argument, the memdup is undefined behavior.
1918  DBUG_ASSERT(min_key_arg != is_null_string);
1919  DBUG_ASSERT(max_key_arg != is_null_string);
1920 }
1921 
1922 SEL_ARG::SEL_ARG(SEL_ARG &arg) :Sql_alloc()
1923 {
1924  DBUG_ASSERT(arg.type != MAYBE_KEY); // Would need left=right=NULL
1925  left=right= &null_element;
1926  prev=next= NULL;
1927  type=arg.type;
1928  min_flag=arg.min_flag;
1929  max_flag=arg.max_flag;
1930  maybe_flag=arg.maybe_flag;
1931  maybe_null=arg.maybe_null;
1932  part=arg.part;
1933  field=arg.field;
1934  min_value=arg.min_value;
1935  max_value=arg.max_value;
1936  next_key_part=arg.next_key_part;
1937  use_count=1; elements=1;
1938 }
1939 
1940 
1941 inline void SEL_ARG::make_root()
1942 {
1943  left=right= &null_element;
1944  color=BLACK;
1945  next=prev= NULL;
1946  use_count=0; elements=1;
1947 }
1948 
1949 SEL_ARG::SEL_ARG(Field *f,const uchar *min_value_arg,
1950  const uchar *max_value_arg)
1951  :min_flag(0), max_flag(0), maybe_flag(0), maybe_null(f->real_maybe_null()),
1952  elements(1), use_count(1), field(f), min_value((uchar*) min_value_arg),
1953  max_value((uchar*) max_value_arg), next(NULL), prev(NULL),
1954  next_key_part(0), color(BLACK), type(KEY_RANGE)
1955 {
1956  left=right= &null_element;
1957 }
1958 
1959 SEL_ARG::SEL_ARG(Field *field_,uint8 part_,
1960  uchar *min_value_, uchar *max_value_,
1961  uint8 min_flag_,uint8 max_flag_,uint8 maybe_flag_)
1962  :min_flag(min_flag_),max_flag(max_flag_),maybe_flag(maybe_flag_),
1963  part(part_),maybe_null(field_->real_maybe_null()), elements(1),use_count(1),
1964  field(field_), min_value(min_value_), max_value(max_value_),
1965  next(NULL), prev(NULL), next_key_part(0), color(BLACK), type(KEY_RANGE)
1966 {
1967  left=right= &null_element;
1968 }
1969 
1970 SEL_ARG *SEL_ARG::clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent,
1971  SEL_ARG **next_arg)
1972 {
1973  SEL_ARG *tmp;
1974 
1975  /* Bail out if we have already generated too many SEL_ARGs */
1976  if (++param->alloced_sel_args > MAX_SEL_ARGS)
1977  return 0;
1978 
1979  if (type != KEY_RANGE)
1980  {
1981  if (!(tmp= new (param->mem_root) SEL_ARG(type)))
1982  return 0; // out of memory
1983  tmp->prev= *next_arg; // Link into next/prev chain
1984  (*next_arg)->next=tmp;
1985  (*next_arg)= tmp;
1986  tmp->part= this->part;
1987  }
1988  else
1989  {
1990  if (!(tmp= new (param->mem_root) SEL_ARG(field,part, min_value,max_value,
1991  min_flag, max_flag, maybe_flag)))
1992  return 0; // OOM
1993  tmp->parent=new_parent;
1994  tmp->next_key_part=next_key_part;
1995  if (left != &null_element)
1996  if (!(tmp->left=left->clone(param, tmp, next_arg)))
1997  return 0; // OOM
1998 
1999  tmp->prev= *next_arg; // Link into next/prev chain
2000  (*next_arg)->next=tmp;
2001  (*next_arg)= tmp;
2002 
2003  if (right != &null_element)
2004  if (!(tmp->right= right->clone(param, tmp, next_arg)))
2005  return 0; // OOM
2006  }
2007  increment_use_count(1);
2008  tmp->color= color;
2009  tmp->elements= this->elements;
2010  return tmp;
2011 }
2012 
2013 SEL_ARG *SEL_ARG::first()
2014 {
2015  SEL_ARG *next_arg=this;
2016  if (!next_arg->left)
2017  return 0; // MAYBE_KEY
2018  while (next_arg->left != &null_element)
2019  next_arg=next_arg->left;
2020  return next_arg;
2021 }
2022 
2023 SEL_ARG *SEL_ARG::last()
2024 {
2025  SEL_ARG *next_arg=this;
2026  if (!next_arg->right)
2027  return 0; // MAYBE_KEY
2028  while (next_arg->right != &null_element)
2029  next_arg=next_arg->right;
2030  return next_arg;
2031 }
2032 
2033 
2034 /*
2035  Check if a compare is ok, when one takes ranges in account
2036  Returns -2 or 2 if the ranges where 'joined' like < 2 and >= 2
2037 */
2038 
2039 static int sel_cmp(Field *field, uchar *a, uchar *b, uint8 a_flag,
2040  uint8 b_flag)
2041 {
2042  int cmp;
2043  /* First check if there was a compare to a min or max element */
2044  if (a_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
2045  {
2046  if ((a_flag & (NO_MIN_RANGE | NO_MAX_RANGE)) ==
2047  (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE)))
2048  return 0;
2049  return (a_flag & NO_MIN_RANGE) ? -1 : 1;
2050  }
2051  if (b_flag & (NO_MIN_RANGE | NO_MAX_RANGE))
2052  return (b_flag & NO_MIN_RANGE) ? 1 : -1;
2053 
2054  if (field->real_maybe_null()) // If null is part of key
2055  {
2056  if (*a != *b)
2057  {
2058  return *a ? -1 : 1;
2059  }
2060  if (*a)
2061  goto end; // NULL where equal
2062  a++; b++; // Skip NULL marker
2063  }
2064  cmp=field->key_cmp(a , b);
2065  if (cmp) return cmp < 0 ? -1 : 1; // The values differed
2066 
2067  // Check if the compared equal arguments was defined with open/closed range
2068  end:
2069  if (a_flag & (NEAR_MIN | NEAR_MAX))
2070  {
2071  if ((a_flag & (NEAR_MIN | NEAR_MAX)) == (b_flag & (NEAR_MIN | NEAR_MAX)))
2072  return 0;
2073  if (!(b_flag & (NEAR_MIN | NEAR_MAX)))
2074  return (a_flag & NEAR_MIN) ? 2 : -2;
2075  return (a_flag & NEAR_MIN) ? 1 : -1;
2076  }
2077  if (b_flag & (NEAR_MIN | NEAR_MAX))
2078  return (b_flag & NEAR_MIN) ? -2 : 2;
2079  return 0; // The elements where equal
2080 }
2081 
2082 
2083 SEL_ARG *SEL_ARG::clone_tree(RANGE_OPT_PARAM *param)
2084 {
2085  SEL_ARG tmp_link,*next_arg,*root;
2086  next_arg= &tmp_link;
2087  if (!(root= clone(param, (SEL_ARG *) 0, &next_arg)))
2088  return 0;
2089  next_arg->next=0; // Fix last link
2090  tmp_link.next->prev=0; // Fix first link
2091  if (root) // If not OOM
2092  root->use_count= 0;
2093  return root;
2094 }
2095 
2096 
2097 /*
2098  Table rows retrieval plan. Range optimizer creates QUICK_SELECT_I-derived
2099  objects from table read plans.
2100 */
2102 {
2103 public:
2104  /*
2105  Plan read cost, with or without cost of full row retrieval, depending
2106  on plan creation parameters.
2107  */
2108  double read_cost;
2109  ha_rows records; /* estimate of #rows to be examined */
2110 
2111  /*
2112  If TRUE, the scan returns rows in rowid order. This is used only for
2113  scans that can be both ROR and non-ROR.
2114  */
2115  bool is_ror;
2116 
2117  /*
2118  Create quick select for this plan.
2119  SYNOPSIS
2120  make_quick()
2121  param Parameter from test_quick_select
2122  retrieve_full_rows If TRUE, created quick select will do full record
2123  retrieval.
2124  parent_alloc Memory pool to use, if any.
2125 
2126  NOTES
2127  retrieve_full_rows is ignored by some implementations.
2128 
2129  RETURN
2130  created quick select
2131  NULL on any error.
2132  */
2133  virtual QUICK_SELECT_I *make_quick(PARAM *param,
2134  bool retrieve_full_rows,
2135  MEM_ROOT *parent_alloc=NULL) = 0;
2136 
2137  /* Table read plans are allocated on MEM_ROOT and are never deleted */
2138  static void *operator new(size_t size, MEM_ROOT *mem_root)
2139  { return (void*) alloc_root(mem_root, (uint) size); }
2140  static void operator delete(void *ptr,size_t size) { TRASH(ptr, size); }
2141  static void operator delete(void *ptr, MEM_ROOT *mem_root) { /* Never called */ }
2142  virtual ~TABLE_READ_PLAN() {} /* Remove gcc warning */
2143 
2150  virtual void trace_basic_info(const PARAM *param,
2151  Opt_trace_object *trace_object) const = 0;
2152 };
2153 
2154 /*
2155  Plan for a QUICK_RANGE_SELECT scan.
2156  TRP_RANGE::make_quick ignores retrieve_full_rows parameter because
2157  QUICK_RANGE_SELECT doesn't distinguish between 'index only' scans and full
2158  record retrieval scans.
2159 */
2160 
2162 {
2163 public:
2169  uint key_idx; /* key number in PARAM::key and PARAM::real_keynr*/
2170  uint mrr_flags;
2171  uint mrr_buf_size;
2172 
2173  TRP_RANGE(SEL_ARG *key_arg, uint idx_arg, uint mrr_flags_arg)
2174  : key(key_arg), key_idx(idx_arg), mrr_flags(mrr_flags_arg)
2175  {}
2176  virtual ~TRP_RANGE() {} /* Remove gcc warning */
2177 
2178  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2179  MEM_ROOT *parent_alloc)
2180  {
2181  DBUG_ENTER("TRP_RANGE::make_quick");
2182  QUICK_RANGE_SELECT *quick;
2183  if ((quick= get_quick_select(param, key_idx, key, mrr_flags, mrr_buf_size,
2184  parent_alloc)))
2185  {
2186  quick->records= records;
2187  quick->read_time= read_cost;
2188  }
2189  DBUG_RETURN(quick);
2190  }
2191 
2192  void trace_basic_info(const PARAM *param,
2193  Opt_trace_object *trace_object) const;
2194 };
2195 
2197  Opt_trace_object *trace_object) const
2198 {
2199 #ifdef OPTIMIZER_TRACE
2200  DBUG_ASSERT(param->using_real_indexes);
2201  const uint keynr_in_table= param->real_keynr[key_idx];
2202 
2203  const KEY &cur_key= param->table->key_info[keynr_in_table];
2204  const KEY_PART_INFO *key_part= cur_key.key_part;
2205 
2206  trace_object->add_alnum("type", "range_scan").
2207  add_utf8("index", cur_key.name).add("rows", records);
2208 
2209  Opt_trace_array trace_range(&param->thd->opt_trace, "ranges");
2210 
2211  // TRP_RANGE should not be created if there are no range intervals
2212  DBUG_ASSERT(key);
2213 
2214  String range_info;
2215  range_info.set_charset(system_charset_info);
2216  append_range_all_keyparts(&trace_range, NULL, &range_info, key, key_part);
2217 
2218 #endif
2219 }
2220 
2221 
2222 typedef struct st_ror_scan_info
2223 {
2224  uint idx;
2225  uint keynr;
2226  ha_rows records;
2227 
2230 
2241 
2247 } ROR_SCAN_INFO;
2248 
2249 /* Plan for QUICK_ROR_INTERSECT_SELECT scan. */
2250 
2252 {
2253 public:
2254  TRP_ROR_INTERSECT() {} /* Remove gcc warning */
2255  virtual ~TRP_ROR_INTERSECT() {} /* Remove gcc warning */
2256  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2257  MEM_ROOT *parent_alloc);
2258 
2259  /* Array of pointers to ROR range scans used in this intersection */
2260  struct st_ror_scan_info **first_scan;
2261  struct st_ror_scan_info **last_scan; /* End of the above array */
2262  struct st_ror_scan_info *cpk_scan; /* Clustered PK scan, if there is one */
2263  bool is_covering; /* TRUE if no row retrieval phase is necessary */
2264  double index_scan_costs; /* SUM(cost(index_scan)) */
2265 
2266  void trace_basic_info(const PARAM *param,
2267  Opt_trace_object *trace_object) const;
2268 };
2269 
2271  Opt_trace_object *trace_object) const
2272 {
2273 #ifdef OPTIMIZER_TRACE
2274  trace_object->add_alnum("type", "index_roworder_intersect").
2275  add("rows", records).
2276  add("cost", read_cost).
2277  add("covering", is_covering).
2278  add("clustered_pk_scan", cpk_scan != NULL);
2279 
2280  Opt_trace_context * const trace= &param->thd->opt_trace;
2281  Opt_trace_array ota(trace, "intersect_of");
2282  for (st_ror_scan_info **cur_scan= first_scan;
2283  cur_scan != last_scan;
2284  cur_scan++)
2285  {
2286  const KEY &cur_key= param->table->key_info[(*cur_scan)->keynr];
2287  const KEY_PART_INFO *key_part= cur_key.key_part;
2288 
2289  Opt_trace_object trace_isect_idx(trace);
2290  trace_isect_idx.add_alnum("type", "range_scan").
2291  add_utf8("index", cur_key.name).add("rows", (*cur_scan)->records);
2292 
2293  Opt_trace_array trace_range(trace, "ranges");
2294  for (const SEL_ARG *current= (*cur_scan)->sel_arg;
2295  current;
2296  current= current->next)
2297  {
2298  String range_info;
2299  range_info.set_charset(system_charset_info);
2300  for (const SEL_ARG *part= current;
2301  part;
2302  part= part->next_key_part)
2303  {
2304  const KEY_PART_INFO *cur_key_part= key_part + part->part;
2305  append_range(&range_info, cur_key_part,
2306  part->min_value, part->max_value,
2307  part->min_flag | part->max_flag);
2308  }
2309  trace_range.add_utf8(range_info.ptr(), range_info.length());
2310  }
2311  }
2312 #endif
2313 }
2314 
2315 /*
2316  Plan for QUICK_ROR_UNION_SELECT scan.
2317  QUICK_ROR_UNION_SELECT always retrieves full rows, so retrieve_full_rows
2318  is ignored by make_quick.
2319 */
2320 
2322 {
2323 public:
2324  TRP_ROR_UNION() {} /* Remove gcc warning */
2325  virtual ~TRP_ROR_UNION() {} /* Remove gcc warning */
2326  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2327  MEM_ROOT *parent_alloc);
2328  TABLE_READ_PLAN **first_ror; /* array of ptrs to plans for merged scans */
2329  TABLE_READ_PLAN **last_ror; /* end of the above array */
2330 
2331  void trace_basic_info(const PARAM *param,
2332  Opt_trace_object *trace_object) const;
2333 };
2334 
2336  Opt_trace_object *trace_object) const
2337 {
2338 #ifdef OPTIMIZER_TRACE
2339  Opt_trace_context * const trace= &param->thd->opt_trace;
2340  trace_object->add_alnum("type", "index_roworder_union");
2341  Opt_trace_array ota(trace, "union_of");
2342  for (TABLE_READ_PLAN **current= first_ror;
2343  current != last_ror;
2344  current++)
2345  {
2346  Opt_trace_object trp_info(trace);
2347  (*current)->trace_basic_info(param, &trp_info);
2348  }
2349 #endif
2350 }
2351 
2352 /*
2353  Plan for QUICK_INDEX_MERGE_SELECT scan.
2354  QUICK_ROR_INTERSECT_SELECT always retrieves full rows, so retrieve_full_rows
2355  is ignored by make_quick.
2356 */
2357 
2359 {
2360 public:
2361  TRP_INDEX_MERGE() {} /* Remove gcc warning */
2362  virtual ~TRP_INDEX_MERGE() {} /* Remove gcc warning */
2363  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2364  MEM_ROOT *parent_alloc);
2365  TRP_RANGE **range_scans; /* array of ptrs to plans of merged scans */
2366  TRP_RANGE **range_scans_end; /* end of the array */
2367 
2368  void trace_basic_info(const PARAM *param,
2369  Opt_trace_object *trace_object) const;
2370 };
2371 
2373  Opt_trace_object *trace_object) const
2374 {
2375 #ifdef OPTIMIZER_TRACE
2376  Opt_trace_context * const trace= &param->thd->opt_trace;
2377  trace_object->add_alnum("type", "index_merge");
2378  Opt_trace_array ota(trace, "index_merge_of");
2379  for (TRP_RANGE **current= range_scans;
2380  current != range_scans_end;
2381  current++)
2382  {
2383  Opt_trace_object trp_info(trace);
2384  (*current)->trace_basic_info(param, &trp_info);
2385  }
2386 #endif
2387 }
2388 
2389 /*
2390  Plan for a QUICK_GROUP_MIN_MAX_SELECT scan.
2391 */
2392 
2394 {
2395 private:
2396  bool have_min;
2397  bool have_max;
2398 
2402  bool have_agg_distinct;
2408  KEY_PART_INFO *min_max_arg_part;
2409  uint group_prefix_len;
2410  uint used_key_parts;
2411  uint group_key_parts;
2412  KEY *index_info;
2413  uint index;
2414  uchar key_infix[MAX_KEY_LENGTH];
2415  uint key_infix_len;
2416  SEL_TREE *range_tree;
2417  SEL_ARG *index_tree;
2418  uint param_idx;
2419  bool is_index_scan;
2420 public:
2423 public:
2424 
2425  void trace_basic_info(const PARAM *param,
2426  Opt_trace_object *trace_object) const;
2427 
2428  TRP_GROUP_MIN_MAX(bool have_min_arg, bool have_max_arg,
2429  bool have_agg_distinct_arg,
2430  KEY_PART_INFO *min_max_arg_part_arg,
2431  uint group_prefix_len_arg, uint used_key_parts_arg,
2432  uint group_key_parts_arg, KEY *index_info_arg,
2433  uint index_arg, uint key_infix_len_arg,
2434  uchar *key_infix_arg,
2435  SEL_TREE *tree_arg, SEL_ARG *index_tree_arg,
2436  uint param_idx_arg, ha_rows quick_prefix_records_arg)
2437  : have_min(have_min_arg), have_max(have_max_arg),
2438  have_agg_distinct(have_agg_distinct_arg),
2439  min_max_arg_part(min_max_arg_part_arg),
2440  group_prefix_len(group_prefix_len_arg), used_key_parts(used_key_parts_arg),
2441  group_key_parts(group_key_parts_arg), index_info(index_info_arg),
2442  index(index_arg), key_infix_len(key_infix_len_arg), range_tree(tree_arg),
2443  index_tree(index_tree_arg), param_idx(param_idx_arg), is_index_scan(FALSE),
2444  quick_prefix_records(quick_prefix_records_arg)
2445  {
2446  if (key_infix_len)
2447  memcpy(this->key_infix, key_infix_arg, key_infix_len);
2448  }
2449  virtual ~TRP_GROUP_MIN_MAX() {} /* Remove gcc warning */
2450 
2451  QUICK_SELECT_I *make_quick(PARAM *param, bool retrieve_full_rows,
2452  MEM_ROOT *parent_alloc);
2453  void use_index_scan() { is_index_scan= TRUE; }
2454 };
2455 
2457  Opt_trace_object *trace_object) const
2458 {
2459 #ifdef OPTIMIZER_TRACE
2460  trace_object->add_alnum("type", "index_group").
2461  add_utf8("index", index_info->name);
2462  if (min_max_arg_part)
2463  trace_object->add_utf8("group_attribute",
2464  min_max_arg_part->field->field_name);
2465  else
2466  trace_object->add_null("group_attribute");
2467  trace_object->add("min_aggregate", have_min).
2468  add("max_aggregate", have_max).
2469  add("distinct_aggregate", have_agg_distinct).
2470  add("rows", records).
2471  add("cost", read_cost);
2472 
2473  const KEY_PART_INFO *key_part= index_info->key_part;
2474  Opt_trace_context * const trace= &param->thd->opt_trace;
2475  {
2476  Opt_trace_array trace_keyparts(trace, "key_parts_used_for_access");
2477  for (uint partno= 0; partno < used_key_parts; partno++)
2478  {
2479  const KEY_PART_INFO *cur_key_part= key_part + partno;
2480  trace_keyparts.add_utf8(cur_key_part->field->field_name);
2481  }
2482  }
2483  Opt_trace_array trace_range(trace, "ranges");
2484 
2485  // can have group quick without ranges
2486  if (index_tree)
2487  {
2488  String range_info;
2489  range_info.set_charset(system_charset_info);
2490  append_range_all_keyparts(&trace_range, NULL,
2491  &range_info, index_tree, key_part);
2492  }
2493 #endif
2494 }
2495 
2496 /*
2497  Fill param->needed_fields with bitmap of fields used in the query.
2498  SYNOPSIS
2499  fill_used_fields_bitmap()
2500  param Parameter from test_quick_select function.
2501 
2502  NOTES
2503  Clustered PK members are not put into the bitmap as they are implicitly
2504  present in all keys (and it is impossible to avoid reading them).
2505  RETURN
2506  0 Ok
2507  1 Out of memory.
2508 */
2509 
2510 static int fill_used_fields_bitmap(PARAM *param)
2511 {
2512  TABLE *table= param->table;
2513  my_bitmap_map *tmp;
2514  uint pk;
2515  param->tmp_covered_fields.bitmap= 0;
2516  param->fields_bitmap_size= table->s->column_bitmap_size;
2517  if (!(tmp= (my_bitmap_map*) alloc_root(param->mem_root,
2518  param->fields_bitmap_size)) ||
2519  bitmap_init(&param->needed_fields, tmp, table->s->fields, FALSE))
2520  return 1;
2521 
2522  bitmap_copy(&param->needed_fields, table->read_set);
2523  bitmap_union(&param->needed_fields, table->write_set);
2524 
2525  pk= param->table->s->primary_key;
2526  if (pk != MAX_KEY && param->table->file->primary_key_is_clustered())
2527  {
2528  /* The table uses clustered PK and it is not internally generated */
2529  KEY_PART_INFO *key_part= param->table->key_info[pk].key_part;
2530  KEY_PART_INFO *key_part_end=
2531  key_part + param->table->key_info[pk].user_defined_key_parts;
2532  for (;key_part != key_part_end; ++key_part)
2533  bitmap_clear_bit(&param->needed_fields, key_part->fieldnr-1);
2534  }
2535  return 0;
2536 }
2537 
2538 
2539 /*
2540  Test if a key can be used in different ranges
2541 
2542  SYNOPSIS
2543  SQL_SELECT::test_quick_select()
2544  thd Current thread
2545  keys_to_use Keys to use for range retrieval
2546  prev_tables Tables assumed to be already read when the scan is
2547  performed (but not read at the moment of this call)
2548  limit Query limit
2549  force_quick_range Prefer to use range (instead of full table scan) even
2550  if it is more expensive.
2551  interesting_order The sort order the range access method must be able
2552  to provide. Three-value logic: asc/desc/don't care
2553 
2554  NOTES
2555  Updates the following in the select parameter:
2556  needed_reg - Bits for keys with may be used if all prev regs are read
2557  quick - Parameter to use when reading records.
2558 
2559  In the table struct the following information is updated:
2560  quick_keys - Which keys can be used
2561  quick_rows - How many rows the key matches
2562  quick_condition_rows - E(# rows that will satisfy the table condition)
2563 
2564  IMPLEMENTATION
2565  quick_condition_rows value is obtained as follows:
2566 
2567  It is a minimum of E(#output rows) for all considered table access
2568  methods (range and index_merge accesses over various indexes).
2569 
2570  The obtained value is not a true E(#rows that satisfy table condition)
2571  but rather a pessimistic estimate. To obtain a true E(#...) one would
2572  need to combine estimates of various access methods, taking into account
2573  correlations between sets of rows they will return.
2574 
2575  For example, if values of tbl.key1 and tbl.key2 are independent (a right
2576  assumption if we have no information about their correlation) then the
2577  correct estimate will be:
2578 
2579  E(#rows("tbl.key1 < c1 AND tbl.key2 < c2")) =
2580  = E(#rows(tbl.key1 < c1)) / total_rows(tbl) * E(#rows(tbl.key2 < c2)
2581 
2582  which is smaller than
2583 
2584  MIN(E(#rows(tbl.key1 < c1), E(#rows(tbl.key2 < c2)))
2585 
2586  which is currently produced.
2587 
2588  TODO
2589  * Change the value returned in quick_condition_rows from a pessimistic
2590  estimate to true E(#rows that satisfy table condition).
2591  (we can re-use some of E(#rows) calcuation code from index_merge/intersection
2592  for this)
2593 
2594  * Check if this function really needs to modify keys_to_use, and change the
2595  code to pass it by reference if it doesn't.
2596 
2597  * In addition to force_quick_range other means can be (an usually are) used
2598  to make this function prefer range over full table scan. Figure out if
2599  force_quick_range is really needed.
2600 
2601  RETURN
2602  -1 if impossible select (i.e. certainly no rows will be selected)
2603  0 if can't use quick_select
2604  1 if found usable ranges and quick select has been successfully created.
2605 */
2606 
2607 int SQL_SELECT::test_quick_select(THD *thd, key_map keys_to_use,
2608  table_map prev_tables,
2609  ha_rows limit, bool force_quick_range,
2610  const ORDER::enum_order interesting_order)
2611 {
2612  uint idx;
2613  double scan_time;
2614  DBUG_ENTER("SQL_SELECT::test_quick_select");
2615  DBUG_PRINT("enter",("keys_to_use: %lu prev_tables: %lu const_tables: %lu",
2616  (ulong) keys_to_use.to_ulonglong(), (ulong) prev_tables,
2617  (ulong) const_tables));
2618 
2619  set_quick(NULL);
2620  needed_reg.clear_all();
2621  quick_keys.clear_all();
2622  if (keys_to_use.is_clear_all())
2623  DBUG_RETURN(0);
2624  records= head->file->stats.records;
2625  if (!records)
2626  records++; /* purecov: inspected */
2627  scan_time= records * ROW_EVALUATE_COST + 1;
2628  read_time= head->file->scan_time() + scan_time + 1.1;
2629  if (head->force_index)
2630  scan_time= read_time= DBL_MAX;
2631  if (limit < records)
2632  read_time= (double) records + scan_time + 1; // Force to use index
2633  else if (read_time <= 2.0 && !force_quick_range)
2634  DBUG_RETURN(0); /* No need for quick select */
2635 
2636  Opt_trace_context * const trace= &thd->opt_trace;
2637  Opt_trace_object trace_range(trace, "range_analysis");
2638  Opt_trace_object(trace, "table_scan").
2639  add("rows", head->file->stats.records).
2640  add("cost", read_time);
2641 
2642  keys_to_use.intersect(head->keys_in_use_for_query);
2643  if (!keys_to_use.is_clear_all())
2644  {
2645  MEM_ROOT alloc;
2646  SEL_TREE *tree= NULL;
2647  KEY_PART *key_parts;
2648  KEY *key_info;
2649  PARAM param;
2650 
2651  /*
2652  Use the 3 multiplier as range optimizer allocates big PARAM structure
2653  and may evaluate a subquery expression
2654  TODO During the optimization phase we should evaluate only inexpensive
2655  single-lookup subqueries.
2656  */
2657  if (check_stack_overrun(thd, 3*STACK_MIN_SIZE + sizeof(PARAM), NULL))
2658  DBUG_RETURN(0); // Fatal error flag is set
2659 
2660  /* set up parameter that is passed to all functions */
2661  param.thd= thd;
2662  param.baseflag= head->file->ha_table_flags();
2663  param.prev_tables=prev_tables | const_tables;
2664  param.read_tables=read_tables;
2665  param.current_table= head->map;
2666  param.table=head;
2667  param.keys=0;
2668  param.mem_root= &alloc;
2669  param.old_root= thd->mem_root;
2670  param.needed_reg= &needed_reg;
2671  param.imerge_cost_buff_size= 0;
2672  param.using_real_indexes= TRUE;
2673  param.remove_jump_scans= TRUE;
2674  param.force_default_mrr= (interesting_order == ORDER::ORDER_DESC);
2675  param.order_direction= interesting_order;
2676  param.use_index_statistics= false;
2677 
2678  thd->no_errors=1; // Don't warn about NULL
2679  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
2680  if (!(param.key_parts= (KEY_PART*) alloc_root(&alloc,
2681  sizeof(KEY_PART)*
2682  head->s->key_parts)) ||
2683  fill_used_fields_bitmap(&param))
2684  {
2685  thd->no_errors=0;
2686  free_root(&alloc,MYF(0)); // Return memory & allocator
2687  DBUG_RETURN(0); // Can't use range
2688  }
2689  key_parts= param.key_parts;
2690  thd->mem_root= &alloc;
2691 
2692  {
2693  Opt_trace_array trace_idx(trace,
2694  "potential_range_indices",
2695  Opt_trace_context::RANGE_OPTIMIZER);
2696  /*
2697  Make an array with description of all key parts of all table keys.
2698  This is used in get_mm_parts function.
2699  */
2700  key_info= head->key_info;
2701  for (idx=0 ; idx < head->s->keys ; idx++, key_info++)
2702  {
2703  Opt_trace_object trace_idx_details(trace);
2704  trace_idx_details.add_utf8("index", key_info->name);
2705  KEY_PART_INFO *key_part_info;
2706  if (!keys_to_use.is_set(idx))
2707  {
2708  trace_idx_details.add("usable", false).
2709  add_alnum("cause", "not_applicable");
2710  continue;
2711  }
2712  if (key_info->flags & HA_FULLTEXT)
2713  {
2714  trace_idx_details.add("usable", false).
2715  add_alnum("cause", "fulltext");
2716  continue; // ToDo: ft-keys in non-ft ranges, if possible SerG
2717  }
2718 
2719  trace_idx_details.add("usable", true);
2720 
2721  param.key[param.keys]=key_parts;
2722  key_part_info= key_info->key_part;
2723  Opt_trace_array trace_keypart(trace, "key_parts");
2724  for (uint part=0 ; part < actual_key_parts(key_info) ;
2725  part++, key_parts++, key_part_info++)
2726  {
2727  key_parts->key= param.keys;
2728  key_parts->part= part;
2729  key_parts->length= key_part_info->length;
2730  key_parts->store_length= key_part_info->store_length;
2731  key_parts->field= key_part_info->field;
2732  key_parts->null_bit= key_part_info->null_bit;
2733  key_parts->image_type =
2734  (key_info->flags & HA_SPATIAL) ? Field::itMBR : Field::itRAW;
2735  /* Only HA_PART_KEY_SEG is used */
2736  key_parts->flag= (uint8) key_part_info->key_part_flag;
2737  trace_keypart.add_utf8(key_parts->field->field_name);
2738  }
2739  param.real_keynr[param.keys++]=idx;
2740  }
2741  }
2742  param.key_parts_end=key_parts;
2743  param.alloced_sel_args= 0;
2744 
2745  /* Calculate cost of full index read for the shortest covering index */
2746  if (!head->covering_keys.is_clear_all())
2747  {
2748  int key_for_use= find_shortest_key(head, &head->covering_keys);
2749  double key_read_time=
2750  param.table->file->index_only_read_time(key_for_use,
2751  rows2double(records)) +
2752  records * ROW_EVALUATE_COST;
2753 
2754  bool chosen= false;
2755  if (key_read_time < read_time)
2756  {
2757  read_time= key_read_time;
2758  chosen= true;
2759  }
2760 
2761  Opt_trace_object trace_cov(trace,
2762  "best_covering_index_scan",
2763  Opt_trace_context::RANGE_OPTIMIZER);
2764  trace_cov.add_utf8("index", head->key_info[key_for_use].name).
2765  add("cost", key_read_time).add("chosen", chosen);
2766  if (!chosen)
2767  trace_cov.add_alnum("cause", "cost");
2768  }
2769 
2770  TABLE_READ_PLAN *best_trp= NULL;
2771  TRP_GROUP_MIN_MAX *group_trp;
2772  double best_read_time= read_time;
2773 
2774  if (cond)
2775  {
2776  {
2777  Opt_trace_array trace_setup_cond(trace, "setup_range_conditions");
2778  tree= get_mm_tree(&param,cond);
2779  }
2780  if (tree)
2781  {
2782  if (tree->type == SEL_TREE::IMPOSSIBLE)
2783  {
2784  trace_range.add("impossible_range", true);
2785  records=0L; /* Return -1 from this function. */
2786  read_time= (double) HA_POS_ERROR;
2787  goto free_mem;
2788  }
2789  /*
2790  If the tree can't be used for range scans, proceed anyway, as we
2791  can construct a group-min-max quick select
2792  */
2793  if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
2794  {
2795  trace_range.add("range_scan_possible", false);
2796  if (tree->type == SEL_TREE::ALWAYS)
2797  trace_range.add_alnum("cause", "condition_always_true");
2798 
2799  tree= NULL;
2800  }
2801  }
2802  }
2803 
2804  /*
2805  Try to construct a QUICK_GROUP_MIN_MAX_SELECT.
2806  Notice that it can be constructed no matter if there is a range tree.
2807  */
2808  group_trp= get_best_group_min_max(&param, tree, best_read_time);
2809  if (group_trp)
2810  {
2811  param.table->quick_condition_rows= min(group_trp->records,
2812  head->file->stats.records);
2813  Opt_trace_object grp_summary(trace,
2814  "best_group_range_summary",
2815  Opt_trace_context::RANGE_OPTIMIZER);
2816  if (unlikely(trace->is_started()))
2817  group_trp->trace_basic_info(&param, &grp_summary);
2818  if (group_trp->read_cost < best_read_time)
2819  {
2820  grp_summary.add("chosen", true);
2821  best_trp= group_trp;
2822  best_read_time= best_trp->read_cost;
2823  }
2824  else
2825  grp_summary.add("chosen", false).add_alnum("cause", "cost");
2826  }
2827 
2828  if (tree)
2829  {
2830  /*
2831  It is possible to use a range-based quick select (but it might be
2832  slower than 'all' table scan).
2833  */
2834  dbug_print_tree("final_tree", tree, &param);
2835 
2836  {
2837  /*
2838  Calculate cost of single index range scan and possible
2839  intersections of these
2840  */
2841  Opt_trace_object trace_range(trace,
2842  "analyzing_range_alternatives",
2843  Opt_trace_context::RANGE_OPTIMIZER);
2844  TRP_RANGE *range_trp;
2845  TRP_ROR_INTERSECT *rori_trp;
2846 
2847  /* Get best 'range' plan and prepare data for making other plans */
2848  if ((range_trp= get_key_scans_params(&param, tree, FALSE, TRUE,
2849  best_read_time)))
2850  {
2851  best_trp= range_trp;
2852  best_read_time= best_trp->read_cost;
2853  }
2854 
2855  /*
2856  Simultaneous key scans and row deletes on several handler
2857  objects are not allowed so don't use ROR-intersection for
2858  table deletes. Also, ROR-intersection cannot return rows in
2859  descending order
2860  */
2861  if ((thd->lex->sql_command != SQLCOM_DELETE) &&
2862  thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE) &&
2863  interesting_order != ORDER::ORDER_DESC)
2864  {
2865  /*
2866  Get best non-covering ROR-intersection plan and prepare data for
2867  building covering ROR-intersection.
2868  */
2869  if ((rori_trp= get_best_ror_intersect(&param, tree, best_read_time)))
2870  {
2871  best_trp= rori_trp;
2872  best_read_time= best_trp->read_cost;
2873  }
2874  }
2875  }
2876 
2877  // Here we calculate cost of union index merge
2878  if (!tree->merges.is_empty())
2879  {
2880  // Cannot return rows in descending order.
2881  if (thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE) &&
2882  interesting_order != ORDER::ORDER_DESC &&
2883  param.table->file->stats.records)
2884  {
2885  /* Try creating index_merge/ROR-union scan. */
2886  SEL_IMERGE *imerge;
2887  TABLE_READ_PLAN *best_conj_trp= NULL, *new_conj_trp;
2888  LINT_INIT(new_conj_trp); /* no empty index_merge lists possible */
2889  List_iterator_fast<SEL_IMERGE> it(tree->merges);
2890  Opt_trace_array trace_idx_merge(trace,
2891  "analyzing_index_merge",
2892  Opt_trace_context::RANGE_OPTIMIZER);
2893  while ((imerge= it++))
2894  {
2895  new_conj_trp= get_best_disjunct_quick(&param, imerge,
2896  best_read_time);
2897  if (new_conj_trp)
2898  set_if_smaller(param.table->quick_condition_rows,
2899  new_conj_trp->records);
2900  if (!best_conj_trp ||
2901  (new_conj_trp &&
2902  new_conj_trp->read_cost < best_conj_trp->read_cost))
2903  {
2904  best_conj_trp= new_conj_trp;
2905  }
2906  }
2907  if (best_conj_trp)
2908  best_trp= best_conj_trp;
2909  }
2910  }
2911  }
2912 
2913  thd->mem_root= param.old_root;
2914 
2915  /* If we got a read plan, create a quick select from it. */
2916  if (best_trp)
2917  {
2918  records= best_trp->records;
2919  if (!(quick= best_trp->make_quick(&param, TRUE)) || quick->init())
2920  set_quick(NULL);
2921  }
2922 
2923 free_mem:
2924  if (unlikely(quick && trace->is_started() && best_trp))
2925  {
2926  // best_trp cannot be NULL if quick is set, done to keep fortify happy
2927  Opt_trace_object trace_range_summary(trace,
2928  "chosen_range_access_summary");
2929  {
2930  Opt_trace_object trace_range_plan(trace,
2931  "range_access_plan");
2932  best_trp->trace_basic_info(&param, &trace_range_plan);
2933  }
2934  trace_range_summary.add("rows_for_plan", quick->records).
2935  add("cost_for_plan", quick->read_time).
2936  add("chosen", true);
2937  }
2938 
2939  free_root(&alloc,MYF(0)); // Return memory & allocator
2940  thd->mem_root= param.old_root;
2941  thd->no_errors=0;
2942  }
2943 
2944  DBUG_EXECUTE("info", print_quick(quick, &needed_reg););
2945 
2946  /*
2947  Assume that if the user is using 'limit' we will only need to scan
2948  limit rows if we are using a key
2949  */
2950  DBUG_RETURN(records ? test(quick) : -1);
2951 }
2952 
2953 /****************************************************************************
2954  * Partition pruning module
2955  ****************************************************************************/
2956 #ifdef WITH_PARTITION_STORAGE_ENGINE
2957 
2958 /*
2959  PartitionPruningModule
2960 
2961  This part of the code does partition pruning. Partition pruning solves the
2962  following problem: given a query over partitioned tables, find partitions
2963  that we will not need to access (i.e. partitions that we can assume to be
2964  empty) when executing the query.
2965  The set of partitions to prune doesn't depend on which query execution
2966  plan will be used to execute the query.
2967 
2968  HOW IT WORKS
2969 
2970  Partition pruning module makes use of RangeAnalysisModule. The following
2971  examples show how the problem of partition pruning can be reduced to the
2972  range analysis problem:
2973 
2974  EXAMPLE 1
2975  Consider a query:
2976 
2977  SELECT * FROM t1 WHERE (t1.a < 5 OR t1.a = 10) AND t1.a > 3 AND t1.b='z'
2978 
2979  where table t1 is partitioned using PARTITION BY RANGE(t1.a). An apparent
2980  way to find the used (i.e. not pruned away) partitions is as follows:
2981 
2982  1. analyze the WHERE clause and extract the list of intervals over t1.a
2983  for the above query we will get this list: {(3 < t1.a < 5), (t1.a=10)}
2984 
2985  2. for each interval I
2986  {
2987  find partitions that have non-empty intersection with I;
2988  mark them as used;
2989  }
2990 
2991  EXAMPLE 2
2992  Suppose the table is partitioned by HASH(part_func(t1.a, t1.b)). Then
2993  we need to:
2994 
2995  1. Analyze the WHERE clause and get a list of intervals over (t1.a, t1.b).
2996  The list of intervals we'll obtain will look like this:
2997  ((t1.a, t1.b) = (1,'foo')),
2998  ((t1.a, t1.b) = (2,'bar')),
2999  ((t1,a, t1.b) > (10,'zz'))
3000 
3001  2. for each interval I
3002  {
3003  if (the interval has form "(t1.a, t1.b) = (const1, const2)" )
3004  {
3005  calculate HASH(part_func(t1.a, t1.b));
3006  find which partition has records with this hash value and mark
3007  it as used;
3008  }
3009  else
3010  {
3011  mark all partitions as used;
3012  break;
3013  }
3014  }
3015 
3016  For both examples the step #1 is exactly what RangeAnalysisModule could
3017  be used to do, if it was provided with appropriate index description
3018  (array of KEY_PART structures).
3019  In example #1, we need to provide it with description of index(t1.a),
3020  in example #2, we need to provide it with description of index(t1.a, t1.b).
3021 
3022  These index descriptions are further called "partitioning index
3023  descriptions". Note that it doesn't matter if such indexes really exist,
3024  as range analysis module only uses the description.
3025 
3026  Putting it all together, partitioning module works as follows:
3027 
3028  prune_partitions() {
3029  call create_partition_index_description();
3030 
3031  call get_mm_tree(); // invoke the RangeAnalysisModule
3032 
3033  // analyze the obtained interval list and get used partitions
3034  call find_used_partitions();
3035  }
3036 
3037 */
3038 
3039 struct st_part_prune_param;
3040 struct st_part_opt_info;
3041 
3042 typedef void (*mark_full_part_func)(partition_info*, uint32);
3043 
3044 /*
3045  Partition pruning operation context
3046 */
3047 typedef struct st_part_prune_param
3048 {
3049  RANGE_OPT_PARAM range_param; /* Range analyzer parameters */
3050 
3051  /***************************************************************
3052  Following fields are filled in based solely on partitioning
3053  definition and not modified after that:
3054  **************************************************************/
3055  partition_info *part_info; /* Copy of table->part_info */
3056  /* Function to get partition id from partitioning fields only */
3057  get_part_id_func get_top_partition_id_func;
3058  /* Function to mark a partition as used (w/all subpartitions if they exist)*/
3059  mark_full_part_func mark_full_partition_used;
3060 
3061  /* Partitioning 'index' description, array of key parts */
3062  KEY_PART *key;
3063 
3064  /*
3065  Number of fields in partitioning 'index' definition created for
3066  partitioning (0 if partitioning 'index' doesn't include partitioning
3067  fields)
3068  */
3069  uint part_fields;
3070  uint subpart_fields; /* Same as above for subpartitioning */
3071 
3072  /*
3073  Number of the last partitioning field keypart in the index, or -1 if
3074  partitioning index definition doesn't include partitioning fields.
3075  */
3076  int last_part_partno;
3077  int last_subpart_partno; /* Same as above for supartitioning */
3078 
3079  /*
3080  is_part_keypart[i] == test(keypart #i in partitioning index is a member
3081  used in partitioning)
3082  Used to maintain current values of cur_part_fields and cur_subpart_fields
3083  */
3084  my_bool *is_part_keypart;
3085  /* Same as above for subpartitioning */
3086  my_bool *is_subpart_keypart;
3087 
3088  my_bool ignore_part_fields; /* Ignore rest of partioning fields */
3089 
3090  /***************************************************************
3091  Following fields form find_used_partitions() recursion context:
3092  **************************************************************/
3093  SEL_ARG **arg_stack; /* "Stack" of SEL_ARGs */
3094  SEL_ARG **arg_stack_end; /* Top of the stack */
3095  /* Number of partitioning fields for which we have a SEL_ARG* in arg_stack */
3096  uint cur_part_fields;
3097  /* Same as cur_part_fields, but for subpartitioning */
3098  uint cur_subpart_fields;
3099 
3100  /* Iterator to be used to obtain the "current" set of used partitions */
3101  PARTITION_ITERATOR part_iter;
3102 
3103  /* Initialized bitmap of num_subparts size */
3104  MY_BITMAP subparts_bitmap;
3105 
3106  uchar *cur_min_key;
3107  uchar *cur_max_key;
3108 
3109  uint cur_min_flag, cur_max_flag;
3110 } PART_PRUNE_PARAM;
3111 
3112 static bool create_partition_index_description(PART_PRUNE_PARAM *prune_par);
3113 static int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree);
3114 static int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar,
3115  SEL_IMERGE *imerge);
3116 static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
3117  List<SEL_IMERGE> &merges);
3118 static void mark_all_partitions_as_used(partition_info *part_info);
3119 
3120 #ifndef DBUG_OFF
3121 static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end);
3122 static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part);
3123 static void dbug_print_singlepoint_range(SEL_ARG **start, uint num);
3124 #endif
3125 
3126 
3149 bool prune_partitions(THD *thd, TABLE *table, Item *pprune_cond)
3150 {
3151  partition_info *part_info = table->part_info;
3152  DBUG_ENTER("prune_partitions");
3153  table->all_partitions_pruned_away= false;
3154 
3155  if (!part_info)
3156  DBUG_RETURN(FALSE); /* not a partitioned table */
3157 
3158  if (table->s->db_type()->partition_flags() & HA_USE_AUTO_PARTITION &&
3159  part_info->is_auto_partitioned)
3160  DBUG_RETURN(false); /* Should not prune auto partitioned table */
3161 
3162  if (!pprune_cond)
3163  {
3164  mark_all_partitions_as_used(part_info);
3165  DBUG_RETURN(FALSE);
3166  }
3167 
3168  /* No need to continue pruning if there is no more partitions to prune! */
3169  if (bitmap_is_clear_all(&part_info->lock_partitions))
3170  bitmap_clear_all(&part_info->read_partitions);
3171  if (bitmap_is_clear_all(&part_info->read_partitions))
3172  {
3173  table->all_partitions_pruned_away= true;
3174  DBUG_RETURN(false);
3175  }
3176 
3177  /*
3178  If the prepare stage already have completed pruning successfully,
3179  it is no use of running prune_partitions() again on the same condition.
3180  Since it will not be able to prune anything more than the previous call
3181  from the prepare step.
3182  */
3183  if (part_info->is_pruning_completed)
3184  DBUG_RETURN(false);
3185 
3186  PART_PRUNE_PARAM prune_param;
3187  MEM_ROOT alloc;
3188  RANGE_OPT_PARAM *range_par= &prune_param.range_param;
3189  my_bitmap_map *old_sets[2];
3190 
3191  prune_param.part_info= part_info;
3192  init_sql_alloc(&alloc, thd->variables.range_alloc_block_size, 0);
3193  range_par->mem_root= &alloc;
3194  range_par->old_root= thd->mem_root;
3195 
3196  if (create_partition_index_description(&prune_param))
3197  {
3198  mark_all_partitions_as_used(part_info);
3199  free_root(&alloc,MYF(0)); // Return memory & allocator
3200  DBUG_RETURN(FALSE);
3201  }
3202 
3203  dbug_tmp_use_all_columns(table, old_sets,
3204  table->read_set, table->write_set);
3205  range_par->thd= thd;
3206  range_par->table= table;
3207  /* range_par->cond doesn't need initialization */
3208  range_par->prev_tables= range_par->read_tables= 0;
3209  range_par->current_table= table->map;
3210 
3211  range_par->keys= 1; // one index
3212  range_par->using_real_indexes= FALSE;
3213  range_par->remove_jump_scans= FALSE;
3214  range_par->real_keynr[0]= 0;
3215  range_par->alloced_sel_args= 0;
3216 
3217  thd->no_errors=1; // Don't warn about NULL
3218  thd->mem_root=&alloc;
3219 
3220  bitmap_clear_all(&part_info->read_partitions);
3221 
3222  prune_param.key= prune_param.range_param.key_parts;
3223  SEL_TREE *tree;
3224  int res;
3225 
3226  tree= get_mm_tree(range_par, pprune_cond);
3227  if (!tree)
3228  goto all_used;
3229 
3230  if (tree->type == SEL_TREE::IMPOSSIBLE)
3231  {
3232  /* Cannot improve the pruning any further. */
3233  part_info->is_pruning_completed= true;
3234  goto end;
3235  }
3236 
3237  if (tree->type != SEL_TREE::KEY && tree->type != SEL_TREE::KEY_SMALLER)
3238  goto all_used;
3239 
3240  if (tree->merges.is_empty())
3241  {
3242  /* Range analysis has produced a single list of intervals. */
3243  prune_param.arg_stack_end= prune_param.arg_stack;
3244  prune_param.cur_part_fields= 0;
3245  prune_param.cur_subpart_fields= 0;
3246 
3247  prune_param.cur_min_key= prune_param.range_param.min_key;
3248  prune_param.cur_max_key= prune_param.range_param.max_key;
3249  prune_param.cur_min_flag= prune_param.cur_max_flag= 0;
3250 
3251  init_all_partitions_iterator(part_info, &prune_param.part_iter);
3252  if (!tree->keys[0] || (-1 == (res= find_used_partitions(&prune_param,
3253  tree->keys[0]))))
3254  goto all_used;
3255  }
3256  else
3257  {
3258  if (tree->merges.elements == 1)
3259  {
3260  /*
3261  Range analysis has produced a "merge" of several intervals lists, a
3262  SEL_TREE that represents an expression in form
3263  sel_imerge = (tree1 OR tree2 OR ... OR treeN)
3264  that cannot be reduced to one tree. This can only happen when
3265  partitioning index has several keyparts and the condition is OR of
3266  conditions that refer to different key parts. For example, we'll get
3267  here for "partitioning_field=const1 OR subpartitioning_field=const2"
3268  */
3269  if (-1 == (res= find_used_partitions_imerge(&prune_param,
3270  tree->merges.head())))
3271  goto all_used;
3272  }
3273  else
3274  {
3275  /*
3276  Range analysis has produced a list of several imerges, i.e. a
3277  structure that represents a condition in form
3278  imerge_list= (sel_imerge1 AND sel_imerge2 AND ... AND sel_imergeN)
3279  This is produced for complicated WHERE clauses that range analyzer
3280  can't really analyze properly.
3281  */
3282  if (-1 == (res= find_used_partitions_imerge_list(&prune_param,
3283  tree->merges)))
3284  goto all_used;
3285  }
3286  }
3287 
3288  /*
3289  If the condition can be evaluated now, we are done with pruning.
3290 
3291  During the prepare phase, before locking, subqueries and stored programs
3292  are not evaluated. So we need to run prune_partitions() a second time in
3293  the optimize phase to prune partitions for reading, when subqueries and
3294  stored programs may be evaluated.
3295  */
3296  if (pprune_cond->can_be_evaluated_now())
3297  part_info->is_pruning_completed= true;
3298  goto end;
3299 
3300 all_used:
3301  mark_all_partitions_as_used(prune_param.part_info);
3302 end:
3303  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
3304  thd->no_errors=0;
3305  thd->mem_root= range_par->old_root;
3306  free_root(&alloc,MYF(0)); // Return memory & allocator
3307  /*
3308  Must be a subset of the locked partitions.
3309  lock_partitions contains the partitions marked by explicit partition
3310  selection (... t PARTITION (pX) ...) and we must only use partitions
3311  within that set.
3312  */
3313  bitmap_intersect(&prune_param.part_info->read_partitions,
3314  &prune_param.part_info->lock_partitions);
3315  /*
3316  If not yet locked, also prune partitions to lock if not UPDATEing
3317  partition key fields. This will also prune lock_partitions if we are under
3318  LOCK TABLES, so prune away calls to start_stmt().
3319  TODO: enhance this prune locking to also allow pruning of
3320  'UPDATE t SET part_key = const WHERE cond_is_prunable' so it adds
3321  a lock for part_key partition.
3322  */
3323  if (!thd->lex->is_query_tables_locked() &&
3324  !partition_key_modified(table, table->write_set))
3325  {
3326  bitmap_copy(&prune_param.part_info->lock_partitions,
3327  &prune_param.part_info->read_partitions);
3328  }
3329  if (bitmap_is_clear_all(&(prune_param.part_info->read_partitions)))
3330  table->all_partitions_pruned_away= true;
3331  DBUG_RETURN(false);
3332 }
3333 
3334 
3335 /*
3336  Store field key image to table record
3337 
3338  SYNOPSIS
3339  store_key_image_to_rec()
3340  field Field which key image should be stored
3341  ptr Field value in key format
3342  len Length of the value, in bytes
3343 
3344  DESCRIPTION
3345  Copy the field value from its key image to the table record. The source
3346  is the value in key image format, occupying len bytes in buffer pointed
3347  by ptr. The destination is table record, in "field value in table record"
3348  format.
3349 */
3350 
3351 void store_key_image_to_rec(Field *field, uchar *ptr, uint len)
3352 {
3353  /* Do the same as print_key_value() does */
3354  my_bitmap_map *old_map;
3355 
3356  if (field->real_maybe_null())
3357  {
3358  if (*ptr)
3359  {
3360  field->set_null();
3361  return;
3362  }
3363  field->set_notnull();
3364  ptr++;
3365  }
3366  old_map= dbug_tmp_use_all_columns(field->table,
3367  field->table->write_set);
3368  field->set_key_image(ptr, len);
3369  dbug_tmp_restore_column_map(field->table->write_set, old_map);
3370 }
3371 
3372 
3373 /*
3374  For SEL_ARG* array, store sel_arg->min values into table record buffer
3375 
3376  SYNOPSIS
3377  store_selargs_to_rec()
3378  ppar Partition pruning context
3379  start Array of SEL_ARG* for which the minimum values should be stored
3380  num Number of elements in the array
3381 
3382  DESCRIPTION
3383  For each SEL_ARG* interval in the specified array, store the left edge
3384  field value (sel_arg->min, key image format) into the table record.
3385 */
3386 
3387 static void store_selargs_to_rec(PART_PRUNE_PARAM *ppar, SEL_ARG **start,
3388  int num)
3389 {
3390  KEY_PART *parts= ppar->range_param.key_parts;
3391  for (SEL_ARG **end= start + num; start != end; start++)
3392  {
3393  SEL_ARG *sel_arg= (*start);
3394  store_key_image_to_rec(sel_arg->field, sel_arg->min_value,
3395  parts[sel_arg->part].length);
3396  }
3397 }
3398 
3399 
3400 /* Mark a partition as used in the case when there are no subpartitions */
3401 static void mark_full_partition_used_no_parts(partition_info* part_info,
3402  uint32 part_id)
3403 {
3404  DBUG_ENTER("mark_full_partition_used_no_parts");
3405  DBUG_PRINT("enter", ("Mark partition %u as used", part_id));
3406  bitmap_set_bit(&part_info->read_partitions, part_id);
3407  DBUG_VOID_RETURN;
3408 }
3409 
3410 
3411 /* Mark a partition as used in the case when there are subpartitions */
3412 static void mark_full_partition_used_with_parts(partition_info *part_info,
3413  uint32 part_id)
3414 {
3415  uint32 start= part_id * part_info->num_subparts;
3416  uint32 end= start + part_info->num_subparts;
3417  DBUG_ENTER("mark_full_partition_used_with_parts");
3418 
3419  for (; start != end; start++)
3420  {
3421  DBUG_PRINT("info", ("1:Mark subpartition %u as used", start));
3422  bitmap_set_bit(&part_info->read_partitions, start);
3423  }
3424  DBUG_VOID_RETURN;
3425 }
3426 
3427 /*
3428  Find the set of used partitions for List<SEL_IMERGE>
3429  SYNOPSIS
3430  find_used_partitions_imerge_list
3431  ppar Partition pruning context.
3432  key_tree Intervals tree to perform pruning for.
3433 
3434  DESCRIPTION
3435  List<SEL_IMERGE> represents "imerge1 AND imerge2 AND ...".
3436  The set of used partitions is an intersection of used partitions sets
3437  for imerge_{i}.
3438  We accumulate this intersection in a separate bitmap.
3439 
3440  RETURN
3441  See find_used_partitions()
3442 */
3443 
3444 static int find_used_partitions_imerge_list(PART_PRUNE_PARAM *ppar,
3445  List<SEL_IMERGE> &merges)
3446 {
3447  MY_BITMAP all_merges;
3448  uint bitmap_bytes;
3449  my_bitmap_map *bitmap_buf;
3450  uint n_bits= ppar->part_info->read_partitions.n_bits;
3451  bitmap_bytes= bitmap_buffer_size(n_bits);
3452  if (!(bitmap_buf= (my_bitmap_map*) alloc_root(ppar->range_param.mem_root,
3453  bitmap_bytes)))
3454  {
3455  /*
3456  Fallback, process just the first SEL_IMERGE. This can leave us with more
3457  partitions marked as used then actually needed.
3458  */
3459  return find_used_partitions_imerge(ppar, merges.head());
3460  }
3461  bitmap_init(&all_merges, bitmap_buf, n_bits, FALSE);
3462  bitmap_set_prefix(&all_merges, n_bits);
3463 
3464  List_iterator<SEL_IMERGE> it(merges);
3465  SEL_IMERGE *imerge;
3466  while ((imerge=it++))
3467  {
3468  int res= find_used_partitions_imerge(ppar, imerge);
3469  if (!res)
3470  {
3471  /* no used partitions on one ANDed imerge => no used partitions at all */
3472  return 0;
3473  }
3474 
3475  if (res != -1)
3476  bitmap_intersect(&all_merges, &ppar->part_info->read_partitions);
3477 
3478  if (bitmap_is_clear_all(&all_merges))
3479  return 0;
3480 
3481  bitmap_clear_all(&ppar->part_info->read_partitions);
3482  }
3483  memcpy(ppar->part_info->read_partitions.bitmap, all_merges.bitmap,
3484  bitmap_bytes);
3485  return 1;
3486 }
3487 
3488 
3489 /*
3490  Find the set of used partitions for SEL_IMERGE structure
3491  SYNOPSIS
3492  find_used_partitions_imerge()
3493  ppar Partition pruning context.
3494  key_tree Intervals tree to perform pruning for.
3495 
3496  DESCRIPTION
3497  SEL_IMERGE represents "tree1 OR tree2 OR ...". The implementation is
3498  trivial - just use mark used partitions for each tree and bail out early
3499  if for some tree_{i} all partitions are used.
3500 
3501  RETURN
3502  See find_used_partitions().
3503 */
3504 
3505 static
3506 int find_used_partitions_imerge(PART_PRUNE_PARAM *ppar, SEL_IMERGE *imerge)
3507 {
3508  int res= 0;
3509  for (SEL_TREE **ptree= imerge->trees; ptree < imerge->trees_next; ptree++)
3510  {
3511  ppar->arg_stack_end= ppar->arg_stack;
3512  ppar->cur_part_fields= 0;
3513  ppar->cur_subpart_fields= 0;
3514 
3515  ppar->cur_min_key= ppar->range_param.min_key;
3516  ppar->cur_max_key= ppar->range_param.max_key;
3517  ppar->cur_min_flag= ppar->cur_max_flag= 0;
3518 
3519  init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3520  SEL_ARG *key_tree= (*ptree)->keys[0];
3521  if (!key_tree || (-1 == (res |= find_used_partitions(ppar, key_tree))))
3522  return -1;
3523  }
3524  return res;
3525 }
3526 
3527 
3528 /*
3529  Collect partitioning ranges for the SEL_ARG tree and mark partitions as used
3530 
3531  SYNOPSIS
3532  find_used_partitions()
3533  ppar Partition pruning context.
3534  key_tree SEL_ARG range tree to perform pruning for
3535 
3536  DESCRIPTION
3537  This function
3538  * recursively walks the SEL_ARG* tree collecting partitioning "intervals"
3539  * finds the partitions one needs to use to get rows in these intervals
3540  * marks these partitions as used.
3541  The next session desribes the process in greater detail.
3542 
3543  IMPLEMENTATION
3544  TYPES OF RESTRICTIONS THAT WE CAN OBTAIN PARTITIONS FOR
3545  We can find out which [sub]partitions to use if we obtain restrictions on
3546  [sub]partitioning fields in the following form:
3547  1. "partition_field1=const1 AND ... AND partition_fieldN=constN"
3548  1.1 Same as (1) but for subpartition fields
3549 
3550  If partitioning supports interval analysis (i.e. partitioning is a
3551  function of a single table field, and partition_info::
3552  get_part_iter_for_interval != NULL), then we can also use condition in
3553  this form:
3554  2. "const1 <=? partition_field <=? const2"
3555  2.1 Same as (2) but for subpartition_field
3556 
3557  INFERRING THE RESTRICTIONS FROM SEL_ARG TREE
3558 
3559  The below is an example of what SEL_ARG tree may represent:
3560 
3561  (start)
3562  | $
3563  | Partitioning keyparts $ subpartitioning keyparts
3564  | $
3565  | ... ... $
3566  | | | $
3567  | +---------+ +---------+ $ +-----------+ +-----------+
3568  \-| par1=c1 |--| par2=c2 |-----| subpar1=c3|--| subpar2=c5|
3569  +---------+ +---------+ $ +-----------+ +-----------+
3570  | $ | |
3571  | $ | +-----------+
3572  | $ | | subpar2=c6|
3573  | $ | +-----------+
3574  | $ |
3575  | $ +-----------+ +-----------+
3576  | $ | subpar1=c4|--| subpar2=c8|
3577  | $ +-----------+ +-----------+
3578  | $
3579  | $
3580  +---------+ $ +------------+ +------------+
3581  | par1=c2 |------------------| subpar1=c10|--| subpar2=c12|
3582  +---------+ $ +------------+ +------------+
3583  | $
3584  ... $
3585 
3586  The up-down connections are connections via SEL_ARG::left and
3587  SEL_ARG::right. A horizontal connection to the right is the
3588  SEL_ARG::next_key_part connection.
3589 
3590  find_used_partitions() traverses the entire tree via recursion on
3591  * SEL_ARG::next_key_part (from left to right on the picture)
3592  * SEL_ARG::left|right (up/down on the pic). Left-right recursion is
3593  performed for each depth level.
3594 
3595  Recursion descent on SEL_ARG::next_key_part is used to accumulate (in
3596  ppar->arg_stack) constraints on partitioning and subpartitioning fields.
3597  For the example in the above picture, one of stack states is:
3598  in find_used_partitions(key_tree = "subpar2=c5") (***)
3599  in find_used_partitions(key_tree = "subpar1=c3")
3600  in find_used_partitions(key_tree = "par2=c2") (**)
3601  in find_used_partitions(key_tree = "par1=c1")
3602  in prune_partitions(...)
3603  We apply partitioning limits as soon as possible, e.g. when we reach the
3604  depth (**), we find which partition(s) correspond to "par1=c1 AND par2=c2",
3605  and save them in ppar->part_iter.
3606  When we reach the depth (***), we find which subpartition(s) correspond to
3607  "subpar1=c3 AND subpar2=c5", and then mark appropriate subpartitions in
3608  appropriate subpartitions as used.
3609 
3610  It is possible that constraints on some partitioning fields are missing.
3611  For the above example, consider this stack state:
3612  in find_used_partitions(key_tree = "subpar2=c12") (***)
3613  in find_used_partitions(key_tree = "subpar1=c10")
3614  in find_used_partitions(key_tree = "par1=c2")
3615  in prune_partitions(...)
3616  Here we don't have constraints for all partitioning fields. Since we've
3617  never set the ppar->part_iter to contain used set of partitions, we use
3618  its default "all partitions" value. We get subpartition id for
3619  "subpar1=c3 AND subpar2=c5", and mark that subpartition as used in every
3620  partition.
3621 
3622  The inverse is also possible: we may get constraints on partitioning
3623  fields, but not constraints on subpartitioning fields. In that case,
3624  calls to find_used_partitions() with depth below (**) will return -1,
3625  and we will mark entire partition as used.
3626 
3627  TODO
3628  Replace recursion on SEL_ARG::left and SEL_ARG::right with a loop
3629 
3630  RETURN
3631  1 OK, one or more [sub]partitions are marked as used.
3632  0 The passed condition doesn't match any partitions
3633  -1 Couldn't infer any partition pruning "intervals" from the passed
3634  SEL_ARG* tree (which means that all partitions should be marked as
3635  used) Marking partitions as used is the responsibility of the caller.
3636 */
3637 
3638 static
3639 int find_used_partitions(PART_PRUNE_PARAM *ppar, SEL_ARG *key_tree)
3640 {
3641  int res, left_res=0, right_res=0;
3642  int key_tree_part= (int)key_tree->part;
3643  bool set_full_part_if_bad_ret= FALSE;
3644  bool ignore_part_fields= ppar->ignore_part_fields;
3645  bool did_set_ignore_part_fields= FALSE;
3646  RANGE_OPT_PARAM *range_par= &(ppar->range_param);
3647 
3648  if (check_stack_overrun(range_par->thd, 3*STACK_MIN_SIZE, NULL))
3649  return -1;
3650 
3651  if (key_tree->left != &null_element)
3652  {
3653  if (-1 == (left_res= find_used_partitions(ppar,key_tree->left)))
3654  return -1;
3655  }
3656 
3657  /* Push SEL_ARG's to stack to enable looking backwards as well */
3658  ppar->cur_part_fields+= ppar->is_part_keypart[key_tree_part];
3659  ppar->cur_subpart_fields+= ppar->is_subpart_keypart[key_tree_part];
3660  *(ppar->arg_stack_end++)= key_tree;
3661 
3662  if (ignore_part_fields)
3663  {
3664  /*
3665  We come here when a condition on the first partitioning
3666  fields led to evaluating the partitioning condition
3667  (due to finding a condition of the type a < const or
3668  b > const). Thus we must ignore the rest of the
3669  partitioning fields but we still want to analyse the
3670  subpartitioning fields.
3671  */
3672  if (key_tree->next_key_part)
3673  res= find_used_partitions(ppar, key_tree->next_key_part);
3674  else
3675  res= -1;
3676  goto pop_and_go_right;
3677  }
3678 
3679  if (key_tree->type == SEL_ARG::KEY_RANGE)
3680  {
3681  if (ppar->part_info->get_part_iter_for_interval &&
3682  key_tree->part <= ppar->last_part_partno)
3683  {
3684  /* Collect left and right bound, their lengths and flags */
3685  uchar *min_key= ppar->cur_min_key;
3686  uchar *max_key= ppar->cur_max_key;
3687  uchar *tmp_min_key= min_key;
3688  uchar *tmp_max_key= max_key;
3689  key_tree->store_min(ppar->key[key_tree->part].store_length,
3690  &tmp_min_key, ppar->cur_min_flag);
3691  key_tree->store_max(ppar->key[key_tree->part].store_length,
3692  &tmp_max_key, ppar->cur_max_flag);
3693  uint flag;
3694  if (key_tree->next_key_part &&
3695  key_tree->next_key_part->part == key_tree->part+1 &&
3696  key_tree->next_key_part->part <= ppar->last_part_partno &&
3697  key_tree->next_key_part->type == SEL_ARG::KEY_RANGE)
3698  {
3699  /*
3700  There are more key parts for partition pruning to handle
3701  This mainly happens when the condition is an equality
3702  condition.
3703  */
3704  if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
3705  (memcmp(min_key, max_key, (uint)(tmp_max_key - max_key)) == 0) &&
3706  !key_tree->min_flag && !key_tree->max_flag)
3707  {
3708  /* Set 'parameters' */
3709  ppar->cur_min_key= tmp_min_key;
3710  ppar->cur_max_key= tmp_max_key;
3711  uint save_min_flag= ppar->cur_min_flag;
3712  uint save_max_flag= ppar->cur_max_flag;
3713 
3714  ppar->cur_min_flag|= key_tree->min_flag;
3715  ppar->cur_max_flag|= key_tree->max_flag;
3716 
3717  res= find_used_partitions(ppar, key_tree->next_key_part);
3718 
3719  /* Restore 'parameters' back */
3720  ppar->cur_min_key= min_key;
3721  ppar->cur_max_key= max_key;
3722 
3723  ppar->cur_min_flag= save_min_flag;
3724  ppar->cur_max_flag= save_max_flag;
3725  goto pop_and_go_right;
3726  }
3727  /* We have arrived at the last field in the partition pruning */
3728  uint tmp_min_flag= key_tree->min_flag,
3729  tmp_max_flag= key_tree->max_flag;
3730  if (!tmp_min_flag)
3731  key_tree->next_key_part->store_min_key(ppar->key,
3732  &tmp_min_key,
3733  &tmp_min_flag,
3734  ppar->last_part_partno);
3735  if (!tmp_max_flag)
3736  key_tree->next_key_part->store_max_key(ppar->key,
3737  &tmp_max_key,
3738  &tmp_max_flag,
3739  ppar->last_part_partno);
3740  flag= tmp_min_flag | tmp_max_flag;
3741  }
3742  else
3743  flag= key_tree->min_flag | key_tree->max_flag;
3744 
3745  if (tmp_min_key != range_par->min_key)
3746  flag&= ~NO_MIN_RANGE;
3747  else
3748  flag|= NO_MIN_RANGE;
3749  if (tmp_max_key != range_par->max_key)
3750  flag&= ~NO_MAX_RANGE;
3751  else
3752  flag|= NO_MAX_RANGE;
3753 
3754  /*
3755  We need to call the interval mapper if we have a condition which
3756  makes sense to prune on. In the example of COLUMNS on a and
3757  b it makes sense if we have a condition on a, or conditions on
3758  both a and b. If we only have conditions on b it might make sense
3759  but this is a harder case we will solve later. For the harder case
3760  this clause then turns into use of all partitions and thus we
3761  simply set res= -1 as if the mapper had returned that.
3762  TODO: What to do here is defined in WL#4065.
3763  */
3764  if (ppar->arg_stack[0]->part == 0)
3765  {
3766  uint32 i;
3767  uint32 store_length_array[MAX_KEY];
3768  uint32 num_keys= ppar->part_fields;
3769 
3770  for (i= 0; i < num_keys; i++)
3771  store_length_array[i]= ppar->key[i].store_length;
3772  res= ppar->part_info->
3773  get_part_iter_for_interval(ppar->part_info,
3774  FALSE,
3775  store_length_array,
3776  range_par->min_key,
3777  range_par->max_key,
3778  tmp_min_key - range_par->min_key,
3779  tmp_max_key - range_par->max_key,
3780  flag,
3781  &ppar->part_iter);
3782  if (!res)
3783  goto pop_and_go_right; /* res==0 --> no satisfying partitions */
3784  }
3785  else
3786  res= -1;
3787 
3788  if (res == -1)
3789  {
3790  /* get a full range iterator */
3791  init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3792  }
3793  /*
3794  Save our intent to mark full partition as used if we will not be able
3795  to obtain further limits on subpartitions
3796  */
3797  if (key_tree_part < ppar->last_part_partno)
3798  {
3799  /*
3800  We need to ignore the rest of the partitioning fields in all
3801  evaluations after this
3802  */
3803  did_set_ignore_part_fields= TRUE;
3804  ppar->ignore_part_fields= TRUE;
3805  }
3806  set_full_part_if_bad_ret= TRUE;
3807  goto process_next_key_part;
3808  }
3809 
3810  if (key_tree_part == ppar->last_subpart_partno &&
3811  (NULL != ppar->part_info->get_subpart_iter_for_interval))
3812  {
3813  PARTITION_ITERATOR subpart_iter;
3814  DBUG_EXECUTE("info", dbug_print_segment_range(key_tree,
3815  range_par->key_parts););
3816  res= ppar->part_info->
3817  get_subpart_iter_for_interval(ppar->part_info,
3818  TRUE,
3819  NULL, /* Currently not used here */
3820  key_tree->min_value,
3821  key_tree->max_value,
3822  0, 0, /* Those are ignored here */
3823  key_tree->min_flag |
3824  key_tree->max_flag,
3825  &subpart_iter);
3826  DBUG_ASSERT(res); /* We can't get "no satisfying subpartitions" */
3827  if (res == -1)
3828  goto pop_and_go_right; /* all subpartitions satisfy */
3829 
3830  uint32 subpart_id;
3831  bitmap_clear_all(&ppar->subparts_bitmap);
3832  while ((subpart_id= subpart_iter.get_next(&subpart_iter)) !=
3833  NOT_A_PARTITION_ID)
3834  bitmap_set_bit(&ppar->subparts_bitmap, subpart_id);
3835 
3836  /* Mark each partition as used in each subpartition. */
3837  uint32 part_id;
3838  while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
3839  NOT_A_PARTITION_ID)
3840  {
3841  for (uint i= 0; i < ppar->part_info->num_subparts; i++)
3842  if (bitmap_is_set(&ppar->subparts_bitmap, i))
3843  bitmap_set_bit(&ppar->part_info->read_partitions,
3844  part_id * ppar->part_info->num_subparts + i);
3845  }
3846  goto pop_and_go_right;
3847  }
3848 
3849  if (key_tree->is_singlepoint())
3850  {
3851  if (key_tree_part == ppar->last_part_partno &&
3852  ppar->cur_part_fields == ppar->part_fields &&
3853  ppar->part_info->get_part_iter_for_interval == NULL)
3854  {
3855  /*
3856  Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all partitioning
3857  fields. Save all constN constants into table record buffer.
3858  */
3859  store_selargs_to_rec(ppar, ppar->arg_stack, ppar->part_fields);
3860  DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack,
3861  ppar->part_fields););
3862  uint32 part_id;
3863  longlong func_value;
3864  /* Find in which partition the {const1, ...,constN} tuple goes */
3865  if (ppar->get_top_partition_id_func(ppar->part_info, &part_id,
3866  &func_value))
3867  {
3868  res= 0; /* No satisfying partitions */
3869  goto pop_and_go_right;
3870  }
3871  /* Rembember the limit we got - single partition #part_id */
3872  init_single_partition_iterator(part_id, &ppar->part_iter);
3873 
3874  /*
3875  If there are no subpartitions/we fail to get any limit for them,
3876  then we'll mark full partition as used.
3877  */
3878  set_full_part_if_bad_ret= TRUE;
3879  goto process_next_key_part;
3880  }
3881 
3882  if (key_tree_part == ppar->last_subpart_partno &&
3883  ppar->cur_subpart_fields == ppar->subpart_fields)
3884  {
3885  /*
3886  Ok, we've got "fieldN<=>constN"-type SEL_ARGs for all subpartitioning
3887  fields. Save all constN constants into table record buffer.
3888  */
3889  store_selargs_to_rec(ppar, ppar->arg_stack_end - ppar->subpart_fields,
3890  ppar->subpart_fields);
3891  DBUG_EXECUTE("info", dbug_print_singlepoint_range(ppar->arg_stack_end-
3892  ppar->subpart_fields,
3893  ppar->subpart_fields););
3894  /* Find the subpartition (it's HASH/KEY so we always have one) */
3895  partition_info *part_info= ppar->part_info;
3896  uint32 part_id, subpart_id;
3897 
3898  if (part_info->get_subpartition_id(part_info, &subpart_id))
3899  return 0;
3900 
3901  /* Mark this partition as used in each subpartition. */
3902  while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
3903  NOT_A_PARTITION_ID)
3904  {
3905  bitmap_set_bit(&part_info->read_partitions,
3906  part_id * part_info->num_subparts + subpart_id);
3907  }
3908  res= 1; /* Some partitions were marked as used */
3909  goto pop_and_go_right;
3910  }
3911  }
3912  else
3913  {
3914  /*
3915  Can't handle condition on current key part. If we're that deep that
3916  we're processing subpartititoning's key parts, this means we'll not be
3917  able to infer any suitable condition, so bail out.
3918  */
3919  if (key_tree_part >= ppar->last_part_partno)
3920  {
3921  res= -1;
3922  goto pop_and_go_right;
3923  }
3924  /*
3925  No meaning in continuing with rest of partitioning key parts.
3926  Will try to continue with subpartitioning key parts.
3927  */
3928  ppar->ignore_part_fields= true;
3929  did_set_ignore_part_fields= true;
3930  goto process_next_key_part;
3931  }
3932  }
3933 
3934 process_next_key_part:
3935  if (key_tree->next_key_part)
3936  res= find_used_partitions(ppar, key_tree->next_key_part);
3937  else
3938  res= -1;
3939 
3940  if (did_set_ignore_part_fields)
3941  {
3942  /*
3943  We have returned from processing all key trees linked to our next
3944  key part. We are ready to be moving down (using right pointers) and
3945  this tree is a new evaluation requiring its own decision on whether
3946  to ignore partitioning fields.
3947  */
3948  ppar->ignore_part_fields= FALSE;
3949  }
3950  if (set_full_part_if_bad_ret)
3951  {
3952  if (res == -1)
3953  {
3954  /* Got "full range" for subpartitioning fields */
3955  uint32 part_id;
3956  bool found= FALSE;
3957  while ((part_id= ppar->part_iter.get_next(&ppar->part_iter)) !=
3958  NOT_A_PARTITION_ID)
3959  {
3960  ppar->mark_full_partition_used(ppar->part_info, part_id);
3961  found= TRUE;
3962  }
3963  res= test(found);
3964  }
3965  /*
3966  Restore the "used partitions iterator" to the default setting that
3967  specifies iteration over all partitions.
3968  */
3969  init_all_partitions_iterator(ppar->part_info, &ppar->part_iter);
3970  }
3971 
3972 pop_and_go_right:
3973  /* Pop this key part info off the "stack" */
3974  ppar->arg_stack_end--;
3975  ppar->cur_part_fields-= ppar->is_part_keypart[key_tree_part];
3976  ppar->cur_subpart_fields-= ppar->is_subpart_keypart[key_tree_part];
3977 
3978  if (res == -1)
3979  return -1;
3980  if (key_tree->right != &null_element)
3981  {
3982  if (-1 == (right_res= find_used_partitions(ppar,key_tree->right)))
3983  return -1;
3984  }
3985  return (left_res || right_res || res);
3986 }
3987 
3988 
3989 static void mark_all_partitions_as_used(partition_info *part_info)
3990 {
3991  bitmap_copy(&(part_info->read_partitions),
3992  &(part_info->lock_partitions));
3993 }
3994 
3995 
3996 /*
3997  Check if field types allow to construct partitioning index description
3998 
3999  SYNOPSIS
4000  fields_ok_for_partition_index()
4001  pfield NULL-terminated array of pointers to fields.
4002 
4003  DESCRIPTION
4004  For an array of fields, check if we can use all of the fields to create
4005  partitioning index description.
4006 
4007  We can't process GEOMETRY fields - for these fields singlepoint intervals
4008  cant be generated, and non-singlepoint are "special" kinds of intervals
4009  to which our processing logic can't be applied.
4010 
4011  It is not known if we could process ENUM fields, so they are disabled to be
4012  on the safe side.
4013 
4014  RETURN
4015  TRUE Yes, fields can be used in partitioning index
4016  FALSE Otherwise
4017 */
4018 
4019 static bool fields_ok_for_partition_index(Field **pfield)
4020 {
4021  if (!pfield)
4022  return FALSE;
4023  for (; (*pfield); pfield++)
4024  {
4025  enum_field_types ftype= (*pfield)->real_type();
4026  if (ftype == MYSQL_TYPE_ENUM || ftype == MYSQL_TYPE_GEOMETRY)
4027  return FALSE;
4028  }
4029  return TRUE;
4030 }
4031 
4032 
4033 /*
4034  Create partition index description and fill related info in the context
4035  struct
4036 
4037  SYNOPSIS
4038  create_partition_index_description()
4039  prune_par INOUT Partition pruning context
4040 
4041  DESCRIPTION
4042  Create partition index description. Partition index description is:
4043 
4044  part_index(used_fields_list(part_expr), used_fields_list(subpart_expr))
4045 
4046  If partitioning/sub-partitioning uses BLOB or Geometry fields, then
4047  corresponding fields_list(...) is not included into index description
4048  and we don't perform partition pruning for partitions/subpartitions.
4049 
4050  RETURN
4051  TRUE Out of memory or can't do partition pruning at all
4052  FALSE OK
4053 */
4054 
4055 static bool create_partition_index_description(PART_PRUNE_PARAM *ppar)
4056 {
4057  RANGE_OPT_PARAM *range_par= &(ppar->range_param);
4058  partition_info *part_info= ppar->part_info;
4059  uint used_part_fields, used_subpart_fields;
4060 
4061  used_part_fields= fields_ok_for_partition_index(part_info->part_field_array) ?
4062  part_info->num_part_fields : 0;
4063  used_subpart_fields=
4064  fields_ok_for_partition_index(part_info->subpart_field_array)?
4065  part_info->num_subpart_fields : 0;
4066 
4067  uint total_parts= used_part_fields + used_subpart_fields;
4068 
4069  ppar->ignore_part_fields= FALSE;
4070  ppar->part_fields= used_part_fields;
4071  ppar->last_part_partno= (int)used_part_fields - 1;
4072 
4073  ppar->subpart_fields= used_subpart_fields;
4074  ppar->last_subpart_partno=
4075  used_subpart_fields?(int)(used_part_fields + used_subpart_fields - 1): -1;
4076 
4077  if (part_info->is_sub_partitioned())
4078  {
4079  ppar->mark_full_partition_used= mark_full_partition_used_with_parts;
4080  ppar->get_top_partition_id_func= part_info->get_part_partition_id;
4081  }
4082  else
4083  {
4084  ppar->mark_full_partition_used= mark_full_partition_used_no_parts;
4085  ppar->get_top_partition_id_func= part_info->get_partition_id;
4086  }
4087 
4088  KEY_PART *key_part;
4089  MEM_ROOT *alloc= range_par->mem_root;
4090  if (!total_parts ||
4091  !(key_part= (KEY_PART*)alloc_root(alloc, sizeof(KEY_PART)*
4092  total_parts)) ||
4093  !(ppar->arg_stack= (SEL_ARG**)alloc_root(alloc, sizeof(SEL_ARG*)*
4094  total_parts)) ||
4095  !(ppar->is_part_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
4096  total_parts)) ||
4097  !(ppar->is_subpart_keypart= (my_bool*)alloc_root(alloc, sizeof(my_bool)*
4098  total_parts)))
4099  return TRUE;
4100 
4101  if (ppar->subpart_fields)
4102  {
4103  my_bitmap_map *buf;
4104  uint32 bufsize= bitmap_buffer_size(ppar->part_info->num_subparts);
4105  if (!(buf= (my_bitmap_map*) alloc_root(alloc, bufsize)))
4106  return TRUE;
4107  bitmap_init(&ppar->subparts_bitmap, buf, ppar->part_info->num_subparts,
4108  FALSE);
4109  }
4110  range_par->key_parts= key_part;
4111  Field **field= (ppar->part_fields)? part_info->part_field_array :
4112  part_info->subpart_field_array;
4113  bool in_subpart_fields= FALSE;
4114  for (uint part= 0; part < total_parts; part++, key_part++)
4115  {
4116  key_part->key= 0;
4117  key_part->part= part;
4118  key_part->length= (uint16)(*field)->key_length();
4119  key_part->store_length= (uint16)get_partition_field_store_length(*field);
4120 
4121  DBUG_PRINT("info", ("part %u length %u store_length %u", part,
4122  key_part->length, key_part->store_length));
4123 
4124  key_part->field= (*field);
4125  key_part->image_type = Field::itRAW;
4126  /*
4127  We set keypart flag to 0 here as the only HA_PART_KEY_SEG is checked
4128  in the RangeAnalysisModule.
4129  */
4130  key_part->flag= 0;
4131  /* We don't set key_parts->null_bit as it will not be used */
4132 
4133  ppar->is_part_keypart[part]= !in_subpart_fields;
4134  ppar->is_subpart_keypart[part]= in_subpart_fields;
4135 
4136  /*
4137  Check if this was last field in this array, in this case we
4138  switch to subpartitioning fields. (This will only happens if
4139  there are subpartitioning fields to cater for).
4140  */
4141  if (!*(++field))
4142  {
4143  field= part_info->subpart_field_array;
4144  in_subpart_fields= TRUE;
4145  }
4146  }
4147  range_par->key_parts_end= key_part;
4148 
4149  DBUG_EXECUTE("info", print_partitioning_index(range_par->key_parts,
4150  range_par->key_parts_end););
4151  return FALSE;
4152 }
4153 
4154 
4155 #ifndef DBUG_OFF
4156 
4157 static void print_partitioning_index(KEY_PART *parts, KEY_PART *parts_end)
4158 {
4159  DBUG_ENTER("print_partitioning_index");
4160  DBUG_LOCK_FILE;
4161  fprintf(DBUG_FILE, "partitioning INDEX(");
4162  for (KEY_PART *p=parts; p != parts_end; p++)
4163  {
4164  fprintf(DBUG_FILE, "%s%s", p==parts?"":" ,", p->field->field_name);
4165  }
4166  fputs(");\n", DBUG_FILE);
4167  DBUG_UNLOCK_FILE;
4168  DBUG_VOID_RETURN;
4169 }
4170 
4171 
4172 /* Print a "c1 < keypartX < c2" - type interval into debug trace. */
4173 static void dbug_print_segment_range(SEL_ARG *arg, KEY_PART *part)
4174 {
4175  DBUG_ENTER("dbug_print_segment_range");
4176  DBUG_LOCK_FILE;
4177  if (!(arg->min_flag & NO_MIN_RANGE))
4178  {
4179  store_key_image_to_rec(part->field, arg->min_value, part->length);
4180  part->field->dbug_print();
4181  if (arg->min_flag & NEAR_MIN)
4182  fputs(" < ", DBUG_FILE);
4183  else
4184  fputs(" <= ", DBUG_FILE);
4185  }
4186 
4187  fprintf(DBUG_FILE, "%s", part->field->field_name);
4188 
4189  if (!(arg->max_flag & NO_MAX_RANGE))
4190  {
4191  if (arg->max_flag & NEAR_MAX)
4192  fputs(" < ", DBUG_FILE);
4193  else
4194  fputs(" <= ", DBUG_FILE);
4195  store_key_image_to_rec(part->field, arg->max_value, part->length);
4196  part->field->dbug_print();
4197  }
4198  fputs("\n", DBUG_FILE);
4199  DBUG_UNLOCK_FILE;
4200  DBUG_VOID_RETURN;
4201 }
4202 
4203 
4204 /*
4205  Print a singlepoint multi-keypart range interval to debug trace
4206 
4207  SYNOPSIS
4208  dbug_print_singlepoint_range()
4209  start Array of SEL_ARG* ptrs representing conditions on key parts
4210  num Number of elements in the array.
4211 
4212  DESCRIPTION
4213  This function prints a "keypartN=constN AND ... AND keypartK=constK"-type
4214  interval to debug trace.
4215 */
4216 
4217 static void dbug_print_singlepoint_range(SEL_ARG **start, uint num)
4218 {
4219  DBUG_ENTER("dbug_print_singlepoint_range");
4220  DBUG_LOCK_FILE;
4221  SEL_ARG **end= start + num;
4222 
4223  for (SEL_ARG **arg= start; arg != end; arg++)
4224  {
4225  Field *field= (*arg)->field;
4226  fprintf(DBUG_FILE, "%s%s=", (arg==start)?"":", ", field->field_name);
4227  field->dbug_print();
4228  }
4229  fputs("\n", DBUG_FILE);
4230  DBUG_UNLOCK_FILE;
4231  DBUG_VOID_RETURN;
4232 }
4233 #endif
4234 
4235 /****************************************************************************
4236  * Partition pruning code ends
4237  ****************************************************************************/
4238 #endif
4239 
4240 
4241 /*
4242  Get best plan for a SEL_IMERGE disjunctive expression.
4243  SYNOPSIS
4244  get_best_disjunct_quick()
4245  param Parameter from check_quick_select function
4246  imerge Expression to use
4247  read_time Don't create scans with cost > read_time
4248 
4249  NOTES
4250  index_merge cost is calculated as follows:
4251  index_merge_cost =
4252  cost(index_reads) + (see #1)
4253  cost(rowid_to_row_scan) + (see #2)
4254  cost(unique_use) (see #3)
4255 
4256  1. cost(index_reads) =SUM_i(cost(index_read_i))
4257  For non-CPK scans,
4258  cost(index_read_i) = {cost of ordinary 'index only' scan}
4259  For CPK scan,
4260  cost(index_read_i) = {cost of non-'index only' scan}
4261 
4262  2. cost(rowid_to_row_scan)
4263  If table PK is clustered then
4264  cost(rowid_to_row_scan) =
4265  {cost of ordinary clustered PK scan with n_ranges=n_rows}
4266 
4267  Otherwise, we use the following model to calculate costs:
4268  We need to retrieve n_rows rows from file that occupies n_blocks blocks.
4269  We assume that offsets of rows we need are independent variates with
4270  uniform distribution in [0..max_file_offset] range.
4271 
4272  We'll denote block as "busy" if it contains row(s) we need to retrieve
4273  and "empty" if doesn't contain rows we need.
4274 
4275  Probability that a block is empty is (1 - 1/n_blocks)^n_rows (this
4276  applies to any block in file). Let x_i be a variate taking value 1 if
4277  block #i is empty and 0 otherwise.
4278 
4279  Then E(x_i) = (1 - 1/n_blocks)^n_rows;
4280 
4281  E(n_empty_blocks) = E(sum(x_i)) = sum(E(x_i)) =
4282  = n_blocks * ((1 - 1/n_blocks)^n_rows) =
4283  ~= n_blocks * exp(-n_rows/n_blocks).
4284 
4285  E(n_busy_blocks) = n_blocks*(1 - (1 - 1/n_blocks)^n_rows) =
4286  ~= n_blocks * (1 - exp(-n_rows/n_blocks)).
4287 
4288  Average size of "hole" between neighbor non-empty blocks is
4289  E(hole_size) = n_blocks/E(n_busy_blocks).
4290 
4291  The total cost of reading all needed blocks in one "sweep" is:
4292 
4293  E(n_busy_blocks)*
4294  (DISK_SEEK_BASE_COST + DISK_SEEK_PROP_COST*n_blocks/E(n_busy_blocks)).
4295 
4296  3. Cost of Unique use is calculated in Unique::get_use_cost function.
4297 
4298  ROR-union cost is calculated in the same way index_merge, but instead of
4299  Unique a priority queue is used.
4300 
4301  RETURN
4302  Created read plan
4303  NULL - Out of memory or no read scan could be built.
4304 */
4305 
4306 static
4307 TABLE_READ_PLAN *get_best_disjunct_quick(PARAM *param, SEL_IMERGE *imerge,
4308  double read_time)
4309 {
4310  SEL_TREE **ptree;
4311  TRP_INDEX_MERGE *imerge_trp= NULL;
4312  uint n_child_scans= imerge->trees_next - imerge->trees;
4313  TRP_RANGE **range_scans;
4314  TRP_RANGE **cur_child;
4315  TRP_RANGE **cpk_scan= NULL;
4316  bool imerge_too_expensive= FALSE;
4317  double imerge_cost= 0.0;
4318  ha_rows cpk_scan_records= 0;
4319  ha_rows non_cpk_scan_records= 0;
4320  bool pk_is_clustered= param->table->file->primary_key_is_clustered();
4321  bool all_scans_ror_able= TRUE;
4322  bool all_scans_rors= TRUE;
4323  uint unique_calc_buff_size;
4324  TABLE_READ_PLAN **roru_read_plans;
4325  TABLE_READ_PLAN **cur_roru_plan;
4326  double roru_index_costs;
4327  ha_rows roru_total_records;
4328  double roru_intersect_part= 1.0;
4329  DBUG_ENTER("get_best_disjunct_quick");
4330  DBUG_PRINT("info", ("Full table scan cost: %g", read_time));
4331 
4332  DBUG_ASSERT(param->table->file->stats.records);
4333 
4334  Opt_trace_context * const trace= &param->thd->opt_trace;
4335  Opt_trace_object trace_best_disjunct(trace);
4336  if (!(range_scans= (TRP_RANGE**)alloc_root(param->mem_root,
4337  sizeof(TRP_RANGE*)*
4338  n_child_scans)))
4339  DBUG_RETURN(NULL);
4340  // Note: to_merge.end() is called to close this object after this for-loop.
4341  Opt_trace_array to_merge(trace, "indices_to_merge");
4342  /*
4343  Collect best 'range' scan for each of disjuncts, and, while doing so,
4344  analyze possibility of ROR scans. Also calculate some values needed by
4345  other parts of the code.
4346  */
4347  for (ptree= imerge->trees, cur_child= range_scans;
4348  ptree != imerge->trees_next;
4349  ptree++, cur_child++)
4350  {
4351  DBUG_EXECUTE("info", print_sel_tree(param, *ptree, &(*ptree)->keys_map,
4352  "tree in SEL_IMERGE"););
4353  Opt_trace_object trace_idx(trace);
4354  if (!(*cur_child=
4355  get_key_scans_params(param, *ptree, true, false, read_time)))
4356  {
4357  /*
4358  One of index scans in this index_merge is more expensive than entire
4359  table read for another available option. The entire index_merge (and
4360  any possible ROR-union) will be more expensive then, too. We continue
4361  here only to update SQL_SELECT members.
4362  */
4363  imerge_too_expensive= true;
4364  }
4365  if (imerge_too_expensive)
4366  {
4367  trace_idx.add("chosen", false).add_alnum("cause", "cost");
4368  continue;
4369  }
4370 
4371  const uint keynr_in_table= param->real_keynr[(*cur_child)->key_idx];
4372  imerge_cost += (*cur_child)->read_cost;
4373  all_scans_ror_able &= ((*ptree)->n_ror_scans > 0);
4374  all_scans_rors &= (*cur_child)->is_ror;
4375  if (pk_is_clustered &&
4376  keynr_in_table == param->table->s->primary_key)
4377  {
4378  cpk_scan= cur_child;
4379  cpk_scan_records= (*cur_child)->records;
4380  }
4381  else
4382  non_cpk_scan_records += (*cur_child)->records;
4383 
4384  trace_idx.
4385  add_utf8("index_to_merge", param->table->key_info[keynr_in_table].name).
4386  add("cumulated_cost", imerge_cost);
4387  }
4388 
4389  // Note: to_merge trace object is closed here
4390  to_merge.end();
4391 
4392 
4393  trace_best_disjunct.add("cost_of_reading_ranges", imerge_cost);
4394  if (imerge_too_expensive || (imerge_cost > read_time) ||
4395  ((non_cpk_scan_records+cpk_scan_records >= param->table->file->stats.records) &&
4396  read_time != DBL_MAX))
4397  {
4398  /*
4399  Bail out if it is obvious that both index_merge and ROR-union will be
4400  more expensive
4401  */
4402  DBUG_PRINT("info", ("Sum of index_merge scans is more expensive than "
4403  "full table scan, bailing out"));
4404  trace_best_disjunct.add("chosen", false).add_alnum("cause", "cost");
4405  DBUG_RETURN(NULL);
4406  }
4407 
4408  /*
4409  If all scans happen to be ROR, proceed to generate a ROR-union plan (it's
4410  guaranteed to be cheaper than non-ROR union), unless ROR-unions are
4411  disabled in @@optimizer_switch
4412  */
4413  if (all_scans_rors &&
4414  param->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
4415  {
4416  roru_read_plans= (TABLE_READ_PLAN**)range_scans;
4417  trace_best_disjunct.add("use_roworder_union", true).
4418  add_alnum("cause", "always_cheaper_than_not_roworder_retrieval");
4419  goto skip_to_ror_scan;
4420  }
4421 
4422  if (cpk_scan)
4423  {
4424  /*
4425  Add one ROWID comparison for each row retrieved on non-CPK scan. (it
4426  is done in QUICK_RANGE_SELECT::row_in_ranges)
4427  */
4428  const double rid_comp_cost= non_cpk_scan_records * ROWID_COMPARE_COST;
4429  imerge_cost+= rid_comp_cost;
4430  trace_best_disjunct.add("cost_of_mapping_rowid_in_non_clustered_pk_scan",
4431  rid_comp_cost);
4432  }
4433 
4434  /* Calculate cost(rowid_to_row_scan) */
4435  {
4436  Cost_estimate sweep_cost;
4437  JOIN *join= param->thd->lex->select_lex.join;
4438  const bool is_interrupted= join && join->tables != 1;
4439  get_sweep_read_cost(param->table, non_cpk_scan_records, is_interrupted,
4440  &sweep_cost);
4441  const double sweep_total_cost= sweep_cost.total_cost();
4442  imerge_cost+= sweep_total_cost;
4443  trace_best_disjunct.add("cost_sort_rowid_and_read_disk",
4444  sweep_total_cost);
4445  }
4446  DBUG_PRINT("info",("index_merge cost with rowid-to-row scan: %g",
4447  imerge_cost));
4448  if (imerge_cost > read_time ||
4449  !param->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE_SORT_UNION))
4450  {
4451  trace_best_disjunct.add("use_roworder_index_merge", true).
4452  add_alnum("cause", "cost");
4453  goto build_ror_index_merge;
4454  }
4455 
4456  /* Add Unique operations cost */
4457  unique_calc_buff_size=
4458  Unique::get_cost_calc_buff_size((ulong)non_cpk_scan_records,
4459  param->table->file->ref_length,
4460  param->thd->variables.sortbuff_size);
4461  if (param->imerge_cost_buff_size < unique_calc_buff_size)
4462  {
4463  if (!(param->imerge_cost_buff= (uint*)alloc_root(param->mem_root,
4464  unique_calc_buff_size)))
4465  DBUG_RETURN(NULL);
4466  param->imerge_cost_buff_size= unique_calc_buff_size;
4467  }
4468 
4469  {
4470  const double dup_removal_cost=
4471  Unique::get_use_cost(param->imerge_cost_buff,
4472  (uint)non_cpk_scan_records,
4473  param->table->file->ref_length,
4474  param->thd->variables.sortbuff_size);
4475 
4476  trace_best_disjunct.add("cost_duplicate_removal", dup_removal_cost);
4477  imerge_cost += dup_removal_cost;
4478  trace_best_disjunct.add("total_cost", imerge_cost);
4479  DBUG_PRINT("info",("index_merge total cost: %g (wanted: less then %g)",
4480  imerge_cost, read_time));
4481  }
4482  if (imerge_cost < read_time)
4483  {
4484  if ((imerge_trp= new (param->mem_root)TRP_INDEX_MERGE))
4485  {
4486  imerge_trp->read_cost= imerge_cost;
4487  imerge_trp->records= non_cpk_scan_records + cpk_scan_records;
4488  imerge_trp->records= min(imerge_trp->records,
4489  param->table->file->stats.records);
4490  imerge_trp->range_scans= range_scans;
4491  imerge_trp->range_scans_end= range_scans + n_child_scans;
4492  read_time= imerge_cost;
4493  }
4494  }
4495 
4496 build_ror_index_merge:
4497  if (!all_scans_ror_able ||
4498  param->thd->lex->sql_command == SQLCOM_DELETE ||
4499  !param->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE_UNION))
4500  DBUG_RETURN(imerge_trp);
4501 
4502  /* Ok, it is possible to build a ROR-union, try it. */
4503  if (!(roru_read_plans=
4504  (TABLE_READ_PLAN**)alloc_root(param->mem_root,
4505  sizeof(TABLE_READ_PLAN*)*
4506  n_child_scans)))
4507  DBUG_RETURN(imerge_trp);
4508 skip_to_ror_scan:
4509  roru_index_costs= 0.0;
4510  roru_total_records= 0;
4511  cur_roru_plan= roru_read_plans;
4512 
4513  /*
4514  Note: trace_analyze_ror.end() is called to close this object after
4515  this for-loop.
4516  */
4517  Opt_trace_array trace_analyze_ror(trace, "analyzing_roworder_scans");
4518  /* Find 'best' ROR scan for each of trees in disjunction */
4519  for (ptree= imerge->trees, cur_child= range_scans;
4520  ptree != imerge->trees_next;
4521  ptree++, cur_child++, cur_roru_plan++)
4522  {
4523  Opt_trace_object trp_info(trace);
4524  if (unlikely(trace->is_started()))
4525  (*cur_child)->trace_basic_info(param, &trp_info);
4526 
4527  /*
4528  Assume the best ROR scan is the one that has cheapest
4529  full-row-retrieval scan cost.
4530  Also accumulate index_only scan costs as we'll need them to
4531  calculate overall index_intersection cost.
4532  */
4533  double cost;
4534  if ((*cur_child)->is_ror)
4535  {
4536  /* Ok, we have index_only cost, now get full rows scan cost */
4537  cost= param->table->file->
4538  read_time(param->real_keynr[(*cur_child)->key_idx], 1,
4539  (*cur_child)->records) +
4540  rows2double((*cur_child)->records) * ROW_EVALUATE_COST;
4541  }
4542  else
4543  cost= read_time;
4544 
4545  TABLE_READ_PLAN *prev_plan= *cur_child;
4546  if (!(*cur_roru_plan= get_best_ror_intersect(param, *ptree, cost)))
4547  {
4548  if (prev_plan->is_ror)
4549  *cur_roru_plan= prev_plan;
4550  else
4551  DBUG_RETURN(imerge_trp);
4552  roru_index_costs += (*cur_roru_plan)->read_cost;
4553  }
4554  else
4555  roru_index_costs +=
4556  ((TRP_ROR_INTERSECT*)(*cur_roru_plan))->index_scan_costs;
4557  roru_total_records += (*cur_roru_plan)->records;
4558  roru_intersect_part *= (*cur_roru_plan)->records /
4559  param->table->file->stats.records;
4560  }
4561  // Note: trace_analyze_ror trace object is closed here
4562  trace_analyze_ror.end();
4563 
4564  /*
4565  rows to retrieve=
4566  SUM(rows_in_scan_i) - table_rows * PROD(rows_in_scan_i / table_rows).
4567  This is valid because index_merge construction guarantees that conditions
4568  in disjunction do not share key parts.
4569  */
4570  roru_total_records -= (ha_rows)(roru_intersect_part*
4571  param->table->file->stats.records);
4572  /* ok, got a ROR read plan for each of the disjuncts
4573  Calculate cost:
4574  cost(index_union_scan(scan_1, ... scan_n)) =
4575  SUM_i(cost_of_index_only_scan(scan_i)) +
4576  queue_use_cost(rowid_len, n) +
4577  cost_of_row_retrieval
4578  See get_merge_buffers_cost function for queue_use_cost formula derivation.
4579  */
4580  double roru_total_cost;
4581  {
4582  Cost_estimate sweep_cost;
4583  JOIN *join= param->thd->lex->select_lex.join;
4584  const bool is_interrupted= join && join->tables != 1;
4585  get_sweep_read_cost(param->table, roru_total_records, is_interrupted,
4586  &sweep_cost);
4587  roru_total_cost= roru_index_costs +
4588  rows2double(roru_total_records) *
4589  log((double)n_child_scans) * ROWID_COMPARE_COST / M_LN2 +
4590  sweep_cost.total_cost();
4591  }
4592 
4593  trace_best_disjunct.add("index_roworder_union_cost", roru_total_cost).
4594  add("members", n_child_scans);
4595  TRP_ROR_UNION* roru;
4596  if (roru_total_cost < read_time)
4597  {
4598  if ((roru= new (param->mem_root) TRP_ROR_UNION))
4599  {
4600  trace_best_disjunct.add("chosen", true);
4601  roru->first_ror= roru_read_plans;
4602  roru->last_ror= roru_read_plans + n_child_scans;
4603  roru->read_cost= roru_total_cost;
4604  roru->records= roru_total_records;
4605  DBUG_RETURN(roru);
4606  }
4607  }
4608  trace_best_disjunct.add("chosen", false);
4609 
4610  DBUG_RETURN(imerge_trp);
4611 }
4612 
4613 
4614 /*
4615  Create ROR_SCAN_INFO* structure with a single ROR scan on index idx using
4616  sel_arg set of intervals.
4617 
4618  SYNOPSIS
4619  make_ror_scan()
4620  param Parameter from test_quick_select function
4621  idx Index of key in param->keys
4622  sel_arg Set of intervals for a given key
4623 
4624  RETURN
4625  NULL - out of memory
4626  ROR scan structure containing a scan for {idx, sel_arg}
4627 */
4628 
4629 static
4630 ROR_SCAN_INFO *make_ror_scan(const PARAM *param, int idx, SEL_ARG *sel_arg)
4631 {
4632  ROR_SCAN_INFO *ror_scan;
4633  my_bitmap_map *bitmap_buf1;
4634  my_bitmap_map *bitmap_buf2;
4635  uint keynr;
4636  DBUG_ENTER("make_ror_scan");
4637 
4638  if (!(ror_scan= (ROR_SCAN_INFO*)alloc_root(param->mem_root,
4639  sizeof(ROR_SCAN_INFO))))
4640  DBUG_RETURN(NULL);
4641 
4642  ror_scan->idx= idx;
4643  ror_scan->keynr= keynr= param->real_keynr[idx];
4644  ror_scan->sel_arg= sel_arg;
4645  ror_scan->records= param->table->quick_rows[keynr];
4646 
4647  if (!(bitmap_buf1= (my_bitmap_map*) alloc_root(param->mem_root,
4648  param->fields_bitmap_size)))
4649  DBUG_RETURN(NULL);
4650  if (!(bitmap_buf2= (my_bitmap_map*) alloc_root(param->mem_root,
4651  param->fields_bitmap_size)))
4652  DBUG_RETURN(NULL);
4653 
4654  if (bitmap_init(&ror_scan->covered_fields, bitmap_buf1,
4655  param->table->s->fields, FALSE))
4656  DBUG_RETURN(NULL);
4657  if (bitmap_init(&ror_scan->covered_fields_remaining, bitmap_buf2,
4658  param->table->s->fields, FALSE))
4659  DBUG_RETURN(NULL);
4660 
4661  bitmap_clear_all(&ror_scan->covered_fields);
4662 
4663  KEY_PART_INFO *key_part= param->table->key_info[keynr].key_part;
4664  KEY_PART_INFO *key_part_end=
4665  key_part + param->table->key_info[keynr].user_defined_key_parts;
4666  for (;key_part != key_part_end; ++key_part)
4667  {
4668  if (bitmap_is_set(&param->needed_fields, key_part->fieldnr-1))
4669  bitmap_set_bit(&ror_scan->covered_fields, key_part->fieldnr-1);
4670  }
4671  bitmap_copy(&ror_scan->covered_fields_remaining, &ror_scan->covered_fields);
4672 
4673  double rows= rows2double(param->table->quick_rows[ror_scan->keynr]);
4674  ror_scan->index_read_cost=
4675  param->table->file->index_only_read_time(ror_scan->keynr, rows);
4676  DBUG_RETURN(ror_scan);
4677 }
4678 
4679 
4691 static bool is_better_intersect_match(const ROR_SCAN_INFO *scan1,
4692  const ROR_SCAN_INFO *scan2)
4693 {
4694  if (scan1 == scan2)
4695  return false;
4696 
4697  if (scan1->num_covered_fields_remaining >
4699  return false;
4700 
4701  if (scan1->num_covered_fields_remaining <
4703  return true;
4704 
4705  return (scan1->records > scan2->records);
4706 }
4707 
4727 static void find_intersect_order(ROR_SCAN_INFO **start,
4728  ROR_SCAN_INFO **end,
4729  const PARAM *param)
4730 {
4731  // nothing to sort if there are only zero or one ROR scans
4732  if ((start == end) || (start + 1 == end))
4733  return;
4734 
4735  /*
4736  Bitmap of fields we would like the ROR scans to cover. Will be
4737  modified by the loop below so that when we're looking for a ROR
4738  scan in position 'x' in the ordering, all fields covered by ROR
4739  scans 0,...,x-1 have been removed.
4740  */
4741  MY_BITMAP fields_to_cover;
4742  my_bitmap_map *map;
4743  if (!(map= (my_bitmap_map*) alloc_root(param->mem_root,
4744  param->fields_bitmap_size)))
4745  return;
4746  bitmap_init(&fields_to_cover, map, param->needed_fields.n_bits, FALSE);
4747  bitmap_copy(&fields_to_cover, &param->needed_fields);
4748 
4749  // Sort ROR scans in [start,...,end-1]
4750  for (ROR_SCAN_INFO **place= start; place < (end - 1); place++)
4751  {
4752  /* Placeholder for the best ROR scan found for position 'place' so far */
4753  ROR_SCAN_INFO **best= place;
4754  ROR_SCAN_INFO **current= place + 1;
4755 
4756  {
4757  /*
4758  Calculate how many fields in 'fields_to_cover' not already
4759  covered by [start,...,place-1] the 'best' index covers. The
4760  result is used in is_better_intersect_match() and is valid
4761  when finding the best ROR scan for position 'place' only.
4762  */
4763  bitmap_intersect(&(*best)->covered_fields_remaining, &fields_to_cover);
4764  (*best)->num_covered_fields_remaining=
4765  bitmap_bits_set(&(*best)->covered_fields_remaining);
4766  }
4767  for (; current < end; current++)
4768  {
4769  {
4770  /*
4771  Calculate how many fields in 'fields_to_cover' not already
4772  covered by [start,...,place-1] the 'current' index covers.
4773  The result is used in is_better_intersect_match() and is
4774  valid when finding the best ROR scan for position 'place' only.
4775  */
4776  bitmap_intersect(&(*current)->covered_fields_remaining,
4777  &fields_to_cover);
4778  (*current)->num_covered_fields_remaining=
4779  bitmap_bits_set(&(*current)->covered_fields_remaining);
4780 
4781  /*
4782  No need to compare with 'best' if 'current' does not
4783  contribute with uncovered fields.
4784  */
4785  if ((*current)->num_covered_fields_remaining == 0)
4786  continue;
4787  }
4788 
4789  if (is_better_intersect_match(*best, *current))
4790  best= current;
4791  }
4792 
4793  /*
4794  'best' is now the ROR scan that will be sorted in position
4795  'place'. When searching for the best ROR scans later in the sort
4796  sequence we do not need coverage of the fields covered by 'best'
4797  */
4798  bitmap_subtract(&fields_to_cover, &(*best)->covered_fields);
4799  if (best != place)
4800  swap_variables(ROR_SCAN_INFO*, *best, *place);
4801 
4802  if (bitmap_is_clear_all(&fields_to_cover))
4803  return; // No more fields to cover
4804  }
4805 }
4806 
4807 /* Auxiliary structure for incremental ROR-intersection creation */
4808 typedef struct
4809 {
4810  const PARAM *param;
4811  MY_BITMAP covered_fields; /* union of fields covered by all scans */
4812  /*
4813  Fraction of table records that satisfies conditions of all scans.
4814  This is the number of full records that will be retrieved if a
4815  non-index_only index intersection will be employed.
4816  */
4817  double out_rows;
4818  /* TRUE if covered_fields is a superset of needed_fields */
4819  bool is_covering;
4820 
4821  ha_rows index_records; /* sum(#records to look in indexes) */
4822  double index_scan_costs; /* SUM(cost of 'index-only' scans) */
4823  double total_cost;
4825 
4826 
4827 /*
4828  Allocate a ROR_INTERSECT_INFO and initialize it to contain zero scans.
4829 
4830  SYNOPSIS
4831  ror_intersect_init()
4832  param Parameter from test_quick_select
4833 
4834  RETURN
4835  allocated structure
4836  NULL on error
4837 */
4838 
4839 static
4840 ROR_INTERSECT_INFO* ror_intersect_init(const PARAM *param)
4841 {
4842  ROR_INTERSECT_INFO *info;
4843  my_bitmap_map* buf;
4844  if (!(info= (ROR_INTERSECT_INFO*)alloc_root(param->mem_root,
4845  sizeof(ROR_INTERSECT_INFO))))
4846  return NULL;
4847  info->param= param;
4848  if (!(buf= (my_bitmap_map*) alloc_root(param->mem_root,
4849  param->fields_bitmap_size)))
4850  return NULL;
4851  if (bitmap_init(&info->covered_fields, buf, param->table->s->fields,
4852  FALSE))
4853  return NULL;
4854  info->is_covering= FALSE;
4855  info->index_scan_costs= 0.0;
4856  info->total_cost= 0.0;
4857  info->index_records= 0;
4858  info->out_rows= (double) param->table->file->stats.records;
4859  bitmap_clear_all(&info->covered_fields);
4860  return info;
4861 }
4862 
4863 void ror_intersect_cpy(ROR_INTERSECT_INFO *dst, const ROR_INTERSECT_INFO *src)
4864 {
4865  dst->param= src->param;
4866  memcpy(dst->covered_fields.bitmap, src->covered_fields.bitmap,
4867  no_bytes_in_map(&src->covered_fields));
4868  dst->out_rows= src->out_rows;
4869  dst->is_covering= src->is_covering;
4870  dst->index_records= src->index_records;
4871  dst->index_scan_costs= src->index_scan_costs;
4872  dst->total_cost= src->total_cost;
4873 }
4874 
4875 
4876 /*
4877  Get selectivity of adding a ROR scan to the ROR-intersection.
4878 
4879  SYNOPSIS
4880  ror_scan_selectivity()
4881  info ROR-interection, an intersection of ROR index scans
4882  scan ROR scan that may or may not improve the selectivity
4883  of 'info'
4884 
4885  NOTES
4886  Suppose we have conditions on several keys
4887  cond=k_11=c_11 AND k_12=c_12 AND ... // key_parts of first key in 'info'
4888  k_21=c_21 AND k_22=c_22 AND ... // key_parts of second key in 'info'
4889  ...
4890  k_n1=c_n1 AND k_n3=c_n3 AND ... (1) //key_parts of 'scan'
4891 
4892  where k_ij may be the same as any k_pq (i.e. keys may have common parts).
4893 
4894  Note that for ROR retrieval, only equality conditions are usable so there
4895  are no open ranges (e.g., k_ij > c_ij) in 'scan' or 'info'
4896 
4897  A full row is retrieved if entire condition holds.
4898 
4899  The recursive procedure for finding P(cond) is as follows:
4900 
4901  First step:
4902  Pick 1st part of 1st key and break conjunction (1) into two parts:
4903  cond= (k_11=c_11 AND R)
4904 
4905  Here R may still contain condition(s) equivalent to k_11=c_11.
4906  Nevertheless, the following holds:
4907 
4908  P(k_11=c_11 AND R) = P(k_11=c_11) * P(R | k_11=c_11).
4909 
4910  Mark k_11 as fixed field (and satisfied condition) F, save P(F),
4911  save R to be cond and proceed to recursion step.
4912 
4913  Recursion step:
4914  We have a set of fixed fields/satisfied conditions) F, probability P(F),
4915  and remaining conjunction R
4916  Pick next key part on current key and its condition "k_ij=c_ij".
4917  We will add "k_ij=c_ij" into F and update P(F).
4918  Lets denote k_ij as t, R = t AND R1, where R1 may still contain t. Then
4919 
4920  P((t AND R1)|F) = P(t|F) * P(R1|t|F) = P(t|F) * P(R1|(t AND F)) (2)
4921 
4922  (where '|' mean conditional probability, not "or")
4923 
4924  Consider the first multiplier in (2). One of the following holds:
4925  a) F contains condition on field used in t (i.e. t AND F = F).
4926  Then P(t|F) = 1
4927 
4928  b) F doesn't contain condition on field used in t. Then F and t are
4929  considered independent.
4930 
4931  P(t|F) = P(t|(fields_before_t_in_key AND other_fields)) =
4932  = P(t|fields_before_t_in_key).
4933 
4934  P(t|fields_before_t_in_key) = #records(fields_before_t_in_key) /
4935  #records(fields_before_t_in_key, t)
4936 
4937  The second multiplier is calculated by applying this step recursively.
4938 
4939  IMPLEMENTATION
4940  This function calculates the result of application of the "recursion step"
4941  described above for all fixed key members of a single key, accumulating set
4942  of covered fields, selectivity, etc.
4943 
4944  The calculation is conducted as follows:
4945  Lets denote #records(keypart1, ... keypartK) as n_k. We need to calculate
4946 
4947  n_{k1} n_{k2}
4948  --------- * --------- * .... (3)
4949  n_{k1-1} n_{k2-1}
4950 
4951  where k1,k2,... are key parts which fields were not yet marked as fixed
4952  ( this is result of application of option b) of the recursion step for
4953  parts of a single key).
4954  Since it is reasonable to expect that most of the fields are not marked
4955  as fixed, we calculate (3) as
4956 
4957  n_{i1} n_{i2}
4958  (3) = n_{max_key_part} / ( --------- * --------- * .... )
4959  n_{i1-1} n_{i2-1}
4960 
4961  where i1,i2, .. are key parts that were already marked as fixed.
4962 
4963  In order to minimize number of expensive records_in_range calls we
4964  group and reduce adjacent fractions. Note that on the optimizer's
4965  request, index statistics may be used instead of records_in_range
4966  @see RANGE_OPT_PARAM::use_index_statistics.
4967 
4968  RETURN
4969  Selectivity of given ROR scan, a number between 0 and 1. 1 means that
4970  adding 'scan' to the intersection does not improve the selectivity.
4971 */
4972 
4973 static double ror_scan_selectivity(const ROR_INTERSECT_INFO *info,
4974  const ROR_SCAN_INFO *scan)
4975 {
4976  double selectivity_mult= 1.0;
4977  const TABLE * const table= info->param->table;
4978  const KEY_PART_INFO * const key_part= table->key_info[scan->keynr].key_part;
4986  uchar key_val[MAX_KEY_LENGTH+MAX_FIELD_WIDTH];
4987  uchar *key_ptr= key_val;
4988  SEL_ARG *sel_arg, *tuple_arg= NULL;
4989  key_part_map keypart_map= 0;
4990  bool cur_covered;
4991  bool prev_covered= test(bitmap_is_set(&info->covered_fields,
4992  key_part->fieldnr-1));
4993  key_range min_range;
4994  key_range max_range;
4995  min_range.key= key_val;
4996  min_range.flag= HA_READ_KEY_EXACT;
4997  max_range.key= key_val;
4998  max_range.flag= HA_READ_AFTER_KEY;
4999  ha_rows prev_records= table->file->stats.records;
5000  DBUG_ENTER("ror_scan_selectivity");
5001 
5002  for (sel_arg= scan->sel_arg; sel_arg;
5003  sel_arg= sel_arg->next_key_part)
5004  {
5005  DBUG_PRINT("info",("sel_arg step"));
5006  cur_covered= test(bitmap_is_set(&info->covered_fields,
5007  key_part[sel_arg->part].fieldnr-1));
5008  if (cur_covered != prev_covered)
5009  {
5010  /* create (part1val, ..., part{n-1}val) tuple. */
5011  bool is_null_range= false;
5012  ha_rows records;
5013  if (!tuple_arg)
5014  {
5015  tuple_arg= scan->sel_arg;
5016  /* Here we use the length of the first key part */
5017  tuple_arg->store_min(key_part[0].store_length, &key_ptr, 0);
5018  is_null_range|= tuple_arg->is_null_interval();
5019  keypart_map= 1;
5020  }
5021  while (tuple_arg->next_key_part != sel_arg)
5022  {
5023  tuple_arg= tuple_arg->next_key_part;
5024  tuple_arg->store_min(key_part[tuple_arg->part].store_length,
5025  &key_ptr, 0);
5026  is_null_range|= tuple_arg->is_null_interval();
5027  keypart_map= (keypart_map << 1) | 1;
5028  }
5029  min_range.length= max_range.length= (size_t) (key_ptr - key_val);
5030  min_range.keypart_map= max_range.keypart_map= keypart_map;
5031 
5032  /*
5033  Get the number of rows in this range. This is done by calling
5034  records_in_range() unless all these are true:
5035  1) The user has requested that index statistics should be used
5036  for equality ranges to avoid the incurred overhead of
5037  index dives in records_in_range()
5038  2) The range is not on the form "x IS NULL". The reason is
5039  that the number of rows with this value are likely to be
5040  very different than the values in the index statistics
5041  3) Index statistics is available.
5042  @see key_val
5043  */
5044  if (!info->param->use_index_statistics || // (1)
5045  is_null_range || // (2)
5046  !(records= table->key_info[scan->keynr].
5047  rec_per_key[tuple_arg->part])) // (3)
5048  {
5049  DBUG_EXECUTE_IF("crash_records_in_range", DBUG_SUICIDE(););
5050  DBUG_ASSERT(min_range.length > 0);
5051  records= (table->file->
5052  records_in_range(scan->keynr, &min_range, &max_range));
5053  }
5054  if (cur_covered)
5055  {
5056  /* uncovered -> covered */
5057  double tmp= rows2double(records)/rows2double(prev_records);
5058  DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
5059  selectivity_mult *= tmp;
5060  prev_records= HA_POS_ERROR;
5061  }
5062  else
5063  {
5064  /* covered -> uncovered */
5065  prev_records= records;
5066  }
5067  }
5068  prev_covered= cur_covered;
5069  }
5070  if (!prev_covered)
5071  {
5072  double tmp= rows2double(table->quick_rows[scan->keynr]) /
5073  rows2double(prev_records);
5074  DBUG_PRINT("info", ("Selectivity multiplier: %g", tmp));
5075  selectivity_mult *= tmp;
5076  }
5077  // Todo: This assert fires in PB sysqa RQG tests.
5078  // DBUG_ASSERT(selectivity_mult <= 1.0);
5079  DBUG_PRINT("info", ("Returning multiplier: %g", selectivity_mult));
5080  DBUG_RETURN(selectivity_mult);
5081 }
5082 
5083 
5084 /*
5085  Check if adding a ROR scan to a ROR-intersection reduces its cost of
5086  ROR-intersection and if yes, update parameters of ROR-intersection,
5087  including its cost.
5088 
5089  SYNOPSIS
5090  ror_intersect_add()
5091  param Parameter from test_quick_select
5092  info ROR-intersection structure to add the scan to.
5093  ror_scan ROR scan info to add.
5094  is_cpk_scan If TRUE, add the scan as CPK scan (this can be inferred
5095  from other parameters and is passed separately only to
5096  avoid duplicating the inference code)
5097  trace_costs Optimizer trace object cost details are added to
5098 
5099  NOTES
5100  Adding a ROR scan to ROR-intersect "makes sense" iff the cost of ROR-
5101  intersection decreases. The cost of ROR-intersection is calculated as
5102  follows:
5103 
5104  cost= SUM_i(key_scan_cost_i) + cost_of_full_rows_retrieval
5105 
5106  When we add a scan the first increases and the second decreases.
5107 
5108  cost_of_full_rows_retrieval=
5109  (union of indexes used covers all needed fields) ?
5110  cost_of_sweep_read(E(rows_to_retrieve), rows_in_table) :
5111  0
5112 
5113  E(rows_to_retrieve) = #rows_in_table * ror_scan_selectivity(null, scan1) *
5114  ror_scan_selectivity({scan1}, scan2) * ... *
5115  ror_scan_selectivity({scan1,...}, scanN).
5116  RETURN
5117  TRUE ROR scan added to ROR-intersection, cost updated.
5118  FALSE It doesn't make sense to add this ROR scan to this ROR-intersection.
5119 */
5120 
5121 static bool ror_intersect_add(ROR_INTERSECT_INFO *info,
5122  ROR_SCAN_INFO* ror_scan, bool is_cpk_scan,
5123  Opt_trace_object *trace_costs)
5124 {
5125  double selectivity_mult= 1.0;
5126 
5127  DBUG_ENTER("ror_intersect_add");
5128  DBUG_PRINT("info", ("Current out_rows= %g", info->out_rows));
5129  DBUG_PRINT("info", ("Adding scan on %s",
5130  info->param->table->key_info[ror_scan->keynr].name));
5131  DBUG_PRINT("info", ("is_cpk_scan: %d",is_cpk_scan));
5132 
5133  selectivity_mult = ror_scan_selectivity(info, ror_scan);
5134  if (selectivity_mult == 1.0)
5135  {
5136  /* Don't add this scan if it doesn't improve selectivity. */
5137  DBUG_PRINT("info", ("The scan doesn't improve selectivity."));
5138  DBUG_RETURN(FALSE);
5139  }
5140 
5141  info->out_rows *= selectivity_mult;
5142 
5143  if (is_cpk_scan)
5144  {
5145  /*
5146  CPK scan is used to filter out rows. We apply filtering for
5147  each record of every scan. Assuming ROWID_COMPARE_COST
5148  per check this gives us:
5149  */
5150  const double idx_cost=
5151  rows2double(info->index_records) * ROWID_COMPARE_COST;
5152  info->index_scan_costs+= idx_cost;
5153  trace_costs->add("index_scan_cost", idx_cost);
5154  }
5155  else
5156  {
5157  info->index_records += info->param->table->quick_rows[ror_scan->keynr];
5158  info->index_scan_costs += ror_scan->index_read_cost;
5159  trace_costs->add("index_scan_cost", ror_scan->index_read_cost);
5160  bitmap_union(&info->covered_fields, &ror_scan->covered_fields);
5161  if (!info->is_covering && bitmap_is_subset(&info->param->needed_fields,
5162  &info->covered_fields))
5163  {
5164  DBUG_PRINT("info", ("ROR-intersect is covering now"));
5165  info->is_covering= TRUE;
5166  }
5167  }
5168 
5169  info->total_cost= info->index_scan_costs;
5170  trace_costs->add("cumulated_index_scan_cost", info->index_scan_costs);
5171 
5172  if (!info->is_covering)
5173  {
5174  Cost_estimate sweep_cost;
5175  JOIN *join= info->param->thd->lex->select_lex.join;
5176  const bool is_interrupted= join && join->tables == 1;
5177  get_sweep_read_cost(info->param->table, double2rows(info->out_rows),
5178  is_interrupted, &sweep_cost);
5179  info->total_cost += sweep_cost.total_cost();
5180  trace_costs->add("disk_sweep_cost", sweep_cost.total_cost());
5181  }
5182  else
5183  trace_costs->add("disk_sweep_cost", 0);
5184 
5185  DBUG_PRINT("info", ("New out_rows: %g", info->out_rows));
5186  DBUG_PRINT("info", ("New cost: %g, %scovering", info->total_cost,
5187  info->is_covering?"" : "non-"));
5188  DBUG_RETURN(TRUE);
5189 }
5190 
5191 
5192 /*
5193  Get best ROR-intersection plan using non-covering ROR-intersection search
5194  algorithm. The returned plan may be covering.
5195 
5196  SYNOPSIS
5197  get_best_ror_intersect()
5198  param Parameter from test_quick_select function.
5199  tree Transformed restriction condition to be used to look
5200  for ROR scans.
5201  read_time Do not return read plans with cost > read_time.
5202  are_all_covering [out] set to TRUE if union of all scans covers all
5203  fields needed by the query (and it is possible to build
5204  a covering ROR-intersection)
5205 
5206  NOTES
5207  get_key_scans_params must be called before this function can be called.
5208 
5209  When this function is called by ROR-union construction algorithm it
5210  assumes it is building an uncovered ROR-intersection (and thus # of full
5211  records to be retrieved is wrong here). This is a hack.
5212 
5213  IMPLEMENTATION
5214  The approximate best non-covering plan search algorithm is as follows:
5215 
5216  find_min_ror_intersection_scan()
5217  {
5218  R= select all ROR scans;
5219  order R by (E(#records_matched) * key_record_length).
5220 
5221  S= first(R); -- set of scans that will be used for ROR-intersection
5222  R= R-first(S);
5223  min_cost= cost(S);
5224  min_scan= make_scan(S);
5225  while (R is not empty)
5226  {
5227  firstR= R - first(R);
5228  if (!selectivity(S + firstR < selectivity(S)))
5229  continue;
5230 
5231  S= S + first(R);
5232  if (cost(S) < min_cost)
5233  {
5234  min_cost= cost(S);
5235  min_scan= make_scan(S);
5236  }
5237  }
5238  return min_scan;
5239  }
5240 
5241  See ror_intersect_add function for ROR intersection costs.
5242 
5243  Special handling for Clustered PK scans
5244  Clustered PK contains all table fields, so using it as a regular scan in
5245  index intersection doesn't make sense: a range scan on CPK will be less
5246  expensive in this case.
5247  Clustered PK scan has special handling in ROR-intersection: it is not used
5248  to retrieve rows, instead its condition is used to filter row references
5249  we get from scans on other keys.
5250 
5251  RETURN
5252  ROR-intersection table read plan
5253  NULL if out of memory or no suitable plan found.
5254 */
5255 
5256 static
5257 TRP_ROR_INTERSECT *get_best_ror_intersect(const PARAM *param, SEL_TREE *tree,
5258  double read_time)
5259 {
5260  uint idx;
5261  double min_cost= DBL_MAX;
5262  Opt_trace_context * const trace= &param->thd->opt_trace;
5263  DBUG_ENTER("get_best_ror_intersect");
5264 
5265  Opt_trace_object trace_ror(trace, "analyzing_roworder_intersect");
5266 
5267  if ((tree->n_ror_scans < 2) || !param->table->file->stats.records ||
5268  !param->thd->optimizer_switch_flag(OPTIMIZER_SWITCH_INDEX_MERGE_INTERSECT))
5269  {
5270  trace_ror.add("usable", false);
5271  if (tree->n_ror_scans < 2)
5272  trace_ror.add_alnum("cause", "too_few_roworder_scans");
5273  else
5274  trace_ror.add("need_tracing", true);
5275  DBUG_RETURN(NULL);
5276  }
5277 
5278  if (param->order_direction == ORDER::ORDER_DESC)
5279  DBUG_RETURN(NULL);
5280 
5281  /*
5282  Step1: Collect ROR-able SEL_ARGs and create ROR_SCAN_INFO for each of
5283  them. Also find and save clustered PK scan if there is one.
5284  */
5285  ROR_SCAN_INFO **cur_ror_scan;
5286  ROR_SCAN_INFO *cpk_scan= NULL;
5287  uint cpk_no;
5288  bool cpk_scan_used= FALSE;
5289 
5290  if (!(tree->ror_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
5291  sizeof(ROR_SCAN_INFO*)*
5292  param->keys)))
5293  return NULL;
5294  cpk_no= ((param->table->file->primary_key_is_clustered()) ?
5295  param->table->s->primary_key : MAX_KEY);
5296 
5297  for (idx= 0, cur_ror_scan= tree->ror_scans; idx < param->keys; idx++)
5298  {
5299  ROR_SCAN_INFO *scan;
5300  if (!tree->ror_scans_map.is_set(idx))
5301  continue;
5302  if (!(scan= make_ror_scan(param, idx, tree->keys[idx])))
5303  return NULL;
5304  if (param->real_keynr[idx] == cpk_no)
5305  {
5306  cpk_scan= scan;
5307  tree->n_ror_scans--;
5308  }
5309  else
5310  *(cur_ror_scan++)= scan;
5311  }
5312 
5313  tree->ror_scans_end= cur_ror_scan;
5314  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "original",
5315  tree->ror_scans,
5316  tree->ror_scans_end););
5317  /*
5318  Ok, [ror_scans, ror_scans_end) is array of ptrs to initialized
5319  ROR_SCAN_INFO's.
5320  Step 2: Get best ROR-intersection using an approximate algorithm.
5321  */
5322  find_intersect_order(tree->ror_scans, tree->ror_scans_end, param);
5323 
5324  DBUG_EXECUTE("info",print_ror_scans_arr(param->table, "ordered",
5325  tree->ror_scans,
5326  tree->ror_scans_end););
5327 
5328  ROR_SCAN_INFO **intersect_scans; /* ROR scans used in index intersection */
5329  ROR_SCAN_INFO **intersect_scans_end;
5330  if (!(intersect_scans= (ROR_SCAN_INFO**)alloc_root(param->mem_root,
5331  sizeof(ROR_SCAN_INFO*)*
5332  tree->n_ror_scans)))
5333  return NULL;
5334  intersect_scans_end= intersect_scans;
5335 
5336  /* Create and incrementally update ROR intersection. */
5337  ROR_INTERSECT_INFO *intersect, *intersect_best;
5338  if (!(intersect= ror_intersect_init(param)) ||
5339  !(intersect_best= ror_intersect_init(param)))
5340  return NULL;
5341 
5342  /* [intersect_scans,intersect_scans_best) will hold the best intersection */
5343  ROR_SCAN_INFO **intersect_scans_best;
5344  cur_ror_scan= tree->ror_scans;
5345  intersect_scans_best= intersect_scans;
5346  /*
5347  Note: trace_isect_idx.end() is called to close this object after
5348  this while-loop.
5349  */
5350  Opt_trace_array trace_isect_idx(trace, "intersecting_indices");
5351  while (cur_ror_scan != tree->ror_scans_end && !intersect->is_covering)
5352  {
5353  Opt_trace_object trace_idx(trace);
5354  trace_idx.add_utf8("index",
5355  param->table->key_info[(*cur_ror_scan)->keynr].name);
5356  /* S= S + first(R); R= R - first(R); */
5357  if (!ror_intersect_add(intersect, *cur_ror_scan, FALSE, &trace_idx))
5358  {
5359  trace_idx.add("cumulated_total_cost", intersect->total_cost).
5360  add("usable", false).
5361  add_alnum("cause", "does_not_reduce_cost_of_intersect");
5362  cur_ror_scan++;
5363  continue;
5364  }
5365 
5366  trace_idx.add("cumulated_total_cost", intersect->total_cost).
5367  add("usable", true).
5368  add("matching_rows_now", intersect->out_rows).
5369  add("isect_covering_with_this_index", intersect->is_covering);
5370 
5371  *(intersect_scans_end++)= *(cur_ror_scan++);
5372 
5373  if (intersect->total_cost < min_cost)
5374  {
5375  /* Local minimum found, save it */
5376  ror_intersect_cpy(intersect_best, intersect);
5377  intersect_scans_best= intersect_scans_end;
5378  min_cost = intersect->total_cost;
5379  trace_idx.add("chosen", true);
5380  }
5381  else
5382  {
5383  trace_idx.add("chosen", false).
5384  add_alnum("cause", "does_not_reduce_cost");
5385  }
5386  }
5387  // Note: trace_isect_idx trace object is closed here
5388  trace_isect_idx.end();
5389 
5390  if (intersect_scans_best == intersect_scans)
5391  {
5392  trace_ror.add("chosen", false).
5393  add_alnum("cause", "does_not_increase_selectivity");
5394  DBUG_PRINT("info", ("None of scans increase selectivity"));
5395  DBUG_RETURN(NULL);
5396  }
5397 
5398  DBUG_EXECUTE("info",print_ror_scans_arr(param->table,
5399  "best ROR-intersection",
5400  intersect_scans,
5401  intersect_scans_best););
5402 
5403  uint best_num= intersect_scans_best - intersect_scans;
5404  ror_intersect_cpy(intersect, intersect_best);
5405 
5406  /*
5407  Ok, found the best ROR-intersection of non-CPK key scans.
5408  Check if we should add a CPK scan. If the obtained ROR-intersection is
5409  covering, it doesn't make sense to add CPK scan.
5410  */
5411  { // Scope for trace object
5412  Opt_trace_object trace_cpk(trace, "clustered_pk");
5413  if (cpk_scan && !intersect->is_covering)
5414  {
5415  if (ror_intersect_add(intersect, cpk_scan, TRUE, &trace_cpk) &&
5416  (intersect->total_cost < min_cost))
5417  {
5418  trace_cpk.add("clustered_pk_scan_added_to_intersect", true).
5419  add("cumulated_cost", intersect->total_cost);
5420  cpk_scan_used= TRUE;
5421  intersect_best= intersect; //just set pointer here
5422  }
5423  else
5424  trace_cpk.add("clustered_pk_added_to_intersect", false).
5425  add_alnum("cause", "cost");
5426  }
5427  else
5428  {
5429  trace_cpk.add("clustered_pk_added_to_intersect", false).
5430  add_alnum("cause", cpk_scan ?
5431  "roworder_is_covering" : "no_clustered_pk_index");
5432  }
5433  }
5434  /* Ok, return ROR-intersect plan if we have found one */
5435  TRP_ROR_INTERSECT *trp= NULL;
5436  if (min_cost < read_time && (cpk_scan_used || best_num > 1))
5437  {
5438  if (!(trp= new (param->mem_root) TRP_ROR_INTERSECT))
5439  DBUG_RETURN(trp);
5440  if (!(trp->first_scan=
5441  (ROR_SCAN_INFO**)alloc_root(param->mem_root,
5442  sizeof(ROR_SCAN_INFO*)*best_num)))
5443  DBUG_RETURN(NULL);
5444  memcpy(trp->first_scan, intersect_scans, best_num*sizeof(ROR_SCAN_INFO*));
5445  trp->last_scan= trp->first_scan + best_num;
5446  trp->is_covering= intersect_best->is_covering;
5447  trp->read_cost= intersect_best->total_cost;
5448  /* Prevent divisons by zero */
5449  ha_rows best_rows = double2rows(intersect_best->out_rows);
5450  if (!best_rows)
5451  best_rows= 1;
5452  set_if_smaller(param->table->quick_condition_rows, best_rows);
5453  trp->records= best_rows;
5454  trp->index_scan_costs= intersect_best->index_scan_costs;
5455  trp->cpk_scan= cpk_scan_used? cpk_scan: NULL;
5456 
5457  trace_ror.add("rows", trp->records).
5458  add("cost", trp->read_cost).
5459  add("covering", trp->is_covering).
5460  add("chosen", true);
5461 
5462  DBUG_PRINT("info", ("Returning non-covering ROR-intersect plan:"
5463  "cost %g, records %lu",
5464  trp->read_cost, (ulong) trp->records));
5465  }
5466  else
5467  {
5468  trace_ror.add("chosen", false).
5469  add_alnum("cause", (min_cost >= read_time) ? "cost" :
5470  "too_few_indexes_to_merge");
5471 
5472  }
5473  DBUG_RETURN(trp);
5474 }
5475 
5476 /*
5477  Get best "range" table read plan for given SEL_TREE, also update some info
5478 
5479  SYNOPSIS
5480  get_key_scans_params()
5481  param Parameters from test_quick_select
5482  tree Make range select for this SEL_TREE
5483  index_read_must_be_used TRUE <=> assume 'index only' option will be set
5484  (except for clustered PK indexes)
5485  update_tbl_stats TRUE <=> update table->quick_* with information
5486  about range scans we've evaluated.
5487  read_time Maximum cost. i.e. don't create read plans with
5488  cost > read_time.
5489 
5490  DESCRIPTION
5491  Find the best "range" table read plan for given SEL_TREE.
5492  The side effects are
5493  - tree->ror_scans is updated to indicate which scans are ROR scans.
5494  - if update_tbl_stats=TRUE then table->quick_* is updated with info
5495  about every possible range scan.
5496 
5497  RETURN
5498  Best range read plan
5499  NULL if no plan found or error occurred
5500 */
5501 
5502 static TRP_RANGE *get_key_scans_params(PARAM *param, SEL_TREE *tree,
5503  bool index_read_must_be_used,
5504  bool update_tbl_stats,
5505  double read_time)
5506 {
5507  uint idx;
5508  SEL_ARG **key,**end, **key_to_read= NULL;
5509  ha_rows UNINIT_VAR(best_records); /* protected by key_to_read */
5510  uint best_mrr_flags, best_buf_size;
5511  TRP_RANGE* read_plan= NULL;
5512  DBUG_ENTER("get_key_scans_params");
5513  LINT_INIT(best_mrr_flags); /* protected by key_to_read */
5514  LINT_INIT(best_buf_size); /* protected by key_to_read */
5515  Opt_trace_context * const trace= &param->thd->opt_trace;
5516  /*
5517  Note that there may be trees that have type SEL_TREE::KEY but contain no
5518  key reads at all, e.g. tree for expression "key1 is not null" where key1
5519  is defined as "not null".
5520  */
5521  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->keys_map,
5522  "tree scans"););
5523  Opt_trace_array ota(trace, "range_scan_alternatives");
5524 
5525  tree->ror_scans_map.clear_all();
5526  tree->n_ror_scans= 0;
5527  for (idx= 0,key=tree->keys, end=key+param->keys; key != end; key++,idx++)
5528  {
5529  if (*key)
5530  {
5531  ha_rows found_records;
5532  Cost_estimate cost;
5533  double found_read_time;
5534  uint mrr_flags, buf_size;
5535  uint keynr= param->real_keynr[idx];
5536  if ((*key)->type == SEL_ARG::MAYBE_KEY ||
5537  (*key)->maybe_flag)
5538  param->needed_reg->set_bit(keynr);
5539 
5540  bool read_index_only= index_read_must_be_used ? TRUE :
5541  (bool) param->table->covering_keys.is_set(keynr);
5542 
5543  Opt_trace_object trace_idx(trace);
5544  trace_idx.add_utf8("index", param->table->key_info[keynr].name);
5545 
5546  found_records= check_quick_select(param, idx, read_index_only, *key,
5547  update_tbl_stats, &mrr_flags,
5548  &buf_size, &cost);
5549 
5550 #ifdef OPTIMIZER_TRACE
5551  // check_quick_select() says don't use range if it returns HA_POS_ERROR
5552  if (found_records != HA_POS_ERROR &&
5553  param->thd->opt_trace.is_started())
5554  {
5555  Opt_trace_array trace_range(&param->thd->opt_trace, "ranges");
5556 
5557  const KEY &cur_key= param->table->key_info[keynr];
5558  const KEY_PART_INFO *key_part= cur_key.key_part;
5559 
5560  String range_info;
5561  range_info.set_charset(system_charset_info);
5562  append_range_all_keyparts(&trace_range, NULL,
5563  &range_info, *key, key_part);
5564  trace_range.end(); // NOTE: ends the tracing scope
5565 
5566  trace_idx.add("index_dives_for_eq_ranges", !param->use_index_statistics).
5567  add("rowid_ordered", param->is_ror_scan).
5568  add("using_mrr", !(mrr_flags & HA_MRR_USE_DEFAULT_IMPL)).
5569  add("index_only", read_index_only).
5570  add("rows", found_records).
5571  add("cost", cost.total_cost());
5572  }
5573 #endif
5574 
5575  if ((found_records != HA_POS_ERROR) && param->is_ror_scan)
5576  {
5577  tree->n_ror_scans++;
5578  tree->ror_scans_map.set_bit(idx);
5579  }
5580 
5581 
5582  if (found_records != HA_POS_ERROR &&
5583  read_time > (found_read_time= cost.total_cost()))
5584  {
5585  trace_idx.add("chosen", true);
5586  read_time= found_read_time;
5587  best_records= found_records;
5588  key_to_read= key;
5589  best_mrr_flags= mrr_flags;
5590  best_buf_size= buf_size;
5591  }
5592  else
5593  trace_idx.add("chosen", false).
5594  add_alnum("cause",
5595  (found_records == HA_POS_ERROR) ? "unknown" : "cost");
5596 
5597  }
5598  }
5599 
5600  DBUG_EXECUTE("info", print_sel_tree(param, tree, &tree->ror_scans_map,
5601  "ROR scans"););
5602  if (key_to_read)
5603  {
5604  idx= key_to_read - tree->keys;
5605  if ((read_plan= new (param->mem_root) TRP_RANGE(*key_to_read, idx,
5606  best_mrr_flags)))
5607  {
5608  read_plan->records= best_records;
5609  read_plan->is_ror= tree->ror_scans_map.is_set(idx);
5610  read_plan->read_cost= read_time;
5611  read_plan->mrr_buf_size= best_buf_size;
5612  DBUG_PRINT("info",
5613  ("Returning range plan for key %s, cost %g, records %lu",
5614  param->table->key_info[param->real_keynr[idx]].name,
5615  read_plan->read_cost, (ulong) read_plan->records));
5616  }
5617  }
5618  else
5619  DBUG_PRINT("info", ("No 'range' table read plan found"));
5620 
5621  DBUG_RETURN(read_plan);
5622 }
5623 
5624 
5625 QUICK_SELECT_I *TRP_INDEX_MERGE::make_quick(PARAM *param,
5626  bool retrieve_full_rows,
5627  MEM_ROOT *parent_alloc)
5628 {
5629  QUICK_INDEX_MERGE_SELECT *quick_imerge;
5630  QUICK_RANGE_SELECT *quick;
5631  /* index_merge always retrieves full rows, ignore retrieve_full_rows */
5632  if (!(quick_imerge= new QUICK_INDEX_MERGE_SELECT(param->thd, param->table)))
5633  return NULL;
5634 
5635  quick_imerge->records= records;
5636  quick_imerge->read_time= read_cost;
5637  for (TRP_RANGE **range_scan= range_scans; range_scan != range_scans_end;
5638  range_scan++)
5639  {
5640  if (!(quick= (QUICK_RANGE_SELECT*)
5641  ((*range_scan)->make_quick(param, FALSE, &quick_imerge->alloc)))||
5642  quick_imerge->push_quick_back(quick))
5643  {
5644  delete quick;
5645  delete quick_imerge;
5646  return NULL;
5647  }
5648  }
5649  return quick_imerge;
5650 }
5651 
5652 QUICK_SELECT_I *TRP_ROR_INTERSECT::make_quick(PARAM *param,
5653  bool retrieve_full_rows,
5654  MEM_ROOT *parent_alloc)
5655 {
5656  QUICK_ROR_INTERSECT_SELECT *quick_intrsect;
5657  QUICK_RANGE_SELECT *quick;
5658  DBUG_ENTER("TRP_ROR_INTERSECT::make_quick");
5659  MEM_ROOT *alloc;
5660 
5661  if ((quick_intrsect=
5662  new QUICK_ROR_INTERSECT_SELECT(param->thd, param->table,
5663  (retrieve_full_rows? (!is_covering) :
5664  FALSE),
5665  parent_alloc)))
5666  {
5667  DBUG_EXECUTE("info", print_ror_scans_arr(param->table,
5668  "creating ROR-intersect",
5669  first_scan, last_scan););
5670  alloc= parent_alloc? parent_alloc: &quick_intrsect->alloc;
5671  for (st_ror_scan_info **current= first_scan;
5672  current != last_scan;
5673  current++)
5674  {
5675  if (!(quick= get_quick_select(param, (*current)->idx,
5676  (*current)->sel_arg,
5677  HA_MRR_SORTED,
5678  0, alloc)) ||
5679  quick_intrsect->push_quick_back(quick))
5680  {
5681  delete quick_intrsect;
5682  DBUG_RETURN(NULL);
5683  }
5684  }
5685  if (cpk_scan)
5686  {
5687  if (!(quick= get_quick_select(param, cpk_scan->idx,
5688  cpk_scan->sel_arg,
5689  HA_MRR_SORTED,
5690  0, alloc)))
5691  {
5692  delete quick_intrsect;
5693  DBUG_RETURN(NULL);
5694  }
5695  quick->file= NULL;
5696  quick_intrsect->cpk_quick= quick;
5697  }
5698  quick_intrsect->records= records;
5699  quick_intrsect->read_time= read_cost;
5700  }
5701  DBUG_RETURN(quick_intrsect);
5702 }
5703 
5704 
5705 QUICK_SELECT_I *TRP_ROR_UNION::make_quick(PARAM *param,
5706  bool retrieve_full_rows,
5707  MEM_ROOT *parent_alloc)
5708 {
5709  QUICK_ROR_UNION_SELECT *quick_roru;
5710  TABLE_READ_PLAN **scan;
5711  QUICK_SELECT_I *quick;
5712  DBUG_ENTER("TRP_ROR_UNION::make_quick");
5713  /*
5714  It is impossible to construct a ROR-union that will not retrieve full
5715  rows, ignore retrieve_full_rows parameter.
5716  */
5717  if ((quick_roru= new QUICK_ROR_UNION_SELECT(param->thd, param->table)))
5718  {
5719  for (scan= first_ror; scan != last_ror; scan++)
5720  {
5721  if (!(quick= (*scan)->make_quick(param, FALSE, &quick_roru->alloc)) ||
5722  quick_roru->push_quick_back(quick))
5723  DBUG_RETURN(NULL);
5724  }
5725  quick_roru->records= records;
5726  quick_roru->read_time= read_cost;
5727  }
5728  DBUG_RETURN(quick_roru);
5729 }
5730 
5731 
5741 static void
5742 if_extended_explain_warn_index_not_applicable(const RANGE_OPT_PARAM *param,
5743  const uint key_num,
5744  const Field *field)
5745 {
5746  if (param->using_real_indexes &&
5747  param->thd->lex->describe & DESCRIBE_EXTENDED)
5748  push_warning_printf(
5749  param->thd,
5750  Sql_condition::WARN_LEVEL_WARN,
5751  ER_WARN_INDEX_NOT_APPLICABLE,
5752  ER(ER_WARN_INDEX_NOT_APPLICABLE),
5753  "range",
5754  field->table->key_info[param->real_keynr[key_num]].name,
5755  field->field_name);
5756 }
5757 
5758 
5759 /*
5760  Build a SEL_TREE for <> or NOT BETWEEN predicate
5761 
5762  SYNOPSIS
5763  get_ne_mm_tree()
5764  param PARAM from SQL_SELECT::test_quick_select
5765  cond_func item for the predicate
5766  field field in the predicate
5767  lt_value constant that field should be smaller
5768  gt_value constant that field should be greaterr
5769  cmp_type compare type for the field
5770 
5771  RETURN
5772  # Pointer to tree built tree
5773  0 on error
5774 */
5775 static SEL_TREE *get_ne_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
5776  Field *field,
5777  Item *lt_value, Item *gt_value,
5778  Item_result cmp_type)
5779 {
5780  SEL_TREE *tree;
5781  tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
5782  lt_value, cmp_type);
5783  if (tree)
5784  {
5785  tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
5786  Item_func::GT_FUNC,
5787  gt_value, cmp_type));
5788  }
5789  return tree;
5790 }
5791 
5792 
5793 /*
5794  Build a SEL_TREE for a simple predicate
5795 
5796  SYNOPSIS
5797  get_func_mm_tree()
5798  param PARAM from SQL_SELECT::test_quick_select
5799  cond_func item for the predicate
5800  field field in the predicate
5801  value constant in the predicate
5802  cmp_type compare type for the field
5803  inv TRUE <> NOT cond_func is considered
5804  (makes sense only when cond_func is BETWEEN or IN)
5805 
5806  RETURN
5807  Pointer to the tree built tree
5808 */
5809 
5810 static SEL_TREE *get_func_mm_tree(RANGE_OPT_PARAM *param, Item_func *cond_func,
5811  Field *field, Item *value,
5812  Item_result cmp_type, bool inv)
5813 {
5814  SEL_TREE *tree= 0;
5815  DBUG_ENTER("get_func_mm_tree");
5816 
5817  switch (cond_func->functype()) {
5818 
5819  case Item_func::XOR_FUNC:
5820  DBUG_RETURN(NULL); // Always true (don't use range access on XOR).
5821  break; // See WL#5800
5822 
5823  case Item_func::NE_FUNC:
5824  tree= get_ne_mm_tree(param, cond_func, field, value, value, cmp_type);
5825  break;
5826 
5827  case Item_func::BETWEEN:
5828  {
5829  if (!value)
5830  {
5831  if (inv)
5832  {
5833  tree= get_ne_mm_tree(param, cond_func, field, cond_func->arguments()[1],
5834  cond_func->arguments()[2], cmp_type);
5835  }
5836  else
5837  {
5838  tree= get_mm_parts(param, cond_func, field, Item_func::GE_FUNC,
5839  cond_func->arguments()[1],cmp_type);
5840  if (tree)
5841  {
5842  tree= tree_and(param, tree, get_mm_parts(param, cond_func, field,
5843  Item_func::LE_FUNC,
5844  cond_func->arguments()[2],
5845  cmp_type));
5846  }
5847  }
5848  }
5849  else
5850  tree= get_mm_parts(param, cond_func, field,
5851  (inv ?
5852  (value == (Item*)1 ? Item_func::GT_FUNC :
5853  Item_func::LT_FUNC):
5854  (value == (Item*)1 ? Item_func::LE_FUNC :
5855  Item_func::GE_FUNC)),
5856  cond_func->arguments()[0], cmp_type);
5857  break;
5858  }
5859  case Item_func::IN_FUNC:
5860  {
5861  Item_func_in *func=(Item_func_in*) cond_func;
5862 
5863  /*
5864  Array for IN() is constructed when all values have the same result
5865  type. Tree won't be built for values with different result types,
5866  so we check it here to avoid unnecessary work.
5867  */
5868  if (!func->arg_types_compatible)
5869  break;
5870 
5871  if (inv)
5872  {
5873  if (func->array && func->array->result_type() != ROW_RESULT)
5874  {
5875  /*
5876  We get here for conditions in form "t.key NOT IN (c1, c2, ...)",
5877  where c{i} are constants. Our goal is to produce a SEL_TREE that
5878  represents intervals:
5879 
5880  ($MIN<t.key<c1) OR (c1<t.key<c2) OR (c2<t.key<c3) OR ... (*)
5881 
5882  where $MIN is either "-inf" or NULL.
5883 
5884  The most straightforward way to produce it is to convert NOT IN
5885  into "(t.key != c1) AND (t.key != c2) AND ... " and let the range
5886  analyzer to build SEL_TREE from that. The problem is that the
5887  range analyzer will use O(N^2) memory (which is probably a bug),
5888  and people do use big NOT IN lists (e.g. see BUG#15872, BUG#21282),
5889  will run out of memory.
5890 
5891  Another problem with big lists like (*) is that a big list is
5892  unlikely to produce a good "range" access, while considering that
5893  range access will require expensive CPU calculations (and for
5894  MyISAM even index accesses). In short, big NOT IN lists are rarely
5895  worth analyzing.
5896 
5897  Considering the above, we'll handle NOT IN as follows:
5898  * if the number of entries in the NOT IN list is less than
5899  NOT_IN_IGNORE_THRESHOLD, construct the SEL_TREE (*) manually.
5900  * Otherwise, don't produce a SEL_TREE.
5901  */
5902 #define NOT_IN_IGNORE_THRESHOLD 1000
5903  MEM_ROOT *tmp_root= param->mem_root;
5904  param->thd->mem_root= param->old_root;
5905  /*
5906  Create one Item_type constant object. We'll need it as
5907  get_mm_parts only accepts constant values wrapped in Item_Type
5908  objects.
5909  We create the Item on param->mem_root which points to
5910  per-statement mem_root (while thd->mem_root is currently pointing
5911  to mem_root local to range optimizer).
5912  */
5913  Item *value_item= func->array->create_item();
5914  param->thd->mem_root= tmp_root;
5915 
5916  if (func->array->count > NOT_IN_IGNORE_THRESHOLD || !value_item)
5917  break;
5918 
5919  /* Get a SEL_TREE for "(-inf|NULL) < X < c_0" interval. */
5920  uint i=0;
5921  do
5922  {
5923  func->array->value_to_item(i, value_item);
5924  tree= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
5925  value_item, cmp_type);
5926  if (!tree)
5927  break;
5928  i++;
5929  } while (i < func->array->count && tree->type == SEL_TREE::IMPOSSIBLE);
5930 
5931  if (!tree || tree->type == SEL_TREE::IMPOSSIBLE)
5932  {
5933  /* We get here in cases like "t.unsigned NOT IN (-1,-2,-3) */
5934  tree= NULL;
5935  break;
5936  }
5937  SEL_TREE *tree2;
5938  for (; i < func->array->count; i++)
5939  {
5940  if (func->array->compare_elems(i, i-1))
5941  {
5942  /* Get a SEL_TREE for "-inf < X < c_i" interval */
5943  func->array->value_to_item(i, value_item);
5944  tree2= get_mm_parts(param, cond_func, field, Item_func::LT_FUNC,
5945  value_item, cmp_type);
5946  if (!tree2)
5947  {
5948  tree= NULL;
5949  break;
5950  }
5951 
5952  /* Change all intervals to be "c_{i-1} < X < c_i" */
5953  for (uint idx= 0; idx < param->keys; idx++)
5954  {
5955  SEL_ARG *new_interval, *last_val;
5956  if (((new_interval= tree2->keys[idx])) &&
5957  (tree->keys[idx]) &&
5958  ((last_val= tree->keys[idx]->last())))
5959  {
5960  new_interval->min_value= last_val->max_value;
5961  new_interval->min_flag= NEAR_MIN;
5962 
5963  /*
5964  If the interval is over a partial keypart, the
5965  interval must be "c_{i-1} <= X < c_i" instead of
5966  "c_{i-1} < X < c_i". Reason:
5967 
5968  Consider a table with a column "my_col VARCHAR(3)",
5969  and an index with definition
5970  "INDEX my_idx my_col(1)". If the table contains rows
5971  with my_col values "f" and "foo", the index will not
5972  distinguish the two rows.
5973 
5974  Note that tree_or() below will effectively merge
5975  this range with the range created for c_{i-1} and
5976  we'll eventually end up with only one range:
5977  "NULL < X".
5978 
5979  Partitioning indexes are never partial.
5980  */
5981  if (param->using_real_indexes)
5982  {
5983  const KEY key=
5984  param->table->key_info[param->real_keynr[idx]];
5985  const KEY_PART_INFO *kpi= key.key_part + new_interval->part;
5986 
5987  if (kpi->key_part_flag & HA_PART_KEY_SEG)
5988  new_interval->min_flag= 0;
5989  }
5990  }
5991  }
5992  /*
5993  The following doesn't try to allocate memory so no need to
5994  check for NULL.
5995  */
5996  tree= tree_or(param, tree, tree2);
5997  }
5998  }
5999 
6000  if (tree && tree->type != SEL_TREE::IMPOSSIBLE)
6001  {
6002  /*
6003  Get the SEL_TREE for the last "c_last < X < +inf" interval
6004  (value_item cotains c_last already)
6005  */
6006  tree2= get_mm_parts(param, cond_func, field, Item_func::GT_FUNC,
6007  value_item, cmp_type);
6008  tree= tree_or(param, tree, tree2);
6009  }
6010  }
6011  else
6012  {
6013  tree= get_ne_mm_tree(param, cond_func, field,
6014  func->arguments()[1], func->arguments()[1],
6015  cmp_type);
6016  if (tree)
6017  {
6018  Item **arg, **end;
6019  for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
6020  arg < end ; arg++)
6021  {
6022  tree= tree_and(param, tree, get_ne_mm_tree(param, cond_func, field,
6023  *arg, *arg, cmp_type));
6024  }
6025  }
6026  }
6027  }
6028  else
6029  {
6030  tree= get_mm_parts(param, cond_func, field, Item_func::EQ_FUNC,
6031  func->arguments()[1], cmp_type);
6032  if (tree)
6033  {
6034  Item **arg, **end;
6035  for (arg= func->arguments()+2, end= arg+func->argument_count()-2;
6036  arg < end ; arg++)
6037  {
6038  tree= tree_or(param, tree, get_mm_parts(param, cond_func, field,
6039  Item_func::EQ_FUNC,
6040  *arg, cmp_type));
6041  }
6042  }
6043  }
6044  break;
6045  }
6046  default:
6047  {
6048  /*
6049  Here the function for the following predicates are processed:
6050  <, <=, =, >=, >, LIKE, IS NULL, IS NOT NULL and GIS functions.
6051  If the predicate is of the form (value op field) it is handled
6052  as the equivalent predicate (field rev_op value), e.g.
6053  2 <= a is handled as a >= 2.
6054  */
6055  Item_func::Functype func_type=
6056  (value != cond_func->arguments()[0]) ? cond_func->functype() :
6057  ((Item_bool_func2*) cond_func)->rev_functype();
6058  tree= get_mm_parts(param, cond_func, field, func_type, value, cmp_type);
6059  }
6060  }
6061 
6062  DBUG_RETURN(tree);
6063 }
6064 
6065 
6066 /*
6067  Build conjunction of all SEL_TREEs for a simple predicate applying equalities
6068 
6069  SYNOPSIS
6070  get_full_func_mm_tree()
6071  param PARAM from SQL_SELECT::test_quick_select
6072  cond_func item for the predicate
6073  field_item field in the predicate
6074  value constant in the predicate (or a field already read from
6075  a table in the case of dynamic range access)
6076  (for BETWEEN it contains the number of the field argument,
6077  for IN it's always 0)
6078  inv TRUE <> NOT cond_func is considered
6079  (makes sense only when cond_func is BETWEEN or IN)
6080 
6081  DESCRIPTION
6082  For a simple SARGable predicate of the form (f op c), where f is a field and
6083  c is a constant, the function builds a conjunction of all SEL_TREES that can
6084  be obtained by the substitution of f for all different fields equal to f.
6085 
6086  NOTES
6087  If the WHERE condition contains a predicate (fi op c),
6088  then not only SELL_TREE for this predicate is built, but
6089  the trees for the results of substitution of fi for
6090  each fj belonging to the same multiple equality as fi
6091  are built as well.
6092  E.g. for WHERE t1.a=t2.a AND t2.a > 10
6093  a SEL_TREE for t2.a > 10 will be built for quick select from t2
6094  and
6095  a SEL_TREE for t1.a > 10 will be built for quick select from t1.
6096 
6097  A BETWEEN predicate of the form (fi [NOT] BETWEEN c1 AND c2) is treated
6098  in a similar way: we build a conjuction of trees for the results
6099  of all substitutions of fi for equal fj.
6100  Yet a predicate of the form (c BETWEEN f1i AND f2i) is processed
6101  differently. It is considered as a conjuction of two SARGable
6102  predicates (f1i <= c) and (f2i <=c) and the function get_full_func_mm_tree
6103  is called for each of them separately producing trees for
6104  AND j (f1j <=c ) and AND j (f2j <= c)
6105  After this these two trees are united in one conjunctive tree.
6106  It's easy to see that the same tree is obtained for
6107  AND j,k (f1j <=c AND f2k<=c)
6108  which is equivalent to
6109  AND j,k (c BETWEEN f1j AND f2k).
6110  The validity of the processing of the predicate (c NOT BETWEEN f1i AND f2i)
6111  which equivalent to (f1i > c OR f2i < c) is not so obvious. Here the
6112  function get_full_func_mm_tree is called for (f1i > c) and (f2i < c)
6113  producing trees for AND j (f1j > c) and AND j (f2j < c). Then this two
6114  trees are united in one OR-tree. The expression
6115  (AND j (f1j > c) OR AND j (f2j < c)
6116  is equivalent to the expression
6117  AND j,k (f1j > c OR f2k < c)
6118  which is just a translation of
6119  AND j,k (c NOT BETWEEN f1j AND f2k)
6120 
6121  In the cases when one of the items f1, f2 is a constant c1 we do not create
6122  a tree for it at all. It works for BETWEEN predicates but does not
6123  work for NOT BETWEEN predicates as we have to evaluate the expression
6124  with it. If it is TRUE then the other tree can be completely ignored.
6125  We do not do it now and no trees are built in these cases for
6126  NOT BETWEEN predicates.
6127 
6128  As to IN predicates only ones of the form (f IN (c1,...,cn)),
6129  where f1 is a field and c1,...,cn are constant, are considered as
6130  SARGable. We never try to narrow the index scan using predicates of
6131  the form (c IN (c1,...,f,...,cn)).
6132 
6133  RETURN
6134  Pointer to the tree representing the built conjunction of SEL_TREEs
6135 */
6136 
6137 static SEL_TREE *get_full_func_mm_tree(RANGE_OPT_PARAM *param,
6138  Item_func *cond_func,
6139  Item_field *field_item, Item *value,
6140  bool inv)
6141 {
6142  SEL_TREE *tree= 0;
6143  SEL_TREE *ftree= 0;
6144  table_map ref_tables= 0;
6145  table_map param_comp= ~(param->prev_tables | param->read_tables |
6146  param->current_table);
6147  DBUG_ENTER("get_full_func_mm_tree");
6148 
6149  for (uint i= 0; i < cond_func->arg_count; i++)
6150  {
6151  Item *arg= cond_func->arguments()[i]->real_item();
6152  if (arg != field_item)
6153  ref_tables|= arg->used_tables();
6154  }
6155  Field *field= field_item->field;
6156  Item_result cmp_type= field->cmp_type();
6157  if (!((ref_tables | field->table->map) & param_comp))
6158  ftree= get_func_mm_tree(param, cond_func, field, value, cmp_type, inv);
6159  Item_equal *item_equal= field_item->item_equal;
6160  if (item_equal)
6161  {
6162  Item_equal_iterator it(*item_equal);
6163  Item_field *item;
6164  while ((item= it++))
6165  {
6166  Field *f= item->field;
6167  if (field->eq(f))
6168  continue;
6169  if (!((ref_tables | f->table->map) & param_comp))
6170  {
6171  tree= get_func_mm_tree(param, cond_func, f, value, cmp_type, inv);
6172  ftree= !ftree ? tree : tree_and(param, ftree, tree);
6173  }
6174  }
6175  }
6176  DBUG_RETURN(ftree);
6177 }
6178 
6212 static SEL_TREE *get_mm_tree(RANGE_OPT_PARAM *param,Item *cond)
6213 {
6214  SEL_TREE *tree=0;
6215  SEL_TREE *ftree= 0;
6216  Item_field *field_item= 0;
6217  bool inv= FALSE;
6218  Item *value= 0;
6219  DBUG_ENTER("get_mm_tree");
6220 
6221  if (cond->type() == Item::COND_ITEM)
6222  {
6223  List_iterator<Item> li(*((Item_cond*) cond)->argument_list());
6224 
6225  if (((Item_cond*) cond)->functype() == Item_func::COND_AND_FUNC)
6226  {
6227  tree= NULL;
6228  Item *item;
6229  while ((item=li++))
6230  {
6231  SEL_TREE *new_tree= get_mm_tree(param,item);
6232  if (param->statement_should_be_aborted())
6233  DBUG_RETURN(NULL);
6234  tree= tree_and(param,tree,new_tree);
6235  dbug_print_tree("after_and", tree, param);
6236  if (tree && tree->type == SEL_TREE::IMPOSSIBLE)
6237  break;
6238  }
6239  }
6240  else
6241  { // Item OR
6242  tree= get_mm_tree(param,li++);
6243  if (param->statement_should_be_aborted())
6244  DBUG_RETURN(NULL);
6245  if (tree)
6246  {
6247  Item *item;
6248  while ((item=li++))
6249  {
6250  SEL_TREE *new_tree=get_mm_tree(param,item);
6251  if (new_tree == NULL || param->statement_should_be_aborted())
6252  DBUG_RETURN(NULL);
6253  tree= tree_or(param,tree,new_tree);
6254  dbug_print_tree("after_or", tree, param);
6255  if (tree == NULL || tree->type == SEL_TREE::ALWAYS)
6256  break;
6257  }
6258  }
6259  }
6260  dbug_print_tree("tree_returned", tree, param);
6261  DBUG_RETURN(tree);
6262  }
6263  /*
6264  Here when simple cond
6265  There are limits on what kinds of const items we can evaluate.
6266  At this stage a subquery in 'cond' might not be fully transformed yet
6267  (example: semijoin) thus cannot be evaluated.
6268  */
6269  if (cond->const_item() && !cond->is_expensive() && !cond->has_subquery())
6270  {
6271  /*
6272  During the cond->val_int() evaluation we can come across a subselect
6273  item which may allocate memory on the thd->mem_root and assumes
6274  all the memory allocated has the same life span as the subselect
6275  item itself. So we have to restore the thread's mem_root here.
6276  */
6277  MEM_ROOT *tmp_root= param->mem_root;
6278  param->thd->mem_root= param->old_root;
6279  tree= cond->val_int() ? new(tmp_root) SEL_TREE(SEL_TREE::ALWAYS) :
6280  new(tmp_root) SEL_TREE(SEL_TREE::IMPOSSIBLE);
6281  param->thd->mem_root= tmp_root;
6282  dbug_print_tree("tree_returned", tree, param);
6283  DBUG_RETURN(tree);
6284  }
6285 
6286  table_map ref_tables= 0;
6287  table_map param_comp= ~(param->prev_tables | param->read_tables |
6288  param->current_table);
6289  if (cond->type() != Item::FUNC_ITEM)
6290  { // Should be a field
6291  ref_tables= cond->used_tables();
6292  if ((ref_tables & param->current_table) ||
6293  (ref_tables & ~(param->prev_tables | param->read_tables)))
6294  DBUG_RETURN(0);
6295  DBUG_RETURN(new SEL_TREE(SEL_TREE::MAYBE));
6296  }
6297 
6298  Item_func *cond_func= (Item_func*) cond;
6299  if (cond_func->functype() == Item_func::BETWEEN ||
6300  cond_func->functype() == Item_func::IN_FUNC)
6301  inv= ((Item_func_opt_neg *) cond_func)->negated;
6302  else
6303  {
6304  /*
6305  During the cond_func->select_optimize() evaluation we can come across a
6306  subselect item which may allocate memory on the thd->mem_root and assumes
6307  all the memory allocated has the same life span as the subselect item
6308  itself. So we have to restore the thread's mem_root here.
6309  */
6310  MEM_ROOT *tmp_root= param->mem_root;
6311  param->thd->mem_root= param->old_root;
6312  Item_func::optimize_type opt_type= cond_func->select_optimize();
6313  param->thd->mem_root= tmp_root;
6314  if (opt_type == Item_func::OPTIMIZE_NONE)
6315  DBUG_RETURN(NULL);
6316  }
6317 
6318  param->cond= cond;
6319 
6320  switch (cond_func->functype()) {
6321  case Item_func::BETWEEN:
6322  if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
6323  {
6324  field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
6325  ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
6326  }
6327 
6328  /*
6329  Concerning the code below see the NOTES section in
6330  the comments for the function get_full_func_mm_tree()
6331  */
6332  for (uint i= 1 ; i < cond_func->arg_count ; i++)
6333  {
6334  if (cond_func->arguments()[i]->real_item()->type() == Item::FIELD_ITEM)
6335  {
6336  field_item= (Item_field*) (cond_func->arguments()[i]->real_item());
6337  SEL_TREE *tmp= get_full_func_mm_tree(param, cond_func,
6338  field_item, (Item*)(intptr)i, inv);
6339  if (inv)
6340  {
6341  tree= !tree ? tmp : tree_or(param, tree, tmp);
6342  if (tree == NULL)
6343  break;
6344  }
6345  else
6346  tree= tree_and(param, tree, tmp);
6347  }
6348  else if (inv)
6349  {
6350  tree= 0;
6351  break;
6352  }
6353  }
6354 
6355  ftree = tree_and(param, ftree, tree);
6356  break;
6357  case Item_func::IN_FUNC:
6358  {
6359  Item_func_in *func=(Item_func_in*) cond_func;
6360  if (func->key_item()->real_item()->type() != Item::FIELD_ITEM)
6361  DBUG_RETURN(0);
6362  field_item= (Item_field*) (func->key_item()->real_item());
6363  ftree= get_full_func_mm_tree(param, cond_func, field_item, NULL, inv);
6364  break;
6365  }
6366  case Item_func::MULT_EQUAL_FUNC:
6367  {
6368  Item_equal *item_equal= (Item_equal *) cond;
6369  if (!(value= item_equal->get_const()))
6370  DBUG_RETURN(0);
6371  Item_equal_iterator it(*item_equal);
6372  ref_tables= value->used_tables();
6373  while ((field_item= it++))
6374  {
6375  Field *field= field_item->field;
6376  Item_result cmp_type= field->cmp_type();
6377  if (!((ref_tables | field->table->map) & param_comp))
6378  {
6379  tree= get_mm_parts(param, item_equal, field, Item_func::EQ_FUNC,
6380  value,cmp_type);
6381  ftree= !ftree ? tree : tree_and(param, ftree, tree);
6382  }
6383  }
6384 
6385  dbug_print_tree("tree_returned", ftree, param);
6386  DBUG_RETURN(ftree);
6387  }
6388  default:
6389 
6390  DBUG_ASSERT (!ftree);
6391  if (cond_func->arguments()[0]->real_item()->type() == Item::FIELD_ITEM)
6392  {
6393  field_item= (Item_field*) (cond_func->arguments()[0]->real_item());
6394  value= cond_func->arg_count > 1 ? cond_func->arguments()[1] : NULL;
6395  ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
6396  }
6397  /*
6398  Even if get_full_func_mm_tree() was executed above and did not
6399  return a range predicate it may still be possible to create one
6400  by reversing the order of the operands. Note that this only
6401  applies to predicates where both operands are fields. Example: A
6402  query of the form
6403 
6404  WHERE t1.a OP t2.b
6405 
6406  In this case, arguments()[0] == t1.a and arguments()[1] == t2.b.
6407  When creating range predicates for t2, get_full_func_mm_tree()
6408  above will return NULL because 'field' belongs to t1 and only
6409  predicates that applies to t2 are of interest. In this case a
6410  call to get_full_func_mm_tree() with reversed operands (see
6411  below) may succeed.
6412  */
6413  if (!ftree && cond_func->have_rev_func() &&
6414  cond_func->arguments()[1]->real_item()->type() == Item::FIELD_ITEM)
6415  {
6416  field_item= (Item_field*) (cond_func->arguments()[1]->real_item());
6417  value= cond_func->arguments()[0];
6418  ftree= get_full_func_mm_tree(param, cond_func, field_item, value, inv);
6419  }
6420  }
6421 
6422  dbug_print_tree("tree_returned", ftree, param);
6423  DBUG_RETURN(ftree);
6424 }
6425 
6437 bool is_spatial_operator(Item_func::Functype op_type)
6438 {
6439  switch (op_type)
6440  {
6441  case Item_func::SP_EQUALS_FUNC:
6442  case Item_func::SP_DISJOINT_FUNC:
6443  case Item_func::SP_INTERSECTS_FUNC:
6444  case Item_func::SP_TOUCHES_FUNC:
6445  case Item_func::SP_CROSSES_FUNC:
6446  case Item_func::SP_WITHIN_FUNC:
6447  case Item_func::SP_CONTAINS_FUNC:
6448  case Item_func::SP_OVERLAPS_FUNC:
6449  case Item_func::SP_STARTPOINT:
6450  case Item_func::SP_ENDPOINT:
6451  case Item_func::SP_EXTERIORRING:
6452  case Item_func::SP_POINTN:
6453  case Item_func::SP_GEOMETRYN:
6454  case Item_func::SP_INTERIORRINGN:
6455  return true;
6456  default:
6457  return false;
6458  }
6459 }
6460 
6461 static SEL_TREE *
6462 get_mm_parts(RANGE_OPT_PARAM *param, Item_func *cond_func, Field *field,
6463  Item_func::Functype type,
6464  Item *value, Item_result cmp_type)
6465 {
6466  DBUG_ENTER("get_mm_parts");
6467  if (field->table != param->table)
6468  DBUG_RETURN(0);
6469 
6470  KEY_PART *key_part = param->key_parts;
6471  KEY_PART *end = param->key_parts_end;
6472  SEL_TREE *tree=0;
6473  if (value &&
6474  value->used_tables() & ~(param->prev_tables | param->read_tables))
6475  DBUG_RETURN(0);
6476  for (; key_part != end ; key_part++)
6477  {
6478  if (field->eq(key_part->field))
6479  {
6480  /*
6481  Cannot do range access for spatial operators when a
6482  non-spatial index is used.
6483  */
6484  if (key_part->image_type != Field::itMBR &&
6485  is_spatial_operator(cond_func->functype()))
6486  continue;
6487 
6488  SEL_ARG *sel_arg=0;
6489  if (!tree && !(tree=new SEL_TREE()))
6490  DBUG_RETURN(0); // OOM
6491  if (!value || !(value->used_tables() & ~param->read_tables))
6492  {
6493  sel_arg=get_mm_leaf(param,cond_func,
6494  key_part->field,key_part,type,value);
6495  if (!sel_arg)
6496  continue;
6497  if (sel_arg->type == SEL_ARG::IMPOSSIBLE)
6498  {
6499  tree->type=SEL_TREE::IMPOSSIBLE;
6500  DBUG_RETURN(tree);
6501  }
6502  }
6503  else
6504  {
6505  // This key may be used later
6506  if (!(sel_arg= new SEL_ARG(SEL_ARG::MAYBE_KEY)))
6507  DBUG_RETURN(0); // OOM
6508  }
6509  sel_arg->part=(uchar) key_part->part;
6510  tree->keys[key_part->key]=sel_add(tree->keys[key_part->key],sel_arg);
6511  tree->keys_map.set_bit(key_part->key);
6512  }
6513  }
6514 
6515  if (tree && tree->merges.is_empty() && tree->keys_map.is_clear_all())
6516  tree= NULL;
6517  DBUG_RETURN(tree);
6518 }
6519 
6543 static bool save_value_and_handle_conversion(SEL_ARG **tree,
6544  Item *value,
6545  const Item_func::Functype comp_op,
6546  Field *field,
6547  const char **impossible_cond_cause,
6548  MEM_ROOT *memroot)
6549 {
6550  // A SEL_ARG should not have been created for this predicate yet.
6551  DBUG_ASSERT(*tree == NULL);
6552 
6553  if (!value->can_be_evaluated_now())
6554  {
6555  /*
6556  We cannot evaluate the value yet (i.e. required tables are not yet
6557  locked.)
6558  This is the case of prune_partitions() called during JOIN::prepare().
6559  */
6560  return true;
6561  }
6562 
6563  // For comparison purposes allow invalid dates like 2000-01-32
6564  const sql_mode_t orig_sql_mode= field->table->in_use->variables.sql_mode;
6565  field->table->in_use->variables.sql_mode|= MODE_INVALID_DATES;
6566 
6567  /*
6568  We want to change "field > value" to "field OP V"
6569  where:
6570  * V is what is in "field" after we stored "value" in it via
6571  save_in_field_no_warning() (such store operation may have done
6572  rounding...)
6573  * OP is > or >=, depending on what's correct.
6574  For example, if c is an INT column,
6575  "c > 2.9" is changed to "c OP 3"
6576  where OP is ">=" (">" would not be correct, as 3 > 2.9, a comparison
6577  done with stored_field_cmp_to_item()). And
6578  "c > 3.1" is changed to "c OP 3" where OP is ">" (3 < 3.1...).
6579  */
6580 
6581  // Note that value may be a stored function call, executed here.
6582  const type_conversion_status err= value->save_in_field_no_warnings(field, 1);
6583  field->table->in_use->variables.sql_mode= orig_sql_mode;
6584 
6585  switch (err) {
6586  case TYPE_OK:
6587  case TYPE_NOTE_TRUNCATED:
6588  return false;
6589  case TYPE_ERR_BAD_VALUE:
6590  /*
6591  In the case of incompatible values, MySQL's SQL dialect has some
6592  strange interpretations. For example,
6593 
6594  "int_col > 'foo'" is interpreted as "int_col > 0"
6595 
6596  instead of always false. Because of this, we assume that the
6597  range predicate is always true instead of always false and let
6598  evaluate_join_record() decide the outcome.
6599  */
6600  return true;
6601  case TYPE_ERR_NULL_CONSTRAINT_VIOLATION:
6602  // Checking NULL value on a field that cannot contain NULL.
6603  *impossible_cond_cause= "null_field_in_non_null_column";
6604  goto impossible_cond;
6605  case TYPE_WARN_OUT_OF_RANGE:
6606  /*
6607  value to store was either higher than field::max_value or lower
6608  than field::min_value. The field's max/min value has been stored
6609  instead.
6610  */
6611  if (comp_op == Item_func::EQUAL_FUNC || comp_op == Item_func::EQ_FUNC)
6612  {
6613  /*
6614  Independent of data type, "out_of_range_value =/<=> field" is
6615  always false.
6616  */
6617  *impossible_cond_cause= "value_out_of_range";
6618  goto impossible_cond;
6619  }
6620 
6621  // If the field is numeric, we can interpret the out of range value.
6622  if ((field->type() != FIELD_TYPE_BIT) &&
6623  (field->result_type() == REAL_RESULT ||
6624  field->result_type() == INT_RESULT ||
6625  field->result_type() == DECIMAL_RESULT))
6626  {
6627  /*
6628  value to store was higher than field::max_value if
6629  a) field has a value greater than 0, or
6630  b) if field is unsigned and has a negative value (which, when
6631  cast to unsigned, means some value higher than LONGLONG_MAX).
6632  */
6633  if ((field->val_int() > 0) || // a)
6634  (static_cast<Field_num*>(field)->unsigned_flag &&
6635  field->val_int() < 0)) // b)
6636  {
6637  if (comp_op == Item_func::LT_FUNC || comp_op == Item_func::LE_FUNC)
6638  {
6639  /*
6640  '<' or '<=' compared to a value higher than the field
6641  can store is always true.
6642  */
6643  return true;
6644  }
6645  if (comp_op == Item_func::GT_FUNC || comp_op == Item_func::GE_FUNC)
6646  {
6647  /*
6648  '>' or '>=' compared to a value higher than the field can
6649  store is always false.
6650  */
6651  *impossible_cond_cause= "value_out_of_range";
6652  goto impossible_cond;
6653  }
6654  }
6655  else // value is lower than field::min_value
6656  {
6657  if (comp_op == Item_func::GT_FUNC || comp_op == Item_func::GE_FUNC)
6658  {
6659  /*
6660  '>' or '>=' compared to a value lower than the field
6661  can store is always true.
6662  */
6663  return true;
6664  }
6665  if (comp_op == Item_func::LT_FUNC || comp_op == Item_func::LE_FUNC)
6666  {
6667  /*
6668  '<' or '=' compared to a value lower than the field can
6669  store is always false.
6670  */
6671  *impossible_cond_cause= "value_out_of_range";
6672  goto impossible_cond;
6673  }
6674  }
6675  }
6676  /*
6677  Value is out of range on a datatype where it can't be decided if
6678  it was underflow or overflow. It is therefore not possible to
6679  determine whether or not the condition is impossible or always
6680  true and we have to assume always true.
6681  */
6682  return true;
6683  case TYPE_NOTE_TIME_TRUNCATED:
6684  if (field->type() == FIELD_TYPE_DATE &&
6685  (comp_op == Item_func::GT_FUNC || comp_op == Item_func::GE_FUNC ||
6686  comp_op == Item_func::LT_FUNC || comp_op == Item_func::LE_FUNC))
6687  {
6688  /*
6689  We were saving DATETIME into a DATE column, the conversion went ok
6690  but a non-zero time part was cut off.
6691 
6692  In MySQL's SQL dialect, DATE and DATETIME are compared as datetime
6693  values. Index over a DATE column uses DATE comparison. Changing
6694  from one comparison to the other is possible:
6695 
6696  datetime(date_col)< '2007-12-10 12:34:55' -> date_col<='2007-12-10'
6697  datetime(date_col)<='2007-12-10 12:34:55' -> date_col<='2007-12-10'
6698 
6699  datetime(date_col)> '2007-12-10 12:34:55' -> date_col>='2007-12-10'
6700  datetime(date_col)>='2007-12-10 12:34:55' -> date_col>='2007-12-10'
6701 
6702  but we'll need to convert '>' to '>=' and '<' to '<='. This will
6703  be done together with other types at the end of get_mm_leaf()
6704  (grep for stored_field_cmp_to_item)
6705  */
6706  return false;
6707  }
6708  if (comp_op == Item_func::EQ_FUNC || comp_op == Item_func::EQUAL_FUNC)
6709  {
6710  // Equality comparison is always false when time info has been truncated.
6711  goto impossible_cond;
6712  }
6713  // Fall through
6714  default:
6715  return true;
6716  }
6717 
6718  DBUG_ASSERT(FALSE); // Should never get here.
6719 
6720 impossible_cond:
6721  *tree= new (memroot) SEL_ARG(field, 0, 0);
6722  (*tree)->type= SEL_ARG::IMPOSSIBLE;
6723  return true;
6724 }
6725 
6726 static SEL_ARG *
6727 get_mm_leaf(RANGE_OPT_PARAM *param, Item *conf_func, Field *field,
6728  KEY_PART *key_part, Item_func::Functype type,Item *value)
6729 {
6730  uint maybe_null=(uint) field->real_maybe_null();
6731  bool optimize_range;
6732  SEL_ARG *tree= 0;
6733  MEM_ROOT *alloc= param->mem_root;
6734  uchar *str;
6735  const char *impossible_cond_cause= NULL;
6736  DBUG_ENTER("get_mm_leaf");
6737 
6738  /*
6739  We need to restore the runtime mem_root of the thread in this
6740  function because it evaluates the value of its argument, while
6741  the argument can be any, e.g. a subselect. The subselect
6742  items, in turn, assume that all the memory allocated during
6743  the evaluation has the same life span as the item itself.
6744  TODO: opt_range.cc should not reset thd->mem_root at all.
6745  */
6746  param->thd->mem_root= param->old_root;
6747  if (!value) // IS NULL or IS NOT NULL
6748  {
6749  if (field->table->maybe_null) // Can't use a key on this
6750  goto end;
6751  if (!maybe_null) // Not null field
6752  {
6753  if (type == Item_func::ISNULL_FUNC)
6754  tree= &null_element;
6755  goto end;
6756  }
6757  uchar *null_string=
6758  static_cast<uchar*>(alloc_root(alloc, key_part->store_length + 1));
6759  if (!null_string)
6760  goto end; // out of memory
6761 
6762  TRASH(null_string, key_part->store_length + 1);
6763  memcpy(null_string, is_null_string, sizeof(is_null_string));
6764 
6765  if (!(tree= new (alloc) SEL_ARG(field, null_string, null_string)))
6766  goto end; // out of memory
6767  if (type == Item_func::ISNOTNULL_FUNC)
6768  {
6769  tree->min_flag=NEAR_MIN; /* IS NOT NULL -> X > NULL */
6770  tree->max_flag=NO_MAX_RANGE;
6771  }
6772  goto end;
6773  }
6774 
6775  /*
6776  1. Usually we can't use an index if the column collation
6777  differ from the operation collation.
6778 
6779  2. However, we can reuse a case insensitive index for
6780  the binary searches:
6781 
6782  WHERE latin1_swedish_ci_column = 'a' COLLATE lati1_bin;
6783 
6784  WHERE latin1_swedish_ci_colimn = BINARY 'a '
6785  */
6786  if ((field->result_type() == STRING_RESULT &&
6787  field->match_collation_to_optimize_range() &&
6788  value->result_type() == STRING_RESULT &&
6789  key_part->image_type == Field::itRAW &&
6790  field->charset() != conf_func->compare_collation() &&
6791  !(conf_func->compare_collation()->state & MY_CS_BINSORT &&
6792  (type == Item_func::EQUAL_FUNC || type == Item_func::EQ_FUNC))))
6793  {
6794  if_extended_explain_warn_index_not_applicable(param, key_part->key, field);
6795  goto end;
6796  }
6797 
6798  /*
6799  Temporal values: Cannot use range access if:
6800  1) 'temporal_value = indexed_varchar_column' because there are
6801  many ways to represent the same date as a string. A few
6802  examples: "01-01-2001", "1-1-2001", "2001-01-01",
6803  "2001#01#01". The same problem applies to time. Thus, we
6804  cannot create a usefull range predicate for temporal values
6805  into VARCHAR column indexes. @see add_key_field()
6806  2) 'temporal_value_with_date_part = indexed_time' because:
6807  - without index, a TIME column with value '48:00:00' is
6808  equal to a DATETIME column with value
6809  'CURDATE() + 2 days'
6810  - with range access into the TIME column, CURDATE() + 2
6811  days becomes "00:00:00" (Field_timef::store_internal()
6812  simply extracts the time part from the datetime) which
6813  is a lookup key which does not match "48:00:00"; so
6814  ref access is not be able to give the same result as
6815  On the other hand, we can do ref access for
6816  IndexedDatetimeComparedToTime because
6817  Field_temporal_with_date::store_time() will convert
6818  48:00:00 to CURDATE() + 2 days which is the correct
6819  lookup key.
6820  */
6821  if ((!field->is_temporal() && value->is_temporal()) || // 1)
6822  field_time_cmp_date(field, value)) // 2)
6823  {
6824  if_extended_explain_warn_index_not_applicable(param, key_part->key, field);
6825  goto end;
6826  }
6827 
6828  if (key_part->image_type == Field::itMBR)
6829  {
6830  // @todo: use is_spatial_operator() instead?
6831  switch (type) {
6832  case Item_func::SP_EQUALS_FUNC:
6833  case Item_func::SP_DISJOINT_FUNC:
6834  case Item_func::SP_INTERSECTS_FUNC:
6835  case Item_func::SP_TOUCHES_FUNC:
6836  case Item_func::SP_CROSSES_FUNC:
6837  case Item_func::SP_WITHIN_FUNC:
6838  case Item_func::SP_CONTAINS_FUNC:
6839  case Item_func::SP_OVERLAPS_FUNC:
6840  break;
6841  default:
6842  /*
6843  We cannot involve spatial indexes for queries that
6844  don't use MBREQUALS(), MBRDISJOINT(), etc. functions.
6845  */
6846  goto end;
6847  }
6848  }
6849 
6850  if (param->using_real_indexes)
6851  optimize_range= field->optimize_range(param->real_keynr[key_part->key],
6852  key_part->part);
6853  else
6854  optimize_range= TRUE;
6855 
6856  if (type == Item_func::LIKE_FUNC)
6857  {
6858  bool like_error;
6859  char buff1[MAX_FIELD_WIDTH];
6860  uchar *min_str,*max_str;
6861  String tmp(buff1,sizeof(buff1),value->collation.collation),*res;
6862  size_t length, offset, min_length, max_length;
6863  uint field_length= field->pack_length()+maybe_null;
6864 
6865  if (!optimize_range)
6866  goto end;
6867  if (!(res= value->val_str(&tmp)))
6868  {
6869  tree= &null_element;
6870  goto end;
6871  }
6872 
6873  /*
6874  TODO:
6875  Check if this was a function. This should have be optimized away
6876  in the sql_select.cc
6877  */
6878  if (res != &tmp)
6879  {
6880  tmp.copy(*res); // Get own copy
6881  res= &tmp;
6882  }
6883  if (field->cmp_type() != STRING_RESULT)
6884  goto end; // Can only optimize strings
6885 
6886  offset=maybe_null;
6887  length=key_part->store_length;
6888 
6889  if (length != key_part->length + maybe_null)
6890  {
6891  /* key packed with length prefix */
6892  offset+= HA_KEY_BLOB_LENGTH;
6893  field_length= length - HA_KEY_BLOB_LENGTH;
6894  }
6895  else
6896  {
6897  if (unlikely(length < field_length))
6898  {
6899  /*
6900  This can only happen in a table created with UNIREG where one key
6901  overlaps many fields
6902  */
6903  length= field_length;
6904  }
6905  else
6906  field_length= length;
6907  }
6908  length+=offset;
6909  if (!(min_str= (uchar*) alloc_root(alloc, length*2)))
6910  goto end;
6911 
6912  max_str=min_str+length;
6913  if (maybe_null)
6914  max_str[0]= min_str[0]=0;
6915 
6916  field_length-= maybe_null;
6917  like_error= my_like_range(field->charset(),
6918  res->ptr(), res->length(),
6919  ((Item_func_like*)(param->cond))->escape,
6920  wild_one, wild_many,
6921  field_length,
6922  (char*) min_str+offset, (char*) max_str+offset,
6923  &min_length, &max_length);
6924  if (like_error) // Can't optimize with LIKE
6925  goto end;
6926 
6927  if (offset != maybe_null) // BLOB or VARCHAR
6928  {
6929  int2store(min_str+maybe_null,min_length);
6930  int2store(max_str+maybe_null,max_length);
6931  }
6932  tree= new (alloc) SEL_ARG(field, min_str, max_str);
6933  goto end;
6934  }
6935 
6936  if (!optimize_range &&
6937  type != Item_func::EQ_FUNC &&
6938  type != Item_func::EQUAL_FUNC)
6939  goto end; // Can't optimize this
6940 
6941  /*
6942  We can't always use indexes when comparing a string index to a number
6943  cmp_type() is checked to allow compare of dates to numbers
6944  */
6945  if (field->result_type() == STRING_RESULT &&
6946  value->result_type() != STRING_RESULT &&
6947  field->cmp_type() != value->result_type())
6948  {
6949  if_extended_explain_warn_index_not_applicable(param, key_part->key, field);
6950  goto end;
6951  }
6952 
6953  if (save_value_and_handle_conversion(&tree, value, type, field,
6954  &impossible_cond_cause, alloc))
6955  goto end;
6956 
6957  /*
6958  Any sargable predicate except "<=>" involving NULL as a constant is always
6959  FALSE
6960  */
6961  if (type != Item_func::EQUAL_FUNC && field->is_real_null())
6962  {
6963  impossible_cond_cause= "comparison_with_null_always_false";
6964  tree= &null_element;
6965  goto end;
6966  }
6967 
6968  str= (uchar*) alloc_root(alloc, key_part->store_length+1);
6969  if (!str)
6970  goto end;
6971  if (maybe_null)
6972  *str= (uchar) field->is_real_null(); // Set to 1 if null
6973  field->get_key_image(str+maybe_null, key_part->length,
6974  key_part->image_type);
6975  if (!(tree= new (alloc) SEL_ARG(field, str, str)))
6976  goto end; // out of memory
6977 
6978  /*
6979  Check if we are comparing an UNSIGNED integer with a negative constant.
6980  In this case we know that:
6981  (a) (unsigned_int [< | <=] negative_constant) == FALSE
6982  (b) (unsigned_int [> | >=] negative_constant) == TRUE
6983  In case (a) the condition is false for all values, and in case (b) it
6984  is true for all values, so we can avoid unnecessary retrieval and condition
6985  testing, and we also get correct comparison of unsinged integers with
6986  negative integers (which otherwise fails because at query execution time
6987  negative integers are cast to unsigned if compared with unsigned).
6988  */
6989  if (field->result_type() == INT_RESULT &&
6990  value->result_type() == INT_RESULT &&
6991  ((field->type() == FIELD_TYPE_BIT ||
6992  ((Field_num *) field)->unsigned_flag) &&
6993  !((Item_int*) value)->unsigned_flag))
6994  {
6995  longlong item_val= value->val_int();
6996  if (item_val < 0)
6997  {
6998  if (type == Item_func::LT_FUNC || type == Item_func::LE_FUNC)
6999  {
7000  impossible_cond_cause= "unsigned_int_cannot_be_negative";
7001  tree->type= SEL_ARG::IMPOSSIBLE;
7002  goto end;
7003  }
7004  if (type == Item_func::GT_FUNC || type == Item_func::GE_FUNC)
7005  {
7006  tree= 0;
7007  goto end;
7008  }
7009  }
7010  }
7011 
7012  switch (type) {
7013  case Item_func::LT_FUNC:
7014  if (stored_field_cmp_to_item(param->thd, field, value) == 0)
7015  tree->max_flag=NEAR_MAX;
7016  /* fall through */
7017  case Item_func::LE_FUNC:
7018  if (!maybe_null)
7019  tree->min_flag=NO_MIN_RANGE; /* From start */
7020  else
7021  { // > NULL
7022  if (!(tree->min_value=
7023  static_cast<uchar*>(alloc_root(alloc, key_part->store_length+1))))
7024  goto end;
7025  TRASH(tree->min_value, key_part->store_length + 1);
7026  memcpy(tree->min_value, is_null_string, sizeof(is_null_string));
7027  tree->min_flag=NEAR_MIN;
7028  }
7029  break;
7030  case Item_func::GT_FUNC:
7031  /* Don't use open ranges for partial key_segments */
7032  if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
7033  (stored_field_cmp_to_item(param->thd, field, value) <= 0))
7034  tree->min_flag=NEAR_MIN;
7035  tree->max_flag= NO_MAX_RANGE;
7036  break;
7037  case Item_func::GE_FUNC:
7038  /* Don't use open ranges for partial key_segments */
7039  if ((!(key_part->flag & HA_PART_KEY_SEG)) &&
7040  (stored_field_cmp_to_item(param->thd, field, value) < 0))
7041  tree->min_flag= NEAR_MIN;
7042  tree->max_flag=NO_MAX_RANGE;
7043  break;
7044  case Item_func::SP_EQUALS_FUNC:
7045  tree->min_flag=GEOM_FLAG | HA_READ_MBR_EQUAL;// NEAR_MIN;//512;
7046  tree->max_flag=NO_MAX_RANGE;
7047  break;
7048  case Item_func::SP_DISJOINT_FUNC:
7049  tree->min_flag=GEOM_FLAG | HA_READ_MBR_DISJOINT;// NEAR_MIN;//512;
7050  tree->max_flag=NO_MAX_RANGE;
7051  break;
7052  case Item_func::SP_INTERSECTS_FUNC:
7053  tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
7054  tree->max_flag=NO_MAX_RANGE;
7055  break;
7056  case Item_func::SP_TOUCHES_FUNC:
7057  tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
7058  tree->max_flag=NO_MAX_RANGE;
7059  break;
7060 
7061  case Item_func::SP_CROSSES_FUNC:
7062  tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
7063  tree->max_flag=NO_MAX_RANGE;
7064  break;
7065  case Item_func::SP_WITHIN_FUNC:
7066  /*
7067  Adjust the min_flag as MyISAM implements this function
7068  in reverse order.
7069  */
7070  tree->min_flag=GEOM_FLAG | HA_READ_MBR_CONTAIN;// NEAR_MIN;//512;
7071  tree->max_flag=NO_MAX_RANGE;
7072  break;
7073 
7074  case Item_func::SP_CONTAINS_FUNC:
7075  /*
7076  Adjust the min_flag as MyISAM implements this function
7077  in reverse order.
7078  */
7079  tree->min_flag=GEOM_FLAG | HA_READ_MBR_WITHIN;// NEAR_MIN;//512;
7080  tree->max_flag=NO_MAX_RANGE;
7081  break;
7082  case Item_func::SP_OVERLAPS_FUNC:
7083  tree->min_flag=GEOM_FLAG | HA_READ_MBR_INTERSECT;// NEAR_MIN;//512;
7084  tree->max_flag=NO_MAX_RANGE;
7085  break;
7086 
7087  default:
7088  break;
7089  }
7090 
7091 end:
7092  if (impossible_cond_cause != NULL)
7093  {
7094  Opt_trace_object wrapper (&param->thd->opt_trace);
7095  Opt_trace_object (&param->thd->opt_trace, "impossible_condition",
7096  Opt_trace_context::RANGE_OPTIMIZER).
7097  add_alnum("cause", impossible_cond_cause);
7098  }
7099  param->thd->mem_root= alloc;
7100  DBUG_RETURN(tree);
7101 }
7102 
7103 
7104 /******************************************************************************
7105 ** Tree manipulation functions
7106 ** If tree is 0 it means that the condition can't be tested. It refers
7107 ** to a non existent table or to a field in current table with isn't a key.
7108 ** The different tree flags:
7109 ** IMPOSSIBLE: Condition is never TRUE
7110 ** ALWAYS: Condition is always TRUE
7111 ** MAYBE: Condition may exists when tables are read
7112 ** MAYBE_KEY: Condition refers to a key that may be used in join loop
7113 ** KEY_RANGE: Condition uses a key
7114 ******************************************************************************/
7115 
7116 /*
7117  Add a new key test to a key when scanning through all keys
7118  This will never be called for same key parts.
7119 */
7120 
7121 static SEL_ARG *
7122 sel_add(SEL_ARG *key1,SEL_ARG *key2)
7123 {
7124  SEL_ARG *root,**key_link;
7125 
7126  if (!key1)
7127  return key2;
7128  if (!key2)
7129  return key1;
7130 
7131  key_link= &root;
7132  while (key1 && key2)
7133  {
7134  if (key1->part < key2->part)
7135  {
7136  *key_link= key1;
7137  key_link= &key1->next_key_part;
7138  key1=key1->next_key_part;
7139  }
7140  else
7141  {
7142  *key_link= key2;
7143  key_link= &key2->next_key_part;
7144  key2=key2->next_key_part;
7145  }
7146  }
7147  *key_link=key1 ? key1 : key2;
7148  return root;
7149 }
7150 
7151 #define CLONE_KEY1_MAYBE 1
7152 #define CLONE_KEY2_MAYBE 2
7153 #define swap_clone_flag(A) ((A & 1) << 1) | ((A & 2) >> 1)
7154 
7155 
7156 static SEL_TREE *
7157 tree_and(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
7158 {
7159  DBUG_ENTER("tree_and");
7160  if (!tree1)
7161  DBUG_RETURN(tree2);
7162  if (!tree2)
7163  DBUG_RETURN(tree1);
7164  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
7165  DBUG_RETURN(tree1);
7166  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
7167  DBUG_RETURN(tree2);
7168  if (tree1->type == SEL_TREE::MAYBE)
7169  {
7170  if (tree2->type == SEL_TREE::KEY)
7171  tree2->type=SEL_TREE::KEY_SMALLER;
7172  DBUG_RETURN(tree2);
7173  }
7174  if (tree2->type == SEL_TREE::MAYBE)
7175  {
7176  tree1->type=SEL_TREE::KEY_SMALLER;
7177  DBUG_RETURN(tree1);
7178  }
7179 
7180  dbug_print_tree("tree1", tree1, param);
7181  dbug_print_tree("tree2", tree2, param);
7182 
7183  key_map result_keys;
7184 
7185  /* Join the trees key per key */
7186  SEL_ARG **key1,**key2,**end;
7187  for (key1= tree1->keys,key2= tree2->keys,end=key1+param->keys ;
7188  key1 != end ; key1++,key2++)
7189  {
7190  uint flag=0;
7191  if (*key1 || *key2)
7192  {
7193  if (*key1 && !(*key1)->simple_key())
7194  flag|=CLONE_KEY1_MAYBE;
7195  if (*key2 && !(*key2)->simple_key())
7196  flag|=CLONE_KEY2_MAYBE;
7197  *key1=key_and(param, *key1, *key2, flag);
7198  if (*key1 && (*key1)->type == SEL_ARG::IMPOSSIBLE)
7199  {
7200  tree1->type= SEL_TREE::IMPOSSIBLE;
7201  DBUG_RETURN(tree1);
7202  }
7203  result_keys.set_bit(key1 - tree1->keys);
7204 #ifndef DBUG_OFF
7205  if (*key1 && param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
7206  (*key1)->test_use_count(*key1);
7207 #endif
7208  }
7209  }
7210  tree1->keys_map= result_keys;
7211 
7212  /* ok, both trees are index_merge trees */
7213  imerge_list_and_list(&tree1->merges, &tree2->merges);
7214  DBUG_RETURN(tree1);
7215 }
7216 
7217 
7218 /*
7219  Check if two SEL_TREES can be combined into one (i.e. a single key range
7220  read can be constructed for "cond_of_tree1 OR cond_of_tree2" ) without
7221  using index_merge.
7222 */
7223 
7224 bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
7225  RANGE_OPT_PARAM* param)
7226 {
7227  key_map common_keys= tree1->keys_map;
7228  DBUG_ENTER("sel_trees_can_be_ored");
7229  common_keys.intersect(tree2->keys_map);
7230 
7231  dbug_print_tree("tree1", tree1, param);
7232  dbug_print_tree("tree2", tree2, param);
7233 
7234  if (common_keys.is_clear_all())
7235  DBUG_RETURN(FALSE);
7236 
7237  /* trees have a common key, check if they refer to same key part */
7238  SEL_ARG **key1,**key2;
7239  for (uint key_no=0; key_no < param->keys; key_no++)
7240  {
7241  if (common_keys.is_set(key_no))
7242  {
7243  key1= tree1->keys + key_no;
7244  key2= tree2->keys + key_no;
7245  if ((*key1)->part == (*key2)->part)
7246  DBUG_RETURN(TRUE);
7247  }
7248  }
7249  DBUG_RETURN(FALSE);
7250 }
7251 
7252 
7253 /*
7254  Remove the trees that are not suitable for record retrieval.
7255  SYNOPSIS
7256  param Range analysis parameter
7257  tree Tree to be processed, tree->type is KEY or KEY_SMALLER
7258 
7259  DESCRIPTION
7260  This function walks through tree->keys[] and removes the SEL_ARG* trees
7261  that are not "maybe" trees (*) and cannot be used to construct quick range
7262  selects.
7263  (*) - have type MAYBE or MAYBE_KEY. Perhaps we should remove trees of
7264  these types here as well.
7265 
7266  A SEL_ARG* tree cannot be used to construct quick select if it has
7267  tree->part != 0. (e.g. it could represent "keypart2 < const").
7268 
7269  WHY THIS FUNCTION IS NEEDED
7270 
7271  Normally we allow construction of SEL_TREE objects that have SEL_ARG
7272  trees that do not allow quick range select construction. For example for
7273  " keypart1=1 AND keypart2=2 " the execution will proceed as follows:
7274  tree1= SEL_TREE { SEL_ARG{keypart1=1} }
7275  tree2= SEL_TREE { SEL_ARG{keypart2=2} } -- can't make quick range select
7276  from this
7277  call tree_and(tree1, tree2) -- this joins SEL_ARGs into a usable SEL_ARG
7278  tree.
7279 
7280  There is an exception though: when we construct index_merge SEL_TREE,
7281  any SEL_ARG* tree that cannot be used to construct quick range select can
7282  be removed, because current range analysis code doesn't provide any way
7283  that tree could be later combined with another tree.
7284  Consider an example: we should not construct
7285  st1 = SEL_TREE {
7286  merges = SEL_IMERGE {
7287  SEL_TREE(t.key1part1 = 1),
7288  SEL_TREE(t.key2part2 = 2) -- (*)
7289  }
7290  };
7291  because
7292  - (*) cannot be used to construct quick range select,
7293  - There is no execution path that would cause (*) to be converted to
7294  a tree that could be used.
7295 
7296  The latter is easy to verify: first, notice that the only way to convert
7297  (*) into a usable tree is to call tree_and(something, (*)).
7298 
7299  Second look at what tree_and/tree_or function would do when passed a
7300  SEL_TREE that has the structure like st1 tree has, and conlcude that
7301  tree_and(something, (*)) will not be called.
7302 
7303  RETURN
7304  0 Ok, some suitable trees left
7305  1 No tree->keys[] left.
7306 */
7307 
7308 static bool remove_nonrange_trees(RANGE_OPT_PARAM *param, SEL_TREE *tree)
7309 {
7310  bool res= FALSE;
7311  for (uint i=0; i < param->keys; i++)
7312  {
7313  if (tree->keys[i])
7314  {
7315  if (tree->keys[i]->part)
7316  {
7317  tree->keys[i]= NULL;
7318  tree->keys_map.clear_bit(i);
7319  }
7320  else
7321  res= TRUE;
7322  }
7323  }
7324  return !res;
7325 }
7326 
7327 
7328 static SEL_TREE *
7329 tree_or(RANGE_OPT_PARAM *param,SEL_TREE *tree1,SEL_TREE *tree2)
7330 {
7331  DBUG_ENTER("tree_or");
7332  if (!tree1 || !tree2)
7333  DBUG_RETURN(0);
7334  if (tree1->type == SEL_TREE::IMPOSSIBLE || tree2->type == SEL_TREE::ALWAYS)
7335  DBUG_RETURN(tree2);
7336  if (tree2->type == SEL_TREE::IMPOSSIBLE || tree1->type == SEL_TREE::ALWAYS)
7337  DBUG_RETURN(tree1);
7338  if (tree1->type == SEL_TREE::MAYBE)
7339  DBUG_RETURN(tree1); // Can't use this
7340  if (tree2->type == SEL_TREE::MAYBE)
7341  DBUG_RETURN(tree2);
7342 
7343  /*
7344  It is possible that a tree contains both
7345  a) simple range predicates (in tree->keys[]) and
7346  b) index merge range predicates (in tree->merges)
7347 
7348  If a tree has both, they represent equally *valid* range
7349  predicate alternatives; both will return all relevant rows from
7350  the table but one may return more unnecessary rows than the
7351  other (additional rows will be filtered later). However, doing
7352  an OR operation on trees with both types of predicates is too
7353  complex at the time. We therefore remove the index merge
7354  predicates (if we have both types) before OR'ing the trees.
7355 
7356  TODO: enable tree_or() for trees with both simple and index
7357  merge range predicates.
7358  */
7359  if (!tree1->merges.is_empty())
7360  {
7361  for (uint i= 0; i < param->keys; i++)
7362  if (tree1->keys[i] != NULL && tree2->keys[i] != &null_element)
7363  {
7364  tree1->merges.empty();
7365  break;
7366  }
7367  }
7368  if (!tree2->merges.is_empty())
7369  {
7370  for (uint i= 0; i< param->keys; i++)
7371  if (tree2->keys[i] != NULL && tree2->keys[i] != &null_element)
7372  {
7373  tree2->merges.empty();
7374  break;
7375  }
7376  }
7377 
7378  SEL_TREE *result= 0;
7379  key_map result_keys;
7380  if (sel_trees_can_be_ored(tree1, tree2, param))
7381  {
7382  /* Join the trees key per key */
7383  SEL_ARG **key1,**key2,**end;
7384  for (key1= tree1->keys,key2= tree2->keys,end= key1+param->keys ;
7385  key1 != end ; key1++,key2++)
7386  {
7387  *key1=key_or(param, *key1, *key2);
7388  if (*key1)
7389  {
7390  result=tree1; // Added to tree1
7391  result_keys.set_bit(key1 - tree1->keys);
7392 #ifndef DBUG_OFF
7393  if (param->alloced_sel_args < SEL_ARG::MAX_SEL_ARGS)
7394  (*key1)->test_use_count(*key1);
7395 #endif
7396  }
7397  }
7398  if (result)
7399  result->keys_map= result_keys;
7400  }
7401  else
7402  {
7403  /* ok, two trees have KEY type but cannot be used without index merge */
7404  if (tree1->merges.is_empty() && tree2->merges.is_empty())
7405  {
7406  if (param->remove_jump_scans)
7407  {
7408  bool no_trees= remove_nonrange_trees(param, tree1);
7409  no_trees= no_trees || remove_nonrange_trees(param, tree2);
7410  if (no_trees)
7411  DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
7412  }
7413  SEL_IMERGE *merge;
7414  /* both trees are "range" trees, produce new index merge structure */
7415  if (!(result= new SEL_TREE()) || !(merge= new SEL_IMERGE()) ||
7416  (result->merges.push_back(merge)) ||
7417  (merge->or_sel_tree(param, tree1)) ||
7418  (merge->or_sel_tree(param, tree2)))
7419  result= NULL;
7420  else
7421  result->type= tree1->type;
7422  }
7423  else if (!tree1->merges.is_empty() && !tree2->merges.is_empty())
7424  {
7425  if (imerge_list_or_list(param, &tree1->merges, &tree2->merges))
7426  result= new SEL_TREE(SEL_TREE::ALWAYS);
7427  else
7428  result= tree1;
7429  }
7430  else
7431  {
7432  /* one tree is index merge tree and another is range tree */
7433  if (tree1->merges.is_empty())
7434  swap_variables(SEL_TREE*, tree1, tree2);
7435 
7436  if (param->remove_jump_scans && remove_nonrange_trees(param, tree2))
7437  DBUG_RETURN(new SEL_TREE(SEL_TREE::ALWAYS));
7438  /* add tree2 to tree1->merges, checking if it collapses to ALWAYS */
7439  if (imerge_list_or_tree(param, &tree1->merges, tree2))
7440  result= new SEL_TREE(SEL_TREE::ALWAYS);
7441  else
7442  result= tree1;
7443  }
7444  }
7445  DBUG_RETURN(result);
7446 }
7447 
7448 
7449 /* And key trees where key1->part < key2 -> part */
7450 
7451 static SEL_ARG *
7452 and_all_keys(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2,
7453  uint clone_flag)
7454 {
7455  SEL_ARG *next;
7456  ulong use_count=key1->use_count;
7457 
7458  if (key1->elements != 1)
7459  {
7460  key2->use_count+=key1->elements-1; //psergey: why we don't count that key1 has n-k-p?
7461  key2->increment_use_count((int) key1->elements-1);
7462  }
7463  if (key1->type == SEL_ARG::MAYBE_KEY)
7464  {
7465  // See todo for left/right pointers
7466  DBUG_ASSERT(!key1->left);
7467  DBUG_ASSERT(!key1->right);
7468  key1->next= key1->prev= 0;
7469  }
7470  for (next=key1->first(); next ; next=next->next)
7471  {
7472  if (next->next_key_part)
7473  {
7474  SEL_ARG *tmp= key_and(param, next->next_key_part, key2, clone_flag);
7475  if (tmp && tmp->type == SEL_ARG::IMPOSSIBLE)
7476  {
7477  key1=key1->tree_delete(next);
7478  continue;
7479  }
7480  next->next_key_part=tmp;
7481  if (use_count)
7482  next->increment_use_count(use_count);
7483  if (param->alloced_sel_args > SEL_ARG::MAX_SEL_ARGS)
7484  break;
7485  }
7486  else
7487  next->next_key_part=key2;
7488  }
7489  if (!key1)
7490  return &null_element; // Impossible ranges
7491  key1->use_count++;
7492  return key1;
7493 }
7494 
7495 
7496 /*
7497  Produce a SEL_ARG graph that represents "key1 AND key2"
7498 
7499  SYNOPSIS
7500  key_and()
7501  param Range analysis context (needed to track if we have allocated
7502  too many SEL_ARGs)
7503  key1 First argument, root of its RB-tree
7504  key2 Second argument, root of its RB-tree
7505 
7506  RETURN
7507  RB-tree root of the resulting SEL_ARG graph.
7508  NULL if the result of AND operation is an empty interval {0}.
7509 */
7510 
7511 static SEL_ARG *
7512 key_and(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2, uint clone_flag)
7513 {
7514  if (!key1)
7515  return key2;
7516  if (!key2)
7517  return key1;
7518  if (key1->part != key2->part)
7519  {
7520  if (key1->part > key2->part)
7521  {
7522  swap_variables(SEL_ARG *, key1, key2);
7523  clone_flag=swap_clone_flag(clone_flag);
7524  }
7525  // key1->part < key2->part
7526  key1->use_count--;
7527  if (key1->use_count > 0)
7528  if (!(key1= key1->clone_tree(param)))
7529  return 0; // OOM
7530  return and_all_keys(param, key1, key2, clone_flag);
7531  }
7532 
7533  if (((clone_flag & CLONE_KEY2_MAYBE) &&
7534  !(clone_flag & CLONE_KEY1_MAYBE) &&
7535  key2->type != SEL_ARG::MAYBE_KEY) ||
7536  key1->type == SEL_ARG::MAYBE_KEY)
7537  { // Put simple key in key2
7538  swap_variables(SEL_ARG *, key1, key2);
7539  clone_flag=swap_clone_flag(clone_flag);
7540  }
7541 
7542  /* If one of the key is MAYBE_KEY then the found region may be smaller */
7543  if (key2->type == SEL_ARG::MAYBE_KEY)
7544  {
7545  if (key1->use_count > 1)
7546  {
7547  key1->use_count--;
7548  if (!(key1=key1->clone_tree(param)))
7549  return 0; // OOM
7550  key1->use_count++;
7551  }
7552  if (key1->type == SEL_ARG::MAYBE_KEY)
7553  { // Both are maybe key
7554  key1->next_key_part=key_and(param, key1->next_key_part,
7555  key2->next_key_part, clone_flag);
7556  if (key1->next_key_part &&
7557  key1->next_key_part->type == SEL_ARG::IMPOSSIBLE)
7558  return key1;
7559  }
7560  else
7561  {
7562  key1->maybe_smaller();
7563  if (key2->next_key_part)
7564  {
7565  key1->use_count--; // Incremented in and_all_keys
7566  return and_all_keys(param, key1, key2, clone_flag);
7567  }
7568  key2->use_count--; // Key2 doesn't have a tree
7569  }
7570  return key1;
7571  }
7572 
7573  if ((key1->min_flag | key2->min_flag) & GEOM_FLAG)
7574  {
7575  /* TODO: why not leave one of the trees? */
7576  key1->free_tree();
7577  key2->free_tree();
7578  return 0; // Can't optimize this
7579  }
7580 
7581  key1->use_count--;
7582  key2->use_count--;
7583  SEL_ARG *e1=key1->first(), *e2=key2->first(), *new_tree=0;
7584 
7585  while (e1 && e2)
7586  {
7587  int cmp=e1->cmp_min_to_min(e2);
7588  if (cmp < 0)
7589  {
7590  if (get_range(&e1,&e2,key1))
7591  continue;
7592  }
7593  else if (get_range(&e2,&e1,key2))
7594  continue;
7595  SEL_ARG *next=key_and(param, e1->next_key_part, e2->next_key_part,
7596  clone_flag);
7597  e1->increment_use_count(1);
7598  e2->increment_use_count(1);
7599  if (!next || next->type != SEL_ARG::IMPOSSIBLE)
7600  {
7601  SEL_ARG *new_arg= e1->clone_and(e2);
7602  if (!new_arg)
7603  return &null_element; // End of memory
7604  new_arg->next_key_part=next;
7605  if (!new_tree)
7606  {
7607  new_tree=new_arg;
7608  }
7609  else
7610  new_tree=new_tree->insert(new_arg);
7611  }
7612  if (e1->cmp_max_to_max(e2) < 0)
7613  e1=e1->next; // e1 can't overlapp next e2
7614  else
7615  e2=e2->next;
7616  }
7617  key1->free_tree();
7618  key2->free_tree();
7619  if (!new_tree)
7620  return &null_element; // Impossible range
7621  return new_tree;
7622 }
7623 
7624 
7625 static bool
7626 get_range(SEL_ARG **e1,SEL_ARG **e2,SEL_ARG *root1)
7627 {
7628  (*e1)=root1->find_range(*e2); // first e1->min < e2->min
7629  if ((*e1)->cmp_max_to_min(*e2) < 0)
7630  {
7631  if (!((*e1)=(*e1)->next))
7632  return 1;
7633  if ((*e1)->cmp_min_to_max(*e2) > 0)
7634  {
7635  (*e2)=(*e2)->next;
7636  return 1;
7637  }
7638  }
7639  return 0;
7640 }
7641 
7642 
7704 static SEL_ARG *
7705 key_or(RANGE_OPT_PARAM *param, SEL_ARG *key1, SEL_ARG *key2)
7706 {
7707  if (!key1)
7708  {
7709  if (key2)
7710  {
7711  key2->use_count--;
7712  key2->free_tree();
7713  }
7714  return 0;
7715  }
7716  if (!key2)
7717  {
7718  key1->use_count--;
7719  key1->free_tree();
7720  return 0;
7721  }
7722  key1->use_count--;
7723  key2->use_count--;
7724 
7725  if (key1->part != key2->part ||
7726  (key1->min_flag | key2->min_flag) & GEOM_FLAG)
7727  {
7728  key1->free_tree();
7729  key2->free_tree();
7730  return 0; // Can't optimize this
7731  }
7732 
7733  // If one of the key is MAYBE_KEY then the found region may be bigger
7734  if (key1->type == SEL_ARG::MAYBE_KEY)
7735  {
7736  key2->free_tree();
7737  key1->use_count++;
7738  return key1;
7739  }
7740  if (key2->type == SEL_ARG::MAYBE_KEY)
7741  {
7742  key1->free_tree();
7743  key2->use_count++;
7744  return key2;
7745  }
7746 
7747  if (key1->use_count > 0)
7748  {
7749  if (key2->use_count == 0 || key1->elements > key2->elements)
7750  {
7751  swap_variables(SEL_ARG *,key1,key2);
7752  }
7753  if (key1->use_count > 0 || !(key1=key1->clone_tree(param)))
7754  return 0; // OOM
7755  }
7756 
7757  // Add tree at key2 to tree at key1
7758  const bool key2_shared= (key2->use_count != 0);
7759  key1->maybe_flag|= key2->maybe_flag;
7760 
7761  /*
7762  Notation for illustrations used in the rest of this function:
7763 
7764  Range: [--------]
7765  ^ ^
7766  start stop
7767 
7768  Two overlapping ranges:
7769  [-----] [----] [--]
7770  [---] or [---] or [-------]
7771 
7772  Ambiguity: ***
7773  The range starts or stops somewhere in the "***" range.
7774  Example: a starts before b and may end before/the same place/after b
7775  a: [----***]
7776  b: [---]
7777 
7778  Adjacent ranges:
7779  Ranges that meet but do not overlap. Example: a = "x < 3", b = "x >= 3"
7780  a: ----]
7781  b: [----
7782  */
7783 
7784  SEL_ARG *cur_key2= key2->first();
7785  while (cur_key2)
7786  {
7787  /*
7788  key1 consists of one or more ranges. cur_key1 is the
7789  range currently being handled.
7790 
7791  initialize cur_key1 to the latest range in key1 that starts the
7792  same place or before the range in cur_key2 starts
7793 
7794  cur_key2: [------]
7795  key1: [---] [-----] [----]
7796  ^
7797  cur_key1
7798  */
7799  SEL_ARG *cur_key1= key1->find_range(cur_key2);
7800 
7801  /*
7802  Used to describe how two key values are positioned compared to
7803  each other. Consider key_value_a.<cmp_func>(key_value_b):
7804 
7805  -2: key_value_a is smaller than key_value_b, and they are adjacent
7806  -1: key_value_a is smaller than key_value_b (not adjacent)
7807  0: the key values are equal
7808  1: key_value_a is bigger than key_value_b (not adjacent)
7809  2: key_value_a is bigger than key_value_b, and they are adjacent
7810 
7811  Example: "cmp= cur_key1->cmp_max_to_min(cur_key2)"
7812 
7813  cur_key2: [-------- (10 <= x ... )
7814  cur_key1: -----] ( ... x < 10) => cmp==-2
7815  cur_key1: ----] ( ... x < 9) => cmp==-1
7816  cur_key1: ------] ( ... x <= 10) => cmp== 0
7817  cur_key1: --------] ( ... x <= 12) => cmp== 1
7818  (cmp == 2 does not make sense for cmp_max_to_min())
7819  */
7820  int cmp= 0;
7821 
7822  if (!cur_key1)
7823  {
7824  /*
7825  The range in cur_key2 starts before the first range in key1. Use
7826  the first range in key1 as cur_key1.
7827 
7828  cur_key2: [--------]
7829  key1: [****--] [----] [-------]
7830  ^
7831  cur_key1
7832  */
7833  cur_key1= key1->first();
7834  cmp= -1;
7835  }
7836  else if ((cmp= cur_key1->cmp_max_to_min(cur_key2)) < 0)
7837  {
7838  /*
7839  This is the case:
7840  cur_key2: [-------]
7841  cur_key1: [----**]
7842  */
7843  SEL_ARG *next_key1= cur_key1->next;
7844  if (cmp == -2 &&
7845  eq_tree(cur_key1->next_key_part, cur_key2->next_key_part))
7846  {
7847  /*
7848  Adjacent (cmp==-2) and equal next_key_parts => ranges can be merged
7849 
7850  This is the case:
7851  cur_key2: [-------]
7852  cur_key1: [----]
7853 
7854  Result:
7855  cur_key2: [-------------] => inserted into key1 below
7856  cur_key1: => deleted
7857  */
7858  SEL_ARG *next_key2= cur_key2->next;
7859  if (key2_shared)
7860  {
7861  if (!(cur_key2= new SEL_ARG(*cur_key2)))
7862  return 0; // out of memory
7863  cur_key2->increment_use_count(key1->use_count+1);
7864  cur_key2->next= next_key2; // New copy of cur_key2
7865  }
7866 
7867  if (cur_key2->copy_min(cur_key1))
7868  {
7869  // cur_key2 is full range: [-inf <= cur_key2 <= +inf]
7870  key1->free_tree();
7871  key2->free_tree();
7872  key1->type= SEL_ARG::ALWAYS;
7873  key2->type= SEL_ARG::ALWAYS;
7874  if (key1->maybe_flag)
7875  return new SEL_ARG(SEL_ARG::MAYBE_KEY);
7876  return 0;
7877  }
7878 
7879  if (!(key1= key1->tree_delete(cur_key1)))
7880  {
7881  /*
7882  cur_key1 was the last range in key1; move the cur_key2
7883  range that was merged above to key1
7884  */
7885  key1= cur_key2;
7886  key1->make_root();
7887  cur_key2= next_key2;
7888  break;
7889  }
7890  }
7891  // Move to next range in key1. Now cur_key1.min > cur_key2.min
7892  if (!(cur_key1= next_key1))
7893  break; // No more ranges in key1. Copy rest of key2
7894  }
7895 
7896  if (cmp < 0)
7897  {
7898  /*
7899  This is the case:
7900  cur_key2: [--***]
7901  cur_key1: [----]
7902  */
7903  int cur_key1_cmp;
7904  if ((cur_key1_cmp= cur_key1->cmp_min_to_max(cur_key2)) > 0)
7905  {
7906  /*
7907  This is the case:
7908  cur_key2: [------**]
7909  cur_key1: [----]
7910  */
7911  if (cur_key1_cmp == 2 &&
7912  eq_tree(cur_key1->next_key_part, cur_key2->next_key_part))
7913  {
7914  /*
7915  Adjacent ranges with equal next_key_part. Merge like this:
7916 
7917  This is the case:
7918  cur_key2: [------]
7919  cur_key1: [-----]
7920 
7921  Result:
7922  cur_key2: [------]
7923  cur_key1: [-------------]
7924 
7925  Then move on to next key2 range.
7926  */
7927  cur_key1->copy_min_to_min(cur_key2);
7928  key1->merge_flags(cur_key2); //should be cur_key1->merge...() ?
7929  if (cur_key1->min_flag & NO_MIN_RANGE &&
7930  cur_key1->max_flag & NO_MAX_RANGE)
7931  {
7932  if (key1->maybe_flag)
7933  return new SEL_ARG(SEL_ARG::MAYBE_KEY);
7934  return 0;
7935  }
7936  cur_key2->increment_use_count(-1); // Free not used tree
7937  cur_key2=cur_key2->next;
7938  continue;
7939  }
7940  else
7941  {
7942  /*
7943  cur_key2 not adjacent to cur_key1 or has different next_key_part.
7944  Insert into key1 and move to next range in key2
7945 
7946  This is the case:
7947  cur_key2: [------**]
7948  cur_key1: [----]
7949 
7950  Result:
7951  key1: [------**][----]
7952  ^ ^
7953  insert cur_key1
7954  */
7955  SEL_ARG *next_key2= cur_key2->next;
7956  if (key2_shared)
7957  {
7958  SEL_ARG *cpy= new SEL_ARG(*cur_key2); // Must make copy
7959  if (!cpy)
7960  return 0; // OOM
7961  key1= key1->insert(cpy);
7962  cur_key2->increment_use_count(key1->use_count+1);
7963  }
7964  else
7965  key1= key1->insert(cur_key2); // Will destroy key2_root
7966  cur_key2= next_key2;
7967  continue;
7968  }
7969  }
7970  }
7971 
7972  /*
7973  The ranges in cur_key1 and cur_key2 are overlapping:
7974 
7975  cur_key2: [----------]
7976  cur_key1: [*****-----*****]
7977 
7978  Corollary: cur_key1.min <= cur_key2.max
7979  */
7980  if (eq_tree(cur_key1->next_key_part, cur_key2->next_key_part))
7981  {
7982  // Merge overlapping ranges with equal next_key_part
7983  if (cur_key1->is_same(cur_key2))
7984  {
7985  /*
7986  cur_key1 covers exactly the same range as cur_key2
7987  Use the relevant range in key1.
7988  */
7989  cur_key1->merge_flags(cur_key2); // Copy maybe flags
7990  cur_key2->increment_use_count(-1); // Free not used tree
7991  }
7992  else
7993  {
7994  SEL_ARG *last= cur_key1;
7995  SEL_ARG *first= cur_key1;
7996 
7997  /*
7998  Find the last range in key1 that overlaps cur_key2 and
7999  where all ranges first...last have the same next_key_part as
8000  cur_key2.
8001 
8002  cur_key2: [****----------------------*******]
8003  key1: [--] [----] [---] [-----] [xxxx]
8004  ^ ^ ^
8005  first last different next_key_part
8006 
8007  Since cur_key2 covers them, the ranges between first and last
8008  are merged into one range by deleting first...last-1 from
8009  the key1 tree. In the figure, this applies to first and the
8010  two consecutive ranges. The range of last is then extended:
8011  * last.min: Set to min(cur_key2.min, first.min)
8012  * last.max: If there is a last->next that overlaps cur_key2
8013  (i.e., last->next has a different next_key_part):
8014  Set adjacent to last->next.min
8015  Otherwise: Set to max(cur_key2.max, last.max)
8016 
8017  Result:
8018  cur_key2: [****----------------------*******]
8019  [--] [----] [---] => deleted from key1
8020  key1: [**------------------------***][xxxx]
8021  ^ ^
8022  cur_key1=last different next_key_part
8023  */
8024  while (last->next && last->next->cmp_min_to_max(cur_key2) <= 0 &&
8025  eq_tree(last->next->next_key_part, cur_key2->next_key_part))
8026  {
8027  /*
8028  last->next is covered by cur_key2 and has same next_key_part.
8029  last can be deleted
8030  */
8031  SEL_ARG *save=last;
8032  last=last->next;
8033  key1= key1->tree_delete(save);
8034  }
8035  // Redirect cur_key1 to last which will cover the entire range
8036  cur_key1= last;
8037 
8038  /*
8039  Extend last to cover the entire range of
8040  [min(first.min_value,cur_key2.min_value)...last.max_value].
8041  If this forms a full range (the range covers all possible
8042  values) we return no SEL_ARG RB-tree.
8043  */
8044  bool full_range= last->copy_min(first);
8045  if (!full_range)
8046  full_range= last->copy_min(cur_key2);
8047 
8048  if (!full_range)
8049  {
8050  if (last->next && cur_key2->cmp_max_to_min(last->next) >= 0)
8051  {
8052  /*
8053  This is the case:
8054  cur_key2: [-------------]
8055  key1: [***------] [xxxx]
8056  ^ ^
8057  last different next_key_part
8058 
8059  Extend range of last up to last->next:
8060  cur_key2: [-------------]
8061  key1: [***--------][xxxx]
8062  */
8063  last->copy_min_to_max(last->next);
8064  }
8065  else
8066  /*
8067  This is the case:
8068  cur_key2: [--------*****]
8069  key1: [***---------] [xxxx]
8070  ^ ^
8071  last different next_key_part
8072 
8073  Extend range of last up to max(last.max, cur_key2.max):
8074  cur_key2: [--------*****]
8075  key1: [***----------**] [xxxx]
8076  */
8077  full_range= last->copy_max(cur_key2);
8078  }
8079  if (full_range)
8080  { // Full range
8081  key1->free_tree();
8082  key1->type= SEL_ARG::ALWAYS;
8083  key2->type= SEL_ARG::ALWAYS;
8084  for (; cur_key2 ; cur_key2= cur_key2->next)
8085  cur_key2->increment_use_count(-1); // Free not used tree
8086  if (key1->maybe_flag)
8087  return new SEL_ARG(SEL_ARG::MAYBE_KEY);
8088  return 0;
8089  }
8090  }
8091  }
8092 
8093  if (cmp >= 0 && cur_key1->cmp_min_to_min(cur_key2) < 0)
8094  {
8095  /*
8096  This is the case ("cmp>=0" means that cur_key1.max >= cur_key2.min):
8097  cur_key2: [-------]
8098  cur_key1: [----------*******]
8099  */
8100 
8101  if (!cur_key1->next_key_part)
8102  {
8103  /*
8104  cur_key1->next_key_part is empty: cut the range that
8105  is covered by cur_key1 from cur_key2.
8106  Reason: (cur_key2->next_key_part OR
8107  cur_key1->next_key_part) will be empty and therefore
8108  equal to cur_key1->next_key_part. Thus, this part of
8109  the cur_key2 range is completely covered by cur_key1.
8110  */
8111  if (cur_key1->cmp_max_to_max(cur_key2) >= 0)
8112  {
8113  /*
8114  cur_key1 covers the entire range in cur_key2.
8115  cur_key2: [-------]
8116  cur_key1: [-----------------]
8117 
8118  Move on to next range in key2
8119  */
8120  cur_key2->increment_use_count(-1); // Free not used tree
8121  cur_key2= cur_key2->next;
8122  continue;
8123  }
8124  else
8125  {
8126  /*
8127  This is the case:
8128  cur_key2: [-------]
8129  cur_key1: [---------]
8130 
8131  Result:
8132  cur_key2: [---]
8133  cur_key1: [---------]
8134  */
8135  cur_key2->copy_max_to_min(cur_key1);
8136  continue;
8137  }
8138  }
8139 
8140  /*
8141  The ranges are overlapping but have not been merged because
8142  next_key_part of cur_key1 and cur_key2 differ.
8143  cur_key2: [----]
8144  cur_key1: [------------*****]
8145 
8146  Split cur_key1 in two where cur_key2 starts:
8147  cur_key2: [----]
8148  key1: [--------][--*****]
8149  ^ ^
8150  insert cur_key1
8151  */
8152  SEL_ARG *new_arg= cur_key1->clone_first(cur_key2);
8153  if (!new_arg)
8154  return 0; // OOM
8155  if ((new_arg->next_key_part= cur_key1->next_key_part))
8156  new_arg->increment_use_count(key1->use_count+1);
8157  cur_key1->copy_min_to_min(cur_key2);
8158  key1= key1->insert(new_arg);
8159  } // cur_key1.min >= cur_key2.min due to this if()
8160 
8161  /*
8162  Now cur_key2.min <= cur_key1.min <= cur_key2.max:
8163  cur_key2: [---------]
8164  cur_key1: [****---*****]
8165  */
8166  SEL_ARG key2_cpy(*cur_key2); // Get copy we can modify
8167  for (;;)
8168  {
8169  if (cur_key1->cmp_min_to_min(&key2_cpy) > 0)
8170  {
8171  /*
8172  This is the case:
8173  key2_cpy: [------------]
8174  key1: [-*****]
8175  ^
8176  cur_key1
8177 
8178  Result:
8179  key2_cpy: [---]
8180  key1: [-------][-*****]
8181  ^ ^
8182  insert cur_key1
8183  */
8184  SEL_ARG *new_arg=key2_cpy.clone_first(cur_key1);
8185  if (!new_arg)
8186  return 0; // OOM
8187  if ((new_arg->next_key_part=key2_cpy.next_key_part))
8188  new_arg->increment_use_count(key1->use_count+1);
8189  key1= key1->insert(new_arg);
8190  key2_cpy.copy_min_to_min(cur_key1);
8191  }
8192  // Now key2_cpy.min == cur_key1.min
8193 
8194  if ((cmp= cur_key1->cmp_max_to_max(&key2_cpy)) <= 0)
8195  {
8196  /*
8197  cur_key1.max <= key2_cpy.max:
8198  key2_cpy: a) [-------] or b) [----]
8199  cur_key1: [----] [----]
8200 
8201  Steps:
8202 
8203  1) Update next_key_part of cur_key1: OR it with
8204  key2_cpy->next_key_part.
8205  2) If case a: Insert range [cur_key1.max, key2_cpy.max]
8206  into key1 using next_key_part of key2_cpy
8207 
8208  Result:
8209  key1: a) [----][-] or b) [----]
8210  */
8211  cur_key1->maybe_flag|= key2_cpy.maybe_flag;
8212  key2_cpy.increment_use_count(key1->use_count+1);
8213  cur_key1->next_key_part=
8214  key_or(param, cur_key1->next_key_part, key2_cpy.next_key_part);
8215 
8216  if (!cmp)
8217  break; // case b: done with this key2 range
8218 
8219  // Make key2_cpy the range [cur_key1.max, key2_cpy.max]
8220  key2_cpy.copy_max_to_min(cur_key1);
8221  if (!(cur_key1= cur_key1->next))
8222  {
8223  /*
8224  No more ranges in key1. Insert key2_cpy and go to "end"
8225  label to insert remaining ranges in key2 if any.
8226  */
8227  SEL_ARG *new_key1_range= new SEL_ARG(key2_cpy);
8228  if (!new_key1_range)
8229  return 0; // OOM
8230  key1= key1->insert(new_key1_range);
8231  cur_key2= cur_key2->next;
8232  goto end;
8233  }
8234  if (cur_key1->cmp_min_to_max(&key2_cpy) > 0)
8235  {
8236  /*
8237  The next range in key1 does not overlap with key2_cpy.
8238  Insert this range into key1 and move on to the next range
8239  in key2.
8240  */
8241  SEL_ARG *new_key1_range= new SEL_ARG(key2_cpy);
8242  if (!new_key1_range)
8243  return 0; // OOM
8244  key1= key1->insert(new_key1_range);
8245  break;
8246  }
8247  /*
8248  key2_cpy overlaps with the next range in key1 and the case
8249  is now "cur_key2.min <= cur_key1.min <= cur_key2.max". Go back
8250  to for(;;) to handle this situation.
8251  */
8252  continue;
8253  }
8254  else
8255  {
8256  /*
8257  This is the case:
8258  key2_cpy: [-------]
8259  cur_key1: [------------]
8260 
8261  Result:
8262  key1: [-------][---]
8263  ^ ^
8264  new_arg cur_key1
8265  Steps:
8266 
8267  0) If cur_key1->next_key_part is empty: do nothing.
8268  Reason: (key2_cpy->next_key_part OR
8269  cur_key1->next_key_part) will be empty and
8270  therefore equal to cur_key1->next_key_part. Thus,
8271  the range in key2_cpy is completely covered by
8272  cur_key1
8273  1) Make new_arg with range [cur_key1.min, key2_cpy.max].
8274  new_arg->next_key_part is OR between next_key_part of
8275  cur_key1 and key2_cpy
8276  2) Make cur_key1 the range [key2_cpy.max, cur_key1.max]
8277  3) Insert new_arg into key1
8278  */
8279  if (!cur_key1->next_key_part) // Step 0
8280  {
8281  key2_cpy.increment_use_count(-1); // Free not used tree
8282  break;
8283  }
8284  SEL_ARG *new_arg= cur_key1->clone_last(&key2_cpy);
8285  if (!new_arg)
8286  return 0; // OOM
8287  cur_key1->copy_max_to_min(&key2_cpy);
8288  cur_key1->increment_use_count(key1->use_count+1);
8289  /* Increment key count as it may be used for next loop */
8290  key2_cpy.increment_use_count(1);
8291  new_arg->next_key_part= key_or(param, cur_key1->next_key_part,
8292  key2_cpy.next_key_part);
8293  key1= key1->insert(new_arg);
8294  break;
8295  }
8296  }
8297  // Move on to next range in key2
8298  cur_key2= cur_key2->next;
8299  }
8300 
8301 end:
8302  /*
8303  Add key2 ranges that are non-overlapping with and higher than the
8304  highest range in key1.
8305  */
8306  while (cur_key2)
8307  {
8308  SEL_ARG *next= cur_key2->next;
8309  if (key2_shared)
8310  {
8311  SEL_ARG *key2_cpy=new SEL_ARG(*cur_key2); // Must make copy
8312  if (!key2_cpy)
8313  return 0;
8314  cur_key2->increment_use_count(key1->use_count+1);
8315  key1= key1->insert(key2_cpy);
8316  }
8317  else
8318  key1= key1->insert(cur_key2); // Will destroy key2_root
8319  cur_key2= next;
8320  }
8321  key1->use_count++;
8322 
8323  return key1;
8324 }
8325 
8326 
8327 /* Compare if two trees are equal */
8328 
8329 static bool eq_tree(SEL_ARG* a,SEL_ARG *b)
8330 {
8331  if (a == b)
8332  return 1;
8333  if (!a || !b || !a->is_same(b))
8334  return 0;
8335  if (a->left != &null_element && b->left != &null_element)
8336  {
8337  if (!eq_tree(a->left,b->left))
8338  return 0;
8339  }
8340  else if (a->left != &null_element || b->left != &null_element)
8341  return 0;
8342  if (a->right != &null_element && b->right != &null_element)
8343  {
8344  if (!eq_tree(a->right,b->right))
8345  return 0;
8346  }
8347  else if (a->right != &null_element || b->right != &null_element)
8348  return 0;
8349  if (a->next_key_part != b->next_key_part)
8350  { // Sub range
8351  if (!a->next_key_part != !b->next_key_part ||
8352  !eq_tree(a->next_key_part, b->next_key_part))
8353  return 0;
8354  }
8355  return 1;
8356 }
8357 
8358 
8359 SEL_ARG *
8360 SEL_ARG::insert(SEL_ARG *key)
8361 {
8362  SEL_ARG *element,**UNINIT_VAR(par),*UNINIT_VAR(last_element);
8363 
8364  for (element= this; element != &null_element ; )
8365  {
8366  last_element=element;
8367  if (key->cmp_min_to_min(element) > 0)
8368  {
8369  par= &element->right; element= element->right;
8370  }
8371  else
8372  {
8373  par = &element->left; element= element->left;
8374  }
8375  }
8376  *par=key;
8377  key->parent=last_element;
8378  /* Link in list */
8379  if (par == &last_element->left)
8380  {
8381  key->next=last_element;
8382  if ((key->prev=last_element->prev))
8383  key->prev->next=key;
8384  last_element->prev=key;
8385  }
8386  else
8387  {
8388  if ((key->next=last_element->next))
8389  key->next->prev=key;
8390  key->prev=last_element;
8391  last_element->next=key;
8392  }
8393  key->left=key->right= &null_element;
8394  SEL_ARG *root=rb_insert(key); // rebalance tree
8395  root->use_count=this->use_count; // copy root info
8396  root->elements= this->elements+1;
8397  root->maybe_flag=this->maybe_flag;
8398  return root;
8399 }
8400 
8401 
8402 /*
8403 ** Find best key with min <= given key
8404 ** Because the call context this should never return 0 to get_range
8405 */
8406 
8407 SEL_ARG *
8408 SEL_ARG::find_range(SEL_ARG *key)
8409 {
8410  SEL_ARG *element=this,*found=0;
8411 
8412  for (;;)
8413  {
8414  if (element == &null_element)
8415  return found;
8416  int cmp=element->cmp_min_to_min(key);
8417  if (cmp == 0)
8418  return element;
8419  if (cmp < 0)
8420  {
8421  found=element;
8422  element=element->right;
8423  }
8424  else
8425  element=element->left;
8426  }
8427 }
8428 
8429 
8430 /*
8431  Remove a element from the tree
8432 
8433  SYNOPSIS
8434  tree_delete()
8435  key Key that is to be deleted from tree (this)
8436 
8437  NOTE
8438  This also frees all sub trees that is used by the element
8439 
8440  RETURN
8441  root of new tree (with key deleted)
8442 */
8443 
8444 SEL_ARG *
8445 SEL_ARG::tree_delete(SEL_ARG *key)
8446 {
8447  enum leaf_color remove_color;
8448  SEL_ARG *root,*nod,**par,*fix_par;
8449  DBUG_ENTER("tree_delete");
8450 
8451  root=this;
8452  this->parent= 0;
8453 
8454  /* Unlink from list */
8455  if (key->prev)
8456  key->prev->next=key->next;
8457  if (key->next)
8458  key->next->prev=key->prev;
8459  key->increment_use_count(-1);
8460  if (!key->parent)
8461  par= &root;
8462  else
8463  par=key->parent_ptr();
8464 
8465  if (key->left == &null_element)
8466  {
8467  *par=nod=key->right;
8468  fix_par=key->parent;
8469  if (nod != &null_element)
8470  nod->parent=fix_par;
8471  remove_color= key->color;
8472  }
8473  else if (key->right == &null_element)
8474  {
8475  *par= nod=key->left;
8476  nod->parent=fix_par=key->parent;
8477  remove_color= key->color;
8478  }
8479  else
8480  {
8481  SEL_ARG *tmp=key->next; // next bigger key (exist!)
8482  nod= *tmp->parent_ptr()= tmp->right; // unlink tmp from tree
8483  fix_par=tmp->parent;
8484  if (nod != &null_element)
8485  nod->parent=fix_par;
8486  remove_color= tmp->color;
8487 
8488  tmp->parent=key->parent; // Move node in place of key
8489  (tmp->left=key->left)->parent=tmp;
8490  if ((tmp->right=key->right) != &null_element)
8491  tmp->right->parent=tmp;
8492  tmp->color=key->color;
8493  *par=tmp;
8494  if (fix_par == key) // key->right == key->next
8495  fix_par=tmp; // new parent of nod
8496  }
8497 
8498  if (root == &null_element)
8499  DBUG_RETURN(0); // Maybe root later
8500  if (remove_color == BLACK)
8501  root=rb_delete_fixup(root,nod,fix_par);
8502 #ifndef DBUG_OFF
8503  test_rb_tree(root,root->parent);
8504 #endif
8505  root->use_count=this->use_count; // Fix root counters
8506  root->elements=this->elements-1;
8507  root->maybe_flag=this->maybe_flag;
8508  DBUG_RETURN(root);
8509 }
8510 
8511 
8512  /* Functions to fix up the tree after insert and delete */
8513 
8514 static void left_rotate(SEL_ARG **root,SEL_ARG *leaf)
8515 {
8516  SEL_ARG *y=leaf->right;
8517  leaf->right=y->left;
8518  if (y->left != &null_element)
8519  y->left->parent=leaf;
8520  if (!(y->parent=leaf->parent))
8521  *root=y;
8522  else
8523  *leaf->parent_ptr()=y;
8524  y->left=leaf;
8525  leaf->parent=y;
8526 }
8527 
8528 static void right_rotate(SEL_ARG **root,SEL_ARG *leaf)
8529 {
8530  SEL_ARG *y=leaf->left;
8531  leaf->left=y->right;
8532  if (y->right != &null_element)
8533  y->right->parent=leaf;
8534  if (!(y->parent=leaf->parent))
8535  *root=y;
8536  else
8537  *leaf->parent_ptr()=y;
8538  y->right=leaf;
8539  leaf->parent=y;
8540 }
8541 
8542 
8543 SEL_ARG *
8544 SEL_ARG::rb_insert(SEL_ARG *leaf)
8545 {
8546  SEL_ARG *y,*par,*par2,*root;
8547  root= this; root->parent= 0;
8548 
8549  leaf->color=RED;
8550  while (leaf != root && (par= leaf->parent)->color == RED)
8551  { // This can't be root or 1 level under
8552  if (par == (par2= leaf->parent->parent)->left)
8553  {
8554  y= par2->right;
8555  if (y->color == RED)
8556  {
8557  par->color=BLACK;
8558  y->color=BLACK;
8559  leaf=par2;
8560  leaf->color=RED; /* And the loop continues */
8561  }
8562  else
8563  {
8564  if (leaf == par->right)
8565  {
8566  left_rotate(&root,leaf->parent);
8567  par=leaf; /* leaf is now parent to old leaf */
8568  }
8569  par->color=BLACK;
8570  par2->color=RED;
8571  right_rotate(&root,par2);
8572  break;
8573  }
8574  }
8575  else
8576  {
8577  y= par2->left;
8578  if (y->color == RED)
8579  {
8580  par->color=BLACK;
8581  y->color=BLACK;
8582  leaf=par2;
8583  leaf->color=RED; /* And the loop continues */
8584  }
8585  else
8586  {
8587  if (leaf == par->left)
8588  {
8589  right_rotate(&root,par);
8590  par=leaf;
8591  }
8592  par->color=BLACK;
8593  par2->color=RED;
8594  left_rotate(&root,par2);
8595  break;
8596  }
8597  }
8598  }
8599  root->color=BLACK;
8600 #ifndef DBUG_OFF
8601  test_rb_tree(root,root->parent);
8602 #endif
8603  return root;
8604 }
8605 
8606 
8607 SEL_ARG *rb_delete_fixup(SEL_ARG *root,SEL_ARG *key,SEL_ARG *par)
8608 {
8609  SEL_ARG *x,*w;
8610  root->parent=0;
8611 
8612  x= key;
8613  while (x != root && x->color == SEL_ARG::BLACK)
8614  {
8615  if (x == par->left)
8616  {
8617  w=par->right;
8618  if (w->color == SEL_ARG::RED)
8619  {
8620  w->color=SEL_ARG::BLACK;
8621  par->color=SEL_ARG::RED;
8622  left_rotate(&root,par);
8623  w=par->right;
8624  }
8625  if (w->left->color == SEL_ARG::BLACK && w->right->color == SEL_ARG::BLACK)
8626  {
8627  w->color=SEL_ARG::RED;
8628  x=par;
8629  }
8630  else
8631  {
8632  if (w->right->color == SEL_ARG::BLACK)
8633  {
8634  w->left->color=SEL_ARG::BLACK;
8635  w->color=SEL_ARG::RED;
8636  right_rotate(&root,w);
8637  w=par->right;
8638  }
8639  w->color=par->color;
8640  par->color=SEL_ARG::BLACK;
8641  w->right->color=SEL_ARG::BLACK;
8642  left_rotate(&root,par);
8643  x=root;
8644  break;
8645  }
8646  }
8647  else
8648  {
8649  w=par->left;
8650  if (w->color == SEL_ARG::RED)
8651  {
8652  w->color=SEL_ARG::BLACK;
8653  par->color=SEL_ARG::RED;
8654  right_rotate(&root,par);
8655  w=par->left;
8656  }
8657  if (w->right->color == SEL_ARG::BLACK && w->left->color == SEL_ARG::BLACK)
8658  {
8659  w->color=SEL_ARG::RED;
8660  x=par;
8661  }
8662  else
8663  {
8664  if (w->left->color == SEL_ARG::BLACK)
8665  {
8666  w->right->color=SEL_ARG::BLACK;
8667  w->color=SEL_ARG::RED;
8668  left_rotate(&root,w);
8669  w=par->left;
8670  }
8671  w->color=par->color;
8672  par->color=SEL_ARG::BLACK;
8673  w->left->color=SEL_ARG::BLACK;
8674  right_rotate(&root,par);
8675  x=root;
8676  break;
8677  }
8678  }
8679  par=x->parent;
8680  }
8681  x->color=SEL_ARG::BLACK;
8682  return root;
8683 }
8684 
8685 
8686 #ifndef DBUG_OFF
8687  /* Test that the properties for a red-black tree hold */
8688 
8689 int test_rb_tree(SEL_ARG *element,SEL_ARG *parent)
8690 {
8691  int count_l,count_r;
8692 
8693  if (element == &null_element)
8694  return 0; // Found end of tree
8695  if (element->parent != parent)
8696  {
8697  sql_print_error("Wrong tree: Parent doesn't point at parent");
8698  return -1;
8699  }
8700  if (element->color == SEL_ARG::RED &&
8701  (element->left->color == SEL_ARG::RED ||
8702  element->right->color == SEL_ARG::RED))
8703  {
8704  sql_print_error("Wrong tree: Found two red in a row");
8705  return -1;
8706  }
8707  if (element->left == element->right && element->left != &null_element)
8708  { // Dummy test
8709  sql_print_error("Wrong tree: Found right == left");
8710  return -1;
8711  }
8712  count_l=test_rb_tree(element->left,element);
8713  count_r=test_rb_tree(element->right,element);
8714  if (count_l >= 0 && count_r >= 0)
8715  {
8716  if (count_l == count_r)
8717  return count_l+(element->color == SEL_ARG::BLACK);
8718  sql_print_error("Wrong tree: Incorrect black-count: %d - %d",
8719  count_l,count_r);
8720  }
8721  return -1; // Error, no more warnings
8722 }
8723 #endif
8724 
8725 
8768 static ulong count_key_part_usage(SEL_ARG *root, SEL_ARG *key)
8769 {
8770  ulong count= 0;
8771  for (root=root->first(); root ; root=root->next)
8772  {
8773  if (root->next_key_part)
8774  {
8775  if (root->next_key_part == key)
8776  count++;
8777  if (root->next_key_part->part < key->part)
8778  count+=count_key_part_usage(root->next_key_part,key);
8779  }
8780  }
8781  return count;
8782 }
8783 
8784 
8785 #ifndef DBUG_OFF
8786 /*
8787  Check if SEL_ARG::use_count value is correct
8788 
8789  SYNOPSIS
8790  SEL_ARG::test_use_count()
8791  root The root node of the SEL_ARG graph (an RB-tree root node that
8792  has the least value of sel_arg->part in the entire graph, and
8793  thus is the "origin" of the graph)
8794 
8795  DESCRIPTION
8796  Check if SEL_ARG::use_count value is correct. See the definition of
8797  use_count for what is "correct".
8798 */
8799 
8800 void SEL_ARG::test_use_count(SEL_ARG *root)
8801 {
8802  uint e_count=0;
8803  if (this == root && use_count != 1)
8804  {
8805  sql_print_information("Use_count: Wrong count %lu for root",use_count);
8806  // DBUG_ASSERT(false); // Todo - enable and clean up mess
8807  return;
8808  }
8809  if (this->type != SEL_ARG::KEY_RANGE)
8810  return;
8811  for (SEL_ARG *pos=first(); pos ; pos=pos->next)
8812  {
8813  e_count++;
8814  if (pos->next_key_part)
8815  {
8816  ulong count=count_key_part_usage(root,pos->next_key_part);
8817  if (count > pos->next_key_part->use_count)
8818  {
8819  sql_print_information("Use_count: Wrong count for key at 0x%lx, %lu "
8820  "should be %lu", (long unsigned int)pos,
8821  pos->next_key_part->use_count, count);
8822  // DBUG_ASSERT(false); // Todo - enable and clean up mess
8823  return;
8824  }
8825  pos->next_key_part->test_use_count(root);
8826  }
8827  }
8828  if (e_count != elements)
8829  {
8830  sql_print_warning("Wrong use count: %u (should be %u) for tree at 0x%lx",
8831  e_count, elements, (long unsigned int) this);
8832  // DBUG_ASSERT(false); // Todo - enable and clean up mess
8833  }
8834 }
8835 #endif
8836 
8837 /****************************************************************************
8838  MRR Range Sequence Interface implementation that walks a SEL_ARG* tree.
8839  ****************************************************************************/
8840 
8841 /* MRR range sequence, SEL_ARG* implementation: stack entry */
8842 typedef struct st_range_seq_entry
8843 {
8844  /*
8845  Pointers in min and max keys. They point to right-after-end of key
8846  images. The 0-th entry has these pointing to key tuple start.
8847  */
8848  uchar *min_key, *max_key;
8849 
8850  /*
8851  Flags, for {keypart0, keypart1, ... this_keypart} subtuple.
8852  min_key_flag may have NULL_RANGE set.
8853  */
8854  uint min_key_flag, max_key_flag;
8855 
8856  /* Number of key parts */
8857  uint min_key_parts, max_key_parts;
8865 } RANGE_SEQ_ENTRY;
8866 
8867 
8868 /*
8869  MRR range sequence, SEL_ARG* implementation: SEL_ARG graph traversal context
8870 */
8872 {
8873 private:
8874 
8917  RANGE_SEQ_ENTRY stack[MAX_REF_PARTS];
8918  /*
8919  Index of last used element in the above array. A value of -1 means
8920  that the stack is empty.
8921  */
8922  int curr_kp;
8923 
8924 public:
8925  uint keyno; /* index of used tree in SEL_TREE structure */
8926  uint real_keyno; /* Number of the index in tables */
8927 
8928  PARAM * const param;
8929  SEL_ARG *start; /* Root node of the traversed SEL_ARG* graph */
8930 
8931  Sel_arg_range_sequence(PARAM *param_arg) : param(param_arg) { reset(); }
8932 
8933  void reset()
8934  {
8935  stack[0].key_tree= NULL;
8936  stack[0].min_key= (uchar*)param->min_key;
8937  stack[0].min_key_flag= 0;
8938  stack[0].min_key_parts= 0;
8939 
8940  stack[0].max_key= (uchar*)param->max_key;
8941  stack[0].max_key_flag= 0;
8942  stack[0].max_key_parts= 0;
8943  curr_kp= -1;
8944  }
8945 
8946  bool stack_empty() const { return (curr_kp == -1); }
8947 
8948  void stack_push_range(SEL_ARG *key_tree);
8949 
8950  void stack_pop_range()
8951  {
8952  DBUG_ASSERT(!stack_empty());
8953  if (curr_kp == 0)
8954  reset();
8955  else
8956  curr_kp--;
8957  }
8958 
8959  int stack_size() const { return curr_kp + 1; }
8960 
8961  RANGE_SEQ_ENTRY *stack_top()
8962  {
8963  return stack_empty() ? NULL : &stack[curr_kp];
8964  }
8965 };
8966 
8967 
8968 /*
8969  Range sequence interface, SEL_ARG* implementation: Initialize the traversal
8970 
8971  SYNOPSIS
8972  init()
8973  init_params SEL_ARG tree traversal context
8974  n_ranges [ignored] The number of ranges obtained
8975  flags [ignored] HA_MRR_SINGLE_POINT, HA_MRR_FIXED_KEY
8976 
8977  RETURN
8978  Value of init_param
8979 */
8980 
8981 range_seq_t sel_arg_range_seq_init(void *init_param, uint n_ranges, uint flags)
8982 {
8983  Sel_arg_range_sequence *seq=
8984  static_cast<Sel_arg_range_sequence*>(init_param);
8985  seq->reset();
8986  return init_param;
8987 }
8988 
8989 
8990 void Sel_arg_range_sequence::stack_push_range(SEL_ARG *key_tree)
8991 {
8992 
8993  DBUG_ASSERT((uint)curr_kp+1 < MAX_REF_PARTS);
8994 
8995  RANGE_SEQ_ENTRY *push_position= &stack[curr_kp + 1];
8996  RANGE_SEQ_ENTRY *last_added_kp= stack_top();
8997  if (stack_empty())
8998  {
8999  /*
9000  If we get here this is either
9001  a) the first time a range sequence is constructed for this
9002  range access method (in which case stack[0] has not been
9003  modified since the constructor was called), or
9004  b) there are multiple ranges for the first keypart in the
9005  condition (and we have called stack_pop_range() to empty
9006  the stack).
9007  In both cases, reset() has been called and all fields in
9008  push_position have been reset. All we need to do is to copy the
9009  min/max key flags from the predicate we're about to add to
9010  stack[0].
9011  */
9012  push_position->min_key_flag= key_tree->min_flag;
9013  push_position->max_key_flag= key_tree->max_flag;
9014  }
9015  else
9016  {
9017  push_position->min_key= last_added_kp->min_key;
9018  push_position->max_key= last_added_kp->max_key;
9019  push_position->min_key_parts= last_added_kp->min_key_parts;
9020  push_position->max_key_parts= last_added_kp->max_key_parts;
9021  push_position->min_key_flag= last_added_kp->min_key_flag |
9022  key_tree->min_flag;
9023  push_position->max_key_flag= last_added_kp->max_key_flag |
9024  key_tree->max_flag;
9025  }
9026 
9027  push_position->key_tree= key_tree;
9028  uint16 stor_length= param->key[keyno][key_tree->part].store_length;
9029  /* psergey-merge-done:
9030  key_tree->store(arg->param->key[arg->keyno][key_tree->part].store_length,
9031  &cur->min_key, prev->min_key_flag,
9032  &cur->max_key, prev->max_key_flag);
9033  */
9034  push_position->min_key_parts+=
9035  key_tree->store_min(stor_length, &push_position->min_key,
9036  last_added_kp ? last_added_kp->min_key_flag : 0);
9037  push_position->max_key_parts+=
9038  key_tree->store_max(stor_length, &push_position->max_key,
9039  last_added_kp ? last_added_kp->max_key_flag : 0);
9040 
9041  if (key_tree->is_null_interval())
9042  push_position->min_key_flag |= NULL_RANGE;
9043  curr_kp++;
9044 }
9045 
9046 
9047 /*
9048  Range sequence interface, SEL_ARG* implementation: get the next interval
9049  in the R-B tree
9050 
9051  SYNOPSIS
9052  sel_arg_range_seq_next()
9053  rseq Value returned from sel_arg_range_seq_init
9054  range OUT Store information about the range here
9055 
9056  DESCRIPTION
9057  This is "get_next" function for Range sequence interface implementation
9058  for SEL_ARG* tree.
9059 
9060  IMPLEMENTATION
9061  The traversal also updates those param members:
9062  - is_ror_scan
9063  - range_count
9064  - max_key_part
9065 
9066  RETURN
9067  0 Ok
9068  1 No more ranges in the sequence
9069 
9070  NOTE: append_range_all_keyparts(), which is used to e.g. print
9071  ranges to Optimizer Trace in a human readable format, mimics the
9072  behavior of this function.
9073 */
9074 
9075 //psergey-merge-todo: support check_quick_keys:max_keypart
9076 uint sel_arg_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
9077 {
9078  SEL_ARG *key_tree;
9079  Sel_arg_range_sequence *seq= static_cast<Sel_arg_range_sequence*>(rseq);
9080 
9081  if (seq->stack_empty())
9082  {
9083  /*
9084  This is the first time sel_arg_range_seq_next is called.
9085  seq->start points to the root of the R-B tree for the first
9086  keypart
9087  */
9088  key_tree= seq->start;
9089 
9090  /*
9091  Move to the first range for the first keypart. Save this range
9092  in seq->stack[0] and carry on to ranges in the next keypart if
9093  any
9094  */
9095  key_tree= key_tree->first();
9096  seq->stack_push_range(key_tree);
9097  }
9098  else
9099  {
9100  /*
9101  This is not the first time sel_arg_range_seq_next is called, so
9102  seq->stack is populated with the range the last call to this
9103  function found. seq->stack[current_keypart].key_tree points to a
9104  leaf in the R-B tree of the last keypart that was part of the
9105  former range. This is the starting point for finding the next
9106  range. @see Sel_arg_range_sequence::stack
9107  */
9108  // See if there are more ranges in this or any of the previous keyparts
9109  while (true)
9110  {
9111  key_tree= seq->stack_top()->key_tree;
9112  seq->stack_pop_range();
9113  if (key_tree->next)
9114  {
9115  /* This keypart has more ranges */
9116  DBUG_ASSERT(key_tree->next != &null_element);
9117  key_tree= key_tree->next;
9118 
9119  /*
9120  save the next range for this keypart and carry on to ranges in
9121  the next keypart if any
9122  */
9123  seq->stack_push_range(key_tree);
9124  seq->param->is_ror_scan= FALSE;
9125  break;
9126  }
9127 
9128  if (seq->stack_empty())
9129  {
9130  // There are no more ranges for the first keypart: we're done
9131  return 1;
9132  }
9133  /*
9134  There are no more ranges for the current keypart. Step back
9135  to the previous keypart and see if there are more ranges
9136  there.
9137  */
9138  }
9139  }
9140 
9141  DBUG_ASSERT(!seq->stack_empty());
9142 
9143  /*
9144  Add range info for the next keypart if
9145  1) there is a range predicate for a later keypart
9146  2) the range predicate is for the next keypart in the index: a
9147  range predicate on keypartX+1 can only be used if there is a
9148  range predicate on keypartX.
9149  3) the range predicate on the next keypart is usable
9150  */
9151  while (key_tree->next_key_part && // 1)
9152  key_tree->next_key_part != &null_element && // 1)
9153  key_tree->next_key_part->part == key_tree->part + 1 && // 2)
9154  key_tree->next_key_part->type == SEL_ARG::KEY_RANGE) // 3)
9155  {
9156  {
9157  DBUG_PRINT("info", ("while(): key_tree->part %d",key_tree->part));
9158  RANGE_SEQ_ENTRY *cur= seq->stack_top();
9159  const uint min_key_total_length= cur->min_key - seq->param->min_key;
9160  const uint max_key_total_length= cur->max_key - seq->param->max_key;
9161 
9162  /*
9163  Check if more ranges can be added. This is the case if all
9164  predicates for keyparts handled so far are equality
9165  predicates. If either of the following apply, there are
9166  non-equality predicates in stack[]:
9167 
9168  1) min_key_total_length != max_key_total_length (because
9169  equality ranges are stored as "min_key = max_key = <value>")
9170  2) memcmp(<min_key_values>,<max_key_values>) != 0 (same argument as 1)
9171  3) A min or max flag has been set: Because flags denote ranges
9172  ('<', '<=' etc), any value but 0 indicates a non-equality
9173  predicate.
9174  */
9175 
9176  uchar* min_key_start;
9177  uchar* max_key_start;
9178  uint cur_key_length;
9179 
9180  if (seq->stack_size() == 1)
9181  {
9182  min_key_start= seq->param->min_key;
9183  max_key_start= seq->param->max_key;
9184  cur_key_length= min_key_total_length;
9185  }
9186  else
9187  {
9188  const RANGE_SEQ_ENTRY prev= cur[-1];
9189  min_key_start= prev.min_key;
9190  max_key_start= prev.max_key;
9191  cur_key_length= cur->min_key - prev.min_key;
9192  }
9193 
9194  if ((min_key_total_length != max_key_total_length) || // 1)
9195  (memcmp(min_key_start, max_key_start, cur_key_length)) || // 2)
9196  (key_tree->min_flag || key_tree->max_flag)) // 3)
9197  {
9198  DBUG_PRINT("info", ("while(): inside if()"));
9199  /*
9200  The range predicate up to and including the one in key_tree
9201  is usable by range access but does not allow subranges made
9202  up from predicates in later keyparts. This may e.g. be
9203  because the predicate operator is "<". Since there are range
9204  predicates on more keyparts, we use those to more closely
9205  specify the start and stop locations for the range. Example:
9206 
9207  "SELECT * FROM t1 WHERE a >= 2 AND b >= 3":
9208 
9209  t1 content:
9210  -----------
9211  1 1
9212  2 1 <- 1)
9213  2 2
9214  2 3 <- 2)
9215  2 4
9216  3 1
9217  3 2
9218  3 3
9219 
9220  The predicate cannot be translated into something like
9221  "(a=2 and b>=3) or (a=3 and b>=3) or ..."
9222  I.e., it cannot be divided into subranges, but by storing
9223  min/max key below we can at least start the scan from 2)
9224  instead of 1)
9225  */
9226  SEL_ARG *store_key_part= key_tree->next_key_part;
9227  seq->param->is_ror_scan= FALSE;
9228  if (!key_tree->min_flag)
9229  cur->min_key_parts +=
9230  store_key_part->store_min_key(seq->param->key[seq->keyno],
9231  &cur->min_key,
9232  &cur->min_key_flag,
9233  MAX_KEY);
9234  if (!key_tree->max_flag)
9235  cur->max_key_parts +=
9236  store_key_part->store_max_key(seq->param->key[seq->keyno],
9237  &cur->max_key,
9238  &cur->max_key_flag,
9239  MAX_KEY);
9240  break;
9241  }
9242  }
9243 
9244  /*
9245  There are usable range predicates for the next keypart and the
9246  range predicate for the current keypart allows us to make use of
9247  them. Move to the first range predicate for the next keypart.
9248  Push this range predicate to seq->stack and move on to the next
9249  keypart (if any). @see Sel_arg_range_sequence::stack
9250  */
9251  key_tree= key_tree->next_key_part->first();
9252  seq->stack_push_range(key_tree);
9253  }
9254 
9255  DBUG_ASSERT(!seq->stack_empty() && (seq->stack_top() != NULL));
9256 
9257  // We now have a full range predicate in seq->stack_top()
9258  RANGE_SEQ_ENTRY *cur= seq->stack_top();
9259  PARAM *param= seq->param;
9260  uint min_key_length= cur->min_key - param->min_key;
9261 
9262  if (cur->min_key_flag & GEOM_FLAG)
9263  {
9264  range->range_flag= cur->min_key_flag;
9265 
9266  /* Here minimum contains also function code bits, and maximum is +inf */
9267  range->start_key.key= param->min_key;
9268  range->start_key.length= min_key_length;
9269  range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
9270  range->start_key.flag= (ha_rkey_function) (cur->min_key_flag ^ GEOM_FLAG);
9271  /*
9272  Spatial operators are only allowed on spatial indexes, and no
9273  spatial index can at the moment return rows in ROWID order
9274  */
9275  DBUG_ASSERT(!param->is_ror_scan);
9276  }
9277  else
9278  {
9279  const KEY *cur_key_info= &param->table->key_info[seq->real_keyno];
9280  range->range_flag= cur->min_key_flag | cur->max_key_flag;
9281 
9282  range->start_key.key= param->min_key;
9283  range->start_key.length= cur->min_key - param->min_key;
9284  range->start_key.keypart_map= make_prev_keypart_map(cur->min_key_parts);
9285  range->start_key.flag= (cur->min_key_flag & NEAR_MIN ? HA_READ_AFTER_KEY :
9286  HA_READ_KEY_EXACT);
9287 
9288  range->end_key.key= param->max_key;
9289  range->end_key.length= cur->max_key - param->max_key;
9290  range->end_key.keypart_map= make_prev_keypart_map(cur->max_key_parts);
9291  range->end_key.flag= (cur->max_key_flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
9292  HA_READ_AFTER_KEY);
9293 
9294  /*
9295  This is an equality range (keypart_0=X and ... and keypart_n=Z) if
9296  1) There are no flags indicating open range (e.g.,
9297  "keypart_x > y") or GIS.
9298  2) The lower bound and the upper bound of the range has the
9299  same value (min_key == max_key).
9300  */
9301  const uint is_open_range= (NO_MIN_RANGE | NO_MAX_RANGE |
9302  NEAR_MIN | NEAR_MAX | GEOM_FLAG);
9303  const bool is_eq_range_pred=
9304  !(cur->min_key_flag & is_open_range) && // 1)
9305  !(cur->max_key_flag & is_open_range) && // 1)
9306  range->start_key.length == range->end_key.length && // 2)
9307  !memcmp(param->min_key, param->max_key, range->start_key.length);
9308 
9309  if (is_eq_range_pred)
9310  {
9311  range->range_flag= EQ_RANGE;
9312  /*
9313  Use statistics instead of index dives for estimates of rows in
9314  this range if the user requested it
9315  */
9316  if (param->use_index_statistics)
9317  range->range_flag|= USE_INDEX_STATISTICS;
9318 
9319  /*
9320  An equality range is a unique range (0 or 1 rows in the range)
9321  if the index is unique (1) and all keyparts are used (2).
9322  Note that keys which are extended with PK parts have no
9323  HA_NOSAME flag. So we can use user_defined_key_parts.
9324  */
9325  if (cur_key_info->flags & HA_NOSAME && // 1)
9326  (uint)key_tree->part+1 == cur_key_info->user_defined_key_parts) // 2)
9327  range->range_flag|= UNIQUE_RANGE | (cur->min_key_flag & NULL_RANGE);
9328  }
9329 
9330  if (param->is_ror_scan)
9331  {
9332  const uint key_part_number= key_tree->part + 1;
9333  /*
9334  If we get here, the condition on the key was converted to form
9335  "(keyXpart1 = c1) AND ... AND (keyXpart{key_tree->part - 1} = cN) AND
9336  somecond(keyXpart{key_tree->part})"
9337  Check if
9338  somecond is "keyXpart{key_tree->part} = const" and
9339  uncovered "tail" of KeyX parts is either empty or is identical to
9340  first members of clustered primary key.
9341 
9342  If last key part is PK part added to the key as an extension
9343  and is_key_scan_ror() result is TRUE then it's possible to
9344  use ROR scan.
9345  */
9346  if ((!is_eq_range_pred &&
9347  key_part_number <= cur_key_info->user_defined_key_parts) ||
9348  !is_key_scan_ror(param, seq->real_keyno, key_part_number))
9349  param->is_ror_scan= FALSE;
9350  }
9351  }
9352 
9353  seq->param->range_count++;
9354  seq->param->max_key_part=max<uint>(seq->param->max_key_part,key_tree->part);
9355 
9356  return 0;
9357 }
9358 
9359 
9360 /*
9361  Calculate estimate of number records that will be retrieved by a range
9362  scan on given index using given SEL_ARG intervals tree.
9363 
9364  SYNOPSIS
9365  check_quick_select()
9366  param Parameter from test_quick_select
9367  idx Number of index to use in PARAM::key SEL_TREE::key
9368  index_only TRUE - assume only index tuples will be accessed
9369  FALSE - assume full table rows will be read
9370  tree Transformed selection condition, tree->key[idx] holds
9371  the intervals for the given index.
9372  update_tbl_stats TRUE <=> update table->quick_* with information
9373  about range scan we've evaluated.
9374  mrr_flags INOUT MRR access flags
9375  cost OUT Scan cost
9376 
9377  NOTES
9378  param->is_ror_scan is set to reflect if the key scan is a ROR (see
9379  is_key_scan_ror function for more info)
9380  param->table->quick_*, param->range_count (and maybe others) are
9381  updated with data of given key scan, see quick_range_seq_next for details.
9382 
9383  RETURN
9384  Estimate # of records to be retrieved.
9385  HA_POS_ERROR if estimate calculation failed due to table handler problems.
9386 */
9387 
9388 static
9389 ha_rows check_quick_select(PARAM *param, uint idx, bool index_only,
9390  SEL_ARG *tree, bool update_tbl_stats,
9391  uint *mrr_flags, uint *bufsize, Cost_estimate *cost)
9392 {
9393  Sel_arg_range_sequence seq(param);
9394  RANGE_SEQ_IF seq_if = {sel_arg_range_seq_init, sel_arg_range_seq_next, 0, 0};
9395  handler *file= param->table->file;
9396  ha_rows rows;
9397  uint keynr= param->real_keynr[idx];
9398  DBUG_ENTER("check_quick_select");
9399 
9400  /* Handle cases when we don't have a valid non-empty list of range */
9401  if (!tree)
9402  DBUG_RETURN(HA_POS_ERROR);
9403  if (tree->type == SEL_ARG::IMPOSSIBLE)
9404  DBUG_RETURN(0L);
9405  if (tree->type != SEL_ARG::KEY_RANGE || tree->part != 0)
9406  DBUG_RETURN(HA_POS_ERROR); // Don't use tree
9407 
9408  seq.keyno= idx;
9409  seq.real_keyno= keynr;
9410  seq.start= tree;
9411 
9412  param->range_count=0;
9413  param->max_key_part=0;
9414 
9415  /*
9416  If there are more equality ranges than specified by the
9417  eq_range_index_dive_limit variable we switches from using index
9418  dives to use statistics.
9419  */
9420  uint range_count= 0;
9421  param->use_index_statistics=
9422  eq_ranges_exceeds_limit(tree, &range_count,
9423  param->thd->variables.eq_range_index_dive_limit);
9424 
9425  param->is_ror_scan= TRUE;
9426  if (file->index_flags(keynr, 0, TRUE) & HA_KEY_SCAN_NOT_ROR)
9427  param->is_ror_scan= FALSE;
9428 
9429  *mrr_flags= param->force_default_mrr? HA_MRR_USE_DEFAULT_IMPL: 0;
9430  *mrr_flags|= HA_MRR_NO_ASSOCIATION;
9431  /*
9432  Pass HA_MRR_SORTED to see if MRR implementation can handle sorting.
9433  */
9434  if (param->order_direction != ORDER::ORDER_NOT_RELEVANT)
9435  *mrr_flags|= HA_MRR_SORTED;
9436 
9437  bool pk_is_clustered= file->primary_key_is_clustered();
9438  if (index_only &&
9439  (file->index_flags(keynr, param->max_key_part, 1) & HA_KEYREAD_ONLY) &&
9440  !(pk_is_clustered && keynr == param->table->s->primary_key))
9441  *mrr_flags |= HA_MRR_INDEX_ONLY;
9442 
9443  if (current_thd->lex->sql_command != SQLCOM_SELECT)
9444  *mrr_flags|= HA_MRR_SORTED; // Assumed to give faster ins/upd/del
9445 
9446  *bufsize= param->thd->variables.read_rnd_buff_size;
9447  // Sets is_ror_scan to false for some queries, e.g. multi-ranges
9448  rows= file->multi_range_read_info_const(keynr, &seq_if, (void*)&seq, 0,
9449  bufsize, mrr_flags, cost);
9450  if (rows != HA_POS_ERROR)
9451  {
9452  param->table->quick_rows[keynr]=rows;
9453  if (update_tbl_stats)
9454  {
9455  param->table->quick_keys.set_bit(keynr);
9456  param->table->quick_key_parts[keynr]=param->max_key_part+1;
9457  param->table->quick_n_ranges[keynr]= param->range_count;
9458  param->table->quick_condition_rows=
9459  min(param->table->quick_condition_rows, rows);
9460  }
9461  param->table->possible_quick_keys.set_bit(keynr);
9462  }
9463  /* Figure out if the key scan is ROR (returns rows in ROWID order) or not */
9464  enum ha_key_alg key_alg= param->table->key_info[seq.real_keyno].algorithm;
9465  if ((key_alg != HA_KEY_ALG_BTREE) && (key_alg!= HA_KEY_ALG_UNDEF))
9466  {
9467  /*
9468  All scans are non-ROR scans for those index types.
9469  TODO: Don't have this logic here, make table engines return
9470  appropriate flags instead.
9471  */
9472  param->is_ror_scan= FALSE;
9473  }
9474  else
9475  {
9476  /* Clustered PK scan is always a ROR scan (TODO: same as above) */
9477  if (param->table->s->primary_key == keynr && pk_is_clustered)
9478  param->is_ror_scan= TRUE;
9479  }
9480  if (param->table->file->index_flags(keynr, 0, TRUE) & HA_KEY_SCAN_NOT_ROR)
9481  param->is_ror_scan= FALSE;
9482  DBUG_PRINT("exit", ("Records: %lu", (ulong) rows));
9483  DBUG_RETURN(rows);
9484 }
9485 
9486 
9487 /*
9488  Check if key scan on given index with equality conditions on first n key
9489  parts is a ROR scan.
9490 
9491  SYNOPSIS
9492  is_key_scan_ror()
9493  param Parameter from test_quick_select
9494  keynr Number of key in the table. The key must not be a clustered
9495  primary key.
9496  nparts Number of first key parts for which equality conditions
9497  are present.
9498 
9499  NOTES
9500  ROR (Rowid Ordered Retrieval) key scan is a key scan that produces
9501  ordered sequence of rowids (ha_xxx::cmp_ref is the comparison function)
9502 
9503  This function is needed to handle a practically-important special case:
9504  an index scan is a ROR scan if it is done using a condition in form
9505 
9506  "key1_1=c_1 AND ... AND key1_n=c_n"
9507 
9508  where the index is defined on (key1_1, ..., key1_N [,a_1, ..., a_n])
9509 
9510  and the table has a clustered Primary Key defined as
9511 
9512  PRIMARY KEY(a_1, ..., a_n, b1, ..., b_k)
9513 
9514  i.e. the first key parts of it are identical to uncovered parts ot the
9515  key being scanned. This function assumes that the index flags do not
9516  include HA_KEY_SCAN_NOT_ROR flag (that is checked elsewhere).
9517 
9518  Check (1) is made in quick_range_seq_next()
9519 
9520  RETURN
9521  TRUE The scan is ROR-scan
9522  FALSE Otherwise
9523 */
9524 
9525 static bool is_key_scan_ror(PARAM *param, uint keynr, uint nparts)
9526 {
9527  KEY *table_key= param->table->key_info + keynr;
9528 
9529  /*
9530  Range predicates on hidden key parts do not change the fact
9531  that a scan is rowid ordered, so we only care about user
9532  defined keyparts
9533  */
9534  const uint user_defined_nparts=
9535  std::min<uint>(nparts, table_key->user_defined_key_parts);
9536 
9537  KEY_PART_INFO *key_part= table_key->key_part + user_defined_nparts;
9538  KEY_PART_INFO *key_part_end= (table_key->key_part +
9539  table_key->user_defined_key_parts);
9540  uint pk_number;
9541 
9542  for (KEY_PART_INFO *kp= table_key->key_part; kp < key_part; kp++)
9543  {
9544  uint16 fieldnr= param->table->key_info[keynr].
9545  key_part[kp - table_key->key_part].fieldnr - 1;
9546  if (param->table->field[fieldnr]->key_length() != kp->length)
9547  return FALSE;
9548  }
9549 
9550  if (key_part == key_part_end)
9551  return TRUE;
9552 
9553  key_part= table_key->key_part + user_defined_nparts;
9554  pk_number= param->table->s->primary_key;
9555  if (!param->table->file->primary_key_is_clustered() || pk_number == MAX_KEY)
9556  return FALSE;
9557 
9558  KEY_PART_INFO *pk_part= param->table->key_info[pk_number].key_part;
9559  KEY_PART_INFO *pk_part_end=
9560  pk_part + param->table->key_info[pk_number].user_defined_key_parts;
9561  for (;(key_part!=key_part_end) && (pk_part != pk_part_end);
9562  ++key_part, ++pk_part)
9563  {
9564  if ((key_part->field != pk_part->field) ||
9565  (key_part->length != pk_part->length))
9566  return FALSE;
9567  }
9568  return (key_part == key_part_end);
9569 }
9570 
9571 
9572 /*
9573  Create a QUICK_RANGE_SELECT from given key and SEL_ARG tree for that key.
9574 
9575  SYNOPSIS
9576  get_quick_select()
9577  param
9578  idx Index of used key in param->key.
9579  key_tree SEL_ARG tree for the used key
9580  mrr_flags MRR parameter for quick select
9581  mrr_buf_size MRR parameter for quick select
9582  parent_alloc If not NULL, use it to allocate memory for
9583  quick select data. Otherwise use quick->alloc.
9584  NOTES
9585  The caller must call QUICK_SELECT::init for returned quick select.
9586 
9587  CAUTION! This function may change thd->mem_root to a MEM_ROOT which will be
9588  deallocated when the returned quick select is deleted.
9589 
9590  RETURN
9591  NULL on error
9592  otherwise created quick select
9593 */
9594 
9596 get_quick_select(PARAM *param,uint idx,SEL_ARG *key_tree, uint mrr_flags,
9597  uint mrr_buf_size, MEM_ROOT *parent_alloc)
9598 {
9599  QUICK_RANGE_SELECT *quick;
9600  bool create_err= FALSE;
9601  DBUG_ENTER("get_quick_select");
9602 
9603  if (param->table->key_info[param->real_keynr[idx]].flags & HA_SPATIAL)
9604  quick=new QUICK_RANGE_SELECT_GEOM(param->thd, param->table,
9605  param->real_keynr[idx],
9606  test(parent_alloc),
9607  parent_alloc, &create_err);
9608  else
9609  quick=new QUICK_RANGE_SELECT(param->thd, param->table,
9610  param->real_keynr[idx],
9611  test(parent_alloc), NULL, &create_err);
9612 
9613  if (quick)
9614  {
9615  if (create_err ||
9616  get_quick_keys(param,quick,param->key[idx],key_tree,param->min_key,0,
9617  param->max_key,0))
9618  {
9619  delete quick;
9620  quick=0;
9621  }
9622  else
9623  {
9624  quick->mrr_flags= mrr_flags;
9625  quick->mrr_buf_size= mrr_buf_size;
9626  quick->key_parts=(KEY_PART*)
9627  memdup_root(parent_alloc? parent_alloc : &quick->alloc,
9628  (char*) param->key[idx],
9629  sizeof(KEY_PART) *
9630  actual_key_parts(&param->
9631  table->key_info[param->real_keynr[idx]]));
9632  }
9633  }
9634  DBUG_RETURN(quick);
9635 }
9636 
9637 
9638 /*
9639 ** Fix this to get all possible sub_ranges
9640 */
9641 bool
9642 get_quick_keys(PARAM *param,QUICK_RANGE_SELECT *quick,KEY_PART *key,
9643  SEL_ARG *key_tree, uchar *min_key,uint min_key_flag,
9644  uchar *max_key, uint max_key_flag)
9645 {
9646  QUICK_RANGE *range;
9647  uint flag;
9648  int min_part= key_tree->part-1, // # of keypart values in min_key buffer
9649  max_part= key_tree->part-1; // # of keypart values in max_key buffer
9650 
9651  if (key_tree->left != &null_element)
9652  {
9653  if (get_quick_keys(param,quick,key,key_tree->left,
9654  min_key,min_key_flag, max_key, max_key_flag))
9655  return 1;
9656  }
9657  uchar *tmp_min_key=min_key,*tmp_max_key=max_key;
9658  min_part+= key_tree->store_min(key[key_tree->part].store_length,
9659  &tmp_min_key,min_key_flag);
9660  max_part+= key_tree->store_max(key[key_tree->part].store_length,
9661  &tmp_max_key,max_key_flag);
9662 
9663  if (key_tree->next_key_part &&
9664  key_tree->next_key_part->type == SEL_ARG::KEY_RANGE &&
9665  key_tree->next_key_part->part == key_tree->part+1)
9666  { // const key as prefix
9667  if ((tmp_min_key - min_key) == (tmp_max_key - max_key) &&
9668  memcmp(min_key, max_key, (uint)(tmp_max_key - max_key))==0 &&
9669  key_tree->min_flag==0 && key_tree->max_flag==0)
9670  {
9671  if (get_quick_keys(param,quick,key,key_tree->next_key_part,
9672  tmp_min_key, min_key_flag | key_tree->min_flag,
9673  tmp_max_key, max_key_flag | key_tree->max_flag))
9674  return 1;
9675  goto end; // Ugly, but efficient
9676  }
9677  {
9678  uint tmp_min_flag=key_tree->min_flag,tmp_max_flag=key_tree->max_flag;
9679  if (!tmp_min_flag)
9680  min_part+= key_tree->next_key_part->store_min_key(key,
9681  &tmp_min_key,
9682  &tmp_min_flag,
9683  MAX_KEY);
9684  if (!tmp_max_flag)
9685  max_part+= key_tree->next_key_part->store_max_key(key,
9686  &tmp_max_key,
9687  &tmp_max_flag,
9688  MAX_KEY);
9689  flag=tmp_min_flag | tmp_max_flag;
9690  }
9691  }
9692  else
9693  {
9694  flag = (key_tree->min_flag & GEOM_FLAG) ?
9695  key_tree->min_flag : key_tree->min_flag | key_tree->max_flag;
9696  }
9697 
9698  /*
9699  Ensure that some part of min_key and max_key are used. If not,
9700  regard this as no lower/upper range
9701  */
9702  if ((flag & GEOM_FLAG) == 0)
9703  {
9704  if (tmp_min_key != param->min_key)
9705  flag&= ~NO_MIN_RANGE;
9706  else
9707  flag|= NO_MIN_RANGE;
9708  if (tmp_max_key != param->max_key)
9709  flag&= ~NO_MAX_RANGE;
9710  else
9711  flag|= NO_MAX_RANGE;
9712  }
9713  if (flag == 0)
9714  {
9715  uint length= (uint) (tmp_min_key - param->min_key);
9716  if (length == (uint) (tmp_max_key - param->max_key) &&
9717  !memcmp(param->min_key,param->max_key,length))
9718  {
9719  const KEY *table_key=quick->head->key_info+quick->index;
9720  flag=EQ_RANGE;
9721  /*
9722  Note that keys which are extended with PK parts have no
9723  HA_NOSAME flag. So we can use user_defined_key_parts.
9724  */
9725  if ((table_key->flags & HA_NOSAME) &&
9726  key->part == table_key->user_defined_key_parts - 1)
9727  {
9728  if (!(table_key->flags & HA_NULL_PART_KEY) ||
9729  !null_part_in_key(key,
9730  param->min_key,
9731  (uint) (tmp_min_key - param->min_key)))
9732  flag|= UNIQUE_RANGE;
9733  else
9734  flag|= NULL_RANGE;
9735  }
9736  }
9737  }
9738 
9739  /* Get range for retrieving rows in QUICK_SELECT::get_next */
9740  if (!(range= new QUICK_RANGE(param->min_key,
9741  (uint) (tmp_min_key - param->min_key),
9742  min_part >=0 ? make_keypart_map(min_part) : 0,
9743  param->max_key,
9744  (uint) (tmp_max_key - param->max_key),
9745  max_part >=0 ? make_keypart_map(max_part) : 0,
9746  flag)))
9747  return 1; // out of memory
9748 
9749  set_if_bigger(quick->max_used_key_length, range->min_length);
9750  set_if_bigger(quick->max_used_key_length, range->max_length);
9751  set_if_bigger(quick->used_key_parts, (uint) key_tree->part+1);
9752  if (insert_dynamic(&quick->ranges, &range))
9753  return 1;
9754 
9755  end:
9756  if (key_tree->right != &null_element)
9757  return get_quick_keys(param,quick,key,key_tree->right,
9758  min_key,min_key_flag,
9759  max_key,max_key_flag);
9760  return 0;
9761 }
9762 
9763 /*
9764  Return 1 if there is only one range and this uses the whole primary key
9765 */
9766 
9767 bool QUICK_RANGE_SELECT::unique_key_range()
9768 {
9769  if (ranges.elements == 1)
9770  {
9771  QUICK_RANGE *tmp= *((QUICK_RANGE**)ranges.buffer);
9772  if ((tmp->flag & (EQ_RANGE | NULL_RANGE)) == EQ_RANGE)
9773  {
9774  KEY *key=head->key_info+index;
9775  return (key->flags & HA_NOSAME) && key->key_length == tmp->min_length;
9776  }
9777  }
9778  return 0;
9779 }
9780 
9781 
9782 
9783 /*
9784  Return TRUE if any part of the key is NULL
9785 
9786  SYNOPSIS
9787  null_part_in_key()
9788  key_part Array of key parts (index description)
9789  key Key values tuple
9790  length Length of key values tuple in bytes.
9791 
9792  RETURN
9793  TRUE The tuple has at least one "keypartX is NULL"
9794  FALSE Otherwise
9795 */
9796 
9797 static bool null_part_in_key(KEY_PART *key_part, const uchar *key, uint length)
9798 {
9799  for (const uchar *end=key+length ;
9800  key < end;
9801  key+= key_part++->store_length)
9802  {
9803  if (key_part->null_bit && *key)
9804  return 1;
9805  }
9806  return 0;
9807 }
9808 
9809 
9810 bool QUICK_SELECT_I::is_keys_used(const MY_BITMAP *fields)
9811 {
9812  return is_key_used(head, index, fields);
9813 }
9814 
9815 bool QUICK_INDEX_MERGE_SELECT::is_keys_used(const MY_BITMAP *fields)
9816 {
9817  QUICK_RANGE_SELECT *quick;
9818  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
9819  while ((quick= it++))
9820  {
9821  if (is_key_used(head, quick->index, fields))
9822  return 1;
9823  }
9824  return 0;
9825 }
9826 
9827 bool QUICK_ROR_INTERSECT_SELECT::is_keys_used(const MY_BITMAP *fields)
9828 {
9829  QUICK_RANGE_SELECT *quick;
9830  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
9831  while ((quick= it++))
9832  {
9833  if (is_key_used(head, quick->index, fields))
9834  return 1;
9835  }
9836  return 0;
9837 }
9838 
9839 bool QUICK_ROR_UNION_SELECT::is_keys_used(const MY_BITMAP *fields)
9840 {
9841  QUICK_SELECT_I *quick;
9842  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
9843  while ((quick= it++))
9844  {
9845  if (quick->is_keys_used(fields))
9846  return 1;
9847  }
9848  return 0;
9849 }
9850 
9851 
9852 FT_SELECT *get_ft_select(THD *thd, TABLE *table, uint key)
9853 {
9854  bool create_err= FALSE;
9855  FT_SELECT *fts= new FT_SELECT(thd, table, key, &create_err);
9856  if (create_err)
9857  {
9858  delete fts;
9859  return NULL;
9860  }
9861  else
9862  return fts;
9863 }
9864 
9865 #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
9866 static bool
9867 key_has_nulls(const KEY* key_info, const uchar *key, uint key_len)
9868 {
9869  KEY_PART_INFO *curr_part, *end_part;
9870  const uchar* end_ptr= key + key_len;
9871  curr_part= key_info->key_part;
9872  end_part= curr_part + key_info->user_defined_key_parts;
9873 
9874  for (; curr_part != end_part && key < end_ptr; curr_part++)
9875  {
9876  if (curr_part->null_bit && *key)
9877  return TRUE;
9878 
9879  key += curr_part->store_length;
9880  }
9881  return FALSE;
9882 }
9883 #endif
9884 
9885 /*
9886  Create quick select from ref/ref_or_null scan.
9887 
9888  SYNOPSIS
9889  get_quick_select_for_ref()
9890  thd Thread handle
9891  table Table to access
9892  ref ref[_or_null] scan parameters
9893  records Estimate of number of records (needed only to construct
9894  quick select)
9895  NOTES
9896  This allocates things in a new memory root, as this may be called many
9897  times during a query.
9898 
9899  RETURN
9900  Quick select that retrieves the same rows as passed ref scan
9901  NULL on error.
9902 */
9903 
9904 QUICK_RANGE_SELECT *get_quick_select_for_ref(THD *thd, TABLE *table,
9905  TABLE_REF *ref, ha_rows records)
9906 {
9907  MEM_ROOT *old_root, *alloc;
9908  QUICK_RANGE_SELECT *quick;
9909  KEY *key_info = &table->key_info[ref->key];
9910  KEY_PART *key_part;
9911  QUICK_RANGE *range;
9912  uint part;
9913  bool create_err= FALSE;
9914  Cost_estimate cost;
9915 
9916  old_root= thd->mem_root;
9917  /* The following call may change thd->mem_root */
9918  quick= new QUICK_RANGE_SELECT(thd, table, ref->key, 0, 0, &create_err);
9919  /* save mem_root set by QUICK_RANGE_SELECT constructor */
9920  alloc= thd->mem_root;
9921  /*
9922  return back default mem_root (thd->mem_root) changed by
9923  QUICK_RANGE_SELECT constructor
9924  */
9925  thd->mem_root= old_root;
9926 
9927  if (!quick || create_err)
9928  return 0; /* no ranges found */
9929  if (quick->init())
9930  goto err;
9931  quick->records= records;
9932 
9933  if ((cp_buffer_from_ref(thd, table, ref) && thd->is_fatal_error) ||
9934  !(range= new(alloc) QUICK_RANGE()))
9935  goto err; // out of memory
9936 
9937  range->min_key= range->max_key= ref->key_buff;
9938  range->min_length= range->max_length= ref->key_length;
9939  range->min_keypart_map= range->max_keypart_map=
9940  make_prev_keypart_map(ref->key_parts);
9941  range->flag= (ref->key_length == key_info->key_length ? EQ_RANGE : 0);
9942 
9943  if (!(quick->key_parts=key_part=(KEY_PART *)
9944  alloc_root(&quick->alloc,sizeof(KEY_PART)*ref->key_parts)))
9945  goto err;
9946 
9947  for (part=0 ; part < ref->key_parts ;part++,key_part++)
9948  {
9949  key_part->part=part;
9950  key_part->field= key_info->key_part[part].field;
9951  key_part->length= key_info->key_part[part].length;
9952  key_part->store_length= key_info->key_part[part].store_length;
9953  key_part->null_bit= key_info->key_part[part].null_bit;
9954  key_part->flag= (uint8) key_info->key_part[part].key_part_flag;
9955  }
9956  if (insert_dynamic(&quick->ranges, &range))
9957  goto err;
9958 
9959  /*
9960  Add a NULL range if REF_OR_NULL optimization is used.
9961  For example:
9962  if we have "WHERE A=2 OR A IS NULL" we created the (A=2) range above
9963  and have ref->null_ref_key set. Will create a new NULL range here.
9964  */
9965  if (ref->null_ref_key)
9966  {
9967  QUICK_RANGE *null_range;
9968 
9969  *ref->null_ref_key= 1; // Set null byte then create a range
9970  if (!(null_range= new (alloc)
9971  QUICK_RANGE(ref->key_buff, ref->key_length,
9972  make_prev_keypart_map(ref->key_parts),
9973  ref->key_buff, ref->key_length,
9974  make_prev_keypart_map(ref->key_parts), EQ_RANGE)))
9975  goto err;
9976  *ref->null_ref_key= 0; // Clear null byte
9977  if (insert_dynamic(&quick->ranges, &null_range))
9978  goto err;
9979  }
9980 
9981  /* Call multi_range_read_info() to get the MRR flags and buffer size */
9982  quick->mrr_flags= HA_MRR_NO_ASSOCIATION |
9983  (table->key_read ? HA_MRR_INDEX_ONLY : 0);
9984  if (thd->lex->sql_command != SQLCOM_SELECT)
9985  quick->mrr_flags|= HA_MRR_SORTED; // Assumed to give faster ins/upd/del
9986 #ifdef WITH_NDBCLUSTER_STORAGE_ENGINE
9987  if (!ref->null_ref_key && !key_has_nulls(key_info, range->min_key,
9988  ref->key_length))
9989  quick->mrr_flags |= HA_MRR_NO_NULL_ENDPOINTS;
9990 #endif
9991 
9992  quick->mrr_buf_size= thd->variables.read_rnd_buff_size;
9993  if (table->file->multi_range_read_info(quick->index, 1, records,
9994  &quick->mrr_buf_size,
9995  &quick->mrr_flags, &cost))
9996  goto err;
9997 
9998  return quick;
9999 err:
10000  delete quick;
10001  return 0;
10002 }
10003 
10004 
10005 /*
10006  Perform key scans for all used indexes (except CPK), get rowids and merge
10007  them into an ordered non-recurrent sequence of rowids.
10008 
10009  The merge/duplicate removal is performed using Unique class. We put all
10010  rowids into Unique, get the sorted sequence and destroy the Unique.
10011 
10012  If table has a clustered primary key that covers all rows (TRUE for bdb
10013  and innodb currently) and one of the index_merge scans is a scan on PK,
10014  then rows that will be retrieved by PK scan are not put into Unique and
10015  primary key scan is not performed here, it is performed later separately.
10016 
10017  RETURN
10018  0 OK
10019  other error
10020 */
10021 
10022 int QUICK_INDEX_MERGE_SELECT::read_keys_and_merge()
10023 {
10024  List_iterator_fast<QUICK_RANGE_SELECT> cur_quick_it(quick_selects);
10025  QUICK_RANGE_SELECT* cur_quick;
10026  int result;
10027  handler *file= head->file;
10028  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::read_keys_and_merge");
10029 
10030  /* We're going to just read rowids. */
10031  head->set_keyread(TRUE);
10032  head->prepare_for_position();
10033 
10034  cur_quick_it.rewind();
10035  cur_quick= cur_quick_it++;
10036  DBUG_ASSERT(cur_quick != 0);
10037 
10038  DBUG_EXECUTE_IF("simulate_bug13919180",
10039  {
10040  my_error(ER_UNKNOWN_ERROR, MYF(0));
10041  DBUG_RETURN(1);
10042  });
10043  /*
10044  We reuse the same instance of handler so we need to call both init and
10045  reset here.
10046  */
10047  if (cur_quick->init() || cur_quick->reset())
10048  DBUG_RETURN(1);
10049 
10050  if (unique == NULL)
10051  {
10052  DBUG_EXECUTE_IF("index_merge_may_not_create_a_Unique", DBUG_ABORT(); );
10053  DBUG_EXECUTE_IF("only_one_Unique_may_be_created",
10054  DBUG_SET("+d,index_merge_may_not_create_a_Unique"); );
10055 
10056  unique= new Unique(refpos_order_cmp, (void *)file,
10057  file->ref_length,
10058  thd->variables.sortbuff_size);
10059  }
10060  else
10061  {
10062  unique->reset();
10063  filesort_free_buffers(head, false);
10064  }
10065 
10066  DBUG_ASSERT(file->ref_length == unique->get_size());
10067  DBUG_ASSERT(thd->variables.sortbuff_size == unique->get_max_in_memory_size());
10068 
10069  if (!unique)
10070  DBUG_RETURN(1);
10071  for (;;)
10072  {
10073  while ((result= cur_quick->get_next()) == HA_ERR_END_OF_FILE)
10074  {
10075  cur_quick->range_end();
10076  cur_quick= cur_quick_it++;
10077  if (!cur_quick)
10078  break;
10079 
10080  if (cur_quick->file->inited)
10081  cur_quick->file->ha_index_end();
10082  if (cur_quick->init() || cur_quick->reset())
10083  DBUG_RETURN(1);
10084  }
10085 
10086  if (result)
10087  {
10088  if (result != HA_ERR_END_OF_FILE)
10089  {
10090  cur_quick->range_end();
10091  DBUG_RETURN(result);
10092  }
10093  break;
10094  }
10095 
10096  if (thd->killed)
10097  DBUG_RETURN(1);
10098 
10099  /* skip row if it will be retrieved by clustered PK scan */
10100  if (pk_quick_select && pk_quick_select->row_in_ranges())
10101  continue;
10102 
10103  cur_quick->file->position(cur_quick->record);
10104  result= unique->unique_add((char*)cur_quick->file->ref);
10105  if (result)
10106  DBUG_RETURN(1);
10107  }
10108 
10109  /*
10110  Ok all rowids are in the Unique now. The next call will initialize
10111  head->sort structure so it can be used to iterate through the rowids
10112  sequence.
10113  */
10114  result= unique->get(head);
10115  doing_pk_scan= FALSE;
10116  /* index_merge currently doesn't support "using index" at all */
10117  head->set_keyread(FALSE);
10118  if (init_read_record(&read_record, thd, head, (SQL_SELECT*) 0, 1, 1, TRUE))
10119  DBUG_RETURN(1);
10120  DBUG_RETURN(result);
10121 }
10122 
10123 
10124 /*
10125  Get next row for index_merge.
10126  NOTES
10127  The rows are read from
10128  1. rowids stored in Unique.
10129  2. QUICK_RANGE_SELECT with clustered primary key (if any).
10130  The sets of rows retrieved in 1) and 2) are guaranteed to be disjoint.
10131 */
10132 
10133 int QUICK_INDEX_MERGE_SELECT::get_next()
10134 {
10135  int result;
10136  DBUG_ENTER("QUICK_INDEX_MERGE_SELECT::get_next");
10137 
10138  if (doing_pk_scan)
10139  DBUG_RETURN(pk_quick_select->get_next());
10140 
10141  if ((result= read_record.read_record(&read_record)) == -1)
10142  {
10143  result= HA_ERR_END_OF_FILE;
10144  end_read_record(&read_record);
10145  free_io_cache(head);
10146  /* All rows from Unique have been retrieved, do a clustered PK scan */
10147  if (pk_quick_select)
10148  {
10149  doing_pk_scan= TRUE;
10150  if ((result= pk_quick_select->init()) ||
10151  (result= pk_quick_select->reset()))
10152  DBUG_RETURN(result);
10153  DBUG_RETURN(pk_quick_select->get_next());
10154  }
10155  }
10156 
10157  DBUG_RETURN(result);
10158 }
10159 
10160 
10161 /*
10162  Retrieve next record.
10163  SYNOPSIS
10164  QUICK_ROR_INTERSECT_SELECT::get_next()
10165 
10166  NOTES
10167  Invariant on enter/exit: all intersected selects have retrieved all index
10168  records with rowid <= some_rowid_val and no intersected select has
10169  retrieved any index records with rowid > some_rowid_val.
10170  We start fresh and loop until we have retrieved the same rowid in each of
10171  the key scans or we got an error.
10172 
10173  If a Clustered PK scan is present, it is used only to check if row
10174  satisfies its condition (and never used for row retrieval).
10175 
10176  Locking: to ensure that exclusive locks are only set on records that
10177  are included in the final result we must release the lock
10178  on all rows we read but do not include in the final result. This
10179  must be done on each index that reads the record and the lock
10180  must be released using the same handler (the same quick object) as
10181  used when reading the record.
10182 
10183  RETURN
10184  0 - Ok
10185  other - Error code if any error occurred.
10186 */
10187 
10188 int QUICK_ROR_INTERSECT_SELECT::get_next()
10189 {
10190  List_iterator_fast<QUICK_RANGE_SELECT> quick_it(quick_selects);
10191  QUICK_RANGE_SELECT* quick;
10192 
10193  /* quick that reads the given rowid first. This is needed in order
10194  to be able to unlock the row using the same handler object that locked
10195  it */
10196  QUICK_RANGE_SELECT* quick_with_last_rowid;
10197 
10198  int error, cmp;
10199  uint last_rowid_count=0;
10200  DBUG_ENTER("QUICK_ROR_INTERSECT_SELECT::get_next");
10201 
10202  do
10203  {
10204  /* Get a rowid for first quick and save it as a 'candidate' */
10205  quick= quick_it++;
10206  error= quick->get_next();
10207  if (cpk_quick)
10208  {
10209  while (!error && !cpk_quick->row_in_ranges())
10210  {
10211  quick->file->unlock_row(); /* row not in range; unlock */
10212  error= quick->get_next();
10213  }
10214  }
10215  if (error)
10216  DBUG_RETURN(error);
10217 
10218  quick->file->position(quick->record);
10219  memcpy(last_rowid, quick->file->ref, head->file->ref_length);
10220  last_rowid_count= 1;
10221  quick_with_last_rowid= quick;
10222 
10223  while (last_rowid_count < quick_selects.elements)
10224  {
10225  if (!(quick= quick_it++))
10226  {
10227  quick_it.rewind();
10228  quick= quick_it++;
10229  }
10230 
10231  do
10232  {
10233  DBUG_EXECUTE_IF("innodb_quick_report_deadlock",
10234  DBUG_SET("+d,innodb_report_deadlock"););
10235  if ((error= quick->get_next()))
10236  {
10237  /* On certain errors like deadlock, trx might be rolled back.*/
10238  if (!current_thd->transaction_rollback_request)
10239  quick_with_last_rowid->file->unlock_row();
10240  DBUG_RETURN(error);
10241  }
10242  quick->file->position(quick->record);
10243  cmp= head->file->cmp_ref(quick->file->ref, last_rowid);
10244  if (cmp < 0)
10245  {
10246  /* This row is being skipped. Release lock on it. */
10247  quick->file->unlock_row();
10248  }
10249  } while (cmp < 0);
10250 
10251  /* Ok, current select 'caught up' and returned ref >= cur_ref */
10252  if (cmp > 0)
10253  {
10254  /* Found a row with ref > cur_ref. Make it a new 'candidate' */
10255  if (cpk_quick)
10256  {
10257  while (!cpk_quick->row_in_ranges())
10258  {
10259  quick->file->unlock_row(); /* row not in range; unlock */
10260  if ((error= quick->get_next()))
10261  {
10262  /* On certain errors like deadlock, trx might be rolled back.*/
10263  if (!current_thd->transaction_rollback_request)
10264  quick_with_last_rowid->file->unlock_row();
10265  DBUG_RETURN(error);
10266  }
10267  }
10268  quick->file->position(quick->record);
10269  }
10270  memcpy(last_rowid, quick->file->ref, head->file->ref_length);
10271  quick_with_last_rowid->file->unlock_row();
10272  last_rowid_count= 1;
10273  quick_with_last_rowid= quick;
10274  }
10275  else
10276  {
10277  /* current 'candidate' row confirmed by this select */
10278  last_rowid_count++;
10279  }
10280  }
10281 
10282  /* We get here if we got the same row ref in all scans. */
10283  if (need_to_fetch_row)
10284  error= head->file->ha_rnd_pos(head->record[0], last_rowid);
10285  } while (error == HA_ERR_RECORD_DELETED);
10286  DBUG_RETURN(error);
10287 }
10288 
10289 
10290 /*
10291  Retrieve next record.
10292  SYNOPSIS
10293  QUICK_ROR_UNION_SELECT::get_next()
10294 
10295  NOTES
10296  Enter/exit invariant:
10297  For each quick select in the queue a {key,rowid} tuple has been
10298  retrieved but the corresponding row hasn't been passed to output.
10299 
10300  RETURN
10301  0 - Ok
10302  other - Error code if any error occurred.
10303 */
10304 
10305 int QUICK_ROR_UNION_SELECT::get_next()
10306 {
10307  int error, dup_row;
10308  QUICK_SELECT_I *quick;
10309  uchar *tmp;
10310  DBUG_ENTER("QUICK_ROR_UNION_SELECT::get_next");
10311 
10312  do
10313  {
10314  do
10315  {
10316  if (!queue.elements)
10317  DBUG_RETURN(HA_ERR_END_OF_FILE);
10318  /* Ok, we have a queue with >= 1 scans */
10319 
10320  quick= (QUICK_SELECT_I*)queue_top(&queue);
10321  memcpy(cur_rowid, quick->last_rowid, rowid_length);
10322 
10323  /* put into queue rowid from the same stream as top element */
10324  if ((error= quick->get_next()))
10325  {
10326  if (error != HA_ERR_END_OF_FILE)
10327  DBUG_RETURN(error);
10328  queue_remove(&queue, 0);
10329  }
10330  else
10331  {
10332  quick->save_last_pos();
10333  queue_replaced(&queue);
10334  }
10335 
10336  if (!have_prev_rowid)
10337  {
10338  /* No rows have been returned yet */
10339  dup_row= FALSE;
10340  have_prev_rowid= TRUE;
10341  }
10342  else
10343  dup_row= !head->file->cmp_ref(cur_rowid, prev_rowid);
10344  } while (dup_row);
10345 
10346  tmp= cur_rowid;
10347  cur_rowid= prev_rowid;
10348  prev_rowid= tmp;
10349 
10350  error= head->file->ha_rnd_pos(quick->record, prev_rowid);
10351  } while (error == HA_ERR_RECORD_DELETED);
10352  DBUG_RETURN(error);
10353 }
10354 
10355 
10356 int QUICK_RANGE_SELECT::reset()
10357 {
10358  uint buf_size;
10359  uchar *mrange_buff;
10360  int error;
10361  HANDLER_BUFFER empty_buf;
10362  DBUG_ENTER("QUICK_RANGE_SELECT::reset");
10363  last_range= NULL;
10364  cur_range= (QUICK_RANGE**) ranges.buffer;
10365 
10366  /* set keyread to TRUE if index is covering */
10367  if(!head->no_keyread && head->covering_keys.is_set(index))
10368  head->set_keyread(true);
10369  else
10370  head->set_keyread(false);
10371 
10372  if (!file->inited)
10373  {
10374  if (in_ror_merged_scan)
10375  head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap);
10376  const bool sorted= (mrr_flags & HA_MRR_SORTED);
10377  DBUG_EXECUTE_IF("bug14365043_2",
10378  DBUG_SET("+d,ha_index_init_fail"););
10379  if ((error= file->ha_index_init(index, sorted)))
10380  {
10381  file->print_error(error, MYF(0));
10382  DBUG_RETURN(error);
10383  }
10384  }
10385 
10386  /* Allocate buffer if we need one but haven't allocated it yet */
10387  if (mrr_buf_size && !mrr_buf_desc)
10388  {
10389  buf_size= mrr_buf_size;
10390  while (buf_size && !my_multi_malloc(MYF(MY_WME),
10391  &mrr_buf_desc, sizeof(*mrr_buf_desc),
10392  &mrange_buff, buf_size,
10393  NullS))
10394  {
10395  /* Try to shrink the buffers until both are 0. */
10396  buf_size/= 2;
10397  }
10398  if (!mrr_buf_desc)
10399  DBUG_RETURN(HA_ERR_OUT_OF_MEM);
10400 
10401  /* Initialize the handler buffer. */
10402  mrr_buf_desc->buffer= mrange_buff;
10403  mrr_buf_desc->buffer_end= mrange_buff + buf_size;
10404  mrr_buf_desc->end_of_used_area= mrange_buff;
10405 #ifdef HAVE_purify
10406  /*
10407  We need this until ndb will use the buffer efficiently
10408  (Now ndb stores complete row in here, instead of only the used fields
10409  which gives us valgrind warnings in compare_record[])
10410  */
10411  memset(mrange_buff, 0, buf_size);
10412 #endif
10413  }
10414 
10415  if (!mrr_buf_desc)
10416  empty_buf.buffer= empty_buf.buffer_end= empty_buf.end_of_used_area= NULL;
10417 
10418  RANGE_SEQ_IF seq_funcs= {quick_range_seq_init, quick_range_seq_next, 0, 0};
10419  error= file->multi_range_read_init(&seq_funcs, (void*)this, ranges.elements,
10420  mrr_flags, mrr_buf_desc? mrr_buf_desc:
10421  &empty_buf);
10422  DBUG_RETURN(error);
10423 }
10424 
10425 
10426 /*
10427  Range sequence interface implementation for array<QUICK_RANGE>: initialize
10428 
10429  SYNOPSIS
10430  quick_range_seq_init()
10431  init_param Caller-opaque paramenter: QUICK_RANGE_SELECT* pointer
10432  n_ranges Number of ranges in the sequence (ignored)
10433  flags MRR flags (currently not used)
10434 
10435  RETURN
10436  Opaque value to be passed to quick_range_seq_next
10437 */
10438 
10439 range_seq_t quick_range_seq_init(void *init_param, uint n_ranges, uint flags)
10440 {
10441  QUICK_RANGE_SELECT *quick= (QUICK_RANGE_SELECT*)init_param;
10442  quick->qr_traversal_ctx.first= (QUICK_RANGE**)quick->ranges.buffer;
10443  quick->qr_traversal_ctx.cur= (QUICK_RANGE**)quick->ranges.buffer;
10444  quick->qr_traversal_ctx.last= quick->qr_traversal_ctx.cur +
10445  quick->ranges.elements;
10446  return &quick->qr_traversal_ctx;
10447 }
10448 
10449 
10450 /*
10451  Range sequence interface implementation for array<QUICK_RANGE>: get next
10452 
10453  SYNOPSIS
10454  quick_range_seq_next()
10455  rseq Value returned from quick_range_seq_init
10456  range OUT Store information about the range here
10457 
10458  RETURN
10459  0 Ok
10460  1 No more ranges in the sequence
10461 */
10462 
10463 uint quick_range_seq_next(range_seq_t rseq, KEY_MULTI_RANGE *range)
10464 {
10466 
10467  if (ctx->cur == ctx->last)
10468  return 1; /* no more ranges */
10469 
10470  QUICK_RANGE *cur= *(ctx->cur);
10471  key_range *start_key= &range->start_key;
10472  key_range *end_key= &range->end_key;
10473 
10474  start_key->key= cur->min_key;
10475  start_key->length= cur->min_length;
10476  start_key->keypart_map= cur->min_keypart_map;
10477  start_key->flag= ((cur->flag & NEAR_MIN) ? HA_READ_AFTER_KEY :
10478  (cur->flag & EQ_RANGE) ?
10479  HA_READ_KEY_EXACT : HA_READ_KEY_OR_NEXT);
10480  end_key->key= cur->max_key;
10481  end_key->length= cur->max_length;
10482  end_key->keypart_map= cur->max_keypart_map;
10483  /*
10484  We use HA_READ_AFTER_KEY here because if we are reading on a key
10485  prefix. We want to find all keys with this prefix.
10486  */
10487  end_key->flag= (cur->flag & NEAR_MAX ? HA_READ_BEFORE_KEY :
10488  HA_READ_AFTER_KEY);
10489  range->range_flag= cur->flag;
10490  ctx->cur++;
10491  return 0;
10492 }
10493 
10494 
10495 /*
10496  MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
10497 
10498  SYNOPSIS
10499  mrr_persistent_flag_storage()
10500  seq Range sequence being traversed
10501  idx Number of range
10502 
10503  DESCRIPTION
10504  MRR/NDB implementation needs to store some bits for each range. This
10505  function returns a reference to the "range_flag" associated with the
10506  range number idx.
10507 
10508  This function should be removed when we get a proper MRR/NDB
10509  implementation.
10510 
10511  RETURN
10512  Reference to range_flag associated with range number #idx
10513 */
10514 
10515 uint16 &mrr_persistent_flag_storage(range_seq_t seq, uint idx)
10516 {
10518  return ctx->first[idx]->flag;
10519 }
10520 
10521 
10522 /*
10523  MRR range sequence interface: array<QUICK_RANGE> impl: utility func for NDB
10524 
10525  SYNOPSIS
10526  mrr_get_ptr_by_idx()
10527  seq Range sequence bening traversed
10528  idx Number of the range
10529 
10530  DESCRIPTION
10531  An extension of MRR range sequence interface needed by NDB: return the
10532  data associated with the given range.
10533 
10534  A proper MRR interface implementer is supposed to store and return
10535  range-associated data. NDB stores number of the range instead. So this
10536  is a helper function that translates range number to range associated
10537  data.
10538 
10539  This function does nothing, as currrently there is only one user of the
10540  MRR interface - the quick range select code, and this user doesn't need
10541  to use range-associated data.
10542 
10543  RETURN
10544  Reference to range-associated data
10545 */
10546 
10547 char* &mrr_get_ptr_by_idx(range_seq_t seq, uint idx)
10548 {
10549  static char *dummy;
10550  return dummy;
10551 }
10552 
10553 
10554 /*
10555  Get next possible record using quick-struct.
10556 
10557  SYNOPSIS
10558  QUICK_RANGE_SELECT::get_next()
10559 
10560  NOTES
10561  Record is read into table->record[0]
10562 
10563  RETURN
10564  0 Found row
10565  HA_ERR_END_OF_FILE No (more) rows in range
10566  # Error code
10567 */
10568 
10569 int QUICK_RANGE_SELECT::get_next()
10570 {
10571  char *dummy;
10572  MY_BITMAP * const save_read_set= head->read_set;
10573  MY_BITMAP * const save_write_set= head->write_set;
10574  DBUG_ENTER("QUICK_RANGE_SELECT::get_next");
10575 
10576  if (in_ror_merged_scan)
10577  {
10578  /*
10579  We don't need to signal the bitmap change as the bitmap is always the
10580  same for this head->file
10581  */
10582  head->column_bitmaps_set_no_signal(&column_bitmap, &column_bitmap);
10583  }
10584 
10585  int result= file->multi_range_read_next(&dummy);
10586 
10587  if (in_ror_merged_scan)
10588  {
10589  /* Restore bitmaps set on entry */
10590  head->column_bitmaps_set_no_signal(save_read_set, save_write_set);
10591  }
10592  DBUG_RETURN(result);
10593 }
10594 
10595 
10596 /*
10597  Get the next record with a different prefix.
10598 
10599  @param prefix_length length of cur_prefix
10600  @param group_key_parts The number of key parts in the group prefix
10601  @param cur_prefix prefix of a key to be searched for
10602 
10603  Each subsequent call to the method retrieves the first record that has a
10604  prefix with length prefix_length and which is different from cur_prefix,
10605  such that the record with the new prefix is within the ranges described by
10606  this->ranges. The record found is stored into the buffer pointed by
10607  this->record. The method is useful for GROUP-BY queries with range
10608  conditions to discover the prefix of the next group that satisfies the range
10609  conditions.
10610 
10611  @todo
10612 
10613  This method is a modified copy of QUICK_RANGE_SELECT::get_next(), so both
10614  methods should be unified into a more general one to reduce code
10615  duplication.
10616 
10617  @retval 0 on success
10618  @retval HA_ERR_END_OF_FILE if returned all keys
10619  @retval other if some error occurred
10620 */
10621 
10622 int QUICK_RANGE_SELECT::get_next_prefix(uint prefix_length,
10623  uint group_key_parts,
10624  uchar *cur_prefix)
10625 {
10626  DBUG_ENTER("QUICK_RANGE_SELECT::get_next_prefix");
10627  const key_part_map keypart_map= make_prev_keypart_map(group_key_parts);
10628 
10629  for (;;)
10630  {
10631  int result;
10632  if (last_range)
10633  {
10634  /* Read the next record in the same range with prefix after cur_prefix. */
10635  DBUG_ASSERT(cur_prefix != NULL);
10636  result= file->ha_index_read_map(record, cur_prefix, keypart_map,
10637  HA_READ_AFTER_KEY);
10638  if (result || last_range->max_keypart_map == 0)
10639  DBUG_RETURN(result);
10640 
10641  key_range previous_endpoint;
10642  last_range->make_max_endpoint(&previous_endpoint, prefix_length, keypart_map);
10643  if (file->compare_key(&previous_endpoint) <= 0)
10644  DBUG_RETURN(0);
10645  }
10646 
10647  uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
10648  if (count == 0)
10649  {
10650  /* Ranges have already been used up before. None is left for read. */
10651  last_range= 0;
10652  DBUG_RETURN(HA_ERR_END_OF_FILE);
10653  }
10654  last_range= *(cur_range++);
10655 
10656  key_range start_key, end_key;
10657  last_range->make_min_endpoint(&start_key, prefix_length, keypart_map);
10658  last_range->make_max_endpoint(&end_key, prefix_length, keypart_map);
10659 
10660  const bool sorted= (mrr_flags & HA_MRR_SORTED);
10661  result= file->read_range_first(last_range->min_keypart_map ? &start_key : 0,
10662  last_range->max_keypart_map ? &end_key : 0,
10663  test(last_range->flag & EQ_RANGE),
10664  sorted);
10665  if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
10666  last_range= 0; // Stop searching
10667 
10668  if (result != HA_ERR_END_OF_FILE)
10669  DBUG_RETURN(result);
10670  last_range= 0; // No matching rows; go to next range
10671  }
10672 }
10673 
10674 
10675 /* Get next for geometrical indexes */
10676 
10677 int QUICK_RANGE_SELECT_GEOM::get_next()
10678 {
10679  DBUG_ENTER("QUICK_RANGE_SELECT_GEOM::get_next");
10680 
10681  for (;;)
10682  {
10683  int result;
10684  if (last_range)
10685  {
10686  // Already read through key
10687  result= file->ha_index_next_same(record, last_range->min_key,
10688  last_range->min_length);
10689  if (result != HA_ERR_END_OF_FILE)
10690  DBUG_RETURN(result);
10691  }
10692 
10693  uint count= ranges.elements - (cur_range - (QUICK_RANGE**) ranges.buffer);
10694  if (count == 0)
10695  {
10696  /* Ranges have already been used up before. None is left for read. */
10697  last_range= 0;
10698  DBUG_RETURN(HA_ERR_END_OF_FILE);
10699  }
10700  last_range= *(cur_range++);
10701 
10702  result= file->ha_index_read_map(record, last_range->min_key,
10703  last_range->min_keypart_map,
10704  (ha_rkey_function)(last_range->flag ^
10705  GEOM_FLAG));
10706  if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
10707  DBUG_RETURN(result);
10708  last_range= 0; // Not found, to next range
10709  }
10710 }
10711 
10712 
10713 /*
10714  Check if current row will be retrieved by this QUICK_RANGE_SELECT
10715 
10716  NOTES
10717  It is assumed that currently a scan is being done on another index
10718  which reads all necessary parts of the index that is scanned by this
10719  quick select.
10720  The implementation does a binary search on sorted array of disjoint
10721  ranges, without taking size of range into account.
10722 
10723  This function is used to filter out clustered PK scan rows in
10724  index_merge quick select.
10725 
10726  RETURN
10727  TRUE if current row will be retrieved by this quick select
10728  FALSE if not
10729 */
10730 
10731 bool QUICK_RANGE_SELECT::row_in_ranges()
10732 {
10733  QUICK_RANGE *res;
10734  uint min= 0;
10735  uint max= ranges.elements - 1;
10736  uint mid= (max + min)/2;
10737 
10738  while (min != max)
10739  {
10740  if (cmp_next(*(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid)))
10741  {
10742  /* current row value > mid->max */
10743  min= mid + 1;
10744  }
10745  else
10746  max= mid;
10747  mid= (min + max) / 2;
10748  }
10749  res= *(QUICK_RANGE**)dynamic_array_ptr(&ranges, mid);
10750  return (!cmp_next(res) && !cmp_prev(res));
10751 }
10752 
10753 /*
10754  This is a hack: we inherit from QUICK_RANGE_SELECT so that we can use the
10755  get_next() interface, but we have to hold a pointer to the original
10756  QUICK_RANGE_SELECT because its data are used all over the place. What
10757  should be done is to factor out the data that is needed into a base
10758  class (QUICK_SELECT), and then have two subclasses (_ASC and _DESC)
10759  which handle the ranges and implement the get_next() function. But
10760  for now, this seems to work right at least.
10761  */
10762 
10763 QUICK_SELECT_DESC::QUICK_SELECT_DESC(QUICK_RANGE_SELECT *q,
10764  uint used_key_parts_arg,
10765  bool *error)
10766  :QUICK_RANGE_SELECT(*q), rev_it(rev_ranges),
10767  used_key_parts (used_key_parts_arg)
10768 {
10769  QUICK_RANGE *r;
10770  /*
10771  Use default MRR implementation for reverse scans. No table engine
10772  currently can do an MRR scan with output in reverse index order.
10773  */
10774  mrr_buf_desc= NULL;
10775  mrr_flags |= HA_MRR_USE_DEFAULT_IMPL;
10776  mrr_flags |= HA_MRR_SORTED; // 'sorted' as internals use index_last/_prev
10777  mrr_buf_size= 0;
10778 
10779 
10780  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
10781  QUICK_RANGE **end_range= pr + ranges.elements;
10782  for (; pr!=end_range; pr++)
10783  rev_ranges.push_front(*pr);
10784 
10785  /* Remove EQ_RANGE flag for keys that are not using the full key */
10786  for (r = rev_it++; r; r = rev_it++)
10787  {
10788  if ((r->flag & EQ_RANGE) &&
10789  head->key_info[index].key_length != r->max_length)
10790  r->flag&= ~EQ_RANGE;
10791  }
10792  rev_it.rewind();
10793  q->dont_free=1; // Don't free shared mem
10794 }
10795 
10796 
10797 int QUICK_SELECT_DESC::get_next()
10798 {
10799  DBUG_ENTER("QUICK_SELECT_DESC::get_next");
10800 
10801  /* The max key is handled as follows:
10802  * - if there is NO_MAX_RANGE, start at the end and move backwards
10803  * - if it is an EQ_RANGE (which means that max key covers the entire
10804  * key) and the query does not use any hidden key fields that are
10805  * not considered when the range optimzier sets EQ_RANGE (e.g. the
10806  * primary key added by InnoDB), then go directly to the key and
10807  * read through it (sorting backwards is same as sorting forwards).
10808  * - if it is NEAR_MAX, go to the key or next, step back once, and
10809  * move backwards
10810  * - otherwise (not NEAR_MAX == include the key), go after the key,
10811  * step back once, and move backwards
10812  */
10813 
10814  for (;;)
10815  {
10816  int result;
10817  if (last_range)
10818  { // Already read through key
10819  result = ((last_range->flag & EQ_RANGE &&
10820  used_key_parts <=
10821  head->key_info[index].user_defined_key_parts) ?
10822  file->ha_index_next_same(record, last_range->min_key,
10823  last_range->min_length) :
10824  file->ha_index_prev(record));
10825  if (!result)
10826  {
10827  if (cmp_prev(*rev_it.ref()) == 0)
10828  DBUG_RETURN(0);
10829  }
10830  else if (result != HA_ERR_END_OF_FILE)
10831  DBUG_RETURN(result);
10832  }
10833 
10834  if (!(last_range= rev_it++))
10835  DBUG_RETURN(HA_ERR_END_OF_FILE); // All ranges used
10836 
10837  // Case where we can avoid descending scan, see comment above
10838  const bool eqrange_all_keyparts= (last_range->flag & EQ_RANGE) &&
10839  (used_key_parts <= head->key_info[index].user_defined_key_parts);
10840 
10841  /*
10842  If we have pushed an index condition (ICP) and this quick select
10843  will use ha_index_prev() to read data, we need to let the
10844  handler know where to end the scan in order to avoid that the
10845  ICP implemention continues to read past the range boundary.
10846  */
10847  if (file->pushed_idx_cond)
10848  {
10849  if (!eqrange_all_keyparts)
10850  {
10851  key_range min_range;
10852  last_range->make_min_endpoint(&min_range);
10853  if(min_range.length > 0)
10854  file->set_end_range(&min_range, handler::RANGE_SCAN_DESC);
10855  else
10856  file->set_end_range(NULL, handler::RANGE_SCAN_DESC);
10857  }
10858  else
10859  {
10860  /*
10861  Will use ha_index_next_same() for reading records. In case we have
10862  set the end range for an earlier range, this need to be cleared.
10863  */
10864  file->set_end_range(NULL, handler::RANGE_SCAN_ASC);
10865  }
10866  }
10867 
10868  if (last_range->flag & NO_MAX_RANGE) // Read last record
10869  {
10870  int local_error;
10871  if ((local_error= file->ha_index_last(record)))
10872  {
10873  /*
10874  HA_ERR_END_OF_FILE is returned both when the table is empty and when
10875  there are no qualifying records in the range (when using ICP).
10876  Interpret this return value as "no qualifying rows in the range" to
10877  avoid loss of records. If the error code truly meant "empty table"
10878  the next iteration of the loop will exit.
10879  */
10880  if (local_error != HA_ERR_END_OF_FILE)
10881  DBUG_RETURN(local_error);
10882  last_range= NULL; // Go to next range
10883  continue;
10884  }
10885 
10886  if (cmp_prev(last_range) == 0)
10887  DBUG_RETURN(0);
10888  last_range= 0; // No match; go to next range
10889  continue;
10890  }
10891 
10892  if (eqrange_all_keyparts)
10893 
10894  {
10895  result= file->ha_index_read_map(record, last_range->max_key,
10896  last_range->max_keypart_map,
10897  HA_READ_KEY_EXACT);
10898  }
10899  else
10900  {
10901  DBUG_ASSERT(last_range->flag & NEAR_MAX ||
10902  (last_range->flag & EQ_RANGE &&
10903  used_key_parts >
10904  head->key_info[index].user_defined_key_parts) ||
10905  range_reads_after_key(last_range));
10906  result= file->ha_index_read_map(record, last_range->max_key,
10907  last_range->max_keypart_map,
10908  ((last_range->flag & NEAR_MAX) ?
10909  HA_READ_BEFORE_KEY :
10910  HA_READ_PREFIX_LAST_OR_PREV));
10911  }
10912  if (result)
10913  {
10914  if (result != HA_ERR_KEY_NOT_FOUND && result != HA_ERR_END_OF_FILE)
10915  DBUG_RETURN(result);
10916  last_range= 0; // Not found, to next range
10917  continue;
10918  }
10919  if (cmp_prev(last_range) == 0)
10920  {
10921  if (last_range->flag == (UNIQUE_RANGE | EQ_RANGE))
10922  last_range= 0; // Stop searching
10923  DBUG_RETURN(0); // Found key is in range
10924  }
10925  last_range= 0; // To next range
10926  }
10927 }
10928 
10929 
10940 {
10941  bool error= FALSE;
10942  QUICK_SELECT_DESC *new_quick= new QUICK_SELECT_DESC(this, used_key_parts_arg,
10943  &error);
10944  if (new_quick == NULL || error)
10945  {
10946  delete new_quick;
10947  return NULL;
10948  }
10949  return new_quick;
10950 }
10951 
10952 
10953 /*
10954  Compare if found key is over max-value
10955  Returns 0 if key <= range->max_key
10956  TODO: Figure out why can't this function be as simple as cmp_prev().
10957 */
10958 
10959 int QUICK_RANGE_SELECT::cmp_next(QUICK_RANGE *range_arg)
10960 {
10961  if (range_arg->flag & NO_MAX_RANGE)
10962  return 0; /* key can't be to large */
10963 
10964  KEY_PART *key_part=key_parts;
10965  uint store_length;
10966 
10967  for (uchar *key=range_arg->max_key, *end=key+range_arg->max_length;
10968  key < end;
10969  key+= store_length, key_part++)
10970  {
10971  int cmp;
10972  store_length= key_part->store_length;
10973  if (key_part->null_bit)
10974  {
10975  if (*key)
10976  {
10977  if (!key_part->field->is_null())
10978  return 1;
10979  continue;
10980  }
10981  else if (key_part->field->is_null())
10982  return 0;
10983  key++; // Skip null byte
10984  store_length--;
10985  }
10986  if ((cmp=key_part->field->key_cmp(key, key_part->length)) < 0)
10987  return 0;
10988  if (cmp > 0)
10989  return 1;
10990  }
10991  return (range_arg->flag & NEAR_MAX) ? 1 : 0; // Exact match
10992 }
10993 
10994 
10995 /*
10996  Returns 0 if found key is inside range (found key >= range->min_key).
10997 */
10998 
10999 int QUICK_RANGE_SELECT::cmp_prev(QUICK_RANGE *range_arg)
11000 {
11001  int cmp;
11002  if (range_arg->flag & NO_MIN_RANGE)
11003  return 0; /* key can't be to small */
11004 
11005  cmp= key_cmp(key_part_info, range_arg->min_key,
11006  range_arg->min_length);
11007  if (cmp > 0 || (cmp == 0 && !(range_arg->flag & NEAR_MIN)))
11008  return 0;
11009  return 1; // outside of range
11010 }
11011 
11012 
11013 /*
11014  * TRUE if this range will require using HA_READ_AFTER_KEY
11015  See comment in get_next() about this
11016  */
11017 
11018 bool QUICK_SELECT_DESC::range_reads_after_key(QUICK_RANGE *range_arg)
11019 {
11020  return ((range_arg->flag & (NO_MAX_RANGE | NEAR_MAX)) ||
11021  !(range_arg->flag & EQ_RANGE) ||
11022  head->key_info[index].key_length != range_arg->max_length) ? 1 : 0;
11023 }
11024 
11025 
11026 void QUICK_RANGE_SELECT::add_info_string(String *str)
11027 {
11028  KEY *key_info= head->key_info + index;
11029  str->append(key_info->name);
11030 }
11031 
11032 void QUICK_INDEX_MERGE_SELECT::add_info_string(String *str)
11033 {
11034  QUICK_RANGE_SELECT *quick;
11035  bool first= TRUE;
11036  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
11037  str->append(STRING_WITH_LEN("sort_union("));
11038  while ((quick= it++))
11039  {
11040  if (!first)
11041  str->append(',');
11042  else
11043  first= FALSE;
11044  quick->add_info_string(str);
11045  }
11046  if (pk_quick_select)
11047  {
11048  str->append(',');
11049  pk_quick_select->add_info_string(str);
11050  }
11051  str->append(')');
11052 }
11053 
11054 void QUICK_ROR_INTERSECT_SELECT::add_info_string(String *str)
11055 {
11056  bool first= TRUE;
11057  QUICK_RANGE_SELECT *quick;
11058  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
11059  str->append(STRING_WITH_LEN("intersect("));
11060  while ((quick= it++))
11061  {
11062  KEY *key_info= head->key_info + quick->index;
11063  if (!first)
11064  str->append(',');
11065  else
11066  first= FALSE;
11067  str->append(key_info->name);
11068  }
11069  if (cpk_quick)
11070  {
11071  KEY *key_info= head->key_info + cpk_quick->index;
11072  str->append(',');
11073  str->append(key_info->name);
11074  }
11075  str->append(')');
11076 }
11077 
11078 void QUICK_ROR_UNION_SELECT::add_info_string(String *str)
11079 {
11080  bool first= TRUE;
11081  QUICK_SELECT_I *quick;
11082  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
11083  str->append(STRING_WITH_LEN("union("));
11084  while ((quick= it++))
11085  {
11086  if (!first)
11087  str->append(',');
11088  else
11089  first= FALSE;
11090  quick->add_info_string(str);
11091  }
11092  str->append(')');
11093 }
11094 
11095 
11096 void QUICK_RANGE_SELECT::add_keys_and_lengths(String *key_names,
11097  String *used_lengths)
11098 {
11099  char buf[64];
11100  uint length;
11101  KEY *key_info= head->key_info + index;
11102  key_names->append(key_info->name);
11103  length= longlong2str(max_used_key_length, buf, 10) - buf;
11104  used_lengths->append(buf, length);
11105 }
11106 
11107 void QUICK_INDEX_MERGE_SELECT::add_keys_and_lengths(String *key_names,
11108  String *used_lengths)
11109 {
11110  char buf[64];
11111  uint length;
11112  bool first= TRUE;
11113  QUICK_RANGE_SELECT *quick;
11114 
11115  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
11116  while ((quick= it++))
11117  {
11118  if (first)
11119  first= FALSE;
11120  else
11121  {
11122  key_names->append(',');
11123  used_lengths->append(',');
11124  }
11125 
11126  KEY *key_info= head->key_info + quick->index;
11127  key_names->append(key_info->name);
11128  length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
11129  used_lengths->append(buf, length);
11130  }
11131  if (pk_quick_select)
11132  {
11133  KEY *key_info= head->key_info + pk_quick_select->index;
11134  key_names->append(',');
11135  key_names->append(key_info->name);
11136  length= longlong2str(pk_quick_select->max_used_key_length, buf, 10) - buf;
11137  used_lengths->append(',');
11138  used_lengths->append(buf, length);
11139  }
11140 }
11141 
11142 void QUICK_ROR_INTERSECT_SELECT::add_keys_and_lengths(String *key_names,
11143  String *used_lengths)
11144 {
11145  char buf[64];
11146  uint length;
11147  bool first= TRUE;
11148  QUICK_RANGE_SELECT *quick;
11149  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
11150  while ((quick= it++))
11151  {
11152  KEY *key_info= head->key_info + quick->index;
11153  if (first)
11154  first= FALSE;
11155  else
11156  {
11157  key_names->append(',');
11158  used_lengths->append(',');
11159  }
11160  key_names->append(key_info->name);
11161  length= longlong2str(quick->max_used_key_length, buf, 10) - buf;
11162  used_lengths->append(buf, length);
11163  }
11164 
11165  if (cpk_quick)
11166  {
11167  KEY *key_info= head->key_info + cpk_quick->index;
11168  key_names->append(',');
11169  key_names->append(key_info->name);
11170  length= longlong2str(cpk_quick->max_used_key_length, buf, 10) - buf;
11171  used_lengths->append(',');
11172  used_lengths->append(buf, length);
11173  }
11174 }
11175 
11176 void QUICK_ROR_UNION_SELECT::add_keys_and_lengths(String *key_names,
11177  String *used_lengths)
11178 {
11179  bool first= TRUE;
11180  QUICK_SELECT_I *quick;
11181  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
11182  while ((quick= it++))
11183  {
11184  if (first)
11185  first= FALSE;
11186  else
11187  {
11188  used_lengths->append(',');
11189  key_names->append(',');
11190  }
11191  quick->add_keys_and_lengths(key_names, used_lengths);
11192  }
11193 }
11194 
11195 
11196 /*******************************************************************************
11197 * Implementation of QUICK_GROUP_MIN_MAX_SELECT
11198 *******************************************************************************/
11199 
11200 static inline uint get_field_keypart(KEY *index, Field *field);
11201 static inline SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree,
11202  PARAM *param, uint *param_idx);
11203 static bool get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
11204  KEY_PART_INFO *first_non_group_part,
11205  KEY_PART_INFO *min_max_arg_part,
11206  KEY_PART_INFO *last_part, THD *thd,
11207  uchar *key_infix, uint *key_infix_len,
11208  KEY_PART_INFO **first_non_infix_part);
11209 static bool
11210 check_group_min_max_predicates(Item *cond, Item_field *min_max_arg_item,
11211  Field::imagetype image_type);
11212 
11213 static void
11214 cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
11215  uint group_key_parts, SEL_TREE *range_tree,
11216  SEL_ARG *index_tree, ha_rows quick_prefix_records,
11217  bool have_min, bool have_max,
11218  double *read_cost, ha_rows *records);
11219 
11220 
11350 static TRP_GROUP_MIN_MAX *
11351 get_best_group_min_max(PARAM *param, SEL_TREE *tree, double read_time)
11352 {
11353  THD *thd= param->thd;
11354  JOIN *join= thd->lex->current_select->join;
11355  TABLE *table= param->table;
11356  bool have_min= FALSE; /* TRUE if there is a MIN function. */
11357  bool have_max= FALSE; /* TRUE if there is a MAX function. */
11358  Item_field *min_max_arg_item= NULL; // The argument of all MIN/MAX functions
11359  KEY_PART_INFO *min_max_arg_part= NULL; /* The corresponding keypart. */
11360  uint group_prefix_len= 0; /* Length (in bytes) of the key prefix. */
11361  KEY *index_info= NULL; /* The index chosen for data access. */
11362  uint index= 0; /* The id of the chosen index. */
11363  uint group_key_parts= 0; // Number of index key parts in the group prefix.
11364  uint used_key_parts= 0; /* Number of index key parts used for access. */
11365  uchar key_infix[MAX_KEY_LENGTH]; /* Constants from equality predicates.*/
11366  uint key_infix_len= 0; /* Length of key_infix. */
11367  TRP_GROUP_MIN_MAX *read_plan= NULL; /* The eventually constructed TRP. */
11368  uint key_part_nr;
11369  ORDER *tmp_group;
11370  Item *item;
11371  Item_field *item_field;
11372  bool is_agg_distinct;
11373  List<Item_field> agg_distinct_flds;
11374  /* Cost-related variables for the best index so far. */
11375  double best_read_cost= DBL_MAX;
11376  ha_rows best_records= 0;
11377  SEL_ARG *best_index_tree= NULL;
11378  ha_rows best_quick_prefix_records= 0;
11379  uint best_param_idx= 0;
11380  List_iterator<Item> select_items_it;
11381  Opt_trace_context * const trace= &param->thd->opt_trace;
11382 
11383  DBUG_ENTER("get_best_group_min_max");
11384 
11385  Opt_trace_object trace_group(trace, "group_index_range",
11386  Opt_trace_context::RANGE_OPTIMIZER);
11387  const char* cause= NULL;
11388 
11389  /* Perform few 'cheap' tests whether this access method is applicable. */
11390  if (!join)
11391  cause= "no_join";
11392  else if (join->primary_tables != 1) /* Query must reference one table. */
11393  cause= "not_single_table";
11394  else if (join->select_lex->olap == ROLLUP_TYPE) /* Check (B3) for ROLLUP */
11395  cause= "rollup";
11396  else if (table->s->keys == 0) /* There are no indexes to use. */
11397  cause= "no_index";
11398  else if (param->order_direction == ORDER::ORDER_DESC)
11399  cause= "cannot_do_reverse_ordering";
11400  if (cause != NULL)
11401  {
11402  trace_group.add("chosen", false).add_alnum("cause", cause);
11403  DBUG_RETURN(NULL);
11404  }
11405 
11406  /* Check (SA1,SA4) and store the only MIN/MAX argument - the C attribute.*/
11407  is_agg_distinct = is_indexed_agg_distinct(join, &agg_distinct_flds);
11408 
11409  if ((!join->group_list) && /* Neither GROUP BY nor a DISTINCT query. */
11410  (!join->select_distinct) &&
11411  !is_agg_distinct)
11412  {
11413  trace_group.add("chosen", false).
11414  add_alnum("cause", "not_group_by_or_distinct");
11415  DBUG_RETURN(NULL);
11416  }
11417  /* Analyze the query in more detail. */
11418 
11419  if (join->sum_funcs[0])
11420  {
11421  Item_sum *min_max_item;
11422  Item_sum **func_ptr= join->sum_funcs;
11423  while ((min_max_item= *(func_ptr++)))
11424  {
11425  if (min_max_item->sum_func() == Item_sum::MIN_FUNC)
11426  have_min= TRUE;
11427  else if (min_max_item->sum_func() == Item_sum::MAX_FUNC)
11428  have_max= TRUE;
11429  else if (is_agg_distinct &&
11430  (min_max_item->sum_func() == Item_sum::COUNT_DISTINCT_FUNC ||
11431  min_max_item->sum_func() == Item_sum::SUM_DISTINCT_FUNC ||
11432  min_max_item->sum_func() == Item_sum::AVG_DISTINCT_FUNC))
11433  continue;
11434  else
11435  {
11436  trace_group.add("chosen", false).
11437  add_alnum("cause", "not_applicable_aggregate_function");
11438  DBUG_RETURN(NULL);
11439  }
11440 
11441  /* The argument of MIN/MAX. */
11442  Item *expr= min_max_item->get_arg(0)->real_item();
11443  if (expr->type() == Item::FIELD_ITEM) /* Is it an attribute? */
11444  {
11445  if (! min_max_arg_item)
11446  min_max_arg_item= (Item_field*) expr;
11447  else if (! min_max_arg_item->eq(expr, 1))
11448  DBUG_RETURN(NULL);
11449  }
11450  else
11451  DBUG_RETURN(NULL);
11452  }
11453  }
11454 
11455  select_items_it= List_iterator<Item>(join->fields_list);
11456  /* Check (SA5). */
11457  if (join->select_distinct)
11458  {
11459  trace_group.add("distinct_query", true);
11460  while ((item= select_items_it++))
11461  {
11462  if (item->real_item()->type() != Item::FIELD_ITEM)
11463  DBUG_RETURN(NULL);
11464  }
11465  }
11466 
11467  /* Check (GA4) - that there are no expressions among the group attributes. */
11468  for (tmp_group= join->group_list; tmp_group; tmp_group= tmp_group->next)
11469  {
11470  if ((*tmp_group->item)->real_item()->type() != Item::FIELD_ITEM)
11471  {
11472  trace_group.add("chosen", false).
11473  add_alnum("cause", "group_field_is_expression");
11474  DBUG_RETURN(NULL);
11475  }
11476  }
11477 
11478  /*
11479  Check that table has at least one compound index such that the conditions
11480  (GA1,GA2) are all TRUE. If there is more than one such index, select the
11481  first one. Here we set the variables: group_prefix_len and index_info.
11482  */
11483 
11484  const uint pk= param->table->s->primary_key;
11485  KEY *cur_index_info= table->key_info;
11486  KEY *cur_index_info_end= cur_index_info + table->s->keys;
11487  SEL_ARG *cur_index_tree= NULL;
11488  ha_rows cur_quick_prefix_records= 0;
11489  uint cur_param_idx= MAX_KEY;
11490  Opt_trace_array trace_indices(trace, "potential_group_range_indices");
11491  for (uint cur_index= 0 ; cur_index_info != cur_index_info_end ;
11492  cur_index_info++, cur_index++)
11493  {
11494  Opt_trace_object trace_idx(trace);
11495  trace_idx.add_utf8("index", cur_index_info->name);
11496  KEY_PART_INFO *cur_part;
11497  KEY_PART_INFO *end_part; /* Last part for loops. */
11498  /* Last index part. */
11499  KEY_PART_INFO *last_part;
11500  KEY_PART_INFO *first_non_group_part;
11501  KEY_PART_INFO *first_non_infix_part;
11502  uint key_infix_parts;
11503  uint cur_group_key_parts= 0;
11504  uint cur_group_prefix_len= 0;
11505  double cur_read_cost;
11506  ha_rows cur_records;
11507  key_map used_key_parts_map;
11508  uint max_key_part= 0;
11509  uint cur_key_infix_len= 0;
11510  uchar cur_key_infix[MAX_KEY_LENGTH];
11511  uint cur_used_key_parts;
11512 
11513  /* Check (B1) - if current index is covering. */
11514  if (!table->covering_keys.is_set(cur_index))
11515  {
11516  cause= "not_covering";
11517  goto next_index;
11518  }
11519 
11520  /*
11521  If the current storage manager is such that it appends the primary key to
11522  each index, then the above condition is insufficient to check if the
11523  index is covering. In such cases it may happen that some fields are
11524  covered by the PK index, but not by the current index. Since we can't
11525  use the concatenation of both indexes for index lookup, such an index
11526  does not qualify as covering in our case. If this is the case, below
11527  we check that all query fields are indeed covered by 'cur_index'.
11528  */
11529  if (pk < MAX_KEY && cur_index != pk &&
11530  (table->file->ha_table_flags() & HA_PRIMARY_KEY_IN_READ_INDEX))
11531  {
11532  /* For each table field */
11533  for (uint i= 0; i < table->s->fields; i++)
11534  {
11535  Field *cur_field= table->field[i];
11536  /*
11537  If the field is used in the current query ensure that it's
11538  part of 'cur_index'
11539  */
11540  if (bitmap_is_set(table->read_set, cur_field->field_index) &&
11541  !cur_field->part_of_key_not_clustered.is_set(cur_index))
11542  {
11543  cause= "not_covering";
11544  goto next_index; // Field was not part of key
11545  }
11546  }
11547  }
11548  trace_idx.add("covering", true);
11549 
11550  /*
11551  Check (GA1) for GROUP BY queries.
11552  */
11553  if (join->group_list)
11554  {
11555  cur_part= cur_index_info->key_part;
11556  end_part= cur_part + actual_key_parts(cur_index_info);
11557  /* Iterate in parallel over the GROUP list and the index parts. */
11558  for (tmp_group= join->group_list; tmp_group && (cur_part != end_part);
11559  tmp_group= tmp_group->next, cur_part++)
11560  {
11561  /*
11562  TODO:
11563  tmp_group::item is an array of Item, is it OK to consider only the
11564  first Item? If so, then why? What is the array for?
11565  */
11566  /* Above we already checked that all group items are fields. */
11567  DBUG_ASSERT((*tmp_group->item)->real_item()->type() == Item::FIELD_ITEM);
11568  Item_field *group_field= (Item_field *) (*tmp_group->item)->real_item();
11569  if (group_field->field->eq(cur_part->field))
11570  {
11571  cur_group_prefix_len+= cur_part->store_length;
11572  ++cur_group_key_parts;
11573  max_key_part= cur_part - cur_index_info->key_part + 1;
11574  used_key_parts_map.set_bit(max_key_part);
11575  }
11576  else
11577  {
11578  cause= "group_attribute_not_prefix_in_index";
11579  goto next_index;
11580  }
11581  }
11582  }
11583 
11584  /*
11585  Check (GA2) if this is a DISTINCT query.
11586  If GA2, then Store a new ORDER object in group_fields_array at the
11587  position of the key part of item_field->field. Thus we get the ORDER
11588  objects for each field ordered as the corresponding key parts.
11589  Later group_fields_array of ORDER objects is used to convert the query
11590  to a GROUP query.
11591  */
11592  if ((!join->group_list && join->select_distinct) ||
11593  is_agg_distinct)
11594  {
11595  if (!is_agg_distinct)
11596  {
11597  select_items_it.rewind();
11598  }
11599 
11600  List_iterator<Item_field> agg_distinct_flds_it (agg_distinct_flds);
11601  while (NULL !=
11602  (item= (is_agg_distinct ?
11603  (Item *) agg_distinct_flds_it++ : select_items_it++)))
11604  {
11605  /* (SA5) already checked above. */
11606  item_field= (Item_field*) item->real_item();
11607  DBUG_ASSERT(item->real_item()->type() == Item::FIELD_ITEM);
11608 
11609  /* not doing loose index scan for derived tables */
11610  if (!item_field->field)
11611  {
11612  cause= "derived_table";
11613  goto next_index;
11614  }
11615 
11616  /* Find the order of the key part in the index. */
11617  key_part_nr= get_field_keypart(cur_index_info, item_field->field);
11618  /*
11619  Check if this attribute was already present in the select list.
11620  If it was present, then its corresponding key part was alredy used.
11621  */
11622  if (used_key_parts_map.is_set(key_part_nr))
11623  continue;
11624  if (key_part_nr < 1 ||
11625  (!is_agg_distinct && key_part_nr > join->fields_list.elements))
11626  {
11627  cause= "select_attribute_not_prefix_in_index";
11628  goto next_index;
11629  }
11630  cur_part= cur_index_info->key_part + key_part_nr - 1;
11631  cur_group_prefix_len+= cur_part->store_length;
11632  used_key_parts_map.set_bit(key_part_nr);
11633  ++cur_group_key_parts;
11634  max_key_part= max(max_key_part,key_part_nr);
11635  }
11636  /*
11637  Check that used key parts forms a prefix of the index.
11638  To check this we compare bits in all_parts and cur_parts.
11639  all_parts have all bits set from 0 to (max_key_part-1).
11640  cur_parts have bits set for only used keyparts.
11641  */
11642  ulonglong all_parts, cur_parts;
11643  all_parts= (1ULL << max_key_part) - 1;
11644  cur_parts= used_key_parts_map.to_ulonglong() >> 1;
11645  if (all_parts != cur_parts)
11646  goto next_index;
11647  }
11648 
11649  /* Check (SA2). */
11650  if (min_max_arg_item)
11651  {
11652  key_part_nr= get_field_keypart(cur_index_info, min_max_arg_item->field);
11653  if (key_part_nr <= cur_group_key_parts)
11654  {
11655  cause= "aggregate_column_not_suffix_in_idx";
11656  goto next_index;
11657  }
11658  min_max_arg_part= cur_index_info->key_part + key_part_nr - 1;
11659  }
11660 
11661  /* Check (SA6) if clustered key is used. */
11662  if (is_agg_distinct && cur_index == table->s->primary_key &&
11663  table->file->primary_key_is_clustered())
11664  {
11665  cause= "primary_key_is_clustered";
11666  goto next_index;
11667  }
11668 
11669  /*
11670  Check (NGA1, NGA2) and extract a sequence of constants to be used as part
11671  of all search keys.
11672  */
11673 
11674  /*
11675  If there is MIN/MAX, each keypart between the last group part and the
11676  MIN/MAX part must participate in one equality with constants, and all
11677  keyparts after the MIN/MAX part must not be referenced in the query.
11678 
11679  If there is no MIN/MAX, the keyparts after the last group part can be
11680  referenced only in equalities with constants, and the referenced keyparts
11681  must form a sequence without any gaps that starts immediately after the
11682  last group keypart.
11683  */
11684  last_part= cur_index_info->key_part + actual_key_parts(cur_index_info);
11685  first_non_group_part=
11686  (cur_group_key_parts < actual_key_parts(cur_index_info)) ?
11687  cur_index_info->key_part + cur_group_key_parts :
11688  NULL;
11689  first_non_infix_part= min_max_arg_part ?
11690  (min_max_arg_part < last_part) ?
11691  min_max_arg_part :
11692  NULL :
11693  NULL;
11694  if (first_non_group_part &&
11695  (!min_max_arg_part || (min_max_arg_part - first_non_group_part > 0)))
11696  {
11697  if (tree)
11698  {
11699  uint dummy;
11700  SEL_ARG *index_range_tree= get_index_range_tree(cur_index, tree, param,
11701  &dummy);
11702  if (!get_constant_key_infix(cur_index_info, index_range_tree,
11703  first_non_group_part, min_max_arg_part,
11704  last_part, thd, cur_key_infix,
11705  &cur_key_infix_len,
11706  &first_non_infix_part))
11707  {
11708  cause= "nonconst_equality_gap_attribute";
11709  goto next_index;
11710  }
11711  }
11712  else if (min_max_arg_part &&
11713  (min_max_arg_part - first_non_group_part > 0))
11714  {
11715  /*
11716  There is a gap but no range tree, thus no predicates at all for the
11717  non-group keyparts.
11718  */
11719  cause= "no_nongroup_keypart_predicate";
11720  goto next_index;
11721  }
11722  else if (first_non_group_part && join->conds)
11723  {
11724  /*
11725  If there is no MIN/MAX function in the query, but some index
11726  key part is referenced in the WHERE clause, then this index
11727  cannot be used because the WHERE condition over the keypart's
11728  field cannot be 'pushed' to the index (because there is no
11729  range 'tree'), and the WHERE clause must be evaluated before
11730  GROUP BY/DISTINCT.
11731  */
11732  /*
11733  Store the first and last keyparts that need to be analyzed
11734  into one array that can be passed as parameter.
11735  */
11736  KEY_PART_INFO *key_part_range[2];
11737  key_part_range[0]= first_non_group_part;
11738  key_part_range[1]= last_part;
11739 
11740  /* Check if cur_part is referenced in the WHERE clause. */
11741  if (join->conds->walk(&Item::find_item_in_field_list_processor, 0,
11742  (uchar*) key_part_range))
11743  {
11744  cause= "keypart_reference_from_where_clause";
11745  goto next_index;
11746  }
11747  }
11748  }
11749 
11750  /*
11751  Test (WA1) partially - that no other keypart after the last infix part is
11752  referenced in the query.
11753  */
11754  if (first_non_infix_part)
11755  {
11756  cur_part= first_non_infix_part +
11757  (min_max_arg_part && (min_max_arg_part < last_part));
11758  for (; cur_part != last_part; cur_part++)
11759  {
11760  if (bitmap_is_set(table->read_set, cur_part->field->field_index))
11761  {
11762  cause= "keypart_after_infix_in_query";
11763  goto next_index;
11764  }
11765  }
11766  }
11767 
11768  /* If we got to this point, cur_index_info passes the test. */
11769  key_infix_parts= cur_key_infix_len ? (uint)
11770  (first_non_infix_part - first_non_group_part) : 0;
11771  cur_used_key_parts= cur_group_key_parts + key_infix_parts;
11772 
11773  /* Compute the cost of using this index. */
11774  if (tree)
11775  {
11776  /* Find the SEL_ARG sub-tree that corresponds to the chosen index. */
11777  cur_index_tree= get_index_range_tree(cur_index, tree, param,
11778  &cur_param_idx);
11779  /* Check if this range tree can be used for prefix retrieval. */
11780  Cost_estimate dummy_cost;
11781  uint mrr_flags= HA_MRR_SORTED;
11782  uint mrr_bufsize=0;
11783  cur_quick_prefix_records= check_quick_select(param, cur_param_idx,
11784  FALSE /*don't care*/,
11785  cur_index_tree, TRUE,
11786  &mrr_flags, &mrr_bufsize,
11787  &dummy_cost);
11788 #ifdef OPTIMIZER_TRACE
11789  if (unlikely(cur_index_tree && trace->is_started()))
11790  {
11791  trace_idx.add("index_dives_for_eq_ranges", !param->use_index_statistics);
11792  Opt_trace_array trace_range(trace, "ranges");
11793 
11794  const KEY_PART_INFO *key_part= cur_index_info->key_part;
11795 
11796  String range_info;
11797  range_info.set_charset(system_charset_info);
11798  append_range_all_keyparts(&trace_range, NULL, &range_info,
11799  cur_index_tree, key_part);
11800  }
11801 #endif
11802  }
11803  cost_group_min_max(table, cur_index_info, cur_used_key_parts,
11804  cur_group_key_parts, tree, cur_index_tree,
11805  cur_quick_prefix_records, have_min, have_max,
11806  &cur_read_cost, &cur_records);
11807  /*
11808  If cur_read_cost is lower than best_read_cost use cur_index.
11809  Do not compare doubles directly because they may have different
11810  representations (64 vs. 80 bits).
11811  */
11812  trace_idx.add("rows", cur_records).add("cost", cur_read_cost);
11813  if (cur_read_cost < best_read_cost - (DBL_EPSILON * cur_read_cost))
11814  {
11815  index_info= cur_index_info;
11816  index= cur_index;
11817  best_read_cost= cur_read_cost;
11818  best_records= cur_records;
11819  best_index_tree= cur_index_tree;
11820  best_quick_prefix_records= cur_quick_prefix_records;
11821  best_param_idx= cur_param_idx;
11822  group_key_parts= cur_group_key_parts;
11823  group_prefix_len= cur_group_prefix_len;
11824  key_infix_len= cur_key_infix_len;
11825  if (key_infix_len)
11826  memcpy (key_infix, cur_key_infix, sizeof (key_infix));
11827  used_key_parts= cur_used_key_parts;
11828  }
11829 
11830  next_index:
11831  if (cause)
11832  {
11833  trace_idx.add("usable", false).add_alnum("cause", cause);
11834  cause= NULL;
11835  }
11836  }
11837  trace_indices.end();
11838 
11839  if (!index_info) /* No usable index found. */
11840  DBUG_RETURN(NULL);
11841 
11842  /* Check (SA3) for the where clause. */
11843  if (join->conds && min_max_arg_item &&
11844  !check_group_min_max_predicates(join->conds, min_max_arg_item,
11845  (index_info->flags & HA_SPATIAL) ?
11846  Field::itMBR : Field::itRAW))
11847  {
11848  trace_group.add("usable", false).
11849  add_alnum("cause", "unsupported_predicate_on_agg_attribute");
11850  DBUG_RETURN(NULL);
11851  }
11852 
11853  /* The query passes all tests, so construct a new TRP object. */
11854  read_plan= new (param->mem_root)
11855  TRP_GROUP_MIN_MAX(have_min, have_max, is_agg_distinct,
11856  min_max_arg_part,
11857  group_prefix_len, used_key_parts,
11858  group_key_parts, index_info, index,
11859  key_infix_len,
11860  (key_infix_len > 0) ? key_infix : NULL,
11861  tree, best_index_tree, best_param_idx,
11862  best_quick_prefix_records);
11863  if (read_plan)
11864  {
11865  if (tree && read_plan->quick_prefix_records == 0)
11866  DBUG_RETURN(NULL);
11867 
11868  read_plan->read_cost= best_read_cost;
11869  read_plan->records= best_records;
11870  if (read_time < best_read_cost && is_agg_distinct)
11871  {
11872  trace_group.add("index_scan", true);
11873  read_plan->read_cost= 0;
11874  read_plan->use_index_scan();
11875  }
11876 
11877  DBUG_PRINT("info",
11878  ("Returning group min/max plan: cost: %g, records: %lu",
11879  read_plan->read_cost, (ulong) read_plan->records));
11880  }
11881 
11882  DBUG_RETURN(read_plan);
11883 }
11884 
11885 
11886 /*
11887  Check that the MIN/MAX attribute participates only in range predicates
11888  with constants.
11889 
11890  SYNOPSIS
11891  check_group_min_max_predicates()
11892  cond tree (or subtree) describing all or part of the WHERE
11893  clause being analyzed
11894  min_max_arg_item the field referenced by the MIN/MAX function(s)
11895  min_max_arg_part the keypart of the MIN/MAX argument if any
11896 
11897  DESCRIPTION
11898  The function walks recursively over the cond tree representing a WHERE
11899  clause, and checks condition (SA3) - if a field is referenced by a MIN/MAX
11900  aggregate function, it is referenced only by one of the following
11901  predicates: {=, !=, <, <=, >, >=, between, is null, is not null}.
11902 
11903  RETURN
11904  TRUE if cond passes the test
11905  FALSE o/w
11906 */
11907 
11908 static bool
11909 check_group_min_max_predicates(Item *cond, Item_field *min_max_arg_item,
11910  Field::imagetype image_type)
11911 {
11912  DBUG_ENTER("check_group_min_max_predicates");
11913  DBUG_ASSERT(cond && min_max_arg_item);
11914 
11915  cond= cond->real_item();
11916  Item::Type cond_type= cond->type();
11917  if (cond_type == Item::COND_ITEM) /* 'AND' or 'OR' */
11918  {
11919  DBUG_PRINT("info", ("Analyzing: %s", ((Item_func*) cond)->func_name()));
11920  List_iterator_fast<Item> li(*((Item_cond*) cond)->argument_list());
11921  Item *and_or_arg;
11922  while ((and_or_arg= li++))
11923  {
11924  if (!check_group_min_max_predicates(and_or_arg, min_max_arg_item,
11925  image_type))
11926  DBUG_RETURN(FALSE);
11927  }
11928  DBUG_RETURN(TRUE);
11929  }
11930 
11931  /*
11932  TODO:
11933  This is a very crude fix to handle sub-selects in the WHERE clause
11934  (Item_subselect objects). With the test below we rule out from the
11935  optimization all queries with subselects in the WHERE clause. What has to
11936  be done, is that here we should analyze whether the subselect references
11937  the MIN/MAX argument field, and disallow the optimization only if this is
11938  so.
11939  */
11940  if (cond_type == Item::SUBSELECT_ITEM)
11941  DBUG_RETURN(FALSE);
11942 
11943  /*
11944  Condition of the form 'field' is equivalent to 'field <> 0' and thus
11945  satisfies the SA3 condition.
11946  */
11947  if (cond_type == Item::FIELD_ITEM)
11948  {
11949  DBUG_PRINT("info", ("Analyzing: %s", cond->full_name()));
11950  DBUG_RETURN(TRUE);
11951  }
11952 
11953  /* We presume that at this point there are no other Items than functions. */
11954  DBUG_ASSERT(cond_type == Item::FUNC_ITEM);
11955 
11956  /* Test if cond references only group-by or non-group fields. */
11957  Item_func *pred= (Item_func*) cond;
11958  Item *cur_arg;
11959  DBUG_PRINT("info", ("Analyzing: %s", pred->func_name()));
11960  for (uint arg_idx= 0; arg_idx < pred->argument_count (); arg_idx++)
11961  {
11962  Item **arguments= pred->arguments();
11963  cur_arg= arguments[arg_idx]->real_item();
11964  DBUG_PRINT("info", ("cur_arg: %s", cur_arg->full_name()));
11965  if (cur_arg->type() == Item::FIELD_ITEM)
11966  {
11967  if (min_max_arg_item->eq(cur_arg, 1))
11968  {
11969  /*
11970  If pred references the MIN/MAX argument, check whether pred is a range
11971  condition that compares the MIN/MAX argument with a constant.
11972  */
11973  Item_func::Functype pred_type= pred->functype();
11974  if (pred_type != Item_func::EQUAL_FUNC &&
11975  pred_type != Item_func::LT_FUNC &&
11976  pred_type != Item_func::LE_FUNC &&
11977  pred_type != Item_func::GT_FUNC &&
11978  pred_type != Item_func::GE_FUNC &&
11979  pred_type != Item_func::BETWEEN &&
11980  pred_type != Item_func::ISNULL_FUNC &&
11981  pred_type != Item_func::ISNOTNULL_FUNC &&
11982  pred_type != Item_func::EQ_FUNC &&
11983  pred_type != Item_func::NE_FUNC)
11984  DBUG_RETURN(FALSE);
11985 
11986  /* Check that pred compares min_max_arg_item with a constant. */
11987  Item *args[3];
11988  memset(args, 0, 3 * sizeof(Item*));
11989  bool inv;
11990  /* Test if this is a comparison of a field and a constant. */
11991  if (!simple_pred(pred, args, &inv))
11992  DBUG_RETURN(FALSE);
11993 
11994  /* Check for compatible string comparisons - similar to get_mm_leaf. */
11995  if (args[0] && args[1] && !args[2] && // this is a binary function
11996  min_max_arg_item->result_type() == STRING_RESULT &&
11997  /*
11998  Don't use an index when comparing strings of different collations.
11999  */
12000  ((args[1]->result_type() == STRING_RESULT &&
12001  image_type == Field::itRAW &&
12002  min_max_arg_item->field->charset() != pred->compare_collation())
12003  ||
12004  /*
12005  We can't always use indexes when comparing a string index to a
12006  number.
12007  */
12008  (args[1]->result_type() != STRING_RESULT &&
12009  min_max_arg_item->field->cmp_type() != args[1]->result_type())))
12010  DBUG_RETURN(FALSE);
12011  }
12012  }
12013  else if (cur_arg->type() == Item::FUNC_ITEM)
12014  {
12015  if (!check_group_min_max_predicates(cur_arg, min_max_arg_item,
12016  image_type))
12017  DBUG_RETURN(FALSE);
12018  }
12019  else if (cur_arg->const_item())
12020  {
12021  /*
12022  For predicates of the form "const OP expr" we also have to check 'expr'
12023  to make a decision.
12024  */
12025  continue;
12026  }
12027  else
12028  DBUG_RETURN(FALSE);
12029  }
12030 
12031  DBUG_RETURN(TRUE);
12032 }
12033 
12034 
12035 /*
12036  Get SEL_ARG tree, if any, for the keypart covering non grouping
12037  attribute (NGA) field 'nga_field'.
12038 
12039  This function enforces the NGA3 test: If 'keypart_tree' contains a
12040  condition for 'nga_field', there can only be one range. In the
12041  opposite case, this function returns with error and 'cur_range'
12042  should not be used.
12043 
12044  Note that the NGA1 and NGA2 requirements, like whether or not the
12045  range predicate for 'nga_field' is equality, is not tested by this
12046  function.
12047 
12048  @param[in] nga_field The NGA field we want the SEL_ARG tree for
12049  @param[in] keypart_tree Root node of the SEL_ARG* tree for the index
12050  @param[out] cur_range The SEL_ARG tree, if any, for the keypart
12051  covering field 'keypart_field'
12052  @retval true 'keypart_tree' contained a predicate for 'nga_field' but
12053  multiple ranges exists. 'cur_range' should not be used.
12054  @retval false otherwise
12055 */
12056 
12057 static bool
12058 get_sel_arg_for_keypart(Field *nga_field,
12059  SEL_ARG *keypart_tree,
12060  SEL_ARG **cur_range)
12061 {
12062  if(keypart_tree == NULL)
12063  return false;
12064  if(keypart_tree->type != SEL_ARG::KEY_RANGE)
12065  {
12066  /*
12067  A range predicate not usable by Loose Index Scan is found.
12068  Predicates for keypart 'keypart_tree->part' and later keyparts
12069  cannot be used.
12070  */
12071  *cur_range= keypart_tree;
12072  return false;
12073  }
12074  if(keypart_tree->field->eq(nga_field))
12075  {
12076  /*
12077  Enforce NGA3: If a condition for nga_field has been found, only
12078  a single range is allowed.
12079  */
12080  if (keypart_tree->prev || keypart_tree->next)
12081  return true; // There are multiple ranges
12082 
12083  *cur_range= keypart_tree;
12084  return false;
12085  }
12086 
12087  SEL_ARG *found_tree= NULL;
12088  SEL_ARG *first_kp= keypart_tree->first();
12089 
12090  for (SEL_ARG *cur_kp= first_kp; cur_kp && !found_tree;
12091  cur_kp= cur_kp->next)
12092  {
12093  if (cur_kp->next_key_part)
12094  {
12095  if (get_sel_arg_for_keypart(nga_field,
12096  cur_kp->next_key_part,
12097  &found_tree))
12098  return true;
12099 
12100  }
12101  /*
12102  Enforce NGA3: If a condition for nga_field has been found,only
12103  a single range is allowed.
12104  */
12105  if (found_tree && found_tree->type == SEL_ARG::KEY_RANGE && first_kp->next)
12106  return true; // There are multiple ranges
12107  }
12108  *cur_range= found_tree;
12109  return false;
12110 }
12111 
12112 
12113 /*
12114  Extract a sequence of constants from a conjunction of equality predicates.
12115 
12116  SYNOPSIS
12117  get_constant_key_infix()
12118  index_info [in] Descriptor of the chosen index.
12119  index_range_tree [in] Range tree for the chosen index
12120  first_non_group_part [in] First index part after group attribute parts
12121  min_max_arg_part [in] The keypart of the MIN/MAX argument if any
12122  last_part [in] Last keypart of the index
12123  thd [in] Current thread
12124  key_infix [out] Infix of constants to be used for index lookup
12125  key_infix_len [out] Lenghth of the infix
12126  first_non_infix_part [out] The first keypart after the infix (if any)
12127 
12128  DESCRIPTION
12129  Test conditions (NGA1, NGA2) from get_best_group_min_max(). Namely,
12130  for each keypart field NGF_i not in GROUP-BY, check that there is a
12131  constant equality predicate among conds with the form (NGF_i = const_ci) or
12132  (const_ci = NGF_i).
12133  Thus all the NGF_i attributes must fill the 'gap' between the last group-by
12134  attribute and the MIN/MAX attribute in the index (if present). If these
12135  conditions hold, copy each constant from its corresponding predicate into
12136  key_infix, in the order its NG_i attribute appears in the index, and update
12137  key_infix_len with the total length of the key parts in key_infix.
12138 
12139  RETURN
12140  TRUE if the index passes the test
12141  FALSE o/w
12142 */
12143 
12144 static bool
12145 get_constant_key_infix(KEY *index_info, SEL_ARG *index_range_tree,
12146  KEY_PART_INFO *first_non_group_part,
12147  KEY_PART_INFO *min_max_arg_part,
12148  KEY_PART_INFO *last_part, THD *thd,
12149  uchar *key_infix, uint *key_infix_len,
12150  KEY_PART_INFO **first_non_infix_part)
12151 {
12152  SEL_ARG *cur_range;
12153  KEY_PART_INFO *cur_part;
12154  /* End part for the first loop below. */
12155  KEY_PART_INFO *end_part= min_max_arg_part ? min_max_arg_part : last_part;
12156 
12157  *key_infix_len= 0;
12158  uchar *key_ptr= key_infix;
12159  for (cur_part= first_non_group_part; cur_part != end_part; cur_part++)
12160  {
12161  cur_range= NULL;
12162  /*
12163  Find the range tree for the current keypart. We assume that
12164  index_range_tree points to the leftmost keypart in the index.
12165  */
12166  if(get_sel_arg_for_keypart(cur_part->field, index_range_tree, &cur_range))
12167  return false;
12168 
12169  if (!cur_range || cur_range->type != SEL_ARG::KEY_RANGE)
12170  {
12171  if (min_max_arg_part)
12172  return FALSE; /* The current keypart has no range predicates at all. */
12173  else
12174  {
12175  *first_non_infix_part= cur_part;
12176  return TRUE;
12177  }
12178  }
12179 
12180  if ((cur_range->min_flag & NO_MIN_RANGE) ||
12181  (cur_range->max_flag & NO_MAX_RANGE) ||
12182  (cur_range->min_flag & NEAR_MIN) || (cur_range->max_flag & NEAR_MAX))
12183  return FALSE;
12184 
12185  uint field_length= cur_part->store_length;
12186  if (cur_range->maybe_null &&
12187  cur_range->min_value[0] && cur_range->max_value[0])
12188  {
12189  /*
12190  cur_range specifies 'IS NULL'. In this case the argument points
12191  to a "null value" (a copy of is_null_string) that we do not
12192  memcmp(), or memcpy to a field.
12193  */
12194  DBUG_ASSERT (field_length > 0);
12195  *key_ptr= 1;
12196  key_ptr+= field_length;
12197  *key_infix_len+= field_length;
12198  }
12199  else if (memcmp(cur_range->min_value, cur_range->max_value, field_length) == 0)
12200  { /* cur_range specifies an equality condition. */
12201  memcpy(key_ptr, cur_range->min_value, field_length);
12202  key_ptr+= field_length;
12203  *key_infix_len+= field_length;
12204  }
12205  else
12206  return FALSE;
12207  }
12208 
12209  if (!min_max_arg_part && (cur_part == last_part))
12210  *first_non_infix_part= last_part;
12211 
12212  return TRUE;
12213 }
12214 
12215 
12216 /*
12217  Find the key part referenced by a field.
12218 
12219  SYNOPSIS
12220  get_field_keypart()
12221  index descriptor of an index
12222  field field that possibly references some key part in index
12223 
12224  NOTES
12225  The return value can be used to get a KEY_PART_INFO pointer by
12226  part= index->key_part + get_field_keypart(...) - 1;
12227 
12228  RETURN
12229  Positive number which is the consecutive number of the key part, or
12230  0 if field does not reference any index field.
12231 */
12232 
12233 static inline uint
12234 get_field_keypart(KEY *index, Field *field)
12235 {
12236  KEY_PART_INFO *part, *end;
12237 
12238  for (part= index->key_part, end= part + actual_key_parts(index) ;
12239  part < end; part++)
12240  {
12241  if (field->eq(part->field))
12242  return part - index->key_part + 1;
12243  }
12244  return 0;
12245 }
12246 
12247 
12248 /*
12249  Find the SEL_ARG sub-tree that corresponds to the chosen index.
12250 
12251  SYNOPSIS
12252  get_index_range_tree()
12253  index [in] The ID of the index being looked for
12254  range_tree[in] Tree of ranges being searched
12255  param [in] PARAM from SQL_SELECT::test_quick_select
12256  param_idx [out] Index in the array PARAM::key that corresponds to 'index'
12257 
12258  DESCRIPTION
12259 
12260  A SEL_TREE contains range trees for all usable indexes. This procedure
12261  finds the SEL_ARG sub-tree for 'index'. The members of a SEL_TREE are
12262  ordered in the same way as the members of PARAM::key, thus we first find
12263  the corresponding index in the array PARAM::key. This index is returned
12264  through the variable param_idx, to be used later as argument of
12265  check_quick_select().
12266 
12267  RETURN
12268  Pointer to the SEL_ARG subtree that corresponds to index.
12269 */
12270 
12271 SEL_ARG * get_index_range_tree(uint index, SEL_TREE* range_tree, PARAM *param,
12272  uint *param_idx)
12273 {
12274  uint idx= 0; /* Index nr in param->key_parts */
12275  while (idx < param->keys)
12276  {
12277  if (index == param->real_keynr[idx])
12278  break;
12279  idx++;
12280  }
12281  *param_idx= idx;
12282  return(range_tree->keys[idx]);
12283 }
12284 
12285 
12286 /*
12287  Compute the cost of a quick_group_min_max_select for a particular index.
12288 
12289  SYNOPSIS
12290  cost_group_min_max()
12291  table [in] The table being accessed
12292  index_info [in] The index used to access the table
12293  used_key_parts [in] Number of key parts used to access the index
12294  group_key_parts [in] Number of index key parts in the group prefix
12295  range_tree [in] Tree of ranges for all indexes
12296  index_tree [in] The range tree for the current index
12297  quick_prefix_records [in] Number of records retrieved by the internally
12298  used quick range select if any
12299  have_min [in] True if there is a MIN function
12300  have_max [in] True if there is a MAX function
12301  read_cost [out] The cost to retrieve rows via this quick select
12302  records [out] The number of rows retrieved
12303 
12304  DESCRIPTION
12305  This method computes the access cost of a TRP_GROUP_MIN_MAX instance and
12306  the number of rows returned.
12307 
12308  NOTES
12309  The cost computation distinguishes several cases:
12310  1) No equality predicates over non-group attributes (thus no key_infix).
12311  If groups are bigger than blocks on the average, then we assume that it
12312  is very unlikely that block ends are aligned with group ends, thus even
12313  if we look for both MIN and MAX keys, all pairs of neighbor MIN/MAX
12314  keys, except for the first MIN and the last MAX keys, will be in the
12315  same block. If groups are smaller than blocks, then we are going to
12316  read all blocks.
12317  2) There are equality predicates over non-group attributes.
12318  In this case the group prefix is extended by additional constants, and
12319  as a result the min/max values are inside sub-groups of the original
12320  groups. The number of blocks that will be read depends on whether the
12321  ends of these sub-groups will be contained in the same or in different
12322  blocks. We compute the probability for the two ends of a subgroup to be
12323  in two different blocks as the ratio of:
12324  - the number of positions of the left-end of a subgroup inside a group,
12325  such that the right end of the subgroup is past the end of the buffer
12326  containing the left-end, and
12327  - the total number of possible positions for the left-end of the
12328  subgroup, which is the number of keys in the containing group.
12329  We assume it is very unlikely that two ends of subsequent subgroups are
12330  in the same block.
12331  3) The are range predicates over the group attributes.
12332  Then some groups may be filtered by the range predicates. We use the
12333  selectivity of the range predicates to decide how many groups will be
12334  filtered.
12335 
12336  TODO
12337  - Take into account the optional range predicates over the MIN/MAX
12338  argument.
12339  - Check if we have a PK index and we use all cols - then each key is a
12340  group, and it will be better to use an index scan.
12341 
12342  RETURN
12343  None
12344 */
12345 
12346 void cost_group_min_max(TABLE* table, KEY *index_info, uint used_key_parts,
12347  uint group_key_parts, SEL_TREE *range_tree,
12348  SEL_ARG *index_tree, ha_rows quick_prefix_records,
12349  bool have_min, bool have_max,
12350  double *read_cost, ha_rows *records)
12351 {
12352  ha_rows table_records;
12353  uint num_groups;
12354  uint num_blocks;
12355  uint keys_per_block;
12356  uint keys_per_group;
12357  uint keys_per_subgroup; /* Average number of keys in sub-groups */
12358  /* formed by a key infix. */
12359  double p_overlap; /* Probability that a sub-group overlaps two blocks. */
12360  double quick_prefix_selectivity;
12361  double io_cost;
12362  DBUG_ENTER("cost_group_min_max");
12363 
12364  table_records= table->file->stats.records;
12365  keys_per_block= (table->file->stats.block_size / 2 /
12366  (index_info->key_length + table->file->ref_length)
12367  + 1);
12368  num_blocks= (uint)(table_records / keys_per_block) + 1;
12369 
12370  /* Compute the number of keys in a group. */
12371  keys_per_group= index_info->rec_per_key[group_key_parts - 1];
12372  if (keys_per_group == 0) /* If there is no statistics try to guess */
12373  /* each group contains 10% of all records */
12374  keys_per_group= (uint)(table_records / 10) + 1;
12375  num_groups= (uint)(table_records / keys_per_group) + 1;
12376 
12377  /* Apply the selectivity of the quick select for group prefixes. */
12378  if (range_tree && (quick_prefix_records != HA_POS_ERROR))
12379  {
12380  quick_prefix_selectivity= (double) quick_prefix_records /
12381  (double) table_records;
12382  num_groups= (uint) rint(num_groups * quick_prefix_selectivity);
12383  set_if_bigger(num_groups, 1);
12384  }
12385 
12386  if (used_key_parts > group_key_parts)
12387  { /*
12388  Compute the probability that two ends of a subgroup are inside
12389  different blocks.
12390  */
12391  keys_per_subgroup= index_info->rec_per_key[used_key_parts - 1];
12392  if (keys_per_subgroup >= keys_per_block) /* If a subgroup is bigger than */
12393  p_overlap= 1.0; /* a block, it will overlap at least two blocks. */
12394  else
12395  {
12396  double blocks_per_group= (double) num_blocks / (double) num_groups;
12397  p_overlap= (blocks_per_group * (keys_per_subgroup - 1)) / keys_per_group;
12398  p_overlap= min(p_overlap, 1.0);
12399  }
12400  io_cost= min<double>(num_groups * (1 + p_overlap), num_blocks);
12401  }
12402  else
12403  io_cost= (keys_per_group > keys_per_block) ?
12404  (have_min && have_max) ? (double) (num_groups + 1) :
12405  (double) num_groups :
12406  (double) num_blocks;
12407 
12408  /*
12409  CPU cost must be comparable to that of an index scan as computed
12410  in SQL_SELECT::test_quick_select(). When the groups are small,
12411  e.g. for a unique index, using index scan will be cheaper since it
12412  reads the next record without having to re-position to it on every
12413  group. To make the CPU cost reflect this, we estimate the CPU cost
12414  as the sum of:
12415  1. Cost for evaluating the condition (similarly as for index scan).
12416  2. Cost for navigating the index structure (assuming a b-tree).
12417  Note: We only add the cost for one comparision per block. For a
12418  b-tree the number of comparisons will be larger.
12419  TODO: This cost should be provided by the storage engine.
12420  */
12421  const double tree_traversal_cost=
12422  ceil(log(static_cast<double>(table_records))/
12423  log(static_cast<double>(keys_per_block))) * ROWID_COMPARE_COST;
12424 
12425  const double cpu_cost= num_groups * (tree_traversal_cost + ROW_EVALUATE_COST);
12426 
12427  *read_cost= io_cost + cpu_cost;
12428  *records= num_groups;
12429 
12430  DBUG_PRINT("info",
12431  ("table rows: %lu keys/block: %u keys/group: %u result rows: %lu blocks: %u",
12432  (ulong)table_records, keys_per_block, keys_per_group,
12433  (ulong) *records, num_blocks));
12434  DBUG_VOID_RETURN;
12435 }
12436 
12437 
12438 /*
12439  Construct a new quick select object for queries with group by with min/max.
12440 
12441  SYNOPSIS
12442  TRP_GROUP_MIN_MAX::make_quick()
12443  param Parameter from test_quick_select
12444  retrieve_full_rows ignored
12445  parent_alloc Memory pool to use, if any.
12446 
12447  NOTES
12448  Make_quick ignores the retrieve_full_rows parameter because
12449  QUICK_GROUP_MIN_MAX_SELECT always performs 'index only' scans.
12450  The other parameter are ignored as well because all necessary
12451  data to create the QUICK object is computed at this TRP creation
12452  time.
12453 
12454  RETURN
12455  New QUICK_GROUP_MIN_MAX_SELECT object if successfully created,
12456  NULL otherwise.
12457 */
12458 
12460 TRP_GROUP_MIN_MAX::make_quick(PARAM *param, bool retrieve_full_rows,
12461  MEM_ROOT *parent_alloc)
12462 {
12464  DBUG_ENTER("TRP_GROUP_MIN_MAX::make_quick");
12465 
12466  quick= new QUICK_GROUP_MIN_MAX_SELECT(param->table,
12467  param->thd->lex->current_select->join,
12468  have_min, have_max,
12469  have_agg_distinct, min_max_arg_part,
12470  group_prefix_len, group_key_parts,
12471  used_key_parts, index_info, index,
12472  read_cost, records, key_infix_len,
12473  key_infix, parent_alloc, is_index_scan);
12474  if (!quick)
12475  DBUG_RETURN(NULL);
12476 
12477  if (quick->init())
12478  {
12479  delete quick;
12480  DBUG_RETURN(NULL);
12481  }
12482 
12483  if (range_tree)
12484  {
12485  DBUG_ASSERT(quick_prefix_records > 0);
12486  if (quick_prefix_records == HA_POS_ERROR)
12487  quick->quick_prefix_select= NULL; /* Can't construct a quick select. */
12488  else
12489  {
12490  /* Make a QUICK_RANGE_SELECT to be used for group prefix retrieval. */
12491  quick->quick_prefix_select= get_quick_select(param, param_idx,
12492  index_tree,
12493  HA_MRR_SORTED,
12494  0,
12495  &quick->alloc);
12496  if (!quick->quick_prefix_select)
12497  {
12498  delete quick;
12499  DBUG_RETURN(NULL);
12500  }
12501  }
12502  /*
12503  Extract the SEL_ARG subtree that contains only ranges for the MIN/MAX
12504  attribute, and create an array of QUICK_RANGES to be used by the
12505  new quick select.
12506  */
12507  if (min_max_arg_part)
12508  {
12509  SEL_ARG *min_max_range= index_tree;
12510  while (min_max_range) /* Find the tree for the MIN/MAX key part. */
12511  {
12512  if (min_max_range->field->eq(min_max_arg_part->field))
12513  break;
12514  min_max_range= min_max_range->next_key_part;
12515  }
12516  /* Scroll to the leftmost interval for the MIN/MAX argument. */
12517  while (min_max_range && min_max_range->prev)
12518  min_max_range= min_max_range->prev;
12519  /* Create an array of QUICK_RANGEs for the MIN/MAX argument. */
12520  while (min_max_range)
12521  {
12522  if (quick->add_range(min_max_range))
12523  {
12524  delete quick;
12525  quick= NULL;
12526  DBUG_RETURN(NULL);
12527  }
12528  min_max_range= min_max_range->next;
12529  }
12530  }
12531  }
12532  else
12533  quick->quick_prefix_select= NULL;
12534 
12535  quick->update_key_stat();
12536  quick->adjust_prefix_ranges();
12537 
12538  DBUG_RETURN(quick);
12539 }
12540 
12541 
12542 /*
12543  Construct new quick select for group queries with min/max.
12544 
12545  SYNOPSIS
12546  QUICK_GROUP_MIN_MAX_SELECT::QUICK_GROUP_MIN_MAX_SELECT()
12547  table The table being accessed
12548  join Descriptor of the current query
12549  have_min TRUE if the query selects a MIN function
12550  have_max TRUE if the query selects a MAX function
12551  min_max_arg_part The only argument field of all MIN/MAX functions
12552  group_prefix_len Length of all key parts in the group prefix
12553  prefix_key_parts All key parts in the group prefix
12554  index_info The index chosen for data access
12555  use_index The id of index_info
12556  read_cost Cost of this access method
12557  records Number of records returned
12558  key_infix_len Length of the key infix appended to the group prefix
12559  key_infix Infix of constants from equality predicates
12560  parent_alloc Memory pool for this and quick_prefix_select data
12561  is_index_scan get the next different key not by jumping on it via
12562  index read, but by scanning until the end of the
12563  rows with equal key value.
12564 
12565  RETURN
12566  None
12567 */
12568 
12569 QUICK_GROUP_MIN_MAX_SELECT::
12570 QUICK_GROUP_MIN_MAX_SELECT(TABLE *table, JOIN *join_arg, bool have_min_arg,
12571  bool have_max_arg, bool have_agg_distinct_arg,
12572  KEY_PART_INFO *min_max_arg_part_arg,
12573  uint group_prefix_len_arg, uint group_key_parts_arg,
12574  uint used_key_parts_arg, KEY *index_info_arg,
12575  uint use_index, double read_cost_arg,
12576  ha_rows records_arg, uint key_infix_len_arg,
12577  uchar *key_infix_arg, MEM_ROOT *parent_alloc,
12578  bool is_index_scan_arg)
12579  :join(join_arg), index_info(index_info_arg),
12580  group_prefix_len(group_prefix_len_arg),
12581  group_key_parts(group_key_parts_arg), have_min(have_min_arg),
12582  have_max(have_max_arg), have_agg_distinct(have_agg_distinct_arg),
12583  seen_first_key(FALSE), min_max_arg_part(min_max_arg_part_arg),
12584  key_infix(key_infix_arg), key_infix_len(key_infix_len_arg),
12585  min_functions_it(NULL), max_functions_it(NULL),
12586  is_index_scan(is_index_scan_arg)
12587 {
12588  head= table;
12589  index= use_index;
12590  record= head->record[0];
12591  tmp_record= head->record[1];
12592  read_time= read_cost_arg;
12593  records= records_arg;
12594  used_key_parts= used_key_parts_arg;
12595  real_key_parts= used_key_parts_arg;
12596  real_prefix_len= group_prefix_len + key_infix_len;
12597  group_prefix= NULL;
12598  min_max_arg_len= min_max_arg_part ? min_max_arg_part->store_length : 0;
12599 
12600  /*
12601  We can't have parent_alloc set as the init function can't handle this case
12602  yet.
12603  */
12604  DBUG_ASSERT(!parent_alloc);
12605  if (!parent_alloc)
12606  {
12607  init_sql_alloc(&alloc, join->thd->variables.range_alloc_block_size, 0);
12608  join->thd->mem_root= &alloc;
12609  }
12610  else
12611  memset(&alloc, 0, sizeof(MEM_ROOT)); // ensure that it's not used
12612 }
12613 
12614 
12615 /*
12616  Do post-constructor initialization.
12617 
12618  SYNOPSIS
12619  QUICK_GROUP_MIN_MAX_SELECT::init()
12620 
12621  DESCRIPTION
12622  The method performs initialization that cannot be done in the constructor
12623  such as memory allocations that may fail. It allocates memory for the
12624  group prefix and inifix buffers, and for the lists of MIN/MAX item to be
12625  updated during execution.
12626 
12627  RETURN
12628  0 OK
12629  other Error code
12630 */
12631 
12632 int QUICK_GROUP_MIN_MAX_SELECT::init()
12633 {
12634  if (group_prefix) /* Already initialized. */
12635  return 0;
12636 
12637  if (!(last_prefix= (uchar*) alloc_root(&alloc, group_prefix_len)))
12638  return 1;
12639  /*
12640  We may use group_prefix to store keys with all select fields, so allocate
12641  enough space for it.
12642  */
12643  if (!(group_prefix= (uchar*) alloc_root(&alloc,
12644  real_prefix_len + min_max_arg_len)))
12645  return 1;
12646 
12647  if (key_infix_len > 0)
12648  {
12649  /*
12650  The memory location pointed to by key_infix will be deleted soon, so
12651  allocate a new buffer and copy the key_infix into it.
12652  */
12653  uchar *tmp_key_infix= (uchar*) alloc_root(&alloc, key_infix_len);
12654  if (!tmp_key_infix)
12655  return 1;
12656  memcpy(tmp_key_infix, this->key_infix, key_infix_len);
12657  this->key_infix= tmp_key_infix;
12658  }
12659 
12660  if (min_max_arg_part)
12661  {
12662  if (my_init_dynamic_array(&min_max_ranges, sizeof(QUICK_RANGE*), 16, 16))
12663  return 1;
12664 
12665  if (have_min)
12666  {
12667  if (!(min_functions= new List<Item_sum>))
12668  return 1;
12669  }
12670  else
12671  min_functions= NULL;
12672  if (have_max)
12673  {
12674  if (!(max_functions= new List<Item_sum>))
12675  return 1;
12676  }
12677  else
12678  max_functions= NULL;
12679 
12680  Item_sum *min_max_item;
12681  Item_sum **func_ptr= join->sum_funcs;
12682  while ((min_max_item= *(func_ptr++)))
12683  {
12684  if (have_min && (min_max_item->sum_func() == Item_sum::MIN_FUNC))
12685  min_functions->push_back(min_max_item);
12686  else if (have_max && (min_max_item->sum_func() == Item_sum::MAX_FUNC))
12687  max_functions->push_back(min_max_item);
12688  }
12689 
12690  if (have_min)
12691  {
12692  if (!(min_functions_it= new List_iterator<Item_sum>(*min_functions)))
12693  return 1;
12694  }
12695 
12696  if (have_max)
12697  {
12698  if (!(max_functions_it= new List_iterator<Item_sum>(*max_functions)))
12699  return 1;
12700  }
12701  }
12702  else
12703  min_max_ranges.elements= 0;
12704 
12705  return 0;
12706 }
12707 
12708 
12709 QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT()
12710 {
12711  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::~QUICK_GROUP_MIN_MAX_SELECT");
12712  if (head->file->inited)
12713  /*
12714  We may have used this object for index access during
12715  create_sort_index() and then switched to rnd access for the rest
12716  of execution. Since we don't do cleanup until now, we must call
12717  ha_*_end() for whatever is the current access method.
12718  */
12719  head->file->ha_index_or_rnd_end();
12720  if (min_max_arg_part)
12721  delete_dynamic(&min_max_ranges);
12722  free_root(&alloc,MYF(0));
12723  delete min_functions_it;
12724  delete max_functions_it;
12725  delete quick_prefix_select;
12726  DBUG_VOID_RETURN;
12727 }
12728 
12729 
12730 /*
12731  Eventually create and add a new quick range object.
12732 
12733  SYNOPSIS
12734  QUICK_GROUP_MIN_MAX_SELECT::add_range()
12735  sel_range Range object from which a
12736 
12737  NOTES
12738  Construct a new QUICK_RANGE object from a SEL_ARG object, and
12739  add it to the array min_max_ranges. If sel_arg is an infinite
12740  range, e.g. (x < 5 or x > 4), then skip it and do not construct
12741  a quick range.
12742 
12743  RETURN
12744  FALSE on success
12745  TRUE otherwise
12746 */
12747 
12748 bool QUICK_GROUP_MIN_MAX_SELECT::add_range(SEL_ARG *sel_range)
12749 {
12750  QUICK_RANGE *range;
12751  uint range_flag= sel_range->min_flag | sel_range->max_flag;
12752 
12753  /* Skip (-inf,+inf) ranges, e.g. (x < 5 or x > 4). */
12754  if ((range_flag & NO_MIN_RANGE) && (range_flag & NO_MAX_RANGE))
12755  return FALSE;
12756 
12757  if (!(sel_range->min_flag & NO_MIN_RANGE) &&
12758  !(sel_range->max_flag & NO_MAX_RANGE))
12759  {
12760  if (sel_range->maybe_null &&
12761  sel_range->min_value[0] && sel_range->max_value[0])
12762  range_flag|= NULL_RANGE; /* IS NULL condition */
12763  else if (memcmp(sel_range->min_value, sel_range->max_value,
12764  min_max_arg_len) == 0)
12765  range_flag|= EQ_RANGE; /* equality condition */
12766  }
12767  range= new QUICK_RANGE(sel_range->min_value, min_max_arg_len,
12768  make_keypart_map(sel_range->part),
12769  sel_range->max_value, min_max_arg_len,
12770  make_keypart_map(sel_range->part),
12771  range_flag);
12772  if (!range)
12773  return TRUE;
12774  if (insert_dynamic(&min_max_ranges, &range))
12775  return TRUE;
12776  return FALSE;
12777 }
12778 
12779 
12780 /*
12781  Opens the ranges if there are more conditions in quick_prefix_select than
12782  the ones used for jumping through the prefixes.
12783 
12784  SYNOPSIS
12785  QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges()
12786 
12787  NOTES
12788  quick_prefix_select is made over the conditions on the whole key.
12789  It defines a number of ranges of length x.
12790  However when jumping through the prefixes we use only the the first
12791  few most significant keyparts in the range key. However if there
12792  are more keyparts to follow the ones we are using we must make the
12793  condition on the key inclusive (because x < "ab" means
12794  x[0] < 'a' OR (x[0] == 'a' AND x[1] < 'b').
12795  To achive the above we must turn off the NEAR_MIN/NEAR_MAX
12796 */
12797 void QUICK_GROUP_MIN_MAX_SELECT::adjust_prefix_ranges ()
12798 {
12799  if (quick_prefix_select &&
12800  group_prefix_len < quick_prefix_select->max_used_key_length)
12801  {
12802  DYNAMIC_ARRAY *arr;
12803  uint inx;
12804 
12805  for (inx= 0, arr= &quick_prefix_select->ranges; inx < arr->elements; inx++)
12806  {
12807  QUICK_RANGE *range;
12808 
12809  get_dynamic(arr, (uchar*)&range, inx);
12810  range->flag &= ~(NEAR_MIN | NEAR_MAX);
12811  }
12812  }
12813 }
12814 
12815 
12816 /*
12817  Determine the total number and length of the keys that will be used for
12818  index lookup.
12819 
12820  SYNOPSIS
12821  QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
12822 
12823  DESCRIPTION
12824  The total length of the keys used for index lookup depends on whether
12825  there are any predicates referencing the min/max argument, and/or if
12826  the min/max argument field can be NULL.
12827  This function does an optimistic analysis whether the search key might
12828  be extended by a constant for the min/max keypart. It is 'optimistic'
12829  because during actual execution it may happen that a particular range
12830  is skipped, and then a shorter key will be used. However this is data
12831  dependent and can't be easily estimated here.
12832 
12833  RETURN
12834  None
12835 */
12836 
12837 void QUICK_GROUP_MIN_MAX_SELECT::update_key_stat()
12838 {
12839  max_used_key_length= real_prefix_len;
12840  if (min_max_ranges.elements > 0)
12841  {
12842  QUICK_RANGE *cur_range;
12843  if (have_min)
12844  { /* Check if the right-most range has a lower boundary. */
12845  get_dynamic(&min_max_ranges, (uchar*)&cur_range,
12846  min_max_ranges.elements - 1);
12847  if (!(cur_range->flag & NO_MIN_RANGE))
12848  {
12849  max_used_key_length+= min_max_arg_len;
12850  used_key_parts++;
12851  return;
12852  }
12853  }
12854  if (have_max)
12855  { /* Check if the left-most range has an upper boundary. */
12856  get_dynamic(&min_max_ranges, (uchar*)&cur_range, 0);
12857  if (!(cur_range->flag & NO_MAX_RANGE))
12858  {
12859  max_used_key_length+= min_max_arg_len;
12860  used_key_parts++;
12861  return;
12862  }
12863  }
12864  }
12865  else if (have_min && min_max_arg_part &&
12866  min_max_arg_part->field->real_maybe_null())
12867  {
12868  /*
12869  If a MIN/MAX argument value is NULL, we can quickly determine
12870  that we're in the beginning of the next group, because NULLs
12871  are always < any other value. This allows us to quickly
12872  determine the end of the current group and jump to the next
12873  group (see next_min()) and thus effectively increases the
12874  usable key length.
12875  */
12876  max_used_key_length+= min_max_arg_len;
12877  used_key_parts++;
12878  }
12879 }
12880 
12881 
12882 /*
12883  Initialize a quick group min/max select for key retrieval.
12884 
12885  SYNOPSIS
12886  QUICK_GROUP_MIN_MAX_SELECT::reset()
12887 
12888  DESCRIPTION
12889  Initialize the index chosen for access and find and store the prefix
12890  of the last group. The method is expensive since it performs disk access.
12891 
12892  RETURN
12893  0 OK
12894  other Error code
12895 */
12896 
12897 int QUICK_GROUP_MIN_MAX_SELECT::reset(void)
12898 {
12899  int result;
12900  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::reset");
12901 
12902  seen_first_key= false;
12903  head->set_keyread(TRUE); /* We need only the key attributes */
12904  /*
12905  Request ordered index access as usage of ::index_last(),
12906  ::index_first() within QUICK_GROUP_MIN_MAX_SELECT depends on it.
12907  */
12908  if ((result= head->file->ha_index_init(index, true)))
12909  {
12910  head->file->print_error(result, MYF(0));
12911  DBUG_RETURN(result);
12912  }
12913  if (quick_prefix_select && quick_prefix_select->reset())
12914  DBUG_RETURN(1);
12915  result= head->file->ha_index_last(record);
12916  if (result == HA_ERR_END_OF_FILE)
12917  DBUG_RETURN(0);
12918  /* Save the prefix of the last group. */
12919  key_copy(last_prefix, record, index_info, group_prefix_len);
12920 
12921  DBUG_RETURN(0);
12922 }
12923 
12924 
12925 
12926 /*
12927  Get the next key containing the MIN and/or MAX key for the next group.
12928 
12929  SYNOPSIS
12930  QUICK_GROUP_MIN_MAX_SELECT::get_next()
12931 
12932  DESCRIPTION
12933  The method finds the next subsequent group of records that satisfies the
12934  query conditions and finds the keys that contain the MIN/MAX values for
12935  the key part referenced by the MIN/MAX function(s). Once a group and its
12936  MIN/MAX values are found, store these values in the Item_sum objects for
12937  the MIN/MAX functions. The rest of the values in the result row are stored
12938  in the Item_field::result_field of each select field. If the query does
12939  not contain MIN and/or MAX functions, then the function only finds the
12940  group prefix, which is a query answer itself.
12941 
12942  NOTES
12943  If both MIN and MAX are computed, then we use the fact that if there is
12944  no MIN key, there can't be a MAX key as well, so we can skip looking
12945  for a MAX key in this case.
12946 
12947  RETURN
12948  0 on success
12949  HA_ERR_END_OF_FILE if returned all keys
12950  other if some error occurred
12951 */
12952 
12953 int QUICK_GROUP_MIN_MAX_SELECT::get_next()
12954 {
12955  int min_res= 0;
12956  int max_res= 0;
12957 #ifdef HPUX11
12958  /*
12959  volatile is required by a bug in the HP compiler due to which the
12960  last test of result fails.
12961  */
12962  volatile int result;
12963 #else
12964  int result;
12965 #endif
12966  int is_last_prefix= 0;
12967 
12968  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::get_next");
12969 
12970  /*
12971  Loop until a group is found that satisfies all query conditions or the last
12972  group is reached.
12973  */
12974  do
12975  {
12976  result= next_prefix();
12977  /*
12978  Check if this is the last group prefix. Notice that at this point
12979  this->record contains the current prefix in record format.
12980  */
12981  if (!result)
12982  {
12983  is_last_prefix= key_cmp(index_info->key_part, last_prefix,
12984  group_prefix_len);
12985  DBUG_ASSERT(is_last_prefix <= 0);
12986  }
12987  else
12988  {
12989  if (result == HA_ERR_KEY_NOT_FOUND)
12990  continue;
12991  break;
12992  }
12993 
12994  if (have_min)
12995  {
12996  min_res= next_min();
12997  if (min_res == 0)
12998  update_min_result();
12999  }
13000  /* If there is no MIN in the group, there is no MAX either. */
13001  if ((have_max && !have_min) ||
13002  (have_max && have_min && (min_res == 0)))
13003  {
13004  max_res= next_max();
13005  if (max_res == 0)
13006  update_max_result();
13007  /* If a MIN was found, a MAX must have been found as well. */
13008  DBUG_ASSERT((have_max && !have_min) ||
13009  (have_max && have_min && (max_res == 0)));
13010  }
13011  /*
13012  If this is just a GROUP BY or DISTINCT without MIN or MAX and there
13013  are equality predicates for the key parts after the group, find the
13014  first sub-group with the extended prefix.
13015  */
13016  if (!have_min && !have_max && key_infix_len > 0)
13017  result= head->file->ha_index_read_map(record, group_prefix,
13018  make_prev_keypart_map(real_key_parts),
13019  HA_READ_KEY_EXACT);
13020 
13021  result= have_min ? min_res : have_max ? max_res : result;
13022  } while ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
13023  is_last_prefix != 0);
13024 
13025  if (result == HA_ERR_KEY_NOT_FOUND)
13026  result= HA_ERR_END_OF_FILE;
13027 
13028  DBUG_RETURN(result);
13029 }
13030 
13031 
13032 /*
13033  Retrieve the minimal key in the next group.
13034 
13035  SYNOPSIS
13036  QUICK_GROUP_MIN_MAX_SELECT::next_min()
13037 
13038  DESCRIPTION
13039  Find the minimal key within this group such that the key satisfies the query
13040  conditions and NULL semantics. The found key is loaded into this->record.
13041 
13042  IMPLEMENTATION
13043  Depending on the values of min_max_ranges.elements, key_infix_len, and
13044  whether there is a NULL in the MIN field, this function may directly
13045  return without any data access. In this case we use the key loaded into
13046  this->record by the call to this->next_prefix() just before this call.
13047 
13048  RETURN
13049  0 on success
13050  HA_ERR_KEY_NOT_FOUND if no MIN key was found that fulfills all conditions.
13051  HA_ERR_END_OF_FILE - "" -
13052  other if some error occurred
13053 */
13054 
13055 int QUICK_GROUP_MIN_MAX_SELECT::next_min()
13056 {
13057  int result= 0;
13058  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_min");
13059 
13060  /* Find the MIN key using the eventually extended group prefix. */
13061  if (min_max_ranges.elements > 0)
13062  {
13063  if ((result= next_min_in_range()))
13064  DBUG_RETURN(result);
13065  }
13066  else
13067  {
13068  /* Apply the constant equality conditions to the non-group select fields */
13069  if (key_infix_len > 0)
13070  {
13071  if ((result= head->file->ha_index_read_map(record, group_prefix,
13072  make_prev_keypart_map(real_key_parts),
13073  HA_READ_KEY_EXACT)))
13074  DBUG_RETURN(result);
13075  }
13076 
13077  /*
13078  If the min/max argument field is NULL, skip subsequent rows in the same
13079  group with NULL in it. Notice that:
13080  - if the first row in a group doesn't have a NULL in the field, no row
13081  in the same group has (because NULL < any other value),
13082  - min_max_arg_part->field->ptr points to some place in 'record'.
13083  */
13084  if (min_max_arg_part && min_max_arg_part->field->is_null())
13085  {
13086  uchar key_buf[MAX_KEY_LENGTH];
13087 
13088  /* Find the first subsequent record without NULL in the MIN/MAX field. */
13089  key_copy(key_buf, record, index_info, max_used_key_length);
13090  result= head->file->ha_index_read_map(record, key_buf,
13091  make_keypart_map(real_key_parts),
13092  HA_READ_AFTER_KEY);
13093  /*
13094  Check if the new record belongs to the current group by comparing its
13095  prefix with the group's prefix. If it is from the next group, then the
13096  whole group has NULLs in the MIN/MAX field, so use the first record in
13097  the group as a result.
13098  TODO:
13099  It is possible to reuse this new record as the result candidate for the
13100  next call to next_min(), and to save one lookup in the next call. For
13101  this add a new member 'this->next_group_prefix'.
13102  */
13103  if (!result)
13104  {
13105  if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
13106  key_restore(record, key_buf, index_info, 0);
13107  }
13108  else if (result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE)
13109  result= 0; /* There is a result in any case. */
13110  }
13111  }
13112 
13113  /*
13114  If the MIN attribute is non-nullable, this->record already contains the
13115  MIN key in the group, so just return.
13116  */
13117  DBUG_RETURN(result);
13118 }
13119 
13120 
13121 /*
13122  Retrieve the maximal key in the next group.
13123 
13124  SYNOPSIS
13125  QUICK_GROUP_MIN_MAX_SELECT::next_max()
13126 
13127  DESCRIPTION
13128  Lookup the maximal key of the group, and store it into this->record.
13129 
13130  RETURN
13131  0 on success
13132  HA_ERR_KEY_NOT_FOUND if no MAX key was found that fulfills all conditions.
13133  HA_ERR_END_OF_FILE - "" -
13134  other if some error occurred
13135 */
13136 
13137 int QUICK_GROUP_MIN_MAX_SELECT::next_max()
13138 {
13139  int result;
13140 
13141  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_max");
13142 
13143  /* Get the last key in the (possibly extended) group. */
13144  if (min_max_ranges.elements > 0)
13145  result= next_max_in_range();
13146  else
13147  result= head->file->ha_index_read_map(record, group_prefix,
13148  make_prev_keypart_map(real_key_parts),
13149  HA_READ_PREFIX_LAST);
13150  DBUG_RETURN(result);
13151 }
13152 
13153 
13179 static int index_next_different (bool is_index_scan, handler *file,
13180  KEY_PART_INFO *key_part, uchar * record,
13181  const uchar * group_prefix,
13182  uint group_prefix_len,
13183  uint group_key_parts)
13184 {
13185  if (is_index_scan)
13186  {
13187  int result= 0;
13188 
13189  while (!key_cmp (key_part, group_prefix, group_prefix_len))
13190  {
13191  result= file->ha_index_next(record);
13192  if (result)
13193  return(result);
13194  }
13195  return result;
13196  }
13197  else
13198  return file->ha_index_read_map(record, group_prefix,
13199  make_prev_keypart_map(group_key_parts),
13200  HA_READ_AFTER_KEY);
13201 }
13202 
13203 
13204 /*
13205  Determine the prefix of the next group.
13206 
13207  SYNOPSIS
13208  QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
13209 
13210  DESCRIPTION
13211  Determine the prefix of the next group that satisfies the query conditions.
13212  If there is a range condition referencing the group attributes, use a
13213  QUICK_RANGE_SELECT object to retrieve the *first* key that satisfies the
13214  condition. If there is a key infix of constants, append this infix
13215  immediately after the group attributes. The possibly extended prefix is
13216  stored in this->group_prefix. The first key of the found group is stored in
13217  this->record, on which relies this->next_min().
13218 
13219  RETURN
13220  0 on success
13221  HA_ERR_KEY_NOT_FOUND if there is no key with the formed prefix
13222  HA_ERR_END_OF_FILE if there are no more keys
13223  other if some error occurred
13224 */
13225 int QUICK_GROUP_MIN_MAX_SELECT::next_prefix()
13226 {
13227  int result;
13228  DBUG_ENTER("QUICK_GROUP_MIN_MAX_SELECT::next_prefix");
13229 
13230  if (quick_prefix_select)
13231  {
13232  uchar *cur_prefix= seen_first_key ? group_prefix : NULL;
13233  if ((result= quick_prefix_select->get_next_prefix(group_prefix_len,
13234  group_key_parts,
13235  cur_prefix)))
13236  DBUG_RETURN(result);
13237  seen_first_key= TRUE;
13238  }
13239  else
13240  {
13241  if (!seen_first_key)
13242  {
13243  result= head->file->ha_index_first(record);
13244  if (result)
13245  DBUG_RETURN(result);
13246  seen_first_key= TRUE;
13247  }
13248  else
13249  {
13250  /* Load the first key in this group into record. */
13251  result= index_next_different (is_index_scan, head->file,
13252  index_info->key_part,
13253  record, group_prefix, group_prefix_len,
13254  group_key_parts);
13255  if (result)
13256  DBUG_RETURN(result);
13257  }
13258  }
13259 
13260  /* Save the prefix of this group for subsequent calls. */
13261  key_copy(group_prefix, record, index_info, group_prefix_len);
13262  /* Append key_infix to group_prefix. */
13263  if (key_infix_len > 0)
13264  memcpy(group_prefix + group_prefix_len,
13265  key_infix, key_infix_len);
13266 
13267  DBUG_RETURN(0);
13268 }
13269 
13270 
13271 /*
13272  Find the minimal key in a group that satisfies some range conditions for the
13273  min/max argument field.
13274 
13275  SYNOPSIS
13276  QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
13277 
13278  DESCRIPTION
13279  Given the sequence of ranges min_max_ranges, find the minimal key that is
13280  in the left-most possible range. If there is no such key, then the current
13281  group does not have a MIN key that satisfies the WHERE clause. If a key is
13282  found, its value is stored in this->record.
13283 
13284  RETURN
13285  0 on success
13286  HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
13287  the ranges
13288  HA_ERR_END_OF_FILE - "" -
13289  other if some error
13290 */
13291 
13292 int QUICK_GROUP_MIN_MAX_SELECT::next_min_in_range()
13293 {
13294  ha_rkey_function find_flag;
13295  key_part_map keypart_map;
13296  QUICK_RANGE *cur_range;
13297  bool found_null= FALSE;
13298  int result= HA_ERR_KEY_NOT_FOUND;
13299 
13300  DBUG_ASSERT(min_max_ranges.elements > 0);
13301 
13302  for (uint range_idx= 0; range_idx < min_max_ranges.elements; range_idx++)
13303  { /* Search from the left-most range to the right. */
13304  get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx);
13305 
13306  /*
13307  If the current value for the min/max argument is bigger than the right
13308  boundary of cur_range, there is no need to check this range.
13309  */
13310  if (range_idx != 0 && !(cur_range->flag & NO_MAX_RANGE) &&
13311  (key_cmp(min_max_arg_part, (const uchar*) cur_range->max_key,
13312  min_max_arg_len) == 1))
13313  continue;
13314 
13315  if (cur_range->flag & NO_MIN_RANGE)
13316  {
13317  keypart_map= make_prev_keypart_map(real_key_parts);
13318  find_flag= HA_READ_KEY_EXACT;
13319  }
13320  else
13321  {
13322  /* Extend the search key with the lower boundary for this range. */
13323  memcpy(group_prefix + real_prefix_len, cur_range->min_key,
13324  cur_range->min_length);
13325  keypart_map= make_keypart_map(real_key_parts);
13326  find_flag= (cur_range->flag & (EQ_RANGE | NULL_RANGE)) ?
13327  HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MIN) ?
13328  HA_READ_AFTER_KEY : HA_READ_KEY_OR_NEXT;
13329  }
13330 
13331  result= head->file->ha_index_read_map(record, group_prefix, keypart_map,
13332  find_flag);
13333  if (result)
13334  {
13335  if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
13336  (cur_range->flag & (EQ_RANGE | NULL_RANGE)))
13337  continue; /* Check the next range. */
13338 
13339  /*
13340  In all other cases (HA_ERR_*, HA_READ_KEY_EXACT with NO_MIN_RANGE,
13341  HA_READ_AFTER_KEY, HA_READ_KEY_OR_NEXT) if the lookup failed for this
13342  range, it can't succeed for any other subsequent range.
13343  */
13344  break;
13345  }
13346 
13347  /* A key was found. */
13348  if (cur_range->flag & EQ_RANGE)
13349  break; /* No need to perform the checks below for equal keys. */
13350 
13351  if (cur_range->flag & NULL_RANGE)
13352  {
13353  /*
13354  Remember this key, and continue looking for a non-NULL key that
13355  satisfies some other condition.
13356  */
13357  memcpy(tmp_record, record, head->s->rec_buff_length);
13358  found_null= TRUE;
13359  continue;
13360  }
13361 
13362  /* Check if record belongs to the current group. */
13363  if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
13364  {
13365  result= HA_ERR_KEY_NOT_FOUND;
13366  continue;
13367  }
13368 
13369  /* If there is an upper limit, check if the found key is in the range. */
13370  if ( !(cur_range->flag & NO_MAX_RANGE) )
13371  {
13372  /* Compose the MAX key for the range. */
13373  uchar *max_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
13374  memcpy(max_key, group_prefix, real_prefix_len);
13375  memcpy(max_key + real_prefix_len, cur_range->max_key,
13376  cur_range->max_length);
13377  /* Compare the found key with max_key. */
13378  int cmp_res= key_cmp(index_info->key_part, max_key,
13379  real_prefix_len + min_max_arg_len);
13380  /*
13381  The key is outside of the range if:
13382  the interval is open and the key is equal to the maximum boundry
13383  or
13384  the key is greater than the maximum
13385  */
13386  if (((cur_range->flag & NEAR_MAX) && cmp_res == 0) ||
13387  cmp_res > 0)
13388  {
13389  result= HA_ERR_KEY_NOT_FOUND;
13390  continue;
13391  }
13392  }
13393  /* If we got to this point, the current key qualifies as MIN. */
13394  DBUG_ASSERT(result == 0);
13395  break;
13396  }
13397  /*
13398  If there was a key with NULL in the MIN/MAX field, and there was no other
13399  key without NULL from the same group that satisfies some other condition,
13400  then use the key with the NULL.
13401  */
13402  if (found_null && result)
13403  {
13404  memcpy(record, tmp_record, head->s->rec_buff_length);
13405  result= 0;
13406  }
13407  return result;
13408 }
13409 
13410 
13411 /*
13412  Find the maximal key in a group that satisfies some range conditions for the
13413  min/max argument field.
13414 
13415  SYNOPSIS
13416  QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
13417 
13418  DESCRIPTION
13419  Given the sequence of ranges min_max_ranges, find the maximal key that is
13420  in the right-most possible range. If there is no such key, then the current
13421  group does not have a MAX key that satisfies the WHERE clause. If a key is
13422  found, its value is stored in this->record.
13423 
13424  RETURN
13425  0 on success
13426  HA_ERR_KEY_NOT_FOUND if there is no key with the given prefix in any of
13427  the ranges
13428  HA_ERR_END_OF_FILE - "" -
13429  other if some error
13430 */
13431 
13432 int QUICK_GROUP_MIN_MAX_SELECT::next_max_in_range()
13433 {
13434  ha_rkey_function find_flag;
13435  key_part_map keypart_map;
13436  QUICK_RANGE *cur_range;
13437  int result;
13438 
13439  DBUG_ASSERT(min_max_ranges.elements > 0);
13440 
13441  for (uint range_idx= min_max_ranges.elements; range_idx > 0; range_idx--)
13442  { /* Search from the right-most range to the left. */
13443  get_dynamic(&min_max_ranges, (uchar*)&cur_range, range_idx - 1);
13444 
13445  /*
13446  If the current value for the min/max argument is smaller than the left
13447  boundary of cur_range, there is no need to check this range.
13448  */
13449  if (range_idx != min_max_ranges.elements &&
13450  !(cur_range->flag & NO_MIN_RANGE) &&
13451  (key_cmp(min_max_arg_part, (const uchar*) cur_range->min_key,
13452  min_max_arg_len) == -1))
13453  continue;
13454 
13455  if (cur_range->flag & NO_MAX_RANGE)
13456  {
13457  keypart_map= make_prev_keypart_map(real_key_parts);
13458  find_flag= HA_READ_PREFIX_LAST;
13459  }
13460  else
13461  {
13462  /* Extend the search key with the upper boundary for this range. */
13463  memcpy(group_prefix + real_prefix_len, cur_range->max_key,
13464  cur_range->max_length);
13465  keypart_map= make_keypart_map(real_key_parts);
13466  find_flag= (cur_range->flag & EQ_RANGE) ?
13467  HA_READ_KEY_EXACT : (cur_range->flag & NEAR_MAX) ?
13468  HA_READ_BEFORE_KEY : HA_READ_PREFIX_LAST_OR_PREV;
13469  }
13470 
13471  result= head->file->ha_index_read_map(record, group_prefix, keypart_map,
13472  find_flag);
13473 
13474  if (result)
13475  {
13476  if ((result == HA_ERR_KEY_NOT_FOUND || result == HA_ERR_END_OF_FILE) &&
13477  (cur_range->flag & EQ_RANGE))
13478  continue; /* Check the next range. */
13479 
13480  /*
13481  In no key was found with this upper bound, there certainly are no keys
13482  in the ranges to the left.
13483  */
13484  return result;
13485  }
13486  /* A key was found. */
13487  if (cur_range->flag & EQ_RANGE)
13488  return 0; /* No need to perform the checks below for equal keys. */
13489 
13490  /* Check if record belongs to the current group. */
13491  if (key_cmp(index_info->key_part, group_prefix, real_prefix_len))
13492  continue; // Row not found
13493 
13494  /* If there is a lower limit, check if the found key is in the range. */
13495  if ( !(cur_range->flag & NO_MIN_RANGE) )
13496  {
13497  /* Compose the MIN key for the range. */
13498  uchar *min_key= (uchar*) my_alloca(real_prefix_len + min_max_arg_len);
13499  memcpy(min_key, group_prefix, real_prefix_len);
13500  memcpy(min_key + real_prefix_len, cur_range->min_key,
13501  cur_range->min_length);
13502  /* Compare the found key with min_key. */
13503  int cmp_res= key_cmp(index_info->key_part, min_key,
13504  real_prefix_len + min_max_arg_len);
13505  /*
13506  The key is outside of the range if:
13507  the interval is open and the key is equal to the minimum boundry
13508  or
13509  the key is less than the minimum
13510  */
13511  if (((cur_range->flag & NEAR_MIN) && cmp_res == 0) ||
13512  cmp_res < 0)
13513  continue;
13514  }
13515  /* If we got to this point, the current key qualifies as MAX. */
13516  return result;
13517  }
13518  return HA_ERR_KEY_NOT_FOUND;
13519 }
13520 
13521 
13522 /*
13523  Update all MIN function results with the newly found value.
13524 
13525  SYNOPSIS
13526  QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
13527 
13528  DESCRIPTION
13529  The method iterates through all MIN functions and updates the result value
13530  of each function by calling Item_sum::reset(), which in turn picks the new
13531  result value from this->head->record[0], previously updated by
13532  next_min(). The updated value is stored in a member variable of each of the
13533  Item_sum objects, depending on the value type.
13534 
13535  IMPLEMENTATION
13536  The update must be done separately for MIN and MAX, immediately after
13537  next_min() was called and before next_max() is called, because both MIN and
13538  MAX take their result value from the same buffer this->head->record[0]
13539  (i.e. this->record).
13540 
13541  RETURN
13542  None
13543 */
13544 
13545 void QUICK_GROUP_MIN_MAX_SELECT::update_min_result()
13546 {
13547  Item_sum *min_func;
13548 
13549  min_functions_it->rewind();
13550  while ((min_func= (*min_functions_it)++))
13551  min_func->reset_and_add();
13552 }
13553 
13554 
13555 /*
13556  Update all MAX function results with the newly found value.
13557 
13558  SYNOPSIS
13559  QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
13560 
13561  DESCRIPTION
13562  The method iterates through all MAX functions and updates the result value
13563  of each function by calling Item_sum::reset(), which in turn picks the new
13564  result value from this->head->record[0], previously updated by
13565  next_max(). The updated value is stored in a member variable of each of the
13566  Item_sum objects, depending on the value type.
13567 
13568  IMPLEMENTATION
13569  The update must be done separately for MIN and MAX, immediately after
13570  next_max() was called, because both MIN and MAX take their result value
13571  from the same buffer this->head->record[0] (i.e. this->record).
13572 
13573  RETURN
13574  None
13575 */
13576 
13577 void QUICK_GROUP_MIN_MAX_SELECT::update_max_result()
13578 {
13579  Item_sum *max_func;
13580 
13581  max_functions_it->rewind();
13582  while ((max_func= (*max_functions_it)++))
13583  max_func->reset_and_add();
13584 }
13585 
13586 
13587 /*
13588  Append comma-separated list of keys this quick select uses to key_names;
13589  append comma-separated list of corresponding used lengths to used_lengths.
13590 
13591  SYNOPSIS
13592  QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths()
13593  key_names [out] Names of used indexes
13594  used_lengths [out] Corresponding lengths of the index names
13595 
13596  DESCRIPTION
13597  This method is used by select_describe to extract the names of the
13598  indexes used by a quick select.
13599 
13600 */
13601 
13602 void QUICK_GROUP_MIN_MAX_SELECT::add_keys_and_lengths(String *key_names,
13603  String *used_lengths)
13604 {
13605  char buf[64];
13606  uint length;
13607  key_names->append(index_info->name);
13608  length= longlong2str(max_used_key_length, buf, 10) - buf;
13609  used_lengths->append(buf, length);
13610 }
13611 
13612 
13613 
13627 static bool eq_ranges_exceeds_limit(SEL_ARG *keypart_root, uint* count, uint limit)
13628 {
13629  // "Statistics instead of index dives" feature is turned off
13630  if (limit == 0)
13631  return false;
13632 
13633  /*
13634  Optimization: if there is at least one equality range, index
13635  statistics will be used when limit is 1. It's safe to return true
13636  even without checking that there is an equality range because if
13637  there are none, index statistics will not be used anyway.
13638  */
13639  if (limit == 1)
13640  return true;
13641 
13642  for(SEL_ARG *keypart_range= keypart_root->first();
13643  keypart_range; keypart_range= keypart_range->next)
13644  {
13645  /*
13646  This is an equality range predicate and should be counted if:
13647  1) the range for this keypart does not have a min/max flag
13648  (which indicates <, <= etc), and
13649  2) the lower and upper range boundaries have the same value
13650  (it's not a "x BETWEEN a AND b")
13651 
13652  Note, however, that if this is an "x IS NULL" condition we don't
13653  count it because the number of NULL-values is likely to be off
13654  the index statistics we plan to use.
13655  */
13656  if (!keypart_range->min_flag && !keypart_range->max_flag && // 1)
13657  !keypart_range->cmp_max_to_min(keypart_range) && // 2)
13658  !keypart_range->is_null_interval()) // "x IS NULL"
13659  {
13660  /*
13661  Count predicates in the next keypart, but only if that keypart
13662  is the next in the index.
13663  */
13664  if (keypart_range->next_key_part &&
13665  keypart_range->next_key_part->part == keypart_range->part + 1)
13666  eq_ranges_exceeds_limit(keypart_range->next_key_part, count, limit);
13667  else
13668  // We've found a path of equlity predicates down to a keypart leaf
13669  (*count)++;
13670 
13671  if (*count >= limit)
13672  return true;
13673  }
13674  }
13675  return false;
13676 }
13677 
13678 #ifndef DBUG_OFF
13679 
13680 static void print_sel_tree(PARAM *param, SEL_TREE *tree, key_map *tree_map,
13681  const char *msg)
13682 {
13683  SEL_ARG **key,**end;
13684  int idx;
13685  char buff[1024];
13686  DBUG_ENTER("print_sel_tree");
13687 
13688  String tmp(buff,sizeof(buff),&my_charset_bin);
13689  tmp.length(0);
13690  for (idx= 0,key=tree->keys, end=key+param->keys ;
13691  key != end ;
13692  key++,idx++)
13693  {
13694  if (tree_map->is_set(idx))
13695  {
13696  uint keynr= param->real_keynr[idx];
13697  if (tmp.length())
13698  tmp.append(',');
13699  tmp.append(param->table->key_info[keynr].name);
13700  }
13701  }
13702  if (!tmp.length())
13703  tmp.append(STRING_WITH_LEN("(empty)"));
13704 
13705  DBUG_PRINT("info", ("SEL_TREE: %p (%s) scans: %s", tree, msg, tmp.ptr()));
13706  DBUG_VOID_RETURN;
13707 }
13708 
13709 
13710 static void print_ror_scans_arr(TABLE *table, const char *msg,
13711  struct st_ror_scan_info **start,
13712  struct st_ror_scan_info **end)
13713 {
13714  DBUG_ENTER("print_ror_scans_arr");
13715 
13716  char buff[1024];
13717  String tmp(buff,sizeof(buff),&my_charset_bin);
13718  tmp.length(0);
13719  for (;start != end; start++)
13720  {
13721  if (tmp.length())
13722  tmp.append(',');
13723  tmp.append(table->key_info[(*start)->keynr].name);
13724  }
13725  if (!tmp.length())
13726  tmp.append(STRING_WITH_LEN("(empty)"));
13727  DBUG_PRINT("info", ("ROR key scans (%s): %s", msg, tmp.ptr()));
13728  fprintf(DBUG_FILE,"ROR key scans (%s): %s", msg, tmp.ptr());
13729 
13730  DBUG_VOID_RETURN;
13731 }
13732 
13733 
13734 #endif /* !DBUG_OFF */
13735 
13744 static void
13745 print_key_value(String *out, const KEY_PART_INFO *key_part, const uchar *key)
13746 {
13747  Field *field= key_part->field;
13748 
13749  if (field->flags & BLOB_FLAG)
13750  {
13751  out->append(STRING_WITH_LEN("unprintable_blob_value"));
13752  return;
13753  }
13754 
13755  char buff[128];
13756  String tmp(buff, sizeof(buff), system_charset_info);
13757  tmp.length(0);
13758 
13759  TABLE *table= field->table;
13760  my_bitmap_map *old_sets[2];
13761 
13762  dbug_tmp_use_all_columns(table, old_sets, table->read_set,
13763  table->write_set);
13764 
13765  uint store_length= key_part->store_length;
13766 
13767  if (field->real_maybe_null())
13768  {
13769  /*
13770  Byte 0 of key is the null-byte. If set, key is NULL.
13771  Otherwise, print the key value starting immediately after the
13772  null-byte
13773  */
13774  if (*key)
13775  {
13776  out->append(STRING_WITH_LEN("NULL"));
13777  goto restore_col_map;
13778  }
13779  key++; // Skip null byte
13780  store_length--;
13781  }
13782  field->set_key_image(key, key_part->length);
13783  if (field->type() == MYSQL_TYPE_BIT)
13784  (void) field->val_int_as_str(&tmp, 1); // may change tmp's charset
13785  else
13786  field->val_str(&tmp); // may change tmp's charset
13787  out->append(tmp.ptr(), tmp.length(), tmp.charset());
13788 
13789 restore_col_map:
13790  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
13791 }
13792 
13803 void append_range(String *out,
13804  const KEY_PART_INFO *key_part,
13805  const uchar *min_key, const uchar *max_key,
13806  const uint flag)
13807 {
13808  if (out->length() > 0)
13809  out->append(STRING_WITH_LEN(" AND "));
13810 
13811  if (!(flag & NO_MIN_RANGE))
13812  {
13813  print_key_value(out, key_part, min_key);
13814  if (flag & NEAR_MIN)
13815  out->append(STRING_WITH_LEN(" < "));
13816  else
13817  out->append(STRING_WITH_LEN(" <= "));
13818  }
13819 
13820  out->append(key_part->field->field_name);
13821 
13822  if (!(flag & NO_MAX_RANGE))
13823  {
13824  if (flag & NEAR_MAX)
13825  out->append(STRING_WITH_LEN(" < "));
13826  else
13827  out->append(STRING_WITH_LEN(" <= "));
13828  print_key_value(out, key_part, max_key);
13829  }
13830 }
13831 
13851 static void append_range_all_keyparts(Opt_trace_array *range_trace,
13852  String *range_string,
13853  String *range_so_far,
13854  SEL_ARG *keypart_root,
13855  const KEY_PART_INFO *key_parts)
13856 {
13857  DBUG_ASSERT(keypart_root && keypart_root != &null_element);
13858 
13859  const bool append_to_trace= (range_trace != NULL);
13860 
13861  // Either add info to range_string or to range_trace
13862  DBUG_ASSERT(append_to_trace ? !range_string : (range_string != NULL));
13863 
13864  // Navigate to first interval in red-black tree
13865  const KEY_PART_INFO *cur_key_part= key_parts + keypart_root->part;
13866  const SEL_ARG *keypart_range= keypart_root->first();
13867 
13868  const uint save_range_so_far_length= range_so_far->length();
13869 
13870  while (keypart_range)
13871  {
13872  /*
13873  Skip the rest of condition printing to avoid OOM if appending to
13874  range_string and the string becomes too long. Printing very long
13875  range conditions normally doesn't make sense either.
13876  */
13877  if (!append_to_trace && range_string->length() > 500)
13878  {
13879  range_string->append(STRING_WITH_LEN("..."));
13880  break;
13881  }
13882 
13883  // Append the current range predicate to the range String
13884  append_range(range_so_far, cur_key_part,
13885  keypart_range->min_value, keypart_range->max_value,
13886  keypart_range->min_flag | keypart_range->max_flag);
13887 
13888  /*
13889  Print range predicates for consecutive keyparts if
13890  1) There are predicates for later keyparts
13891  2) There are no "holes" in the used keyparts (keypartX can only
13892  be used if there is a range predicate on keypartX-1)
13893  3) The current range is an equality range
13894  */
13895  if (keypart_range->next_key_part &&
13896  keypart_range->next_key_part->part == keypart_range->part + 1 &&
13897  keypart_range->is_singlepoint())
13898  {
13899  append_range_all_keyparts(range_trace, range_string, range_so_far,
13900  keypart_range->next_key_part, key_parts);
13901  }
13902  else
13903  {
13904  /*
13905  This is the last keypart with a usable range predicate. Print
13906  full range info to the optimizer trace or to the string
13907  */
13908  if (append_to_trace)
13909  range_trace->add_utf8(range_so_far->ptr(),
13910  range_so_far->length());
13911  else
13912  {
13913  if (range_string->length() == 0)
13914  range_string->append(STRING_WITH_LEN("("));
13915  else
13916  range_string->append(STRING_WITH_LEN(" OR ("));
13917 
13918  range_string->append(range_so_far->ptr(), range_so_far->length());
13919  range_string->append(STRING_WITH_LEN(")"));
13920  }
13921  }
13922  keypart_range= keypart_range->next;
13923  /*
13924  Now moving to next range for this keypart, so "reset"
13925  range_so_far to include only range description of earlier
13926  keyparts
13927  */
13928  range_so_far->length(save_range_so_far_length);
13929  }
13930 }
13931 
13939 static inline void dbug_print_tree(const char *tree_name,
13940  SEL_TREE *tree,
13941  const RANGE_OPT_PARAM *param)
13942 {
13943 #ifndef DBUG_OFF
13944  if (!param->using_real_indexes)
13945  {
13946  DBUG_PRINT("info",
13947  ("sel_tree: "
13948  "%s uses a partitioned index and cannot be printed",
13949  tree_name));
13950  return;
13951  }
13952 
13953  if (!tree)
13954  {
13955  DBUG_PRINT("info", ("sel_tree: %s is NULL", tree_name));
13956  return;
13957  }
13958 
13959  if (tree->type == SEL_TREE::IMPOSSIBLE)
13960  {
13961  DBUG_PRINT("info", ("sel_tree: %s is IMPOSSIBLE", tree_name));
13962  return;
13963  }
13964 
13965  if (tree->type == SEL_TREE::ALWAYS)
13966  {
13967  DBUG_PRINT("info", ("sel_tree: %s is ALWAYS", tree_name));
13968  return;
13969  }
13970 
13971  if (tree->type == SEL_TREE::MAYBE)
13972  {
13973  DBUG_PRINT("info", ("sel_tree: %s is MAYBE", tree_name));
13974  return;
13975  }
13976 
13977  if (!tree->merges.is_empty())
13978  {
13979  DBUG_PRINT("info",
13980  ("sel_tree: "
13981  "%s contains the following merges", tree_name));
13982 
13983  List_iterator<SEL_IMERGE> it(tree->merges);
13984  int i= 0;
13985  for (SEL_IMERGE *el= it++; el; el= it++, i++)
13986  {
13987  for (SEL_TREE** current= el->trees;
13988  current != el->trees_next;
13989  current++)
13990  dbug_print_tree(" merge_tree", *current, param);
13991  }
13992  }
13993 
13994  for (uint i= 0; i< param->keys; i++)
13995  {
13996  if (tree->keys[i] == NULL || tree->keys[i] == &null_element)
13997  continue;
13998 
13999  uint real_key_nr= param->real_keynr[i];
14000 
14001  const KEY &cur_key= param->table->key_info[real_key_nr];
14002  const KEY_PART_INFO *key_part= cur_key.key_part;
14003 
14004  /*
14005  String holding the final range description from
14006  append_range_all_keyparts()
14007  */
14008  char buff1[512];
14009  String range_result(buff1, sizeof(buff1), system_charset_info);
14010  range_result.length(0);
14011 
14012  /*
14013  Range description up to a certain keypart - used internally in
14014  append_range_all_keyparts()
14015  */
14016  char buff2[128];
14017  String range_so_far(buff2, sizeof(buff2), system_charset_info);
14018  range_so_far.length(0);
14019 
14020  append_range_all_keyparts(NULL, &range_result, &range_so_far,
14021  tree->keys[i], key_part);
14022 
14023  DBUG_PRINT("info",
14024  ("sel_tree: %s->keys[%d(real_keynr: %d)]: %s",
14025  tree_name, i, real_key_nr, range_result.ptr()));
14026  }
14027 #endif
14028 }
14029 
14030 /*****************************************************************************
14031 ** Print a quick range for debugging
14032 ** TODO:
14033 ** This should be changed to use a String to store each row instead
14034 ** of locking the DEBUG stream !
14035 *****************************************************************************/
14036 
14037 #ifndef DBUG_OFF
14038 
14039 static void
14040 print_multiple_key_values(KEY_PART *key_part, const uchar *key,
14041  uint used_length)
14042 {
14043  char buff[1024];
14044  const uchar *key_end= key+used_length;
14045  String tmp(buff,sizeof(buff),&my_charset_bin);
14046  uint store_length;
14047  TABLE *table= key_part->field->table;
14048  my_bitmap_map *old_sets[2];
14049 
14050  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);
14051 
14052  for (; key < key_end; key+=store_length, key_part++)
14053  {
14054  Field *field= key_part->field;
14055  store_length= key_part->store_length;
14056 
14057  if (field->real_maybe_null())
14058  {
14059  if (*key)
14060  {
14061  fwrite("NULL",sizeof(char),4,DBUG_FILE);
14062  continue;
14063  }
14064  key++; // Skip null byte
14065  store_length--;
14066  }
14067  field->set_key_image(key, key_part->length);
14068  if (field->type() == MYSQL_TYPE_BIT)
14069  (void) field->val_int_as_str(&tmp, 1);
14070  else
14071  field->val_str(&tmp);
14072  fwrite(tmp.ptr(),sizeof(char),tmp.length(),DBUG_FILE);
14073  if (key+store_length < key_end)
14074  fputc('/',DBUG_FILE);
14075  }
14076  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
14077 }
14078 
14079 static void print_quick(QUICK_SELECT_I *quick, const key_map *needed_reg)
14080 {
14081  char buf[MAX_KEY/8+1];
14082  TABLE *table;
14083  my_bitmap_map *old_sets[2];
14084  DBUG_ENTER("print_quick");
14085  if (!quick)
14086  DBUG_VOID_RETURN;
14087  DBUG_LOCK_FILE;
14088 
14089  table= quick->head;
14090  dbug_tmp_use_all_columns(table, old_sets, table->read_set, table->write_set);
14091  quick->dbug_dump(0, TRUE);
14092  dbug_tmp_restore_column_maps(table->read_set, table->write_set, old_sets);
14093 
14094  fprintf(DBUG_FILE,"other_keys: 0x%s:\n", needed_reg->print(buf));
14095 
14096  DBUG_UNLOCK_FILE;
14097  DBUG_VOID_RETURN;
14098 }
14099 
14100 void QUICK_RANGE_SELECT::dbug_dump(int indent, bool verbose)
14101 {
14102  /* purecov: begin inspected */
14103  fprintf(DBUG_FILE, "%*squick range select, key %s, length: %d\n",
14104  indent, "", head->key_info[index].name, max_used_key_length);
14105 
14106  if (verbose)
14107  {
14108  QUICK_RANGE *range;
14109  QUICK_RANGE **pr= (QUICK_RANGE**)ranges.buffer;
14110  QUICK_RANGE **end_range= pr + ranges.elements;
14111  for (; pr != end_range; ++pr)
14112  {
14113  fprintf(DBUG_FILE, "%*s", indent + 2, "");
14114  range= *pr;
14115  if (!(range->flag & NO_MIN_RANGE))
14116  {
14117  print_multiple_key_values(key_parts, range->min_key,
14118  range->min_length);
14119  if (range->flag & NEAR_MIN)
14120  fputs(" < ",DBUG_FILE);
14121  else
14122  fputs(" <= ",DBUG_FILE);
14123  }
14124  fputs("X",DBUG_FILE);
14125 
14126  if (!(range->flag & NO_MAX_RANGE))
14127  {
14128  if (range->flag & NEAR_MAX)
14129  fputs(" < ",DBUG_FILE);
14130  else
14131  fputs(" <= ",DBUG_FILE);
14132  print_multiple_key_values(key_parts, range->max_key,
14133  range->max_length);
14134  }
14135  fputs("\n",DBUG_FILE);
14136  }
14137  }
14138  /* purecov: end */
14139 }
14140 
14141 void QUICK_INDEX_MERGE_SELECT::dbug_dump(int indent, bool verbose)
14142 {
14143  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
14144  QUICK_RANGE_SELECT *quick;
14145  fprintf(DBUG_FILE, "%*squick index_merge select\n", indent, "");
14146  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
14147  while ((quick= it++))
14148  quick->dbug_dump(indent+2, verbose);
14149  if (pk_quick_select)
14150  {
14151  fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
14152  pk_quick_select->dbug_dump(indent+2, verbose);
14153  }
14154  fprintf(DBUG_FILE, "%*s}\n", indent, "");
14155 }
14156 
14157 void QUICK_ROR_INTERSECT_SELECT::dbug_dump(int indent, bool verbose)
14158 {
14159  List_iterator_fast<QUICK_RANGE_SELECT> it(quick_selects);
14160  QUICK_RANGE_SELECT *quick;
14161  fprintf(DBUG_FILE, "%*squick ROR-intersect select, %scovering\n",
14162  indent, "", need_to_fetch_row? "":"non-");
14163  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
14164  while ((quick= it++))
14165  quick->dbug_dump(indent+2, verbose);
14166  if (cpk_quick)
14167  {
14168  fprintf(DBUG_FILE, "%*sclustered PK quick:\n", indent, "");
14169  cpk_quick->dbug_dump(indent+2, verbose);
14170  }
14171  fprintf(DBUG_FILE, "%*s}\n", indent, "");
14172 }
14173 
14174 void QUICK_ROR_UNION_SELECT::dbug_dump(int indent, bool verbose)
14175 {
14176  List_iterator_fast<QUICK_SELECT_I> it(quick_selects);
14177  QUICK_SELECT_I *quick;
14178  fprintf(DBUG_FILE, "%*squick ROR-union select\n", indent, "");
14179  fprintf(DBUG_FILE, "%*smerged scans {\n", indent, "");
14180  while ((quick= it++))
14181  quick->dbug_dump(indent+2, verbose);
14182  fprintf(DBUG_FILE, "%*s}\n", indent, "");
14183 }
14184 
14185 /*
14186  Print quick select information to DBUG_FILE.
14187 
14188  SYNOPSIS
14189  QUICK_GROUP_MIN_MAX_SELECT::dbug_dump()
14190  indent Indentation offset
14191  verbose If TRUE show more detailed output.
14192 
14193  DESCRIPTION
14194  Print the contents of this quick select to DBUG_FILE. The method also
14195  calls dbug_dump() for the used quick select if any.
14196 
14197  IMPLEMENTATION
14198  Caller is responsible for locking DBUG_FILE before this call and unlocking
14199  it afterwards.
14200 
14201  RETURN
14202  None
14203 */
14204 
14205 void QUICK_GROUP_MIN_MAX_SELECT::dbug_dump(int indent, bool verbose)
14206 {
14207  fprintf(DBUG_FILE,
14208  "%*squick_group_min_max_select: index %s (%d), length: %d\n",
14209  indent, "", index_info->name, index, max_used_key_length);
14210  if (key_infix_len > 0)
14211  {
14212  fprintf(DBUG_FILE, "%*susing key_infix with length %d:\n",
14213  indent, "", key_infix_len);
14214  }
14215  if (quick_prefix_select)
14216  {
14217  fprintf(DBUG_FILE, "%*susing quick_range_select:\n", indent, "");
14218  quick_prefix_select->dbug_dump(indent + 2, verbose);
14219  }
14220  if (min_max_ranges.elements > 0)
14221  {
14222  fprintf(DBUG_FILE, "%*susing %d quick_ranges for MIN/MAX:\n",
14223  indent, "", min_max_ranges.elements);
14224  }
14225 }
14226 
14227 
14228 #endif /* !DBUG_OFF */