1
0
mirror of https://github.com/MariaDB/server.git synced 2025-08-08 11:22:35 +03:00

MDEV-26294 Duplicate entries in unique index not detected when changing collation

Problem:
=======
ALTER TABLE in InnoDB fails to detect duplicate entries
for the unique index when the character set or collation of
an indexed column is changed in such a way that the character
encoding is compatible with the old table definition.
In this case, any secondary indexes on the changed columns
would be rebuilt (DROP INDEX, ADD INDEX).

Solution:
========
During ALTER TABLE, InnoDB keeps track of columns whose collation
changed, and will fill in the correct metadata when sorting the
index records, or applying changes from concurrent DML.
This metadata will be allocated in the dict_index_t::heap of
the being-created secondary indexes.

The fix was developed by Thirunarayanan Balathandayuthapani
and simplified by me.
This commit is contained in:
Marko Mäkelä
2022-07-04 11:15:15 +03:00
parent f43145a0e3
commit f8240a2723
13 changed files with 466 additions and 265 deletions

View File

@@ -9,11 +9,89 @@ msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_swedish_ci UNIQUE
) ENGINE=INNODB;
INSERT INTO t1 VALUES (1, 'aaa');
INSERT INTO t1 VALUES (2, 'ååå');
ALTER TABLE t1
MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
ALGORITHM=NOCOPY;
ERROR 0A000: ALGORITHM=NOCOPY is not supported. Reason: Collation change on an indexed column. Try ALGORITHM=COPY
ALTER TABLE t1 DROP INDEX msg,
MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci,
ALGORITHM=NOCOPY;
ALTER TABLE t1 MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci, ALGORITHM=inplace;
ERROR 23000: Duplicate entry 'NULL' for key 'msg'
DROP TABLE t1;
CREATE TABLE t1 (
id INT PRIMARY KEY,
msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_bin,
id_2 INT not null,
unique index(msg, id_2)
) ENGINE=INNODB;
INSERT INTO t1 VALUES (1, 'aaa', 2);
INSERT INTO t1 VALUES (2, 'AAA', 3);
ALTER TABLE t1 MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci, ALGORITHM=inplace;
DROP TABLE t1;
CREATE TABLE t1 (
id INT PRIMARY KEY,
msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_bin,
unique index(msg)
) ENGINE=INNODB;
INSERT INTO t1 VALUES (1, 'aaa');
INSERT INTO t1 VALUES (2, 'bbb');
INSERT INTO t1 VALUES (3, 'ccc');
SET DEBUG_SYNC = 'RESET';
SET DEBUG_SYNC = 'row_log_apply_before SIGNAL before_apply WAIT_FOR go_ahead';
ALTER TABLE t1 MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci, ALGORITHM=NOCOPY;
connect con1,localhost,root,,;
connection con1;
SET DEBUG_SYNC = 'now WAIT_FOR before_apply';
INSERT INTO t1 VALUES (4, 'AAA');
UPDATE t1 set msg = "ddd" where id = 2;
DELETE FROM t1 WHERE id= 3;
SET DEBUG_SYNC = 'now SIGNAL go_ahead';
SET DEBUG_SYNC = 'RESET';
connection default;
ERROR 23000: Duplicate entry 'NULL' for key 'msg'
SELECT * FROM t1;
id msg
4 AAA
1 aaa
2 ddd
DROP TABLE t1;
CREATE TABLE t1 (
id INT PRIMARY KEY,
msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_bin,
unique index(msg)
) ENGINE=INNODB;
INSERT INTO t1 VALUES (1, 'aaa');
SET DEBUG_DBUG="+d,create_index_fail";
SET DEBUG_SYNC="innodb_inplace_alter_table_enter SIGNAL con1_go WAIT_FOR alter_signal";
ALTER TABLE t1 MODIFY msg VARCHAR(100) CHARACTER SET utf8 COLLATE utf8_unicode_ci, ALGORITHM=NOCOPY;
connection con1;
SET DEBUG_SYNC="now WAIT_FOR con1_go";
BEGIN;
SELECT * FROM t1;
id msg
1 aaa
SET DEBUG_SYNC="now SIGNAL alter_signal";
connection default;
ERROR 23000: Duplicate entry '' for key '*UNKNOWN*'
connection con1;
rollback;
INSERT INTO t1 VALUES(2, 'bbb');
disconnect con1;
connection default;
SET DEBUG_SYNC=reset;
SHOW CREATE TABLE t1;
Table Create Table
t1 CREATE TABLE `t1` (
`id` int(11) NOT NULL,
`msg` varchar(100) CHARACTER SET utf8mb3 COLLATE utf8mb3_bin DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `msg` (`msg`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
INSERT INTO t1 VALUES(3, 'ccc');
DROP TABLE t1;
CREATE TABLE t1(id INT PRIMARY KEY, msg VARCHAR(100),
msg_1 VARCHAR(100) AS (msg) VIRTUAL,
msg_2 VARCHAR(100) AS (msg) STORED,
UNIQUE(msg), UNIQUE(msg_1),
UNIQUE(msg_2))ENGINE=InnoDB;
ALTER TABLE t1 MODIFY msg_1 VARCHAR(100) CHARACTER SET utf8
COLLATE utf8_unicode_ci, ALGORITHM=inplace;
ERROR HY000: This is not yet supported for generated columns
ALTER TABLE t1 MODIFY msg_2 VARCHAR(100) CHARACTER SET utf8
COLLATE utf8_unicode_ci, ALGORITHM=inplace;
ERROR 0A000: ALGORITHM=INPLACE is not supported. Reason: Cannot change column type. Try ALGORITHM=COPY
DROP TABLE t1;