MySQL 8.0.40
Source Code Documentation
row0sel.h File Reference

Select. More...

#include "univ.i"
#include "btr0pcur.h"
#include "data0data.h"
#include "dict0stats.h"
#include "dict0types.h"
#include "pars0sym.h"
#include "que0types.h"
#include "read0types.h"
#include "row0mysql.h"
#include "row0types.h"
#include "trx0types.h"
#include "row0sel.ic"

Go to the source code of this file.

Classes

struct  sel_buf_t
 A structure for caching column values for prefetched rows. More...
 
struct  plan_t
 Query plan. More...
 
struct  sel_node_t
 Select statement node. More...
 
struct  fetch_node_t
 Fetch statement node. More...
 
struct  open_node_t
 Open or close cursor statement node. More...
 

Enumerations

enum  sel_node_state { SEL_NODE_CLOSED , SEL_NODE_OPEN , SEL_NODE_FETCH , SEL_NODE_NO_MORE_ROWS }
 Select node states. More...
 
enum  open_node_op { ROW_SEL_OPEN_CURSOR , ROW_SEL_CLOSE_CURSOR }
 Open or close cursor operation type. More...
 
enum  row_sel_direction { ROW_SEL_NEXT = 1 , ROW_SEL_PREV = 2 }
 Search direction for the MySQL interface. More...
 
enum  row_sel_match_mode { ROW_SEL_EXACT = 1 , ROW_SEL_EXACT_PREFIX }
 Match mode for the MySQL interface. More...
 

Functions

sel_node_tsel_node_create (mem_heap_t *heap)
 Creates a select node struct. More...
 
void sel_node_free_private (sel_node_t *node)
 Frees the memory private to a select node when a query graph is freed, does not free the heap where the node was originally created. More...
 
void sel_col_prefetch_buf_free (sel_buf_t *prefetch_buf)
 Frees a prefetch buffer for a column, including the dynamically allocated memory for data stored there. More...
 
static plan_tsel_node_get_nth_plan (sel_node_t *node, ulint i)
 Gets the plan node for the nth table in a join. More...
 
que_thr_trow_sel_step (que_thr_t *thr)
 Performs a select step. More...
 
static que_thr_topen_step (que_thr_t *thr)
 Performs an execution step of an open or close cursor statement node. More...
 
que_thr_tfetch_step (que_thr_t *thr)
 Performs a fetch for a cursor. More...
 
void row_sel_copy_cached_fields_for_mysql (byte *buf, const byte *cached_rec, row_prebuilt_t *prebuilt)
 Copy used fields from cached row. More...
 
bool row_sel_store_mysql_rec (byte *mysql_rec, row_prebuilt_t *prebuilt, const rec_t *rec, const dtuple_t *vrow, bool rec_clust, const dict_index_t *rec_index, const dict_index_t *prebuilt_index, const ulint *offsets, bool clust_templ_for_sec, lob::undo_vers_t *lob_undo, mem_heap_t *&blob_heap)
 Convert a row in the Innobase format to a row in the MySQL format. More...
 
void row_sel_convert_mysql_key_to_innobase (dtuple_t *tuple, byte *buf, ulint buf_len, dict_index_t *index, const byte *key_ptr, ulint key_len)
 Converts a key value stored in MySQL format to an Innobase dtuple. More...
 
static dberr_t row_search_for_mysql (byte *buf, page_cur_mode_t mode, row_prebuilt_t *prebuilt, ulint match_mode, ulint direction)
 Searches for rows in the database. More...
 
dberr_t row_search_no_mvcc (byte *buf, page_cur_mode_t mode, row_prebuilt_t *prebuilt, ulint match_mode, ulint direction)
 Searches for rows in the database using cursor. More...
 
dberr_t row_search_mvcc (byte *buf, page_cur_mode_t mode, row_prebuilt_t *prebuilt, ulint match_mode, const ulint direction)
 Searches for rows in the database using cursor. More...
 
dberr_t row_count_rtree_recs (row_prebuilt_t *prebuilt, ulint *n_rows, ulint *n_dups)
 Count rows in a R-Tree leaf level. More...
 
dberr_t row_search_max_autoinc (dict_index_t *index, const char *col_name, uint64_t *value)
 Read the max AUTOINC value from an index. More...
 
void row_sel_field_store_in_mysql_format_func (byte *dest, const mysql_row_templ_t *templ, const dict_index_t *index, ulint field_no, const byte *data, ulint len, ulint sec_field)
 Stores a non-SQL-NULL field in the MySQL format. More...
 
