MySQL 8.3.0
Source Code Documentation File Reference
#include "sql/key.h"
#include <string.h>
#include <algorithm>
#include "my_bitmap.h"
#include "my_byteorder.h"
#include "my_compare.h"
#include "my_compiler.h"
#include "my_dbug.h"
#include "my_macros.h"
#include "mysql/strings/m_ctype.h"
#include "sql/field.h"
#include "sql/handler.h"
#include "sql/sql_const.h"
#include "sql/sql_error.h"
#include "sql/table.h"
#include "sql_string.h"
#include "string_with_len.h"


int find_ref_key (KEY *key, uint key_count, uchar *record, Field *field, uint *key_length, uint *keypart)
void key_copy (uchar *to_key, const uchar *from_record, const KEY *key_info, uint key_length)
 Copy part of a record that forms a key or key prefix to a buffer. More...
void key_restore (uchar *to_record, const uchar *from_key, const KEY *key_info, uint key_length)
 Restore a key from some buffer to record. More...
bool key_cmp_if_same (const TABLE *table, const uchar *key, uint idx, uint key_length)
 Compare if a key has changed. More...
void field_unpack (String *to, Field *field, uint max_length, bool prefix_key)
 Unpack a field and append it. More...
void key_unpack (String *to, TABLE *table, KEY *key)
bool is_key_used (TABLE *table, uint idx, const MY_BITMAP *fields)
int key_cmp (KEY_PART_INFO *key_part, const uchar *key, uint key_length)
 Compare key in record buffer to a given key. More...
int key_cmp2 (KEY_PART_INFO *key_part, const uchar *key1, uint key1_length, const uchar *key2, uint key2_length)
 Compare two given keys. More...
int key_rec_cmp (KEY **key, uchar *first_rec, uchar *second_rec)
 Compare two records in index order. More...

Function Documentation

◆ field_unpack()

void field_unpack ( String to,
Field field,
uint  max_length,
bool  prefix_key 

Unpack a field and append it.

[in,out]toString to append the field contents to.
fieldField to unpack.
max_lengthMaximum length of field to unpack or 0 for unlimited.
prefix_keyThe field is used as a prefix key.

◆ find_ref_key()

int find_ref_key ( KEY key,
uint  key_count,
uchar record,
Field field,
uint *  key_length,
uint *  keypart 

◆ is_key_used()

bool is_key_used ( TABLE table,
uint  idx,
const MY_BITMAP fields 

◆ key_cmp()

int key_cmp ( KEY_PART_INFO key_part,
const uchar key,
uint  key_length 

Compare key in record buffer to a given key.

key_partKey part handler
keyKey to compare to value in table->record[0]
key_lengthlength of 'key'

The function compares given key and key in record buffer, part by part, using info from key_part arg. Since callers expect before/after rather than lesser/greater, result depends on the HA_REVERSE_SORT flag of the key part. E.g. For ASC key part and two keys, 'A' and 'Z', -1 will be returned. For same keys, but DESC key part, 1 will be returned.

The return value is SIGN(key_in_row - range_key):
  • 0 Key is equal to record's key
  • -1 Key is before record's key
  • 1 Key is after record's key
: keep this function and key_cmp2() in sync

◆ key_cmp2()

int key_cmp2 ( KEY_PART_INFO key_part,
const uchar key1,
uint  key1_length,
const uchar key2,
uint  key2_length 

Compare two given keys.

key_partKey part handler
key1Key to be compared with key2
key1_lengthlength of 'key1'
key2Key to be compared with key1
key2_lengthlength of 'key2'
The return value is an integral value that takes into account ASC/DESC order of keyparts and indicates the relationship between the two keys:
  • 0 key1 equal to key2
  • -1 Key1 before Key2
  • 1 Key1 after Key2
: keep this function and key_cmp() in sync

Below comparison code is under the assumption that key1_length and key2_length are same and key1_length, key2_length are non zero value.

See also

◆ key_cmp_if_same()

bool key_cmp_if_same ( const TABLE table,
const uchar key,
uint  idx,
uint  key_length 

Compare if a key has changed.

keykey to compare to row
idxIndex used
key_lengthLength of key
In theory we could just call field->cmp() for all field types, but as we are only interested if a key has changed (not if the key is larger or smaller than the previous value) we can do things a bit faster by using memcmp() instead.
Return values
0If key is equal
1Key has changed

◆ key_copy()

void key_copy ( uchar to_key,
const uchar from_record,
const KEY key_info,
uint  key_length 

Copy part of a record that forms a key or key prefix to a buffer.

The function takes a complete table record (as e.g. retrieved by handler::index_read()), and a description of an index on the same table, and extracts the first key_length bytes of the record which are part of a key into to_key. If length == 0 then copy all bytes from the record that form a key.

to_keybuffer that will be used as a key
from_recordfull record to be copied from
key_infodescriptor of the index
key_lengthspecifies length of all keyparts that will be copied

◆ key_rec_cmp()

int key_rec_cmp ( KEY **  key,
uchar first_rec,
uchar second_rec 

Compare two records in index order.

This method is set-up such that it can be called directly from the priority queue and it is attempted to be optimised as much as possible since this will be called O(N * log N) times while performing a merge sort in various places in the code.

We retrieve the pointer to table->record[0] using the fact that key_parts have an offset making it possible to calculate the start of the record. We need to get the diff to the compared record since none of the records being compared are stored in table->record[0].

We first check for NULL values, if there are no NULL values we use a compare method that gets two field pointers and a max length and return the result of the comparison.

key is a null terminated array, since in some cases (clustered primary key) it must compare more than one index. We only compare the fields that are specified in table->read_set and stop at the first non set field. The first must be set!

keyNull terminated array of index information
first_recPointer to record compare with
second_recPointer to record compare against first_rec
Return value is less, equal or greater than 0 if first rec is sorted before, same or after second rec.
Return values
0Keys are equal
-1second_rec is after first_rec
+1first_rec is after second_rec

◆ key_restore()

void key_restore ( uchar to_record,
const uchar from_key,
const KEY key_info,
uint  key_length 

Restore a key from some buffer to record.

This function converts a key into record format. It can be used in cases when we want to return a key as a result row.

to_recordrecord buffer where the key will be restored to
from_keybuffer that contains a key
key_infodescriptor of the index
key_lengthspecifies length of all keyparts that will be restored

◆ key_unpack()

void key_unpack ( String to,
TABLE table,
KEY key