key_copy: fix key_part length calculation for varchars/blobs

This commit is contained in:
Nikita Malyavin 2025-01-25 00:44:43 +01:00
commit 594c0c55d3
3 changed files with 22 additions and 1 deletions

View file

@ -267,3 +267,14 @@ Table Op Msg_type Msg_text
test.t check Warning Bogus foreign key t_ibfk_1 is skipped.
test.t check error Corrupt
drop table t;
# In a->b relation, `a` key_part can be shorter than `b`
create table t (a varchar(1), b varchar(8), key(b),
foreign key(a) references t(b)) engine=innodb;
set foreign_key_checks = off;
insert into t values ('d','dd');
set foreign_key_checks = on;
check table t extended;
Table Op Msg_type Msg_text
test.t check Warning Cannot add or update a child row: a foreign key constraint fails (Key: t_ibfk_1, record: 'd')
test.t check error Corrupt
drop table t;

View file

@ -193,3 +193,13 @@ insert into t values ('00:00:00','8000:0020:2020:2020:2020:2020:2020:2020');
check table t extended;
drop table t;
--echo # In a->b relation, `a` key_part can be shorter than `b`
create table t (a varchar(1), b varchar(8), key(b),
foreign key(a) references t(b)) engine=innodb;
set foreign_key_checks = off;
insert into t values ('d','dd');
set foreign_key_checks = on;
check table t extended;
drop table t;

View file

@ -174,7 +174,7 @@ void key_copy(uchar *to_key, uchar *extra_buff,
if (to_key_part->is_var_length())
key_length-= HA_KEY_BLOB_LENGTH;
length= MY_MIN(key_length, key_part->length);
length= MY_MIN(key_length, to_key_part->length);
uint bytes= field->get_key_image(to_key, length, from_ptr,
Field::image_type(to_key_info->algorithm));
if (with_zerofill && bytes < length)