static void row_sel_field_store_in_mysql_format (byte *dest, const mysql_row_templ_t *templ, const dict_index_t *idx, ulint field, const byte *src, ulint len, ulint sec)
 Convert a non-SQL-NULL field from Innobase format to MySQL format. More...
 
bool row_search_table_stats (const char *db_name, const char *tbl_name, TableStatsRecord &table_stats)
 Search the record present in innodb_table_stats table using db_name, table_name and fill it in table stats structure. More...
 
bool row_search_index_stats (const char *db_name, const char *tbl_name, const char *index_name, ulint col_offset, ulonglong *cardinality)
 Search the record present in innodb_index_stats using db_name, table name and index_name and fill the cardinality for the each column. More...
 

Detailed Description

Select.

Created 12/19/1997 Heikki Tuuri

Enumeration Type Documentation

◆ open_node_op

Open or close cursor operation type.

Enumerator
ROW_SEL_OPEN_CURSOR 

open cursor

ROW_SEL_CLOSE_CURSOR 

close cursor

◆ row_sel_direction

Search direction for the MySQL interface.

Enumerator
ROW_SEL_NEXT 

ascending direction

ROW_SEL_PREV 

descending direction

◆ row_sel_match_mode

Match mode for the MySQL interface.

Enumerator
ROW_SEL_EXACT 

search using a complete key value

ROW_SEL_EXACT_PREFIX 

search using a key prefix which must match rows: the prefix may contain an incomplete field (the last field in prefix may be just a prefix of a fixed length column)

◆ sel_node_state

Select node states.

Enumerator
SEL_NODE_CLOSED 

it is a declared cursor which is not currently open

SEL_NODE_OPEN 

intention locks not yet set on tables

SEL_NODE_FETCH 

intention locks have been set

SEL_NODE_NO_MORE_ROWS 

cursor has reached the result set end

Function Documentation

◆ fetch_step()

que_thr_t * fetch_step ( que_thr_t thr)

Performs a fetch for a cursor.

Returns
query thread to run next or NULL in: query thread
query thread to run next or NULL
Parameters
thrin: query thread

◆ open_step()

static que_thr_t * open_step ( que_thr_t thr)
inlinestatic

Performs an execution step of an open or close cursor statement node.

Returns
query thread to run next or NULL in: query thread

◆ row_count_rtree_recs()

dberr_t row_count_rtree_recs ( row_prebuilt_t prebuilt,
ulint n_rows,
ulint n_dups 
)

Count rows in a R-Tree leaf level.

Returns
DB_SUCCESS if successful out: number of dup entries seen in the consistent read
DB_SUCCESS if successful
Parameters
prebuiltin: prebuilt struct for the table handle; this contains the info of search_tuple, index; if search tuple contains 0 fields then we position the cursor at the start or the end of the index, depending on 'mode'
n_rowsout: number of entries seen in the consistent read
n_dupsout: number of dup entries

◆ row_search_for_mysql()

static dberr_t row_search_for_mysql ( byte buf,
page_cur_mode_t  mode,
row_prebuilt_t prebuilt,
ulint  match_mode,
ulint  direction 
)
inlinestatic

Searches for rows in the database.

This is used in the interface to MySQL. This function opens a cursor, and also implements fetch next and fetch prev. NOTE that if we do a search with a full key value from a unique index (ROW_SEL_EXACT), then we will not store the cursor position and fetch next or fetch prev must not be tried to the cursor!

Parameters
[out]bufbuffer for the fetched row in MySQL format
[in]modesearch mode PAGE_CUR_L
[in,out]prebuiltprebuilt struct for the table handler; this contains the info to search_tuple, index; if search tuple contains 0 field then we position the cursor at start or the end of index, depending on 'mode'
[in]match_mode0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
[in]direction0 or ROW_SEL_NEXT or ROW_SEL_PREV; Note: if this is != 0, then prebuilt must has a pcur with stored position! In opening of a cursor 'direction' should be 0.
Returns
DB_SUCCESS, DB_RECORD_NOT_FOUND, DB_END_OF_INDEX, DB_DEADLOCK, DB_LOCK_TABLE_FULL, DB_CORRUPTION, or DB_TOO_BIG_RECORD

◆ row_search_index_stats()

bool row_search_index_stats ( const char *  db_name,
const char *  tbl_name,
const char *  index_name,
ulint  col_offset,
ulonglong cardinality 
)

Search the record present in innodb_index_stats using db_name, table name and index_name and fill the cardinality for the each column.

Parameters
[in]db_namedatabase name
[in]tbl_nametable name
[in]index_nameindex name
[in]col_offsetoffset of the column in the index
[out]cardinalitycardinality of the column.
Returns
true if successful else false.

