diff options
author | Sachin <sachin.setiya@mariadb.com> | 2019-02-20 02:53:08 +0530 |
---|---|---|
committer | Sergei Golubchik <serg@mariadb.org> | 2019-02-22 00:35:40 +0100 |
commit | d00f19e8325cd2f4beb10cc89f25ada8434099db (patch) | |
tree | d09ee2cc68ef9d7db9c9e7f6fdf175ef9597c647 /sql/unireg.cc | |
parent | 5b4d6595d26aaaf0bf8bb3c9171cf1da96306a7c (diff) | |
download | mariadb-git-d00f19e8325cd2f4beb10cc89f25ada8434099db.tar.gz |
MDEV-371 Unique Index for long columns
This patch implements engine independent unique hash index.
Usage:- Unique HASH index can be created automatically for blob/varchar/test column whose key
length > handler->max_key_length()
or it can be explicitly specified.
Automatic Creation:-
Create TABLE t1 (a blob unique);
Explicit Creation:-
Create TABLE t1 (a int , unique(a) using HASH);
Internal KEY_PART Representations:-
Long unique key_info will have 2 representations.
(lets understand this with an example create table t1(a blob, b blob , unique(a, b)); )
1. User Given Representation:- key_info->key_part array will be similar to what user has defined.
So in case of example it will have 2 key_parts (a, b)
2. Storage Engine Representation:- In this case there will be only one key_part and it will point to
HASH_FIELD. This key_part will be always after user defined key_parts.
So:- User Given Representation [a] [b] [hash_key_part]
key_info->key_part ----^
Storage Engine Representation [a] [b] [hash_key_part]
key_info->key_part ------------^
Table->s->key_info will have User Given Representation, While table->key_info will have Storage Engine
Representation.Representation can be changed into each other by calling re/setup_keyinfo_hash function.
Working:-
1. So when user specifies HASH_INDEX or key_length is > handler->max_key_length(), In mysql_prepare_create_table
One extra vfield is added (for each long unique key). And key_info->algorithm is set to HA_KEY_ALG_LONG_HASH.
2. In init_from_binary_frm_image values for hash_keypart is set (like fieldnr , field and flags)
3. In parse_vcol_defs, HASH_FIELD->vcol_info is created. Item_func_hash is used with list of Item_fields,
When Explicit length is given by user then Item_left is used to concatenate Item_field values.
4. In ha_write_row/ha_update_row check_duplicate_long_entry_key is called which will create the hash key from
table->record[0] and then call ha_index_read_map , if we found duplicated hash , we will compare the result
field by field.
Diffstat (limited to 'sql/unireg.cc')
-rw-r--r-- | sql/unireg.cc | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/sql/unireg.cc b/sql/unireg.cc index 7a20c14c8d4..c84c4eaa973 100644 --- a/sql/unireg.cc +++ b/sql/unireg.cc @@ -39,7 +39,7 @@ /* threshold for safe_alloca */ #define ALLOCA_THRESHOLD 2048 -static uint pack_keys(uchar *,uint, KEY *, ulong); +static uint pack_keys(uchar *,uint, KEY *, ulong, uint); static bool pack_header(THD *, uchar *, List<Create_field> &, HA_CREATE_INFO *, ulong, handler *); static bool pack_vcols(String *, List<Create_field> &, List<Virtual_column_info> *); @@ -184,6 +184,7 @@ LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING &table, + extra2_str_size(create_info->period_info.constr->name.length) + 2 * frm_fieldno_size : 0; + uint e_unique_hash_extra_parts= 0; uchar fileinfo[FRM_HEADER_SIZE],forminfo[FRM_FORMINFO_SIZE]; const partition_info *part_info= IF_PARTITIONING(thd->work_part_info, 0); bool error; @@ -306,6 +307,9 @@ LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING &table, extra2_size+= 1 + extra2_str_size(create_fields.elements); } + for (i= 0; i < keys; i++) + if (key_info[i].algorithm == HA_KEY_ALG_LONG_HASH) + e_unique_hash_extra_parts++; key_buff_length= uint4korr(fileinfo+47); frm.length= FRM_HEADER_SIZE; // fileinfo; @@ -396,7 +400,7 @@ LEX_CUSTRING build_frm_image(THD *thd, const LEX_CSTRING &table, pos+= 4; DBUG_ASSERT(pos == frm_ptr + uint2korr(fileinfo+6)); - key_info_length= pack_keys(pos, keys, key_info, data_offset); + key_info_length= pack_keys(pos, keys, key_info, data_offset, e_unique_hash_extra_parts); if (key_info_length > UINT_MAX16) { my_printf_error(ER_CANT_CREATE_TABLE, @@ -558,7 +562,7 @@ err_frm: /* Pack keyinfo and keynames to keybuff for save in form-file. */ static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo, - ulong data_offset) + ulong data_offset, uint e_unique_hash_extra_parts) { uint key_parts,length; uchar *pos, *keyname_pos; @@ -620,6 +624,7 @@ static uint pack_keys(uchar *keybuff, uint key_count, KEY *keyinfo, } } + key_parts+= e_unique_hash_extra_parts; if (key_count > 127 || key_parts > 127) { keybuff[0]= (key_count & 0x7f) | 0x80; |