Debian Codegraph @

storage/innobase/include/row0sel.h

raw
1 /*****************************************************************************
2
3 Copyright (c) 1997, 2026, Oracle and/or its affiliates.
4
5 This program is free software; you can redistribute it and/or modify it under
6 the terms of the GNU General Public License, version 2.0, as published by the
7 Free Software Foundation.
8
9 This program is designed to work with certain software (including
10 but not limited to OpenSSL) that is licensed under separate terms,
11 as designated in a particular file or component or in included license
12 documentation. The authors of MySQL hereby grant you an additional
13 permission to link the program and your derivative works with the
14 separately licensed software that they have either included with
15 the program or referenced in the documentation.
16
17 This program is distributed in the hope that it will be useful, but WITHOUT
18 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19 FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0,
20 for more details.
21
22 You should have received a copy of the GNU General Public License along with
23 this program; if not, write to the Free Software Foundation, Inc.,
24 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
25
26 *****************************************************************************/
27
28 /** @file include/row0sel.h
29 Select
30
31 Created 12/19/1997 Heikki Tuuri
32 *******************************************************/
33
34 #ifndef row0sel_h
35 #define row0sel_h
36
37 #include "univ.i"
38
39 #include "btr0pcur.h"
40 #include "data0data.h"
41 #include "dict0stats.h"
42 #include "dict0types.h"
43 #include "pars0sym.h"
44 #include "que0types.h"
45 #include "read0types.h"
46 #include "row0mysql.h"
47 #include "row0types.h"
48 #include "trx0types.h"
49
50 /** Creates a select node struct.
51 @return own: select node struct */
52 sel_node_t *sel_node_create(
53 mem_heap_t *heap); /*!< in: memory heap where created */
54 /** Frees the memory private to a select node when a query graph is freed,
55 does not free the heap where the node was originally created. */
56 void sel_node_free_private(sel_node_t *node); /*!< in: select node struct */
57 /** Frees a prefetch buffer for a column, including the dynamically allocated
58 memory for data stored there. */
59 void sel_col_prefetch_buf_free(
60 sel_buf_t *prefetch_buf); /*!< in, own: prefetch buffer */
61
62 /** Gets the plan node for the nth table in a join.
63 @param[in] node select node
64 @param[in] i get ith plan node
65 @return plan node */
66 static inline plan_t *sel_node_get_nth_plan(sel_node_t *node, ulint i);
67
68 /** Performs a select step. This is a high-level function used in SQL execution
69 graphs.
70 @return query thread to run next or NULL */
71 que_thr_t *row_sel_step(que_thr_t *thr); /*!< in: query thread */
72 /** Performs an execution step of an open or close cursor statement node.
73 @return query thread to run next or NULL */
74 static inline que_thr_t *open_step(que_thr_t *thr); /*!< in: query thread */
75 /** Performs a fetch for a cursor.
76 @return query thread to run next or NULL */
77 que_thr_t *fetch_step(que_thr_t *thr); /*!< in: query thread */
78
79 /** Copy used fields from cached row.
80 Copy cache record field by field, don't touch fields that
81 are not covered by current key.
82 @param[out] buf Where to copy the MySQL row.
83 @param[in] cached_rec What to copy (in MySQL row format).
84 @param[in] prebuilt prebuilt struct. */
85 void row_sel_copy_cached_fields_for_mysql(byte *buf, const byte *cached_rec,
86 row_prebuilt_t *prebuilt);
87
88 // clang-format off
89 /** Convert a row in the Innobase format to a row in the MySQL format.
90 Note that the template in prebuilt may advise us to copy only a few
91 columns to mysql_rec, other columns are left blank. All columns may not
92 be needed in the query.
93 @param[out] mysql_rec row in the MySQL format
94 @param[in,out] prebuilt prebuilt structure
95 @param[in] rec Innobase record in the index
96 which was described in prebuilt's
97 template, or in the clustered index;
98 must be protected by a page latch
99 @param[in] vrow virtual columns
100 @param[in] rec_clust true if rec is in the clustered index
101 instead of index which could belong to
102 prebuilt->index
103 @param[in] rec_index index of rec
104 @param[in] prebuilt_index prebuilt->index
105 @param[in] offsets array returned by rec_get_offsets(rec)
106 @param[in] clust_templ_for_sec true if rec belongs to secondary index
107 but the prebuilt->template is in
108 clustered index format and it
109 is used only for end range comparison
110 @param[in] lob_undo the LOB undo information.
111 @param[in,out] blob_heap If not null then use this heap for BLOBs
112 @return true on success, false if not all columns could be retrieved */
113 // clang-format on
114 bool row_sel_store_mysql_rec(byte *mysql_rec, row_prebuilt_t *prebuilt,
115 const rec_t *rec, const dtuple_t *vrow,
116 bool rec_clust, const dict_index_t *rec_index,
117 const dict_index_t *prebuilt_index,
118 const ulint *offsets, bool clust_templ_for_sec,
119 lob::undo_vers_t *lob_undo,
120 mem_heap_t *&blob_heap);
121
122 /** Converts a key value stored in MySQL format to an Innobase dtuple. The last
123 field of the key value may be just a prefix of a fixed length field: hence
124 the parameter key_len. But currently we do not allow search keys where the
125 last field is only a prefix of the full key field len and print a warning if
126 such appears.
127 @param[in,out] tuple Tuple where to build; NOTE: we assume that the type info in
128 the tuple is already according to index!
129 @param[in] buf Buffer to use in field conversions; NOTE that dtuple->data may
130 end up pointing inside buf so do not discard that buffer while the tuple is
131 being used. See row_mysql_store_col_in_innobase_format() in the case of
132 DATA_INT.
133 @param[in] buf_len Buffer length.
134 @param[in] index Index of the key value.
135 @param[in] key_ptr MySQL key value
136 @param[in] key_len MySQL key value length
137 */
138 void row_sel_convert_mysql_key_to_innobase(dtuple_t *tuple, byte *buf,
139 ulint buf_len, dict_index_t *index,
140 const byte *key_ptr, ulint key_len);
141
142 /** Searches for rows in the database. This is used in the interface to
143 MySQL. This function opens a cursor, and also implements fetch next
144 and fetch prev. NOTE that if we do a search with a full key value
145 from a unique index (ROW_SEL_EXACT), then we will not store the cursor
146 position and fetch next or fetch prev must not be tried to the cursor!
147
148 @param[out] buf buffer for the fetched row in MySQL format
149 @param[in] mode search mode PAGE_CUR_L
150 @param[in,out] prebuilt prebuilt struct for the table handler;
151 this contains the info to search_tuple,
152 index; if search tuple contains 0 field then
153 we position the cursor at start or the end of
154 index, depending on 'mode'
155 @param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
156 @param[in] direction 0 or ROW_SEL_NEXT or ROW_SEL_PREV;
157 Note: if this is != 0, then prebuilt must has a
158 pcur with stored position! In opening of a
159 cursor 'direction' should be 0.
160 @return DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK,
161 DB_LOCK_TABLE_FULL, DB_CORRUPTION, or DB_TOO_BIG_RECORD */
162 [[nodiscard]] static inline dberr_t row_search_for_mysql(
163 byte *buf, page_cur_mode_t mode, row_prebuilt_t *prebuilt, ulint match_mode,
164 ulint direction);
165
166 /** Searches for rows in the database using cursor.
167 Function is for temporary tables that are not shared across connections
168 and so lot of complexity is reduced especially locking and transaction related.
169 The cursor is an iterator over the table/index.
170
171 @param[out] buf buffer for the fetched row in MySQL format
172 @param[in] mode search mode PAGE_CUR_L
173 @param[in,out] prebuilt prebuilt struct for the table handler;
174 this contains the info to search_tuple,
175 index; if search tuple contains 0 field then
176 we position the cursor at start or the end of
177 index, depending on 'mode'
178 @param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
179 @param[in] direction 0 or ROW_SEL_NEXT or ROW_SEL_PREV;
180 Note: if this is != 0, then prebuilt must has a
181 pcur with stored position! In opening of a
182 cursor 'direction' should be 0.
183 @return DB_SUCCESS or error code */
184 [[nodiscard]] dberr_t row_search_no_mvcc(byte *buf, page_cur_mode_t mode,
185 row_prebuilt_t *prebuilt,
186 ulint match_mode, ulint direction);
187
188 /** Searches for rows in the database using cursor.
189 Function is mainly used for tables that are shared accorss connection and
190 so it employs technique that can help re-construct the rows that
191 transaction is suppose to see.
192 It also has optimization such as pre-caching the rows, using AHI, etc.
193
194 @param[out] buf buffer for the fetched row in MySQL format
195 @param[in] mode search mode PAGE_CUR_L
196 @param[in,out] prebuilt prebuilt struct for the table handler;
197 this contains the info to search_tuple,
198 index; if search tuple contains 0 field then
199 we position the cursor at start or the end of
200 index, depending on 'mode'
201 @param[in] match_mode 0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
202 @param[in] direction 0 or ROW_SEL_NEXT or ROW_SEL_PREV;
203 Note: if this is != 0, then prebuilt must has a
204 pcur with stored position! In opening of a
205 cursor 'direction' should be 0.
206 @return DB_SUCCESS or error code */
207 [[nodiscard]] dberr_t row_search_mvcc(byte *buf, page_cur_mode_t mode,
208 row_prebuilt_t *prebuilt,
209 ulint match_mode, const ulint direction);
210
211 /** Count rows in a R-Tree leaf level.
212 @return DB_SUCCESS if successful */
213 dberr_t row_count_rtree_recs(
214 row_prebuilt_t *prebuilt, /*!< in: prebuilt struct for the
215 table handle; this contains the info
216 of search_tuple, index; if search
217 tuple contains 0 fields then we
218 position the cursor at the start or
219 the end of the index, depending on
220 'mode' */
221 ulint *n_rows, /*!< out: number of entries
222 seen in the consistent read */
223 ulint *n_dups); /*!< out: number of dup entries
224 seen in the consistent read */
225
226 /** Read the max AUTOINC value from an index.
227 @return DB_SUCCESS if all OK else error code */
228 [[nodiscard]] dberr_t row_search_max_autoinc(
229 dict_index_t *index, /*!< in: index to search */
230 const char *col_name, /*!< in: autoinc column name */
231 uint64_t *value); /*!< out: AUTOINC value read */
232
233 /** A structure for caching column values for prefetched rows */
234 struct sel_buf_t {
235 byte *data; /*!< data, or NULL; if not NULL, this field
236 has allocated memory which must be explicitly
237 freed; can be != NULL even when len is
238 UNIV_SQL_NULL */
239 ulint len; /*!< data length or UNIV_SQL_NULL */
240 ulint val_buf_size;
241 /*!< size of memory buffer allocated for data:
242 this can be more than len; this is defined
243 when data != NULL */
244 };
245
246 /** Query plan */
247 struct plan_t {
248 dict_table_t *table; /*!< table struct in the dictionary
249 cache */
250 dict_index_t *index; /*!< table index used in the search */
251 btr_pcur_t pcur; /*!< persistent cursor used to search
252 the index */
253 bool asc; /*!< true if cursor traveling upwards */
254 bool pcur_is_open; /*!< true if pcur has been positioned
255 and we can try to fetch new rows */
256 bool cursor_at_end; /*!< true if the cursor is open but
257 we know that there are no more
258 qualifying rows left to retrieve from
259 the index tree; NOTE though, that
260 there may still be unprocessed rows in
261 the prefetch stack; always false when
262 pcur_is_open is false */
263 bool stored_cursor_rec_processed;
264 /*!< true if the pcur position has been
265 stored and the record it is positioned
266 on has already been processed */
267 que_node_t **tuple_exps; /*!< array of expressions
268 which are used to calculate
269 the field values in the search
270 tuple: there is one expression
271 for each field in the search
272 tuple */
273 dtuple_t *tuple; /*!< search tuple */
274 page_cur_mode_t mode; /*!< search mode: PAGE_CUR_G, ... */
275 ulint n_exact_match; /*!< number of first fields in
276 the search tuple which must be
277 exactly matched */
278 bool unique_search; /*!< true if we are searching an
279 index record with a unique key */
280 ulint n_rows_fetched; /*!< number of rows fetched using pcur
281 after it was opened */
282 ulint n_rows_prefetched; /*!< number of prefetched rows cached
283 for fetch: fetching several rows in
284 the same mtr saves CPU time */
285 ulint first_prefetched; /*!< index of the first cached row in
286 select buffer arrays for each column */
287 bool no_prefetch; /*!< no prefetch for this table */
288 sym_node_list_t columns; /*!< symbol table nodes for the columns
289 to retrieve from the table */
290 using Cond_list = UT_LIST_BASE_NODE_T_EXTERN(func_node_t, cond_list);
291 /** conditions which determine the fetch limit of the index segment we have to
292 look at: when one of these fails, the result set has been exhausted for the
293 cursor in this index; these conditions are normalized so that in a comparison
294 the column for this table is the first argument */
295 Cond_list end_conds;
296 /** the rest of search conditions we can test at this table in a join */
297 Cond_list other_conds;
298 bool must_get_clust; /*!< true if index is a non-clustered
299 index and we must also fetch the
300 clustered index record; this is the
301 case if the non-clustered record does
302 not contain all the needed columns, or
303 if this is a single-table explicit
304 cursor, or a searched update or
305 delete */
306 ulint *clust_map; /*!< map telling how clust_ref is built
307 from the fields of a non-clustered
308 record */
309 dtuple_t *clust_ref; /*!< the reference to the clustered
310 index entry is built here if index is
311 a non-clustered index */
312 btr_pcur_t clust_pcur; /*!< if index is non-clustered, we use
313 this pcur to search the clustered
314 index */
315 mem_heap_t *old_vers_heap; /*!< memory heap used in building an old
316 version of a row, or NULL */
317 };
318
319 /** Select node states */
320 enum sel_node_state {
321 SEL_NODE_CLOSED, /*!< it is a declared cursor which is not
322 currently open */
323 SEL_NODE_OPEN, /*!< intention locks not yet set on tables */
324 SEL_NODE_FETCH, /*!< intention locks have been set */
325 SEL_NODE_NO_MORE_ROWS /*!< cursor has reached the result set end */
326 };
327
328 /** Select statement node */
329 struct sel_node_t {
330 que_common_t common; /*!< node type: QUE_NODE_SELECT */
331 enum sel_node_state state; /*!< node state */
332 que_node_t *select_list; /*!< select list */
333 sym_node_t *into_list; /*!< variables list or NULL */
334 sym_node_t *table_list; /*!< table list */
335 bool asc; /*!< true if the rows should be fetched
336 in an ascending order */
337 bool set_x_locks; /*!< true if the cursor is for update or
338 delete, which means that a row x-lock
339 should be placed on the cursor row */
340 ulint row_lock_mode; /*!< LOCK_X or LOCK_S */
341 ulint n_tables; /*!< number of tables */
342 ulint fetch_table; /*!< number of the next table to access
343 in the join */
344 plan_t *plans; /*!< array of n_tables many plan nodes
345 containing the search plan and the
346 search data structures */
347 que_node_t *search_cond; /*!< search condition */
348 ReadView *read_view; /*!< if the query is a non-locking
349 consistent read, its read view is
350 placed here, otherwise NULL */
351 bool consistent_read; /*!< true if the select is a consistent,
352 non-locking read */
353 order_node_t *order_by; /*!< order by column definition, or
354 NULL */
355 bool is_aggregate; /*!< true if the select list consists of
356 aggregate functions */
357 bool aggregate_already_fetched;
358 /*!< true if the aggregate row has
359 already been fetched for the current
360 cursor */
361
362 /** this is true if the select is in a single-table explicit cursor which can
363 get updated within the stored procedure, or in a searched update or delete;
364 NOTE that to determine of an explicit cursor if it can get updated, the
365 parser checks from a stored procedure if it contains positioned update or
366 delete statements */
367 bool can_get_updated;
368 /** not NULL if an explicit cursor */
369 sym_node_t *explicit_cursor;
370 /** variables whose values we have to copy when an explicit cursor is opened,
371 so that they do not change between fetches */
372 sym_node_list_t copy_variables;
373 };
374
375 /** Fetch statement node */
376 struct fetch_node_t {
377 que_common_t common; /*!< type: QUE_NODE_FETCH */
378 sel_node_t *cursor_def; /*!< cursor definition */
379 sym_node_t *into_list; /*!< variables to set */
380
381 pars_user_func_t *func; /*!< User callback function or NULL.
382 The first argument to the function
383 is a sel_node_t*, containing the
384 results of the SELECT operation for
385 one row. If the function returns
386 NULL, it is not interested in
387 further rows and the cursor is
388 modified so (cursor % NOTFOUND) is
389 true. If it returns not-NULL,
390 continue normally. */
391 };
392
393 /** Open or close cursor operation type */
394 enum open_node_op {
395 ROW_SEL_OPEN_CURSOR, /*!< open cursor */
396 ROW_SEL_CLOSE_CURSOR /*!< close cursor */
397 };
398
399 /** Open or close cursor statement node */
400 struct open_node_t {
401 que_common_t common; /*!< type: QUE_NODE_OPEN */
402 enum open_node_op op_type; /*!< operation type: open or
403 close cursor */
404 sel_node_t *cursor_def; /*!< cursor definition */
405 };
406
407 /** Search direction for the MySQL interface */
408 enum row_sel_direction {
409 ROW_SEL_NEXT = 1, /*!< ascending direction */
410 ROW_SEL_PREV = 2 /*!< descending direction */
411 };
412
413 /** Match mode for the MySQL interface */
414 enum row_sel_match_mode {
415 ROW_SEL_EXACT = 1, /*!< search using a complete key value */
416 ROW_SEL_EXACT_PREFIX /*!< search using a key prefix which
417 must match rows: the prefix may
418 contain an incomplete field (the last
419 field in prefix may be just a prefix
420 of a fixed length column) */
421 };
422
423 /** Stores a non-SQL-NULL field in the MySQL format. The counterpart of this
424 function is row_mysql_store_col_in_innobase_format() in row0mysql.cc.
425 @param[in,out] dest buffer where to store; NOTE
426 that BLOBs are not in themselves stored
427 here: the caller must allocate and copy
428 the BLOB into buffer before, and pass
429 the pointer to the BLOB in 'data'
430 @param[in] templ MySQL column template. Its following fields
431 are referenced: type, is_unsigned,
432 mysql_col_len, mbminlen, mbmaxlen
433 @param[in] index InnoDB index
434 @param[in] field_no templ->rec_field_no or templ->clust_rec_field_no
435 or templ->icp_rec_field_no
436 @param[in] data data to store
437 @param[in] len length of the data
438 @param[in] sec_field secondary index field no if the secondary index
439 record but the prebuilt template is in
440 clustered index format and used only for end
441 range comparison. */
442 void row_sel_field_store_in_mysql_format_func(
443 byte *dest, const mysql_row_templ_t *templ, const dict_index_t *index,
444 IF_DEBUG(ulint field_no, ) const byte *data,
445 ulint len IF_DEBUG(, ulint sec_field));
446
447 /** Convert a non-SQL-NULL field from Innobase format to MySQL format. */
448 static inline void row_sel_field_store_in_mysql_format(
449 byte *dest, const mysql_row_templ_t *templ, const dict_index_t *idx,
450 ulint field, const byte *src, ulint len, ulint sec) {
451 row_sel_field_store_in_mysql_format_func(
452 dest, templ, idx, IF_DEBUG(field, ) src, len IF_DEBUG(, sec));
453 }
454
455 /** Search the record present in innodb_table_stats table using
456 db_name, table_name and fill it in table stats structure.
457 @param[in] db_name database name
458 @param[in] tbl_name table name
459 @param[out] table_stats stats table structure.
460 @return true if successful else false. */
461 bool row_search_table_stats(const char *db_name, const char *tbl_name,
462 TableStatsRecord &table_stats);
463
464 /** Search the record present in innodb_index_stats using
465 db_name, table name and index_name and fill the
466 cardinality for the each column.
467 @param[in] db_name database name
468 @param[in] tbl_name table name
469 @param[in] index_name index name
470 @param[in] col_offset offset of the column in the index
471 @param[out] cardinality cardinality of the column.
472 @return true if successful else false. */
473 bool row_search_index_stats(const char *db_name, const char *tbl_name,
474 const char *index_name, ulint col_offset,
475 ulonglong *cardinality);
476
477 #include "row0sel.ic"
478
479 #endif