MySQL 8.0.30
Source Code Documentation
range_optimizer.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2022, 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 also distributed 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 included with MySQL.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License, version 2.0, for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
22
23/* classes to use when handling where clause */
24
25#ifndef SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
26#define SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
27
28#include <assert.h>
29#include <sys/types.h>
30#include <algorithm>
31
32#include "my_base.h"
33#include "my_inttypes.h"
34#include "my_table_map.h"
35#include "prealloced_array.h" // Prealloced_array
36#include "sql/field.h" // Field
37#include "sql/handler.h"
38#include "sql/item_func.h"
40#include "sql/key_spec.h"
41#include "sql/malloc_allocator.h" // IWYU pragma: keep
42#include "sql/sql_bitmap.h"
43#include "sql/sql_const.h"
44#include "sql_string.h"
45
46class Item;
48class Query_block;
49class RANGE_OPT_PARAM;
50class THD;
51struct MY_BITMAP;
52struct TABLE;
53
54struct KEY_PART {
56 /* See KEY_PART_INFO for meaning of the next two: */
59 /*
60 Keypart flags (0 when this structure is used by partition pruning code
61 for fake partitioning index description)
62 */
66};
67
69 public:
72
73 /// Stores bitwise-or'ed bits defined in enum key_range_flags.
75
76 /**
77 Stores one of the HA_READ_MBR_XXX items in enum ha_rkey_function, only
78 effective when flag has a GEOM_FLAG bit.
79 */
81 key_part_map min_keypart_map, // bitmap of used keyparts in min_key
82 max_keypart_map; // bitmap of used keyparts in max_key
83
84 QUICK_RANGE(); /* Full range */
85 QUICK_RANGE(MEM_ROOT *mem_root, const uchar *min_key_arg, uint min_length_arg,
86 key_part_map min_keypart_map_arg, const uchar *max_key_arg,
87 uint max_length_arg, key_part_map max_keypart_map_arg,
88 uint flag_arg, enum ha_rkey_function rkey_func);
89
90 /**
91 Initializes a key_range object for communication with storage engine.
92
93 This function facilitates communication with the Storage Engine API by
94 translating the minimum endpoint of the interval represented by this
95 QUICK_RANGE into an index range endpoint specifier for the engine.
96
97 @param kr Pointer to an uninitialized key_range C struct.
98
99 @param prefix_length The length of the search key prefix to be used for
100 lookup.
101
102 @param keypart_map A set (bitmap) of keyparts to be used.
103 */
104 void make_min_endpoint(key_range *kr, uint prefix_length,
105 key_part_map keypart_map) {
107 kr->length = std::min(kr->length, prefix_length);
108 kr->keypart_map &= keypart_map;
109 }
110
111 /**
112 Initializes a key_range object for communication with storage engine.
113
114 This function facilitates communication with the Storage Engine API by
115 translating the minimum endpoint of the interval represented by this
116 QUICK_RANGE into an index range endpoint specifier for the engine.
117
118 @param kr Pointer to an uninitialized key_range C struct.
119 */
121 kr->key = (const uchar *)min_key;
122 kr->length = min_length;
127 }
128
129 /**
130 Initializes a key_range object for communication with storage engine.
131
132 This function facilitates communication with the Storage Engine API by
133 translating the maximum endpoint of the interval represented by this
134 QUICK_RANGE into an index range endpoint specifier for the engine.
135
136 @param kr Pointer to an uninitialized key_range C struct.
137
138 @param prefix_length The length of the search key prefix to be used for
139 lookup.
140
141 @param keypart_map A set (bitmap) of keyparts to be used.
142 */
143 void make_max_endpoint(key_range *kr, uint prefix_length,
144 key_part_map keypart_map) {
146 kr->length = std::min(kr->length, prefix_length);
147 kr->keypart_map &= keypart_map;
148 }
149
150 /**
151 Initializes a key_range object for communication with storage engine.
152
153 This function facilitates communication with the Storage Engine API by
154 translating the maximum endpoint of the interval represented by this
155 QUICK_RANGE into an index range endpoint specifier for the engine.
156
157 @param kr Pointer to an uninitialized key_range C struct.
158 */
160 kr->key = (const uchar *)max_key;
161 kr->length = max_length;
163 /*
164 We use READ_AFTER_KEY here because if we are reading on a key
165 prefix we want to find all keys with this prefix
166 */
168 }
169};
170
173
174bool setup_range_optimizer_param(THD *thd, MEM_ROOT *return_mem_root,
175 MEM_ROOT *temp_mem_root, Key_map keys_to_use,
176 TABLE *table, Query_block *query_block,
177 RANGE_OPT_PARAM *param);
178
179int test_quick_select(THD *thd, MEM_ROOT *return_mem_root,
180 MEM_ROOT *temp_mem_root, Key_map keys_to_use,
181 table_map prev_tables, table_map read_tables,
182 ha_rows limit, bool force_quick_range,
183 const enum_order interesting_order, TABLE *table,
184 bool skip_records_in_range, Item *cond,
185 Key_map *needed_reg, bool ignore_table_scan,
186 Query_block *query_block, AccessPath **path);
187
188void store_key_image_to_rec(Field *field, uchar *ptr, uint len);
189
190extern String null_string;
191
192/// Global initialization of the null_element. Call on server start.
194
195/// Global destruction of the null_element. Call on server stop.
197
198/**
199 Test if 'value' is comparable to 'field' when setting up range
200 access for predicate "field OP value". 'field' is a field in the
201 table being optimized for while 'value' is whatever 'field' is
202 compared to.
203
204 @param cond_func the predicate item that compares 'field' with 'value'
205 @param field field in the predicate
206 @param itype itMBR if indexed field is spatial, itRAW otherwise
207 @param comp_type comparator for the predicate
208 @param value whatever 'field' is compared to
209
210 @return true if 'field' and 'value' are comparable, false otherwise
211*/
212
213bool comparable_in_index(Item *cond_func, const Field *field,
214 const Field::imagetype itype,
215 Item_func::Functype comp_type, const Item *value);
216
218
219#endif // SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
Definition: sql_bitmap.h:137
Definition: field.h:573
imagetype
Definition: field.h:726
Functype
Definition: item_func.h:178
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:802
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:89
Definition: range_optimizer.h:68
void make_max_endpoint(key_range *kr)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:159
uint16 flag
Stores bitwise-or'ed bits defined in enum key_range_flags.
Definition: range_optimizer.h:74
void make_min_endpoint(key_range *kr, uint prefix_length, key_part_map keypart_map)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:104
uint16 min_length
Definition: range_optimizer.h:71
uint16 max_length
Definition: range_optimizer.h:71
QUICK_RANGE()
Definition: range_optimizer.cc:269
uchar * min_key
Definition: range_optimizer.h:70
key_part_map min_keypart_map
Definition: range_optimizer.h:81
void make_max_endpoint(key_range *kr, uint prefix_length, key_part_map keypart_map)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:143
key_part_map max_keypart_map
Definition: range_optimizer.h:82
uchar * max_key
Definition: range_optimizer.h:70
void make_min_endpoint(key_range *kr)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:120
enum ha_rkey_function rkey_func_flag
Stores one of the HA_READ_MBR_XXX items in enum ha_rkey_function, only effective when flag has a GEOM...
Definition: range_optimizer.h:80
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1124
Definition: range_opt_param.h:28
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:166
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_class.h:922
static MEM_ROOT mem_root
Definition: client_plugin.cc:109
enum_order
Definition: key_spec.h:64
This file includes constants used by all storage engines.
ha_rkey_function
Definition: my_base.h:77
@ HA_READ_KEY_EXACT
Definition: my_base.h:78
@ HA_READ_AFTER_KEY
Definition: my_base.h:81
@ HA_READ_BEFORE_KEY
Definition: my_base.h:82
@ HA_READ_KEY_OR_NEXT
Definition: my_base.h:79
@ NEAR_MIN
Definition: my_base.h:1081
@ EQ_RANGE
Definition: my_base.h:1095
@ NEAR_MAX
Definition: my_base.h:1083
ulong key_part_map
Definition: my_base.h:1006
my_off_t ha_rows
Definition: my_base.h:1139
Some integer typedefs for easier portability.
uint8_t uint8
Definition: my_inttypes.h:62
unsigned char uchar
Definition: my_inttypes.h:51
uint16_t uint16
Definition: my_inttypes.h:64
uint64_t table_map
Definition: my_table_map.h:29
static char * path
Definition: mysqldump.cc:133
String null_string
void trace_quick_description(const AccessPath *path, Opt_trace_context *trace)
Definition: range_optimizer.cc:260
void store_key_image_to_rec(Field *field, uchar *ptr, uint len)
Definition: partition_pruning.cc:447
bool comparable_in_index(Item *cond_func, const Field *field, const Field::imagetype itype, Item_func::Functype comp_type, const Item *value)
Test if 'value' is comparable to 'field' when setting up range access for predicate "field OP value".
Definition: range_optimizer.cc:1235
bool setup_range_optimizer_param(THD *thd, MEM_ROOT *return_mem_root, MEM_ROOT *temp_mem_root, Key_map keys_to_use, TABLE *table, Query_block *query_block, RANGE_OPT_PARAM *param)
Definition: range_optimizer.cc:341
void range_optimizer_free()
Global destruction of the null_element. Call on server stop.
Definition: range_optimizer.cc:167
int test_quick_select(THD *thd, MEM_ROOT *return_mem_root, MEM_ROOT *temp_mem_root, Key_map keys_to_use, table_map prev_tables, table_map read_tables, ha_rows limit, bool force_quick_range, const enum_order interesting_order, TABLE *table, bool skip_records_in_range, Item *cond, Key_map *needed_reg, bool ignore_table_scan, Query_block *query_block, AccessPath **path)
Definition: range_optimizer.cc:523
void range_optimizer_init()
Global initialization of the null_element. Call on server start.
Definition: range_optimizer.cc:161
File containing constants that can be used throughout the server.
Our own string classes, used pervasively throughout the executor.
Access paths are a query planning structure that correspond 1:1 to iterators, in that an access path ...
Definition: access_path.h:188
Definition: range_optimizer.h:54
Field::imagetype image_type
Definition: range_optimizer.h:65
uint16 store_length
Definition: range_optimizer.h:57
uint16 flag
Definition: range_optimizer.h:63
uint8 null_bit
Definition: range_optimizer.h:58
Field * field
Definition: range_optimizer.h:64
uint16 part
Definition: range_optimizer.h:55
uint16 key
Definition: range_optimizer.h:55
uint16 length
Definition: range_optimizer.h:57
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:82
Definition: my_bitmap.h:42
Definition: table.h:1394
Definition: my_base.h:1123
uint length
Definition: my_base.h:1125
enum ha_rkey_function flag
Definition: my_base.h:1127
key_part_map keypart_map
Definition: my_base.h:1126
const uchar * key
Definition: my_base.h:1124
unsigned int uint
Definition: uca-dump.cc:29