MySQL 8.4.2
Source Code Documentation
range_optimizer.h
Go to the documentation of this file.
1/* Copyright (c) 2000, 2024, 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/* classes to use when handling where clause */
25
26#ifndef SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
27#define SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
28
29#include <assert.h>
30#include <sys/types.h>
31#include <algorithm>
32
33#include "my_base.h"
34#include "my_inttypes.h"
35#include "my_table_map.h"
36#include "prealloced_array.h" // Prealloced_array
37#include "sql/field.h" // Field
38#include "sql/handler.h"
39#include "sql/item_func.h"
41#include "sql/key_spec.h"
42#include "sql/malloc_allocator.h" // IWYU pragma: keep
43#include "sql/sql_bitmap.h"
44#include "sql/sql_const.h"
45#include "sql_string.h"
46
47class Item;
49class Query_block;
50class RANGE_OPT_PARAM;
51class THD;
52struct MY_BITMAP;
53struct TABLE;
54
55struct KEY_PART {
57 /* See KEY_PART_INFO for meaning of the next two: */
60 /*
61 Keypart flags (0 when this structure is used by partition pruning code
62 for fake partitioning index description)
63 */
67};
68
70 public:
73
74 /// Stores bitwise-or'ed bits defined in enum key_range_flags.
76
77 /**
78 Stores one of the HA_READ_MBR_XXX items in enum ha_rkey_function, only
79 effective when flag has a GEOM_FLAG bit.
80 */
82 key_part_map min_keypart_map, // bitmap of used keyparts in min_key
83 max_keypart_map; // bitmap of used keyparts in max_key
84
85 QUICK_RANGE(); /* Full range */
86 QUICK_RANGE(MEM_ROOT *mem_root, const uchar *min_key_arg, uint min_length_arg,
87 key_part_map min_keypart_map_arg, const uchar *max_key_arg,
88 uint max_length_arg, key_part_map max_keypart_map_arg,
89 uint flag_arg, enum ha_rkey_function rkey_func);
90
91 /**
92 Initializes a key_range object for communication with storage engine.
93
94 This function facilitates communication with the Storage Engine API by
95 translating the minimum endpoint of the interval represented by this
96 QUICK_RANGE into an index range endpoint specifier for the engine.
97
98 @param kr Pointer to an uninitialized key_range C struct.
99
100 @param prefix_length The length of the search key prefix to be used for
101 lookup.
102
103 @param keypart_map A set (bitmap) of keyparts to be used.
104 */
105 void make_min_endpoint(key_range *kr, uint prefix_length,
106 key_part_map keypart_map) {
108 kr->length = std::min(kr->length, prefix_length);
109 kr->keypart_map &= keypart_map;
110 }
111
112 /**
113 Initializes a key_range object for communication with storage engine.
114
115 This function facilitates communication with the Storage Engine API by
116 translating the minimum endpoint of the interval represented by this
117 QUICK_RANGE into an index range endpoint specifier for the engine.
118
119 @param kr Pointer to an uninitialized key_range C struct.
120 */
122 kr->key = (const uchar *)min_key;
123 kr->length = min_length;
128 }
129
130 /**
131 Initializes a key_range object for communication with storage engine.
132
133 This function facilitates communication with the Storage Engine API by
134 translating the maximum endpoint of the interval represented by this
135 QUICK_RANGE into an index range endpoint specifier for the engine.
136
137 @param kr Pointer to an uninitialized key_range C struct.
138
139 @param prefix_length The length of the search key prefix to be used for
140 lookup.
141
142 @param keypart_map A set (bitmap) of keyparts to be used.
143 */
144 void make_max_endpoint(key_range *kr, uint prefix_length,
145 key_part_map keypart_map) {
147 kr->length = std::min(kr->length, prefix_length);
148 kr->keypart_map &= keypart_map;
149 }
150
151 /**
152 Initializes a key_range object for communication with storage engine.
153
154 This function facilitates communication with the Storage Engine API by
155 translating the maximum endpoint of the interval represented by this
156 QUICK_RANGE into an index range endpoint specifier for the engine.
157
158 @param kr Pointer to an uninitialized key_range C struct.
159 */
161 kr->key = (const uchar *)max_key;
162 kr->length = max_length;
164 /*
165 We use READ_AFTER_KEY here because if we are reading on a key
166 prefix we want to find all keys with this prefix
167 */
169 }
170};
171
174
175bool setup_range_optimizer_param(THD *thd, MEM_ROOT *return_mem_root,
176 MEM_ROOT *temp_mem_root, Key_map keys_to_use,
177 TABLE *table, Query_block *query_block,
178 RANGE_OPT_PARAM *param);
179
180int test_quick_select(THD *thd, MEM_ROOT *return_mem_root,
181 MEM_ROOT *temp_mem_root, Key_map keys_to_use,
182 table_map prev_tables, table_map read_tables,
183 ha_rows limit, bool force_quick_range,
184 const enum_order interesting_order, TABLE *table,
185 bool skip_records_in_range, Item *cond,
186 Key_map *needed_reg, bool ignore_table_scan,
187 Query_block *query_block, AccessPath **path);
188
189void store_key_image_to_rec(Field *field, uchar *ptr, uint len);
190
191extern String null_string;
192
193/// Global initialization of the null_element. Call on server start.
195
196/// Global destruction of the null_element. Call on server stop.
198
199/**
200 Test if 'value' is comparable to 'field' when setting up range
201 access for predicate "field OP value". 'field' is a field in the
202 table being optimized for while 'value' is whatever 'field' is
203 compared to.
204
205 @param cond_func the predicate item that compares 'field' with 'value'
206 @param field field in the predicate
207 @param itype itMBR if indexed field is spatial, itRAW otherwise
208 @param comp_type comparator for the predicate
209 @param value whatever 'field' is compared to
210
211 @return true if 'field' and 'value' are comparable, false otherwise
212*/
213
214bool comparable_in_index(Item *cond_func, const Field *field,
215 const Field::imagetype itype,
216 Item_func::Functype comp_type, const Item *value);
217
219
220#endif // SQL_RANGE_OPTIMIZER_RANGE_OPTIMIZER_H_
Definition: sql_bitmap.h:154
Definition: field.h:575
imagetype
Definition: field.h:728
Functype
Definition: item_func.h:187
Base class that is used to represent any kind of expression in a relational query.
Definition: item.h:936
A per-session context which is always available at any point of execution, because in practice it's a...
Definition: opt_trace_context.h:93
Definition: range_optimizer.h:69
void make_max_endpoint(key_range *kr)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:160
uint16 flag
Stores bitwise-or'ed bits defined in enum key_range_flags.
Definition: range_optimizer.h:75
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:105
uint16 min_length
Definition: range_optimizer.h:72
uint16 max_length
Definition: range_optimizer.h:72
QUICK_RANGE()
Definition: range_optimizer.cc:269
uchar * min_key
Definition: range_optimizer.h:71
key_part_map min_keypart_map
Definition: range_optimizer.h:82
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:144
key_part_map max_keypart_map
Definition: range_optimizer.h:83
uchar * max_key
Definition: range_optimizer.h:71
void make_min_endpoint(key_range *kr)
Initializes a key_range object for communication with storage engine.
Definition: range_optimizer.h:121
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:81
This class represents a query block, aka a query specification, which is a query consisting of a SELE...
Definition: sql_lex.h:1163
Definition: range_opt_param.h:29
Using this class is fraught with peril, and you need to be very careful when doing so.
Definition: sql_string.h:167
For each client connection we create a separate thread with THD serving as a thread/connection descri...
Definition: sql_lexer_thd.h:36
static MEM_ROOT mem_root
Definition: client_plugin.cc:114
enum_order
Definition: key_spec.h:65
This file includes constants used by all storage engines.
ha_rkey_function
Definition: my_base.h:78
@ HA_READ_KEY_EXACT
Definition: my_base.h:79
@ HA_READ_AFTER_KEY
Definition: my_base.h:82
@ HA_READ_BEFORE_KEY
Definition: my_base.h:83
@ HA_READ_KEY_OR_NEXT
Definition: my_base.h:80
@ NEAR_MIN
Definition: my_base.h:1083
@ EQ_RANGE
Definition: my_base.h:1097
@ NEAR_MAX
Definition: my_base.h:1085
ulong key_part_map
Definition: my_base.h:1008
my_off_t ha_rows
Definition: my_base.h:1141
Some integer typedefs for easier portability.
uint8_t uint8
Definition: my_inttypes.h:63
unsigned char uchar
Definition: my_inttypes.h:52
uint16_t uint16
Definition: my_inttypes.h:65
uint64_t table_map
Definition: my_table_map.h:30
static char * path
Definition: mysqldump.cc:149
static PFS_engine_table_share_proxy table
Definition: pfs.cc:61
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:449
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:1212
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:302
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:484
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:213
Definition: range_optimizer.h:55
Field::imagetype image_type
Definition: range_optimizer.h:66
uint16 store_length
Definition: range_optimizer.h:58
uint16 flag
Definition: range_optimizer.h:64
uint8 null_bit
Definition: range_optimizer.h:59
Field * field
Definition: range_optimizer.h:65
uint16 part
Definition: range_optimizer.h:56
uint16 key
Definition: range_optimizer.h:56
uint16 length
Definition: range_optimizer.h:58
The MEM_ROOT is a simple arena, where allocations are carved out of larger blocks.
Definition: my_alloc.h:83
Definition: my_bitmap.h:43
Definition: table.h:1406
Definition: my_base.h:1125
uint length
Definition: my_base.h:1127
enum ha_rkey_function flag
Definition: my_base.h:1129
key_part_map keypart_map
Definition: my_base.h:1128
const uchar * key
Definition: my_base.h:1126