MySQL 9.3.0
Source Code Documentation
tree.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2025, Oracle and/or its affiliates.
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, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef SQL_RANGE_OPTIMIZER_TREE_H_
25#define SQL_RANGE_OPTIMIZER_TREE_H_
26
27#include <sys/types.h>
28#include <cassert>
29#include <cstddef>
30#include <cstring>
31
32#include "my_alloc.h"
33#include "my_base.h"
34#include "my_inttypes.h"
35#include "sql/field.h"
36#include "sql/mem_root_array.h"
39#include "sql/sql_bitmap.h"
40#include "sql/sql_const.h"
41#include "sql/sql_list.h"
42
43class Cost_estimate;
44class SEL_ARG;
45class SEL_ROOT;
46class SEL_TREE;
47struct KEY_PART;
48struct ROR_SCAN_INFO;
49
50// Note: tree1 and tree2 are not usable by themselves after tree_and() or
51// tree_or().
52SEL_TREE *tree_and(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2);
53SEL_TREE *tree_or(RANGE_OPT_PARAM *param, bool remove_jump_scans,
54 SEL_TREE *tree1, SEL_TREE *tree2);
57
58bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2,
59 RANGE_OPT_PARAM *param);
60
61/**
62 A graph of (possible multiple) key ranges, represented as a red-black
63 binary tree. There are three types (see the Type enum); if KEY_RANGE,
64 we have zero or more SEL_ARGs, described in the documentation on SEL_ARG.
65
66 As a special case, a nullptr SEL_ROOT means a range that is always true.
67 This is true both for keys[] and next_key_part.
68*/
69class SEL_ROOT {
70 public:
71 /**
72 Used to indicate if the range predicate for an index is always
73 true/false, depends on values from other tables or can be
74 evaluated as is.
75 */
76 enum class Type {
77 /** The range predicate for this index is always false. */
79 /**
80 There is a range predicate that refers to another table. The
81 range access method cannot be used on this index unless that
82 other table is earlier in the join sequence. The bit
83 representing the index is set in JOIN_TAB::needed_reg to
84 notify the join optimizer that there is a table dependency.
85 After deciding on join order, the optimizer may chose to rerun
86 the range optimizer for tables with such dependencies.
87 */
89 /**
90 There is a range condition that can be used on this index. The
91 range conditions for this index in stored in the SEL_ARG tree.
92 */
95
96 /**
97 Constructs a tree of type KEY_RANGE, using the given root.
98 (The root is allowed to have children.)
99 */
101
102 /**
103 Used to construct MAYBE_KEY and IMPOSSIBLE SEL_ARGs.
104 */
105 SEL_ROOT(MEM_ROOT *memroot, Type type_arg);
106
107 /**
108 Note that almost all SEL_ROOTs are created on the MEM_ROOT,
109 so this destructor will only rarely be called.
110 */
111 ~SEL_ROOT() { assert(use_count == 0); }
112
113 /**
114 Returns true iff we have a single node that has no max nor min.
115 Note that by convention, a nullptr SEL_ROOT means the same.
116 */
117 bool is_always() const;
118
119 /**
120 Returns a number of keypart values appended to the key buffer
121 for min key and max key. This function is used by both Range
122 Analysis and Partition pruning. For partition pruning we have
123 to ensure that we don't store also subpartition fields. Thus
124 we have to stop at the last partition part and not step into
125 the subpartition fields. For Range Analysis we set last_part
126 to MAX_KEY which we should never reach.
127 */
128 int store_min_key(KEY_PART *key, uchar **range_key, uint *range_key_flag,
129 uint last_part, bool start_key);
130
131 /* returns a number of keypart values appended to the key buffer */
132 int store_max_key(KEY_PART *key, uchar **range_key, uint *range_key_flag,
133 uint last_part, bool start_key);
134
135 /**
136 Signal to the tree that the caller will shortly be dropping it
137 on the floor; if others are still using it, this is a no-op,
138 but if the caller was the last one, it is now an orphan, and
139 references from it should not count.
140 */
141 void free_tree();
142
143 /**
144 Insert the given node into the tree, and update the root.
145
146 @param key The node to insert.
147 */
148 void insert(SEL_ARG *key);
149
150 /**
151 Delete the given node from the tree, and update the root.
152
153 @param key The node to delete. Must exist in the tree.
154 */
155 void tree_delete(SEL_ARG *key);
156
157 /**
158 Find best key with min <= given key.
159 Because of the call context, this should never return nullptr to get_range.
160
161 @param key The key to search for.
162 */
163 SEL_ARG *find_range(const SEL_ARG *key) const;
164
165 /**
166 Create a new tree that's a duplicate of this one.
167
168 @param param The parameters for the new tree. Used to find out which
169 MEM_ROOT to allocate the new nodes on.
170
171 @return The new tree, or nullptr in case of out of memory.
172 */
173 SEL_ROOT *clone_tree(RANGE_OPT_PARAM *param) const;
174
175 /**
176 Check if SEL_ROOT::use_count value is correct. See the definition
177 of use_count for what is "correct".
178
179 @param root The origin tree of the SEL_ARG graph (an RB-tree that
180 has the least value of root->sel_root->root->part in the
181 entire graph, and thus is the "origin" of the graph)
182
183 @return true iff an incorrect SEL_ARG::use_count is found.
184 */
185 bool test_use_count(const SEL_ROOT *root) const;
186
187 /** Returns true iff this is a single-element, single-field predicate. */
188 inline bool simple_key() const;
189
190 /**
191 The root node of the tree. Note that this may change as the result
192 of rotations during insertions or deletions, so pointers should be
193 to the SEL_ROOT, not individual SEL_ARG nodes.
194
195 This element can never be nullptr, but can be null_element
196 if type == KEY_RANGE and the tree is empty (which then means the same as
197 type == IMPOSSIBLE).
198
199 If type == IMPOSSIBLE or type == MAYBE_KEY, there's a single root
200 element which only serves to hold next_key_part (we don't really care
201 about root->part in this case); the actual min/max values etc.
202 do not matter and should not be accessed.
203 */
205
206 /**
207 Number of references to this SEL_ARG tree. References may be from
208 SEL_ARG::next_key_part of SEL_ARGs from earlier keyparts or
209 SEL_TREE::keys[i].
210
211 The SEL_ARG trees are re-used in a lazy-copy manner based on this
212 reference counting.
213 */
214 ulong use_count{0};
215
216 /**
217 Number of nodes in the RB-tree, not including sentinels.
218 */
219 size_t elements{0};
220};
221
222int sel_cmp(Field *f, uchar *a, uchar *b, uint8 a_flag, uint8 b_flag);
223
224/**
225 A helper function to invert min flags to max flags for DESC key parts.
226 It changes NEAR_MIN, NO_MIN_RANGE to NEAR_MAX, NO_MAX_RANGE appropriately
227*/
228
229inline uint invert_min_flag(uint min_flag) {
230 uint max_flag_out = min_flag & ~(NEAR_MIN | NO_MIN_RANGE);
231 if (min_flag & NEAR_MIN) max_flag_out |= NEAR_MAX;
232 if (min_flag & NO_MIN_RANGE) max_flag_out |= NO_MAX_RANGE;
233 return max_flag_out;
234}
235
236/**
237 A helper function to invert max flags to min flags for DESC key parts.
238 It changes NEAR_MAX, NO_MAX_RANGE to NEAR_MIN, NO_MIN_RANGE appropriately
239*/
240
241inline uint invert_max_flag(uint max_flag) {
242 uint min_flag_out = max_flag & ~(NEAR_MAX | NO_MAX_RANGE);
243 if (max_flag & NEAR_MAX) min_flag_out |= NEAR_MIN;
244 if (max_flag & NO_MAX_RANGE) min_flag_out |= NO_MIN_RANGE;
245 return min_flag_out;
246}
247
248/*
249 A construction block of the SEL_ARG-graph.
250
251 One SEL_ARG object represents an "elementary interval" in form
252
253 min_value <=? table.keypartX <=? max_value
254
255 The interval is a non-empty interval of any kind: with[out] minimum/maximum
256 bound, [half]open/closed, single-point interval, etc.
257
258 1. SEL_ARG GRAPH STRUCTURE
259
260 SEL_ARG objects are linked together in a graph, represented by the SEL_ROOT.
261 The meaning of the graph is better demonstrated by an example:
262
263 tree->keys[i]
264 |
265 | $ $
266 | part=1 $ part=2 $ part=3
267 | $ $
268 | +-------+ $ +-------+ $ +--------+
269 | | kp1<1 |--$-->| kp2=5 |--$-->| kp3=10 |
270 | +-------+ $ +-------+ $ +--------+
271 | | $ $ |
272 | | $ $ +--------+
273 | | $ $ | kp3=12 |
274 | | $ $ +--------+
275 | +-------+ $ $
276 \->| kp1=2 |--$--------------$-+
277 +-------+ $ $ | +--------+
278 | $ $ ==>| kp3=11 |
279 +-------+ $ $ | +--------+
280 | kp1=3 |--$--------------$-+ |
281 +-------+ $ $ +--------+
282 | $ $ | kp3=14 |
283 ... $ $ +--------+
284
285 The entire graph is partitioned into "interval lists".
286
287 An interval list is a sequence of ordered disjoint intervals over
288 the same key part. SEL_ARG are linked via "next" and "prev" pointers
289 with NULL as sentinel.
290
291 In the example pic, there are 4 interval lists:
292 "kp<1 OR kp1=2 OR kp1=3", "kp2=5", "kp3=10 OR kp3=12", "kp3=11 OR kp3=13".
293 The vertical lines represent SEL_ARG::next/prev pointers.
294
295 Additionally, all intervals in the list form a red-black (RB) tree,
296 linked via left/right/parent pointers with null_element as sentinel. The
297 red-black tree root SEL_ARG object will be further called "root of the
298 interval list".
299
300 A red-black tree with 7 SEL_ARGs will look similar to what is shown
301 below. Left/right/parent pointers are shown while next pointers go from a
302 node with number X to the node with number X+1 (and prev in the
303 opposite direction):
304
305 Root
306 +---+
307 | 4 |
308 +---+
309 left/ \ right
310 __/ \__
311 / \
312 +---+ +---+
313 | 2 | | 6 |
314 +---+ +---+
315 left / \ right left / \ right
316 | | | |
317 +---+ +---+ +---+ +---+
318 | 1 | | 3 | | 5 | | 7 |
319 +---+ +---+ +---+ +---+
320
321 In this tree,
322 * node1->prev == node7->next == NULL
323 * node1->left == node1->right ==
324 node3->left == ... node7->right == null_element
325
326 In an interval list, each member X may have SEL_ARG::next_key_part pointer
327 pointing to the root of another interval list Y. The pointed interval list
328 must cover a key part with greater number (i.e. Y->part > X->part).
329
330 In the example pic, the next_key_part pointers are represented by
331 horisontal lines.
332
333 2. SEL_ARG GRAPH SEMANTICS
334
335 It represents a condition in a special form (we don't have a name for it ATM)
336 The SEL_ARG::next/prev is "OR", and next_key_part is "AND".
337
338 For example, the picture represents the condition in form:
339 (kp1 < 1 AND kp2=5 AND (kp3=10 OR kp3=12)) OR
340 (kp1=2 AND (kp3=11 OR kp3=14)) OR
341 (kp1=3 AND (kp3=11 OR kp3=14))
342
343 In red-black tree form:
344
345 +-------+ +--------+
346 | kp1=2 |.................| kp3=14 |
347 +-------+ +--------+
348 / \ /
349 +---------+ +-------+ +--------+
350 | kp1 < 1 | | kp1=3 | | kp3=11 |
351 +---------+ +-------+ +--------+
352 . .
353 ...... .......
354 . .
355 +-------+ +--------+
356 | kp2=5 | | kp3=14 |
357 +-------+ +--------+
358 . /
359 . +--------+
360 (root of R-B tree | kp3=11 |
361 for "kp3={10|12}") +--------+
362
363
364 Where / and \ denote left and right pointers and ... denotes
365 next_key_part pointers to the root of the R-B tree of intervals for
366 consecutive key parts.
367
368 3. SEL_ARG GRAPH USE
369
370 Use get_mm_tree() to construct SEL_ARG graph from WHERE condition.
371 Then walk the SEL_ARG graph and get a list of dijsoint ordered key
372 intervals (i.e. intervals in form
373
374 (constA1, .., const1_K) < (keypart1,.., keypartK) < (constB1, .., constB_K)
375
376 Those intervals can be used to access the index. The uses are in:
377 - check_quick_select() - Walk the SEL_ARG graph and find an estimate of
378 how many table records are contained within all
379 intervals.
380 - get_ranges_from_tree() - Walk the SEL_ARG, materialize the key intervals.
381
382 4. SPACE COMPLEXITY NOTES
383
384 SEL_ARG graph is a representation of an ordered disjoint sequence of
385 intervals over the ordered set of index tuple values.
386
387 For multi-part keys, one can construct a WHERE expression such that its
388 list of intervals will be of combinatorial size. Here is an example:
389
390 (keypart1 IN (1,2, ..., n1)) AND
391 (keypart2 IN (1,2, ..., n2)) AND
392 (keypart3 IN (1,2, ..., n3))
393
394 For this WHERE clause the list of intervals will have n1*n2*n3 intervals
395 of form
396
397 (keypart1, keypart2, keypart3) = (k1, k2, k3), where 1 <= k{i} <= n{i}
398
399 SEL_ARG graph structure aims to reduce the amount of required space by
400 "sharing" the elementary intervals when possible (the pic at the
401 beginning of this comment has examples of such sharing). The sharing may
402 prevent combinatorial blowup:
403
404 There are WHERE clauses that have combinatorial-size interval lists but
405 will be represented by a compact SEL_ARG graph.
406 Example:
407 (keypartN IN (1,2, ..., n1)) AND
408 ...
409 (keypart2 IN (1,2, ..., n2)) AND
410 (keypart1 IN (1,2, ..., n3))
411
412 but not in all cases:
413
414 - There are WHERE clauses that do have a compact SEL_ARG-graph
415 representation but get_mm_tree() and its callees will construct a
416 graph of combinatorial size.
417 Example:
418 (keypart1 IN (1,2, ..., n1)) AND
419 (keypart2 IN (1,2, ..., n2)) AND
420 ...
421 (keypartN IN (1,2, ..., n3))
422
423 - There are WHERE clauses for which the minimal possible SEL_ARG graph
424 representation will have combinatorial size.
425 Example:
426 By induction: Let's take any interval on some keypart in the middle:
427
428 kp15=c0
429
430 Then let's AND it with this interval 'structure' from preceding and
431 following keyparts:
432
433 (kp14=c1 AND kp16=c3) OR keypart14=c2) (*)
434
435 We will obtain this SEL_ARG graph:
436
437 kp14 $ kp15 $ kp16
438 $ $
439 +---------+ $ +---------+ $ +---------+
440 | kp14=c1 |--$-->| kp15=c0 |--$-->| kp16=c3 |
441 +---------+ $ +---------+ $ +---------+
442 | $ $
443 +---------+ $ +---------+ $
444 | kp14=c2 |--$-->| kp15=c0 | $
445 +---------+ $ +---------+ $
446 $ $
447
448 Note that we had to duplicate "kp15=c0" and there was no way to avoid
449 that.
450 The induction step: AND the obtained expression with another "wrapping"
451 expression like (*).
452 When the process ends because of the limit on max. number of keyparts
453 we'll have:
454
455 WHERE clause length is O(3*#max_keyparts)
456 SEL_ARG graph size is O(2^(#max_keyparts/2))
457
458 (it is also possible to construct a case where instead of 2 in 2^n we
459 have a bigger constant, e.g. 4, and get a graph with 4^(31/2)= 2^31
460 nodes)
461
462 We avoid consuming too much memory by setting a limit on the number of
463 SEL_ARG object we can construct during one range analysis invocation.
464*/
465
466class SEL_ARG {
467 public:
469
470 /**
471 maybe_flag signals that this range is AND-ed with some unknown range
472 (a MAYBE_KEY node). This means that the range could be smaller than
473 what it would otherwise denote; e.g., a range such as
474
475 (0 < x < 3) AND x=( SELECT ... )
476
477 could in reality be e.g. (1 < x < 2), depending on what the subselect
478 returns (and we don't know that when planning), but it could never be
479 bigger.
480
481 FIXME: It's unclear if this is really kept separately per SEL_ARG or is
482 meaningful only at the root node, and thus should be moved to the
483 SEL_ROOT. Most code seems to assume the latter, but a few select places,
484 non-root nodes appear to be modified.
485 */
486 bool maybe_flag{false};
487
488 /*
489 Which key part. TODO: This is the same for all values in a SEL_ROOT,
490 so we should move it there.
491 */
493
494 bool maybe_null() const { return field->is_nullable(); }
495
496 /**
497 The rtree index interval to scan, undefined unless
498 SEL_ARG::min_flag == GEOM_FLAG.
499 */
501
502 /*
503 TODO: This is the same for all values in a SEL_ROOT, so we should
504 move it there; however, be careful about cmp_* functions.
505 Note that this should never be nullptr except in the special case
506 where we have a dummy SEL_ARG to hold next_key_part only
507 (see SEL_ROOT::root for more information).
508 */
509 Field *field{nullptr};
510 uchar *min_value, *max_value; // Pointer to range
511
512 /*
513 eq_tree(), first(), last() etc require that left == right == NULL
514 if the type is MAYBE_KEY. Todo: fix this so SEL_ARGs without R-B
515 children are handled consistently. See related WL#5894.
516 */
517 SEL_ARG *left, *right; /* R-B tree children */
518 SEL_ARG *next, *prev; /* Links for bi-directional interval list */
519 SEL_ARG *parent{nullptr}; /* R-B tree parent (nullptr for root) */
520 /*
521 R-B tree of intervals covering keyparts consecutive to this
522 SEL_ARG. See documentation of SEL_ARG GRAPH semantics for details.
523 */
525
526 /**
527 Convenience function for removing the next_key_part. The typical
528 use for this function is to disconnect the next_key_part from the
529 root, send it to key_and() or key_or(), and then connect the
530 result of that function back to the SEL_ARG using set_next_key_part().
531
532 @return The previous value of next_key_part.
533 */
535 SEL_ROOT *ret = next_key_part;
536 if (next_key_part) {
537 assert(next_key_part->use_count > 0);
539 }
540 next_key_part = nullptr;
541 return ret;
542 }
543
544 /**
545 Convenience function for changing next_key_part, including
546 updating the use_count. The argument is allowed to be nullptr.
547
548 @param next_key_part_arg New value for next_key_part.
549 */
550 void set_next_key_part(SEL_ROOT *next_key_part_arg) {
552 next_key_part = next_key_part_arg;
554 }
555
557
558 bool is_ascending{true}; ///< true - ASC order, false - DESC
559
560 SEL_ARG() = default;
561 SEL_ARG(SEL_ARG &);
562 SEL_ARG(Field *, const uchar *, const uchar *, bool asc);
564 uint8 min_flag, uint8 max_flag, bool maybe_flag, bool asc,
565 ha_rkey_function gis_flag);
566 /**
567 Note that almost all SEL_ARGs are created on the MEM_ROOT,
568 so this destructor will only rarely be called.
569 */
571
572 /**
573 returns true if a range predicate is equal. Use all_same()
574 to check for equality of all the predicates on this keypart.
575 */
576 inline bool is_same(const SEL_ARG *arg) const {
577 if (part != arg->part) return false;
578 return cmp_min_to_min(arg) == 0 && cmp_max_to_max(arg) == 0;
579 }
580
581 inline void merge_flags(SEL_ARG *arg) { maybe_flag |= arg->maybe_flag; }
582 inline void maybe_smaller() { maybe_flag = true; }
583 /* Return true iff it's a single-point null interval */
584 inline bool is_null_interval() { return maybe_null() && max_value[0] == 1; }
585 inline int cmp_min_to_min(const SEL_ARG *arg) const {
586 return sel_cmp(field, min_value, arg->min_value, min_flag, arg->min_flag);
587 }
588 inline int cmp_min_to_max(const SEL_ARG *arg) const {
589 return sel_cmp(field, min_value, arg->max_value, min_flag, arg->max_flag);
590 }
591 inline int cmp_max_to_max(const SEL_ARG *arg) const {
592 return sel_cmp(field, max_value, arg->max_value, max_flag, arg->max_flag);
593 }
594 inline int cmp_max_to_min(const SEL_ARG *arg) const {
595 return sel_cmp(field, max_value, arg->min_value, max_flag, arg->min_flag);
596 }
598 MEM_ROOT *mem_root) { // Get intersection of ranges.
599 uchar *new_min, *new_max;
600 uint8 flag_min, flag_max;
601 if (cmp_min_to_min(arg) >= 0) {
602 new_min = min_value;
603 flag_min = min_flag;
604 } else {
605 new_min = arg->min_value;
606 flag_min = arg->min_flag; /* purecov: deadcode */
607 }
608 if (cmp_max_to_max(arg) <= 0) {
609 new_max = max_value;
610 flag_max = max_flag;
611 } else {
612 new_max = arg->max_value;
613 flag_max = arg->max_flag;
614 }
615 return new (mem_root)
616 SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
619 }
621 MEM_ROOT *mem_root) { // arg->min <= X < arg->min
622 return new (mem_root) SEL_ARG(
624 arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX, maybe_flag || arg->maybe_flag,
626 }
628 MEM_ROOT *mem_root) { // arg->min <= X <= key_max
629 return new (mem_root)
633 }
634 SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
635
636 bool copy_min(SEL_ARG *arg) { // max(this->min, arg->min) <= x <= this->max
637 if (cmp_min_to_min(arg) > 0) {
638 min_value = arg->min_value;
639 min_flag = arg->min_flag;
641 return true; // Full range
642 }
643 maybe_flag |= arg->maybe_flag;
644 return false;
645 }
646 bool copy_max(SEL_ARG *arg) { // this->min <= x <= min(this->max, arg->max)
647 if (cmp_max_to_max(arg) <= 0) {
648 max_value = arg->max_value;
649 max_flag = arg->max_flag;
651 return true; // Full range
652 }
653 maybe_flag |= arg->maybe_flag;
654 return false;
655 }
656
658 min_value = arg->min_value;
659 min_flag = arg->min_flag;
660 }
662 max_value = arg->min_value;
663 max_flag = arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
664 }
666 min_value = arg->max_value;
667 min_flag = arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
668 }
669
670 /**
671 Set spatial index range scan parameters. This object will be used to do
672 spatial index range scan after this call.
673
674 @param rkey_func The scan function to perform. It must be one of the
675 spatial index specific scan functions.
676 */
678 assert(rkey_func >= HA_READ_MBR_CONTAIN && rkey_func <= HA_READ_MBR_EQUAL);
680 rkey_func_flag = rkey_func;
682 }
683
684 /* returns a number of keypart values (0 or 1) appended to the key buffer */
685 int store_min_value(uint length, uchar **min_key, uint min_key_flag) {
686 /* "(kp1 > c1) AND (kp2 OP c2) AND ..." -> (kp1 > c1) */
687 if ((min_flag & GEOM_FLAG) ||
688 (!(min_flag & NO_MIN_RANGE) &&
689 !(min_key_flag & (NO_MIN_RANGE | NEAR_MIN)))) {
690 if (maybe_null() && *min_value) {
691 **min_key = 1;
692 memset(*min_key + 1, 0, length - 1);
693 } else
694 memcpy(*min_key, min_value, length);
695 (*min_key) += length;
696 return 1;
697 }
698 return 0;
699 }
700 /* returns a number of keypart values (0 or 1) appended to the key buffer */
701 int store_max_value(uint length, uchar **max_key, uint max_key_flag) {
702 if (!(max_flag & NO_MAX_RANGE) &&
703 !(max_key_flag & (NO_MAX_RANGE | NEAR_MAX))) {
704 if (maybe_null() && *max_value) {
705 **max_key = 1;
706 memset(*max_key + 1, 0, length - 1);
707 } else
708 memcpy(*max_key, max_value, length);
709 (*max_key) += length;
710 return 1;
711 }
712 return 0;
713 }
714
715 /*
716 Returns a number of keypart values appended to the key buffer
717 for min key and max key. This function is used by both Range
718 Analysis and Partition pruning. For partition pruning we have
719 to ensure that we don't store also subpartition fields. Thus
720 we have to stop at the last partition part and not step into
721 the subpartition fields. For Range Analysis we set last_part
722 to MAX_KEY which we should never reach.
723
724 Note: Caller of this function should take care of sending the
725 correct flags and correct key to be stored into. In case of
726 ascending indexes, store_min_key() gets called to store the
727 min_value to range start_key. In case of descending indexes, it's
728 called for storing min_value to range end_key.
729 */
730 /**
731 Helper function for storing min/max values of SEL_ARG taking into account
732 key part's order.
733 */
734 void store_min_max_values(uint length, uchar **min_key, uint min_flag,
735 uchar **max_key, uint max_flag, int *min_part,
736 int *max_part) {
737 if (is_ascending) {
738 *min_part += store_min_value(length, min_key, min_flag);
739 *max_part += store_max_value(length, max_key, max_flag);
740 } else {
741 *max_part += store_min_value(length, max_key, min_flag);
742 *min_part += store_max_value(length, min_key, max_flag);
743 }
744 }
745
746 /**
747 Helper function for storing min/max keys of next SEL_ARG taking into
748 account key part's order.
749
750 @note On checking min/max flags: Flags are used to track whether there's
751 a partial key in the key buffer. So for ASC key parts the flag
752 corresponding to the key being added to should be checked, not
753 corresponding to the value being added. I.e min_flag for min_key.
754 For DESC key parts it's opposite - max_flag for min_key. It's flag
755 of prev key part that should be checked.
756
757 */
759 uint *cur_min_flag, uchar **cur_max_key,
760 uint *cur_max_flag, int *min_part,
761 int *max_part) {
762 assert(next_key_part);
763 const bool asc = next_key_part->root->is_ascending;
764 if (!get_min_flag()) {
765 if (asc)
766 *min_part += next_key_part->store_min_key(key, cur_min_key,
767 cur_min_flag, MAX_KEY, true);
768 else {
769 uint tmp_flag = invert_min_flag(*cur_min_flag);
770 *min_part += next_key_part->store_max_key(key, cur_min_key, &tmp_flag,
771 MAX_KEY, true);
772 *cur_min_flag = invert_max_flag(tmp_flag);
773 }
774 }
775 if (!get_max_flag()) {
776 if (asc)
777 *max_part += next_key_part->store_max_key(key, cur_max_key,
778 cur_max_flag, MAX_KEY, false);
779 else {
780 uint tmp_flag = invert_max_flag(*cur_max_flag);
781 *max_part += next_key_part->store_min_key(key, cur_max_key, &tmp_flag,
782 MAX_KEY, false);
783 *cur_max_flag = invert_min_flag(tmp_flag);
784 }
785 }
786 }
787
788 SEL_ARG *rb_insert(SEL_ARG *leaf);
789 friend SEL_ARG *rb_delete_fixup(SEL_ARG *root, SEL_ARG *key, SEL_ARG *par);
790#ifndef NDEBUG
791 friend int test_rb_tree(SEL_ARG *element, SEL_ARG *parent);
792#endif
793 SEL_ARG *first();
794 const SEL_ARG *first() const;
795 SEL_ARG *last();
796 void make_root() {
798 color = BLACK;
799 parent = next = prev = nullptr;
800 }
801
802 inline SEL_ARG **parent_ptr() {
803 return parent->left == this ? &parent->left : &parent->right;
804 }
805
806 /*
807 Check if this SEL_ARG object represents a single-point interval
808
809 SYNOPSIS
810 is_singlepoint()
811
812 DESCRIPTION
813 Check if this SEL_ARG object (not tree) represents a single-point
814 interval, i.e. if it represents a "keypart = const" or
815 "keypart IS NULL".
816
817 RETURN
818 true This SEL_ARG object represents a singlepoint interval
819 false Otherwise
820 */
821
822 bool is_singlepoint() const {
823 /*
824 Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field)
825 flags, and the same for right edge.
826 */
827 if (min_flag || max_flag) return false;
828 uchar *min_val = min_value;
829 uchar *max_val = max_value;
830
831 if (maybe_null()) {
832 /* First byte is a NULL value indicator */
833 if (*min_val != *max_val) return false;
834
835 if (*min_val) return true; /* This "x IS NULL" */
836 min_val++;
837 max_val++;
838 }
839 return !field->key_cmp(min_val, max_val);
840 }
841 /**
842 Return correct min_flag.
843
844 For DESC key parts max flag should be used as min flag, but in order to
845 be checked correctly, max flag should be flipped as code doesn't expect
846 e.g NEAR_MAX in min flag.
847 */
850 }
851 /**
852 Return correct max_flag.
853
854 For DESC key parts min flag should be used as max flag, but in order to
855 be checked correctly, min flag should be flipped as code doesn't expect
856 e.g NEAR_MIN in max flag.
857 */
860 }
861};
862
863inline bool SEL_ROOT::is_always() const {
864 return type == Type::KEY_RANGE && elements == 1 && !root->maybe_flag &&
866}
867
868inline bool SEL_ROOT::simple_key() const {
869 return elements == 1 && !root->next_key_part;
870}
871
872class SEL_TREE {
873 public:
874 /**
875 Starting an effort to document this field:
876
877 IMPOSSIBLE: if keys[i]->type == SEL_ROOT::Type::IMPOSSIBLE for some i,
878 then type == SEL_TREE::IMPOSSIBLE. Rationale: if the predicate for
879 one of the indexes is always false, then the full predicate is also
880 always false.
881
882 ALWAYS: if either (keys[i]->is_always()) or (keys[i] == NULL) for all i,
883 then type == SEL_TREE::ALWAYS. Rationale: the range access method
884 will not be able to filter out any rows when there are no range
885 predicates that can be used to filter on any index.
886
887 KEY: There are range predicates that can be used on at least one
888 index.
889 */
891
892 /**
893 Whether this SEL_TREE is an inexact (too broad) representation of the
894 predicates it is based on; that is, if it does not necessarily subsume
895 all of them. Note that a nullptr return from get_mm_tree() (which means
896 “could not generate a tree from this predicate”) is by definition inexact.
897
898 There are two main ways a SEL_TREE can become inexact:
899
900 - The predicate references fields not contained in any indexes tracked
901 by the SEL_TREE.
902 - The predicate could be of a form that is not representable as a range.
903 E.g., x > 30 is a range, x mod 2 = 1 is not (although it could
904 in theory be converted to a large amount of disjunct ranges).
905
906 If a SEL_TREE is inexact, the predicates must be rechecked after the
907 range scan, using a filter. (Note that it is never too narrow, only ever
908 exact or too broad.) The old join optimizer always does this, no matter
909 what the inexact flag is set to.
910
911 Note that additional checks are needed to subsume a predicate even if
912 inexact == false. In particular, SEL_TREE contains information for all
913 indexes over a table, but if a regular range scan is chosen, it can use
914 only one index. So one must then go through all predicates to see if they
915 refer to fields not contained in the given index. Furthermore, range scans
916 on composite (multi-part) indexes can drop predicates on the later keyparts
917 (making predicates on those keyparts inexact), since range scans only
918 support inequalities on the last keypart in any given range. This check
919 must be done in get_ranges_from_tree().
920 */
921 bool inexact = false;
922
923 SEL_TREE(enum Type type_arg, MEM_ROOT *root, size_t num_keys)
924 : type(type_arg), keys(root, num_keys), n_ror_scans(0) {}
925 SEL_TREE(MEM_ROOT *root, size_t num_keys)
926 : type(KEY), keys(root, num_keys), n_ror_scans(0) {}
927 /**
928 Constructor that performs deep-copy of the SEL_ARG trees in
929 'keys[]' and the index merge alternatives in 'merges'.
930
931 @param arg The SEL_TREE to copy
932 @param param Parameters for range analysis
933 */
934 SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param);
935 /*
936 Possible ways to read rows using a single index because the
937 conditions of the query consists of single-index conjunctions:
938
939 (ranges_for_idx_1) AND (ranges_for_idx_2) AND ...
940
941 The SEL_ARG graph for each non-NULL element in keys[] may consist
942 of many single-index ranges (disjunctions), so ranges_for_idx_1
943 may e.g. be:
944
945 "idx_field1 = 1 OR (idx_field1 > 5 AND idx_field2 = 10)"
946
947 assuming that index1 is a composite index covering
948 (idx_field1,...,idx_field2,..)
949
950 Index merge intersection intersects ranges on SEL_ARGs from two or
951 more indexes.
952
953 Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
954 keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
955 merit in range analyzer functions (e.g. get_mm_parts) returning a
956 pointer to such SEL_TREE instead of NULL)
957
958 Note: If you want to set an element in keys[], use set_key()
959 or release_key() to make sure the SEL_ARG's use_count is correctly
960 updated.
961 */
963 Key_map keys_map; /* bitmask of non-NULL elements in keys */
964
965 /*
966 Possible ways to read rows using Index merge (sort) union.
967
968 Each element in 'merges' consists of multi-index disjunctions,
969 which means that Index merge (sort) union must be applied to read
970 rows. The nodes in the 'merges' list forms a conjunction of such
971 multi-index disjunctions.
972
973 The list is non-empty only if type==KEY.
974 */
976
977 /* The members below are filled/used only after get_mm_tree is done */
978 Key_map ror_scans_map; /* bitmask of ROR scan-able elements in keys */
979 uint n_ror_scans; /* number of set bits in ror_scans_map */
980
981 /**
982 Convenience function for removing an element in keys[]. The typical
983 use for this function is to disconnect the next_key_part from the
984 root, send it to key_and() or key_or(), and then connect the
985 result of that function back to the SEL_ROOT using set_key().
986
987 @param index Which index slot to release.
988 @return The value in the slot (before removal).
989 */
991 SEL_ROOT *ret = keys[index];
992 if (keys[index]) {
993 assert(keys[index]->use_count > 0);
994 --keys[index]->use_count;
995 }
996 keys[index] = nullptr;
997 return ret;
998 }
999
1000 /**
1001 Convenience function for changing an element in keys[], including
1002 updating the use_count.
1003
1004 @param index Which index slot to change.
1005 @param key The new contents of the index slot. Is allowed to be nullptr.
1006 */
1009 keys[index] = key;
1010 if (key) ++key->use_count;
1011 }
1012};
1013
1014#ifndef NDEBUG
1015void print_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree, Key_map *tree_map,
1016 const char *msg);
1017#endif
1018
1019/*
1020 Get the SEL_ARG tree 'tree' for the keypart covering 'field', if
1021 any. 'tree' must be a unique conjunction to ALL predicates in earlier
1022 keyparts of 'keypart_tree'.
1023
1024 E.g., if 'keypart_tree' is for a composite index (kp1,kp2) and kp2
1025 covers 'field', all these conditions satisfies the requirement:
1026
1027 1. "(kp1=2 OR kp1=3) AND kp2=10" => returns "kp2=10"
1028 2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=10)" => returns "kp2=10"
1029 3. "(kp1=2 AND (kp2=10 OR kp2=11)) OR (kp1=3 AND (kp2=10 OR kp2=11))"
1030 => returns "kp2=10 OR kp2=11"
1031
1032 whereas these do not
1033 1. "(kp1=2 AND kp2=10) OR kp1=3"
1034 2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=11)"
1035 3. "(kp1=2 AND kp2=10) OR (kp1=3 AND (kp2=10 OR kp2=11))"
1036
1037 This function effectively tests requirement WA2.
1038
1039 @param[in] key_part_num Key part number we want the SEL_ARG tree for
1040 @param[in] keypart_tree The SEL_ARG* tree for the index
1041 @param[out] cur_range The SEL_ARG tree, if any, for the keypart
1042
1043 @retval true 'keypart_tree' contained a predicate for key part that
1044 is not conjunction to all predicates on earlier keyparts
1045 @retval false otherwise
1046*/
1047bool get_sel_root_for_keypart(uint key_part_num, SEL_ROOT *keypart_tree,
1048 SEL_ROOT **cur_range);
1049
1050/*
1051 Find the SEL_ROOT tree that corresponds to the chosen index.
1052
1053 SYNOPSIS
1054 get_index_range_tree()
1055 index [in] The ID of the index being looked for
1056 range_tree[in] Tree of ranges being searched
1057 param [in] RANGE_OPT_PARAM from test_quick_select
1058
1059 DESCRIPTION
1060
1061 A SEL_TREE contains range trees for all usable indexes. This procedure
1062 finds the SEL_ROOT tree for 'index'. The members of a SEL_TREE are
1063 ordered in the same way as the members of RANGE_OPT_PARAM::key, thus we
1064 first find the corresponding index in the array RANGE_OPT_PARAM::key.
1065 This index is returned through the variable param_idx, to be used later
1066 as argument of check_quick_select().
1067
1068 RETURN
1069 Pointer to the SEL_ROOT tree that corresponds to index.
1070*/
1071
1073 RANGE_OPT_PARAM *param) {
1074 uint idx = 0; /* Index nr in param->key_parts */
1075 while (idx < param->keys) {
1076 if (index == param->real_keynr[idx]) break;
1077 idx++;
1078 }
1079 return (range_tree->keys[idx]);
1080}
1081
1082/**
1083 Print the ranges in a SEL_TREE to debug log.
1084
1085 @param tree_name Descriptive name of the tree
1086 @param tree The SEL_TREE that will be printed to debug log
1087 @param param RANGE_OPT_PARAM from test_quick_select
1088*/
1089inline void dbug_print_tree([[maybe_unused]] const char *tree_name,
1090 [[maybe_unused]] SEL_TREE *tree,
1091 [[maybe_unused]] const RANGE_OPT_PARAM *param) {
1092#ifndef NDEBUG
1093 if (_db_enabled_()) print_tree(nullptr, tree_name, tree, param, true);
1094#endif
1095}
1096
1097#endif // SQL_RANGE_OPTIMIZER_TREE_H_
Definition: sql_bitmap.h:154
Used to store optimizer cost estimates.
Definition: handler.h:3905
Definition: field.h:573
bool is_nullable() const
Definition: field.h:1298
virtual int key_cmp(const uchar *a, const uchar *b) const
Definition: field.h:1203
Definition: key.h:113
Definition: sql_list.h:494
A typesafe replacement for DYNAMIC_ARRAY.
Definition: mem_root_array.h:432
Definition: range_opt_param.h:29
uint * real_keynr
Definition: range_opt_param.h:69
Definition: tree.h:466
void set_next_key_part(SEL_ROOT *next_key_part_arg)
Convenience function for changing next_key_part, including updating the use_count.
Definition: tree.h:550
SEL_ARG * first()
This gives the first SEL_ARG in the interval list, and the minimal element in the red-black tree.
Definition: tree.cc:386
void copy_max_to_min(SEL_ARG *arg)
Definition: tree.h:665
bool copy_min(SEL_ARG *arg)
Definition: tree.h:636
SEL_ARG * last()
Definition: tree.cc:397
SEL_ARG * rb_insert(SEL_ARG *leaf)
Definition: tree.cc:1886
void set_gis_index_read_function(const enum ha_rkey_function rkey_func)
Set spatial index range scan parameters.
Definition: tree.h:677
bool is_same(const SEL_ARG *arg) const
returns true if a range predicate is equal.
Definition: tree.h:576
bool maybe_flag
maybe_flag signals that this range is AND-ed with some unknown range (a MAYBE_KEY node).
Definition: tree.h:486
SEL_ROOT * release_next_key_part()
Convenience function for removing the next_key_part.
Definition: tree.h:534
enum ha_rkey_function rkey_func_flag
The rtree index interval to scan, undefined unless SEL_ARG::min_flag == GEOM_FLAG.
Definition: tree.h:500
bool is_ascending
true - ASC order, false - DESC
Definition: tree.h:558
SEL_ARG * clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next)
Definition: tree.cc:345
void copy_min_to_min(SEL_ARG *arg)
Definition: tree.h:657
SEL_ROOT * next_key_part
Definition: tree.h:524
uchar * min_value
Definition: tree.h:510
void copy_min_to_max(SEL_ARG *arg)
Definition: tree.h:661
uint8 part
Definition: tree.h:492
Field * field
Definition: tree.h:509
SEL_ARG * prev
Definition: tree.h:518
void make_root()
Definition: tree.h:796
SEL_ARG * clone_and(SEL_ARG *arg, MEM_ROOT *mem_root)
Definition: tree.h:597
int store_min_value(uint length, uchar **min_key, uint min_key_flag)
Definition: tree.h:685
uint get_min_flag()
Return correct min_flag.
Definition: tree.h:848
bool maybe_null() const
Definition: tree.h:494
SEL_ARG * parent
Definition: tree.h:519
leaf_color
Definition: tree.h:556
@ RED
Definition: tree.h:556
@ BLACK
Definition: tree.h:556
bool is_null_interval()
Definition: tree.h:584
int cmp_max_to_max(const SEL_ARG *arg) const
Definition: tree.h:591
uchar * max_value
Definition: tree.h:510
void maybe_smaller()
Definition: tree.h:582
SEL_ARG * right
Definition: tree.h:517
~SEL_ARG()
Note that almost all SEL_ARGs are created on the MEM_ROOT, so this destructor will only rarely be cal...
Definition: tree.h:570
void store_min_max_values(uint length, uchar **min_key, uint min_flag, uchar **max_key, uint max_flag, int *min_part, int *max_part)
Helper function for storing min/max values of SEL_ARG taking into account key part's order.
Definition: tree.h:734
enum SEL_ARG::leaf_color color
int store_max_value(uint length, uchar **max_key, uint max_key_flag)
Definition: tree.h:701
SEL_ARG()=default
uint8 max_flag
Definition: tree.h:468
SEL_ARG * left
Definition: tree.h:517
void store_next_min_max_keys(KEY_PART *key, uchar **cur_min_key, uint *cur_min_flag, uchar **cur_max_key, uint *cur_max_flag, int *min_part, int *max_part)
Helper function for storing min/max keys of next SEL_ARG taking into account key part's order.
Definition: tree.h:758
SEL_ARG * next
Definition: tree.h:518
int cmp_max_to_min(const SEL_ARG *arg) const
Definition: tree.h:594
int cmp_min_to_max(const SEL_ARG *arg) const
Definition: tree.h:588
bool copy_max(SEL_ARG *arg)
Definition: tree.h:646
uint get_max_flag()
Return correct max_flag.
Definition: tree.h:858
uint8 min_flag
Definition: tree.h:468
bool is_singlepoint() const
Definition: tree.h:822
void merge_flags(SEL_ARG *arg)
Definition: tree.h:581
SEL_ARG ** parent_ptr()
Definition: tree.h:802
SEL_ARG * clone_last(SEL_ARG *arg, MEM_ROOT *mem_root)
Definition: tree.h:627
friend int test_rb_tree(SEL_ARG *element, SEL_ARG *parent)
Definition: tree.cc:2007
friend SEL_ARG * rb_delete_fixup(SEL_ARG *root, SEL_ARG *key, SEL_ARG *par)
Definition: tree.cc:1939
SEL_ARG * clone_first(SEL_ARG *arg, MEM_ROOT *mem_root)
Definition: tree.h:620
int cmp_min_to_min(const SEL_ARG *arg) const
Definition: tree.h:585
A graph of (possible multiple) key ranges, represented as a red-black binary tree.
Definition: tree.h:69
size_t elements
Number of nodes in the RB-tree, not including sentinels.
Definition: tree.h:219
Type
Used to indicate if the range predicate for an index is always true/false, depends on values from oth...
Definition: tree.h:76
@ KEY_RANGE
There is a range condition that can be used on this index.
@ MAYBE_KEY
There is a range predicate that refers to another table.
@ IMPOSSIBLE
The range predicate for this index is always false.
bool test_use_count(const SEL_ROOT *root) const
Check if SEL_ROOT::use_count value is correct.
Definition: tree.cc:2102
SEL_ROOT(SEL_ARG *root)
Constructs a tree of type KEY_RANGE, using the given root.
Definition: tree.cc:460
~SEL_ROOT()
Note that almost all SEL_ROOTs are created on the MEM_ROOT, so this destructor will only rarely be ca...
Definition: tree.h:111
int store_min_key(KEY_PART *key, uchar **range_key, uint *range_key_flag, uint last_part, bool start_key)
Returns a number of keypart values appended to the key buffer for min key and max key.
Definition: tree.cc:87
SEL_ROOT * clone_tree(RANGE_OPT_PARAM *param) const
Create a new tree that's a duplicate of this one.
Definition: tree.cc:475
SEL_ARG * find_range(const SEL_ARG *key) const
Find best key with min <= given key.
Definition: tree.cc:1767
void free_tree()
Signal to the tree that the caller will shortly be dropping it on the floor; if others are still usin...
Definition: tree.cc:151
SEL_ARG * root
The root node of the tree.
Definition: tree.h:204
bool simple_key() const
Returns true iff this is a single-element, single-field predicate.
Definition: tree.h:868
void insert(SEL_ARG *key)
Insert the given node into the tree, and update the root.
Definition: tree.cc:1717
bool is_always() const
Returns true iff we have a single node that has no max nor min.
Definition: tree.h:863
void tree_delete(SEL_ARG *key)
Delete the given node from the tree, and update the root.
Definition: tree.cc:1794
int store_max_key(KEY_PART *key, uchar **range_key, uint *range_key_flag, uint last_part, bool start_key)
Definition: tree.cc:124
enum SEL_ROOT::Type type
ulong use_count
Number of references to this SEL_ARG tree.
Definition: tree.h:214
Definition: tree.h:872
enum SEL_TREE::Type type
Key_map keys_map
Definition: tree.h:963
uint n_ror_scans
Definition: tree.h:979
bool inexact
Whether this SEL_TREE is an inexact (too broad) representation of the predicates it is based on; that...
Definition: tree.h:921
List< SEL_IMERGE > merges
Definition: tree.h:975
SEL_TREE(MEM_ROOT *root, size_t num_keys)
Definition: tree.h:925
SEL_ROOT * release_key(int index)
Convenience function for removing an element in keys[].
Definition: tree.h:990
Key_map ror_scans_map
Definition: tree.h:978
Type
Starting an effort to document this field:
Definition: tree.h:890
@ IMPOSSIBLE
Definition: tree.h:890
@ ALWAYS
Definition: tree.h:890
void set_key(int index, SEL_ROOT *key)
Convenience function for changing an element in keys[], including updating the use_count.
Definition: tree.h:1007
SEL_TREE(enum Type type_arg, MEM_ROOT *root, size_t num_keys)
Definition: tree.h:923
Mem_root_array< SEL_ROOT * > keys
Definition: tree.h:962
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
static uint16 key1[1001]
Definition: hp_test2.cc:50
static uint keys
Definition: hp_test2.cc:49
void print_tree(String *out, const char *tree_name, SEL_TREE *tree, const RANGE_OPT_PARAM *param, const bool print_full)
Definition: range_optimizer.cc:1692
This file follows Google coding style, except for the name MEM_ROOT (which is kept for historical rea...
This file includes constants used by all storage engines.
ha_rkey_function
Definition: my_base.h:78
@ HA_READ_MBR_EQUAL
Definition: my_base.h:91
@ HA_READ_INVALID
Definition: my_base.h:93
@ HA_READ_MBR_CONTAIN
Definition: my_base.h:87
@ NEAR_MIN
Definition: my_base.h:1083
@ NO_MIN_RANGE
from -inf
Definition: my_base.h:1080
@ NO_MAX_RANGE
to +inf
Definition: my_base.h:1081
@ NEAR_MAX
Definition: my_base.h:1085
@ GEOM_FLAG
This flag means that the index is an rtree index, and the interval is specified using HA_READ_MBR_XXX...
Definition: my_base.h:1107
int _db_enabled_()
Definition: dbug.cc:1279
Some integer typedefs for easier portability.
uint8_t uint8
Definition: my_inttypes.h:63
unsigned char uchar
Definition: my_inttypes.h:52
bool length(const dd::Spatial_reference_system *srs, const Geometry *g1, double *length, bool *null) noexcept
Computes the length of linestrings and multilinestrings.
Definition: length.cc:76
bool index(const std::string &value, const String &search_for, uint32_t *idx)
Definition: contains.h:75
MediaType
Definition: media_type.h:33
SEL_ARG * null_element
Definition: range_optimizer.cc:157
required string key
Definition: replication_asynchronous_connection_failover.proto:60
File containing constants that can be used throughout the server.
constexpr const unsigned int MAX_KEY
Definition: sql_const.h:45
Definition: range_optimizer.h:55
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: rowid_ordered_retrieval_plan.h:44
void dbug_print_tree(const char *tree_name, SEL_TREE *tree, const RANGE_OPT_PARAM *param)
Print the ranges in a SEL_TREE to debug log.
Definition: tree.h:1089
SEL_ROOT * key_and(RANGE_OPT_PARAM *param, SEL_ROOT *key1, SEL_ROOT *key2)
Definition: tree.cc:886
bool sel_trees_can_be_ored(SEL_TREE *tree1, SEL_TREE *tree2, RANGE_OPT_PARAM *param)
Definition: tree.cc:591
SEL_ROOT * get_index_range_tree(uint index, SEL_TREE *range_tree, RANGE_OPT_PARAM *param)
Definition: tree.h:1072
void print_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree, Key_map *tree_map, const char *msg)
Definition: tree.cc:2177
uint invert_min_flag(uint min_flag)
A helper function to invert min flags to max flags for DESC key parts.
Definition: tree.h:229
SEL_ROOT * key_or(RANGE_OPT_PARAM *param, SEL_ROOT *key1, SEL_ROOT *key2)
Combine two range expression under a common OR.
Definition: tree.cc:1079
SEL_TREE * tree_or(RANGE_OPT_PARAM *param, bool remove_jump_scans, SEL_TREE *tree1, SEL_TREE *tree2)
Definition: tree.cc:683
uint invert_max_flag(uint max_flag)
A helper function to invert max flags to min flags for DESC key parts.
Definition: tree.h:241
int sel_cmp(Field *f, uchar *a, uchar *b, uint8 a_flag, uint8 b_flag)
Definition: tree.cc:409
bool get_sel_root_for_keypart(uint key_part_num, SEL_ROOT *keypart_tree, SEL_ROOT **cur_range)
Definition: tree.cc:2136
SEL_TREE * tree_and(RANGE_OPT_PARAM *param, SEL_TREE *tree1, SEL_TREE *tree2)
Definition: tree.cc:502