diff options
author | Monty <monty@mariadb.org> | 2023-01-29 20:39:37 +0200 |
---|---|---|
committer | Monty <monty@mariadb.org> | 2023-02-10 13:35:31 +0200 |
commit | 00704aff98400e3aed2624ff2a83b82f350df87c (patch) | |
tree | 0c0a1be98a144d394baad1334c191c391a398e6a /sql/sql_select.cc | |
parent | fe1f4ca8ced02aad38fa3e71da67b2a172787bda (diff) | |
download | mariadb-git-00704aff98400e3aed2624ff2a83b82f350df87c.tar.gz |
Fixed bug in extended key handling when there is no primary key
Extended keys works by first checking if the engine supports extended
keys.
If yes, it extends secondary key with primary key components and mark the
secondary keys as HA_EXT_NOSAME (unique).
If we later notice that there where no primary key, the extended key
information for secondary keys in share->key_info is reset. However the
key_info->flag HA_EXT_NOSAME was not reset!
This causes some strange things to happen:
- Tables that have no primary key or secondary index that contained the
primary key would be wrongly optimized as the secondary key could be
thought to be unique when it was not and not unique when it was.
- The problem was not shown in EXPLAIN because of a bug in
create_ref_for_key() that caused EQ_REF to be displayed by EXPLAIN as REF
when extended keys where used and the secondary key contained the primary
key.
This is fixed with:
- Removed wrong test in make_join_select() which did not detect that key
where unique when a secondary key contains the primary.
- Moved initialization of extended keys from create_key_infos() to
init_from_binary_frm_image() after we know if there is a usable primary
key or not. One disadvantage with this approach is that
key_info->key_parts may have not used slots (for keys we thought could
be extended but could not). Fixed by adding a check for unused key_parts
to copy_keys_from_share().
Other things:
- Simplified copying of first key part in create_key_infos().
- Added a lot of code comments in code that I had to check as part of
finding the issue.
- Fixed some indentation.
- Replaced a couple of looks using references to pointers in C
context where the reference does not give any benefit.
- Updated Aria and Maria to not assume the all key_info->rec_per_key
are in one memory block (this could happen when using dervived
tables with many keys).
- Fixed a bug where key_info->rec_per_key where not allocated
- Optimized TABLE::add_tmp_key() to only call alloc() once.
(No logic changes)
Test case changes:
- innodb_mysql.test changed index as an index the optimizer thought
was unique, was not. (Table had no primary key)
TODO:
- Move code that checks for partial or too long keys to the primary loop
earlier that initally decides if we should add extended key fields.
This is needed to ensure that HA_EXT_NOSAME is not set for partial or
too long keys. It will also shorten the current code notable.
Diffstat (limited to 'sql/sql_select.cc')
-rw-r--r-- | sql/sql_select.cc | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/sql/sql_select.cc b/sql/sql_select.cc index b1f28b9af51..bb90f3048d7 100644 --- a/sql/sql_select.cc +++ b/sql/sql_select.cc @@ -12528,16 +12528,14 @@ static bool create_ref_for_key(JOIN *join, JOIN_TAB *j, j->table->const_table= 1; else if (!((keyparts == keyinfo->user_defined_key_parts && ( - (key_flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME || - /* Unique key and all keyparts are NULL rejecting */ - ((key_flags & HA_NOSAME) && keyparts == not_null_keyparts) - )) || - /* true only for extended keys */ - (keyparts > keyinfo->user_defined_key_parts && - MY_TEST(key_flags & HA_EXT_NOSAME) && - keyparts == keyinfo->ext_key_parts) - ) || - null_ref_key) + (key_flags & (HA_NOSAME | HA_NULL_PART_KEY)) == HA_NOSAME || + /* Unique key and all keyparts are NULL rejecting */ + ((key_flags & HA_NOSAME) && keyparts == not_null_keyparts) + )) || + /* true only for extended keys */ + (MY_TEST(key_flags & HA_EXT_NOSAME) && + keyparts == keyinfo->ext_key_parts) ) || + null_ref_key) { /* Must read with repeat */ j->type= null_ref_key ? JT_REF_OR_NULL : JT_REF; @@ -20435,6 +20433,7 @@ TABLE *Create_tmp_table::start(THD *thd, sizeof(*m_key_part_info)*(param->group_parts+1), ¶m->start_recinfo, sizeof(*param->recinfo)*(field_count*2+4), + ¶m->rec_per_key, sizeof(ulong)*param->group_parts, &tmpname, (uint) strlen(path)+1, &m_group_buff, (m_group && ! m_using_unique_constraint ? param->group_length : 0), @@ -20967,13 +20966,15 @@ bool Create_tmp_table::finalize(THD *thd, keyinfo->usable_key_parts=keyinfo->user_defined_key_parts= param->group_parts; keyinfo->ext_key_parts= keyinfo->user_defined_key_parts; + share->ext_key_parts= share->key_parts= keyinfo->ext_key_parts; keyinfo->key_length=0; - keyinfo->rec_per_key=NULL; + keyinfo->rec_per_key= param->rec_per_key; keyinfo->read_stats= NULL; keyinfo->collected_stats= NULL; keyinfo->algorithm= HA_KEY_ALG_UNDEF; keyinfo->is_statistics_from_stat_tables= FALSE; keyinfo->name= group_key; + keyinfo->comment.str= 0; ORDER *cur_group= m_group; for (; cur_group ; cur_group= cur_group->next, m_key_part_info++) { @@ -21074,6 +21075,7 @@ bool Create_tmp_table::finalize(THD *thd, keyinfo->usable_key_parts= keyinfo->user_defined_key_parts; table->distinct= 1; share->keys= 1; + share->ext_key_parts= share->key_parts= keyinfo->ext_key_parts; if (!(m_key_part_info= (KEY_PART_INFO*) alloc_root(&table->mem_root, keyinfo->user_defined_key_parts * sizeof(KEY_PART_INFO)))) @@ -21570,6 +21572,7 @@ bool create_internal_tmp_table(TABLE *table, KEY *keyinfo, /* Can't create a key; Make a unique constraint instead of a key */ share->keys= 0; + share->key_parts= share->ext_key_parts= 0; share->uniques= 1; using_unique_constraint=1; bzero((char*) &uniquedef,sizeof(uniquedef)); @@ -21766,6 +21769,7 @@ bool create_internal_tmp_table(TABLE *table, KEY *keyinfo, { /* Can't create a key; Make a unique constraint instead of a key */ share->keys= 0; + share->key_parts= share->ext_key_parts= 0; share->uniques= 1; using_unique_constraint=1; bzero((char*) &uniquedef,sizeof(uniquedef)); |