Number of fields to search in the table.

Column number of innodb_index_stats.database_name.

Column number of innodb_index_stats.table_name.

Column number of innodb_index_stats.index_name.

Column number of innodb_index_stats.stat_value.

Search the innodb_index_stats table using (database_name, table_name, index_name).

◆ row_search_max_autoinc()

dberr_t row_search_max_autoinc ( dict_index_t index,
const char *  col_name,
uint64_t *  value 
)

Read the max AUTOINC value from an index.

Returns
DB_SUCCESS if all OK else error code out: AUTOINC value read
DB_SUCCESS if all OK else error code, DB_RECORD_NOT_FOUND if column name can't be found in index
Parameters
indexin: index to search
col_namein: name of autoinc column
valueout: AUTOINC value read

◆ row_search_mvcc()

dberr_t row_search_mvcc ( byte buf,
page_cur_mode_t  mode,
row_prebuilt_t prebuilt,
ulint  match_mode,
const ulint  direction 
)

Searches for rows in the database using cursor.

Function is mainly used for tables that are shared accorss connection and so it employs technique that can help re-construct the rows that transaction is suppose to see. It also has optimization such as pre-caching the rows, using AHI, etc.

Parameters
[out]bufbuffer for the fetched row in MySQL format
[in]modesearch mode PAGE_CUR_L
[in,out]prebuiltprebuilt struct for the table handler; this contains the info to search_tuple, index; if search tuple contains 0 field then we position the cursor at start or the end of index, depending on 'mode'
[in]match_mode0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
[in]direction0 or ROW_SEL_NEXT or ROW_SEL_PREV; Note: if this is != 0, then prebuilt must has a pcur with stored position! In opening of a cursor 'direction' should be 0.
Returns
DB_SUCCESS or error code

Compare the last record of the page with end range passed to InnoDB when there is no ICP and number of loops in row_search_mvcc for rows found but not reporting due to search views etc. When scanning a multi-value index, we don't perform the check because we cannot convert the indexed value (single scalar element) into the primary index (virtual) column type (array of values).

Secondary index record but the template based on PK.

Create offsets based on prebuilt index.

In case of prebuilt->fetch, set the error in prebuilt->end_range.

◆ row_search_no_mvcc()

dberr_t row_search_no_mvcc ( byte buf,
page_cur_mode_t  mode,
row_prebuilt_t prebuilt,
ulint  match_mode,
ulint  direction 
)

Searches for rows in the database using cursor.

Function is for temporary tables that are not shared across connections and so lot of complexity is reduced especially locking and transaction related. The cursor is an iterator over the table/index.

Parameters
[out]bufbuffer for the fetched row in MySQL format
[in]modesearch mode PAGE_CUR_L
[in,out]prebuiltprebuilt struct for the table handler; this contains the info to search_tuple, index; if search tuple contains 0 field then we position the cursor at start or the end of index, depending on 'mode'
[in]match_mode0 or ROW_SEL_EXACT or ROW_SEL_EXACT_PREFIX
[in]direction0 or ROW_SEL_NEXT or ROW_SEL_PREV; Note: if this is != 0, then prebuilt must has a pcur with stored position! In opening of a cursor 'direction' should be 0.
Returns
DB_SUCCESS or error code

◆ row_search_table_stats()

bool row_search_table_stats ( const char *  db_name,
const char *  tbl_name,
TableStatsRecord table_stats 
)

Search the record present in innodb_table_stats table using db_name, table_name and fill it in table stats structure.

Parameters
[in]db_namedatabase name
[in]tbl_nametable name
[out]table_statsstats table structure.
Returns
true if successful else false.

◆ row_sel_convert_mysql_key_to_innobase()

void row_sel_convert_mysql_key_to_innobase ( dtuple_t tuple,
byte buf,
ulint  buf_len,
dict_index_t index,
const byte key_ptr,
ulint  key_len 
)

Converts a key value stored in MySQL format to an Innobase dtuple.

The last field of the key value may be just a prefix of a fixed length field: hence the parameter key_len. But currently we do not allow search keys where the last field is only a prefix of the full key field len and print a warning if such appears.

Parameters
[in,out]tupleTuple where to build; NOTE: we assume that the type info in the tuple is already according to index!
[in]bufBuffer to use in field conversions; NOTE that dtuple->data may end up pointing inside buf so do not discard that buffer while the tuple is being used. See row_mysql_store_col_in_innobase_format() in the case of DATA_INT.
[in]buf_lenBuffer length.
[in]indexIndex of the key value.
[in]key_ptrMySQL key value
[in]key_lenMySQL key value length

◆ row_sel_copy_cached_fields_for_mysql()

