MySQL 26.7.0
Source Code Documentation
tree.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2026, 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() {
585 return !min_flag && !max_flag && maybe_null() && max_value[0] == 1;
586 }
587 inline int cmp_min_to_min(const SEL_ARG *arg) const {
588 return sel_cmp(field, min_value, arg->min_value, min_flag, arg->min_flag);
589 }
590 inline int cmp_min_to_max(const SEL_ARG *arg) const {
591 return sel_cmp(field, min_value, arg->max_value, min_flag, arg->max_flag);
592 }
593 inline int cmp_max_to_max(const SEL_ARG *arg) const {
594 return sel_cmp(field, max_value, arg->max_value, max_flag, arg->max_flag);
595 }
596 inline int cmp_max_to_min(const SEL_ARG *arg) const {
597 return sel_cmp(field, max_value, arg->min_value, max_flag, arg->min_flag);
598 }
600 MEM_ROOT *mem_root) { // Get intersection of ranges.
601 uchar *new_min, *new_max;
602 uint8 flag_min, flag_max;
603 if (cmp_min_to_min(arg) >= 0) {
604 new_min = min_value;
605 flag_min = min_flag;
606 } else {
607 new_min = arg->min_value;
608 flag_min = arg->min_flag; /* purecov: deadcode */
609 }
610 if (cmp_max_to_max(arg) <= 0) {
611 new_max = max_value;
612 flag_max = max_flag;
613 } else {
614 new_max = arg->max_value;
615 flag_max = arg->max_flag;
616 }
617 return new (mem_root)
618 SEL_ARG(field, part, new_min, new_max, flag_min, flag_max,
621 }
623 MEM_ROOT *mem_root) { // arg->min <= X < arg->min
624 return new (mem_root) SEL_ARG(
626 arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX, maybe_flag || arg->maybe_flag,
628 }
630 MEM_ROOT *mem_root) { // arg->min <= X <= key_max
631 return new (mem_root)
635 }
636 SEL_ARG *clone(RANGE_OPT_PARAM *param, SEL_ARG *new_parent, SEL_ARG **next);
637
638 bool copy_min(SEL_ARG *arg) { // max(this->min, arg->min) <= x <= this->max
639 if (cmp_min_to_min(arg) > 0) {
640 min_value = arg->min_value;
641 min_flag = arg->min_flag;
643 return true; // Full range
644 }
645 maybe_flag |= arg->maybe_flag;
646 return false;
647 }
648 bool copy_max(SEL_ARG *arg) { // this->min <= x <= min(this->max, arg->max)
649 if (cmp_max_to_max(arg) <= 0) {
650 max_value = arg->max_value;
651 max_flag = arg->max_flag;
653 return true; // Full range
654 }
655 maybe_flag |= arg->maybe_flag;
656 return false;
657 }
658
660 min_value = arg->min_value;
661 min_flag = arg->min_flag;
662 }
664 max_value = arg->min_value;
665 max_flag = arg->min_flag & NEAR_MIN ? 0 : NEAR_MAX;
666 }
668 min_value = arg->max_value;
669 min_flag = arg->max_flag & NEAR_MAX ? 0 : NEAR_MIN;
670 }
671
672 /**
673 Set spatial index range scan parameters. This object will be used to do
674 spatial index range scan after this call.
675
676 @param rkey_func The scan function to perform. It must be one of the
677 spatial index specific scan functions.
678 */
680 assert(rkey_func >= HA_READ_MBR_CONTAIN && rkey_func <= HA_READ_MBR_EQUAL);
682 rkey_func_flag = rkey_func;
684 }
685
686 /* returns a number of keypart values (0 or 1) appended to the key buffer */
687 int store_min_value(uint length, uchar **min_key, uint min_key_flag) {
688 /* "(kp1 > c1) AND (kp2 OP c2) AND ..." -> (kp1 > c1) */
689 if ((min_flag & GEOM_FLAG) ||
690 (!(min_flag & NO_MIN_RANGE) &&
691 !(min_key_flag & (NO_MIN_RANGE | NEAR_MIN)))) {
692 if (maybe_null() && *min_value) {
693 **min_key = 1;
694 memset(*min_key + 1, 0, length - 1);
695 } else
696 memcpy(*min_key, min_value, length);
697 (*min_key) += length;
698 return 1;
699 }
700 return 0;
701 }
702 /* returns a number of keypart values (0 or 1) appended to the key buffer */
703 int store_max_value(uint length, uchar **max_key, uint max_key_flag) {
704 if (!(max_flag & NO_MAX_RANGE) &&
705 !(max_key_flag & (NO_MAX_RANGE | NEAR_MAX))) {
706 if (maybe_null() && *max_value) {
707 **max_key = 1;
708 memset(*max_key + 1, 0, length - 1);
709 } else
710 memcpy(*max_key, max_value, length);
711 (*max_key) += length;
712 return 1;
713 }
714 return 0;
715 }
716
717 /*
718 Returns a number of keypart values appended to the key buffer
719 for min key and max key. This function is used by both Range
720 Analysis and Partition pruning. For partition pruning we have
721 to ensure that we don't store also subpartition fields. Thus
722 we have to stop at the last partition part and not step into
723 the subpartition fields. For Range Analysis we set last_part
724 to MAX_KEY which we should never reach.
725
726 Note: Caller of this function should take care of sending the
727 correct flags and correct key to be stored into. In case of
728 ascending indexes, store_min_key() gets called to store the
729 min_value to range start_key. In case of descending indexes, it's
730 called for storing min_value to range end_key.
731 */
732 /**
733 Helper function for storing min/max values of SEL_ARG taking into account
734 key part's order.
735 */
736 void store_min_max_values(uint length, uchar **min_key, uint min_flag,
737 uchar **max_key, uint max_flag, int *min_part,
738 int *max_part) {
739 if (is_ascending) {
740 *min_part += store_min_value(length, min_key, min_flag);
741 *max_part += store_max_value(length, max_key, max_flag);
742 } else {
743 *max_part += store_min_value(length, max_key, min_flag);
744 *min_part += store_max_value(length, min_key, max_flag);
745 }
746 }
747
748 /**
749 Helper function for storing min/max keys of next SEL_ARG taking into
750 account key part's order.
751
752 @note On checking min/max flags: Flags are used to track whether there's
753 a partial key in the key buffer. So for ASC key parts the flag
754 corresponding to the key being added to should be checked, not
755 corresponding to the value being added. I.e min_flag for min_key.
756 For DESC key parts it's opposite - max_flag for min_key. It's flag
757 of prev key part that should be checked.
758
759 */
761 uint *cur_min_flag, uchar **cur_max_key,
762 uint *cur_max_flag, int *min_part,
763 int *max_part) {
764 assert(next_key_part);
765 const bool asc = next_key_part->root->is_ascending;
766 if (!get_min_flag()) {
767 if (asc)
768 *min_part += next_key_part->store_min_key(key, cur_min_key,
769 cur_min_flag, MAX_KEY, true);
770 else {
771 uint tmp_flag = invert_min_flag(*cur_min_flag);
772 *min_part += next_key_part->store_max_key(key, cur_min_key, &tmp_flag,
773 MAX_KEY, true);
774 *cur_min_flag = invert_max_flag(tmp_flag);
775 }
776 }
777 if (!get_max_flag()) {
778 if (asc)
779 *max_part += next_key_part->store_max_key(key, cur_max_key,
780 cur_max_flag, MAX_KEY, false);
781 else {
782 uint tmp_flag = invert_max_flag(*cur_max_flag);
783 *max_part += next_key_part->store_min_key(key, cur_max_key, &tmp_flag,
784 MAX_KEY, false);
785 *cur_max_flag = invert_min_flag(tmp_flag);
786 }
787 }
788 }
789
790 SEL_ARG *rb_insert(SEL_ARG *leaf);
791 friend SEL_ARG *rb_delete_fixup(SEL_ARG *root, SEL_ARG *key, SEL_ARG *par);
792#ifndef NDEBUG
793 friend int test_rb_tree(SEL_ARG *element, SEL_ARG *parent);
794#endif
795 SEL_ARG *first();
796 const SEL_ARG *first() const;
797 SEL_ARG *last();
798 void make_root() {
800 color = BLACK;
801 parent = next = prev = nullptr;
802 }
803
804 inline SEL_ARG **parent_ptr() {
805 return parent->left == this ? &parent->left : &parent->right;
806 }
807
808 /*
809 Check if this SEL_ARG object represents a single-point interval
810
811 SYNOPSIS
812 is_singlepoint()
813
814 DESCRIPTION
815 Check if this SEL_ARG object (not tree) represents a single-point
816 interval, i.e. if it represents a "keypart = const" or
817 "keypart IS NULL".
818
819 RETURN
820 true This SEL_ARG object represents a singlepoint interval
821 false Otherwise
822 */
823
824 bool is_singlepoint() const {
825 /*
826 Check for NEAR_MIN ("strictly less") and NO_MIN_RANGE (-inf < field)
827 flags, and the same for right edge.
828 */
829 if (min_flag || max_flag) return false;
830 uchar *min_val = min_value;
831 uchar *max_val = max_value;
832
833 if (maybe_null()) {
834 /* First byte is a NULL value indicator */
835 if (*min_val != *max_val) return false;
836
837 if (*min_val) return true; /* This "x IS NULL" */
838 min_val++;
839 max_val++;
840 }
841 return !field->key_cmp(min_val, max_val);
842 }
843 /**
844 Return correct min_flag.
845
846 For DESC key parts max flag should be used as min flag, but in order to
847 be checked correctly, max flag should be flipped as code doesn't expect
848 e.g NEAR_MAX in min flag.
849 */
852 }
853 /**
854 Return correct max_flag.
855
856 For DESC key parts min flag should be used as max flag, but in order to
857 be checked correctly, min flag should be flipped as code doesn't expect
858 e.g NEAR_MIN in max flag.
859 */
862 }
863};
864
865inline bool SEL_ROOT::is_always() const {
866 return type == Type::KEY_RANGE && elements == 1 && !root->maybe_flag &&
868}
869
870inline bool SEL_ROOT::simple_key() const {
871 return elements == 1 && !root->next_key_part;
872}
873
874class SEL_TREE {
875 public:
876 /**
877 Starting an effort to document this field:
878
879 IMPOSSIBLE: if keys[i]->type == SEL_ROOT::Type::IMPOSSIBLE for some i,
880 then type == SEL_TREE::IMPOSSIBLE. Rationale: if the predicate for
881 one of the indexes is always false, then the full predicate is also
882 always false.
883
884 ALWAYS: if either (keys[i]->is_always()) or (keys[i] == NULL) for all i,
885 then type == SEL_TREE::ALWAYS. Rationale: the range access method
886 will not be able to filter out any rows when there are no range
887 predicates that can be used to filter on any index.
888
889 KEY: There are range predicates that can be used on at least one
890 index.
891 */
893
894 /**
895 Whether this SEL_TREE is an inexact (too broad) representation of the
896 predicates it is based on; that is, if it does not necessarily subsume
897 all of them. Note that a nullptr return from get_mm_tree() (which means
898 “could not generate a tree from this predicate”) is by definition inexact.
899
900 There are two main ways a SEL_TREE can become inexact:
901
902 - The predicate references fields not contained in any indexes tracked
903 by the SEL_TREE.
904 - The predicate could be of a form that is not representable as a range.
905 E.g., x > 30 is a range, x mod 2 = 1 is not (although it could
906 in theory be converted to a large amount of disjunct ranges).
907
908 If a SEL_TREE is inexact, the predicates must be rechecked after the
909 range scan, using a filter. (Note that it is never too narrow, only ever
910 exact or too broad.) The old join optimizer always does this, no matter
911 what the inexact flag is set to.
912
913 Note that additional checks are needed to subsume a predicate even if
914 inexact == false. In particular, SEL_TREE contains information for all
915 indexes over a table, but if a regular range scan is chosen, it can use
916 only one index. So one must then go through all predicates to see if they
917 refer to fields not contained in the given index. Furthermore, range scans
918 on composite (multi-part) indexes can drop predicates on the later keyparts
919 (making predicates on those keyparts inexact), since range scans only
920 support inequalities on the last keypart in any given range. This check
921 must be done in get_ranges_from_tree().
922 */
923 bool inexact = false;
924
925 SEL_TREE(enum Type type_arg, MEM_ROOT *root, size_t num_keys)
926 : type(type_arg), keys(root, num_keys), n_ror_scans(0) {}
927 SEL_TREE(MEM_ROOT *root, size_t num_keys)
928 : type(KEY), keys(root, num_keys), n_ror_scans(0) {}
929 /**
930 Constructor that performs deep-copy of the SEL_ARG trees in
931 'keys[]' and the index merge alternatives in 'merges'.
932
933 @param arg The SEL_TREE to copy
934 @param param Parameters for range analysis
935 */
936 SEL_TREE(SEL_TREE *arg, RANGE_OPT_PARAM *param);
937 /*
938 Possible ways to read rows using a single index because the
939 conditions of the query consists of single-index conjunctions:
940
941 (ranges_for_idx_1) AND (ranges_for_idx_2) AND ...
942
943 The SEL_ARG graph for each non-NULL element in keys[] may consist
944 of many single-index ranges (disjunctions), so ranges_for_idx_1
945 may e.g. be:
946
947 "idx_field1 = 1 OR (idx_field1 > 5 AND idx_field2 = 10)"
948
949 assuming that index1 is a composite index covering
950 (idx_field1,...,idx_field2,..)
951
952 Index merge intersection intersects ranges on SEL_ARGs from two or
953 more indexes.
954
955 Note: there may exist SEL_TREE objects with sel_tree->type=KEY and
956 keys[i]=0 for all i. (SergeyP: it is not clear whether there is any
957 merit in range analyzer functions (e.g. get_mm_parts) returning a
958 pointer to such SEL_TREE instead of NULL)
959
960 Note: If you want to set an element in keys[], use set_key()
961 or release_key() to make sure the SEL_ARG's use_count is correctly
962 updated.
963 */
965 Key_map keys_map; /* bitmask of non-NULL elements in keys */
966
967 /*
968 Possible ways to read rows using Index merge (sort) union.
969
970 Each element in 'merges' consists of multi-index disjunctions,
971 which means that Index merge (sort) union must be applied to read
972 rows. The nodes in the 'merges' list forms a conjunction of such
973 multi-index disjunctions.
974
975 The list is non-empty only if type==KEY.
976 */
978
979 /* The members below are filled/used only after get_mm_tree is done */
980 Key_map ror_scans_map; /* bitmask of ROR scan-able elements in keys */
981 uint n_ror_scans; /* number of set bits in ror_scans_map */
982
983 /**
984 Convenience function for removing an element in keys[]. The typical
985 use for this function is to disconnect the next_key_part from the
986 root, send it to key_and() or key_or(), and then connect the
987 result of that function back to the SEL_ROOT using set_key().
988
989 @param index Which index slot to release.
990 @return The value in the slot (before removal).
991 */
993 SEL_ROOT *ret = keys[index];
994 if (keys[index]) {
995 assert(keys[index]->use_count > 0);
996 --keys[index]->use_count;
997 }
998 keys[index] = nullptr;
999 return ret;
1000 }
1001
1002 /**
1003 Convenience function for changing an element in keys[], including
1004 updating the use_count.
1005
1006 @param index Which index slot to change.
1007 @param key The new contents of the index slot. Is allowed to be nullptr.
1008 */
1011 keys[index] = key;
1012 if (key) ++key->use_count;
1013 }
1014};
1015
1016#ifndef NDEBUG
1017void print_sel_tree(RANGE_OPT_PARAM *param, SEL_TREE *tree, Key_map *tree_map,
1018 const char *msg);
1019#endif
1020
1021/*
1022 Get the SEL_ARG tree 'tree' for the keypart covering 'field', if
1023 any. 'tree' must be a unique conjunction to ALL predicates in earlier
1024 keyparts of 'keypart_tree'.
1025
1026 E.g., if 'keypart_tree' is for a composite index (kp1,kp2) and kp2
1027 covers 'field', all these conditions satisfies the requirement:
1028
1029 1. "(kp1=2 OR kp1=3) AND kp2=10" => returns "kp2=10"
1030 2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=10)" => returns "kp2=10"
1031 3. "(kp1=2 AND (kp2=10 OR kp2=11)) OR (kp1=3 AND (kp2=10 OR kp2=11))"
1032 => returns "kp2=10 OR kp2=11"
1033
1034 whereas these do not
1035 1. "(kp1=2 AND kp2=10) OR kp1=3"
1036 2. "(kp1=2 AND kp2=10) OR (kp1=3 AND kp2=11)"
1037 3. "(kp1=2 AND kp2=10) OR (kp1=3 AND (kp2=10 OR kp2=11))"
1038
1039 This function effectively tests requirement WA2.
1040
1041 @param[in] key_part_num Key part number we want the SEL_ARG tree for
1042 @param[in] keypart_tree The SEL_ARG* tree for the index
1043 @param[out] cur_range The SEL_ARG tree, if any, for the keypart
1044
1045 @retval true 'keypart_tree' contained a predicate for key part that
1046 is not conjunction to all predicates on earlier keyparts
1047 @retval false otherwise
1048*/
1049bool get_sel_root_for_keypart(uint key_part_num, SEL_ROOT *keypart_tree,
1050 SEL_ROOT **cur_range);
1051
1052/*
1053 Find the SEL_ROOT tree that corresponds to the chosen index.
1054
1055 SYNOPSIS
1056 get_index_range_tree()
1057 index [in] The ID of the index being looked for
1058 range_tree[in] Tree of ranges being searched
1059 param [in] RANGE_OPT_PARAM from test_quick_select
1060
1061 DESCRIPTION
1062
1063 A SEL_TREE contains range trees for all usable indexes. This procedure
1064 finds the SEL_ROOT tree for 'index'. The members of a SEL_TREE are
1065 ordered in the same way as the members of RANGE_OPT_PARAM::key, thus we
1066 first find the corresponding index in the array RANGE_OPT_PARAM::key.
1067 This index is returned through the variable param_idx, to be used later
1068 as argument of check_quick_select().
1069
1070 RETURN
1071 Pointer to the SEL_ROOT tree that corresponds to index.
1072*/
1073
1075 RANGE_OPT_PARAM *param) {
1076 uint idx = 0; /* Index nr in param->key_parts */
1077 while (idx < param->keys) {
1078 if (index == param->real_keynr[idx]) break;
1079 idx++;
1080 }
1081 return (range_tree->keys[idx]);
1082}
1083
1084/**
1085 Print the ranges in a SEL_TREE to debug log.
1086
1087 @param tree_name Descriptive name of the tree
1088 @param tree The SEL_TREE that will be printed to debug log
1089 @param param RANGE_OPT_PARAM from test_quick_select
1090*/
1091inline void dbug_print_tree([[maybe_unused]] const char *tree_name,
1092 [[maybe_unused]] SEL_TREE *tree,
1093 [[maybe_unused]] const RANGE_OPT_PARAM *param) {
1094#ifndef NDEBUG
1095 if (_db_enabled_()) print_tree(nullptr, tree_name, tree, param, true);
1096#endif
1097}
1098
1099#endif // SQL_RANGE_OPTIMIZER_TREE_H_
Definition: sql_bitmap.h:154
Used to store optimizer cost estimates.
Definition: handler.h:4040
Definition: field.h:573
bool is_nullable() const
Definition: field.h:1291
virtual int key_cmp(const uchar *a, const uchar *b) const
Definition: field.h:1196
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:667
bool copy_min(SEL_ARG *arg)
Definition: tree.h:638
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:679
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:659
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:663
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:798
SEL_ARG * clone_and(SEL_ARG *arg, MEM_ROOT *mem_root)
Definition: tree.h:599
int store_min_value(uint length, uchar **min_key, uint min_key_flag)
Definition: tree.h:687
uint get_min_flag()
Return correct min_flag.
Definition: tree.h:850
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:593
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:736
enum SEL_ARG::leaf_color color
int store_max_value(uint length, uchar **max_key, uint max_key_flag)
Definition: tree.h:703
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:760
SEL_ARG * next
Definition: tree.h:518
int cmp_max_to_min(const SEL_ARG *arg) const
Definition: tree.h:596
int cmp_min_to_max(const SEL_ARG *arg) const
Definition: tree.h:590
bool copy_max(SEL_ARG *arg)
Definition: tree.h:648
uint get_max_flag()
Return correct max_flag.
Definition: tree.h:860
uint8 min_flag
Definition: tree.h:468
bool is_singlepoint() const
Definition: tree.h:824
void merge_flags(SEL_ARG *arg)
Definition: tree.h:581
SEL_ARG ** parent_ptr()
Definition: tree.h:804
SEL_ARG * clone_last(SEL_ARG *arg, MEM_ROOT *mem_root)
Definition: tree.h:629
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:622
int cmp_min_to_min(const SEL_ARG *arg) const
Definition: tree.h:587
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:870
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:865
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:874
enum SEL_TREE::Type type
Key_map keys_map
Definition: tree.h:965
uint n_ror_scans
Definition: tree.h:981
bool inexact
Whether this SEL_TREE is an inexact (too broad) representation of the predicates it is based on; that...
Definition: tree.h:923
List< SEL_IMERGE > merges
Definition: tree.h:977
SEL_TREE(MEM_ROOT *root, size_t num_keys)
Definition: tree.h:927
SEL_ROOT * release_key(int index)
Convenience function for removing an element in keys[].
Definition: tree.h:992
Key_map ror_scans_map
Definition: tree.h:980
Type
Starting an effort to document this field:
Definition: tree.h:892
@ IMPOSSIBLE
Definition: tree.h:892
@ ALWAYS
Definition: tree.h:892
void set_key(int index, SEL_ROOT *key)
Convenience function for changing an element in keys[], including updating the use_count.
Definition: tree.h:1009
SEL_TREE(enum Type type_arg, MEM_ROOT *root, size_t num_keys)
Definition: tree.h:925
Mem_root_array< SEL_ROOT * > keys
Definition: tree.h:964
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:1706
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:1170
@ NO_MIN_RANGE
from -inf
Definition: my_base.h:1167
@ NO_MAX_RANGE
to +inf
Definition: my_base.h:1168
@ NEAR_MAX
Definition: my_base.h:1172
@ 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:1194
int _db_enabled_()
Definition: dbug.cc:1283
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:76
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:1091
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:1074
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