void row_sel_copy_cached_fields_for_mysql ( byte buf,
const byte cached_rec,
row_prebuilt_t prebuilt 
)

Copy used fields from cached row.

Copy cache record field by field, don't touch fields that are not covered by current key.

Parameters
[out]bufWhere to copy the MySQL row.
[in]cached_recWhat to copy (in MySQL row format).
[in]prebuiltprebuilt struct.

◆ row_sel_field_store_in_mysql_format()

static void row_sel_field_store_in_mysql_format ( byte dest,
const mysql_row_templ_t templ,
const dict_index_t idx,
ulint  field,
const byte src,
ulint  len,
ulint  sec 
)
inlinestatic

Convert a non-SQL-NULL field from Innobase format to MySQL format.

◆ row_sel_field_store_in_mysql_format_func()

void row_sel_field_store_in_mysql_format_func ( byte dest,
const mysql_row_templ_t templ,
const dict_index_t index,
ulint  field_no,
const byte data,
ulint  len,
ulint  sec_field 
)

Stores a non-SQL-NULL field in the MySQL format.

The counterpart of this function is row_mysql_store_col_in_innobase_format() in row0mysql.cc.

Parameters
[in,out]destbuffer where to store; NOTE that BLOBs are not in themselves stored here: the caller must allocate and copy the BLOB into buffer before, and pass the pointer to the BLOB in 'data'
[in]templMySQL column template. Its following fields are referenced: type, is_unsigned, mysql_col_len, mbminlen, mbmaxlen
[in]indexInnoDB index
[in]field_notempl->rec_field_no or templ->clust_rec_field_no or templ->icp_rec_field_no
[in]datadata to store
[in]lenlength of the data
[in]sec_fieldsecondary index field no if the secondary index record but the prebuilt template is in clustered index format and used only for end range comparison.

◆ row_sel_step()

que_thr_t * row_sel_step ( que_thr_t thr)

Performs a select step.

This is a high-level function used in SQL execution graphs.

Returns
query thread to run next or NULL in: query thread

This is a high-level function used in SQL execution graphs.

Returns
query thread to run next or NULL
Parameters
thrin: query thread

◆ row_sel_store_mysql_rec()

bool row_sel_store_mysql_rec ( byte mysql_rec,
row_prebuilt_t prebuilt,
const rec_t rec,
const dtuple_t vrow,
bool  rec_clust,
const dict_index_t rec_index,
const dict_index_t prebuilt_index,
const ulint offsets,
bool  clust_templ_for_sec,
lob::undo_vers_t lob_undo,
mem_heap_t *&  blob_heap 
)

Convert a row in the Innobase format to a row in the MySQL format.

Note that the template in prebuilt may advise us to copy only a few columns to mysql_rec, other columns are left blank. All columns may not be needed in the query.

Parameters
[out]mysql_recrow in the MySQL format
[in,out]prebuiltprebuilt structure
[in]recInnobase record in the index which was described in prebuilt's template, or in the clustered index; must be protected by a page latch
[in]vrowvirtual columns
[in]rec_clusttrue if rec is in the clustered index instead of index which could belong to prebuilt->index
[in]rec_indexindex of rec
[in]prebuilt_indexprebuilt->index
[in]offsetsarray returned by rec_get_offsets(rec)
[in]clust_templ_for_sectrue if rec belongs to secondary index but the prebuilt->template is in clustered index format and it is used only for end range comparison
[in]lob_undothe LOB undo information.
[in,out]blob_heapIf not null then use this heap for BLOBs
Returns
true on success, false if not all columns could be retrieved

◆ sel_col_prefetch_buf_free()

void sel_col_prefetch_buf_free ( sel_buf_t prefetch_buf)

Frees a prefetch buffer for a column, including the dynamically allocated memory for data stored there.

in, own: prefetch buffer

Parameters
prefetch_bufin, own: prefetch buffer

◆ sel_node_create()

sel_node_t * sel_node_create ( mem_heap_t heap)

Creates a select node struct.

Returns
own: select node struct in: memory heap where created
own: select node struct
Parameters
heapin: memory heap where created

◆ sel_node_free_private()

void sel_node_free_private ( sel_node_t node)

Frees the memory private to a select node when a query graph is freed, does not free the heap where the node was originally created.

in: select node struct

Parameters
nodein: select node struct

◆ sel_node_get_nth_plan()

static plan_t * sel_node_get_nth_plan ( sel_node_t node,
ulint  i 
)
inlinestatic

Gets the plan node for the nth table in a join.

Parameters
[in]nodeselect node
[in]iget ith plan node
Returns
plan node