Description:
With KEY partitioning (PARTITION BY KEY() over a CHAR primary key, KEY(c),
LINEAR KEY(c), SUBPARTITION BY KEY(c)) on a CHAR(n) column whose collation is
one of the NO PAD utf8mb4_0900 collations, including the default
utf8mb4_0900_ai_ci, two values that the collation treats as equal but that
have different lengths in bytes are put into different partitions. Examples:
'Muller' / 'Müller', 'Jose' / 'José', 'Cafe' / 'Café' under utf8mb4_0900_ai_ci,
and a precomposed 'é' (C3A9) / decomposed 'e' + U+0301 (65CC81) under the
accent- and case-sensitive utf8mb4_0900_as_cs.
Since the uniqueness check runs inside one partition, and pruning sends a
lookup to the partition of the constant:
- the PRIMARY KEY holds both values (the same INSERTs into the same table
without partitioning fail with ERROR 1062);
- INSERT ... ON DUPLICATE KEY UPDATE inserts a second row instead of updating;
- SELECT, UPDATE and DELETE ... WHERE code = 'Jose' read only one partition
and miss 'José' (DELETE leaves it in the table);
- CHECK TABLE ... EXTENDED reports OK, and ALTER TABLE ... REMOVE
PARTITIONING fails with ERROR 1062, so the rows cannot be moved into a
non-partitioned table.
There is no error or warning when the rows are written. VARCHAR columns and
CHAR columns with a PAD SPACE collation (utf8mb4_unicode_ci) reject the second
value as they should. Which pairs end up in different partitions depends on
the number of partitions (the hash values differ, but can still be equal
modulo the partition count); with 8 partitions all three pairs below are split.
The manual allows CHAR columns in KEY partitioning without a caveat ("it is
possible to use columns of any valid MySQL data type other than TEXT or BLOB
as partitioning keys, because the internal key-hashing functions produce the
correct data type from these types",
https://dev.mysql.com/doc/refman/26.10/en/partitioning-limitations.html).
How to repeat:
-- run with: mysql -uroot --table --force < file
SET NAMES utf8mb4;
CREATE DATABASE m220; USE m220;
SELECT @@collation_database, 'Muller' = 'Müller', 'Jose' = 'José', 'Cafe' = 'Café';
-- without partitioning the second spelling is a duplicate key
CREATE TABLE cust_np (code CHAR(8) NOT NULL PRIMARY KEY, name VARCHAR(20));
INSERT INTO cust_np VALUES ('Muller', 'first Muller');
INSERT INTO cust_np VALUES ('Müller', 'second Müller');
-- the same table with KEY partitioning
CREATE TABLE cust (code CHAR(8) NOT NULL PRIMARY KEY, name VARCHAR(20)) PARTITION BY KEY() PARTITIONS 8;
INSERT INTO cust VALUES ('Muller', 'first Muller');
INSERT INTO cust VALUES ('Müller', 'second Müller');
INSERT INTO cust VALUES ('Jose', 'first Jose');
INSERT INTO cust VALUES ('José', 'second José');
INSERT INTO cust VALUES ('Cafe', 'first Cafe');
INSERT INTO cust VALUES ('Café', 'second Café');
SELECT COUNT(*) FROM cust;
EXPLAIN FORMAT=TRADITIONAL SELECT name FROM cust WHERE code = 'Jose';
EXPLAIN FORMAT=TRADITIONAL SELECT name FROM cust WHERE code = 'José';
SELECT name FROM cust WHERE code = 'Muller';
SELECT name FROM cust WHERE CONCAT(code) = 'Muller';
DELETE FROM cust WHERE code = 'Jose';
SELECT code, name FROM cust WHERE CONCAT(code) = 'Jose';
CHECK TABLE cust EXTENDED;
ALTER TABLE cust REMOVE PARTITIONING;
-- an upsert inserts a second row
CREATE TABLE stock (sku CHAR(10) NOT NULL PRIMARY KEY, qty INT NOT NULL) PARTITION BY KEY() PARTITIONS 8;
INSERT INTO stock VALUES ('Cafe', 10);
INSERT INTO stock VALUES ('Café', 5) AS new ON DUPLICATE KEY UPDATE qty = stock.qty + new.qty;
SELECT sku, qty FROM stock ORDER BY qty;
-- NFC and NFD spellings of 'é' are equal under utf8mb4_0900_as_cs as well
CREATE TABLE tag (t CHAR(4) COLLATE utf8mb4_0900_as_cs NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 7;
INSERT INTO tag VALUES (_utf8mb4 X'C3A9');
INSERT INTO tag VALUES (_utf8mb4 X'65CC81');
SELECT HEX(t) FROM tag ORDER BY 1;
-- controls: VARCHAR, and CHAR with a PAD SPACE collation
CREATE TABLE ctl1 (code VARCHAR(8) NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 8;
INSERT INTO ctl1 VALUES ('Muller');
INSERT INTO ctl1 VALUES ('Müller');
CREATE TABLE ctl2 (code CHAR(8) COLLATE utf8mb4_unicode_ci NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 8;
INSERT INTO ctl2 VALUES ('Muller');
INSERT INTO ctl2 VALUES ('Müller');
Result on 26.10.0 (8.4.13 returns the same):
SELECT @@collation_database, ...: utf8mb4_0900_ai_ci | 1 | 1 | 1
INSERT INTO cust_np ... 'Müller': ERROR 1062 (23000): Duplicate entry 'Müller' for key 'cust_np.PRIMARY'
the six INSERTs into cust: Query OK, 1 row affected (each)
SELECT COUNT(*) FROM cust: 6
EXPLAIN ... code = 'Jose': partitions p4
EXPLAIN ... code = 'José': partitions p5
SELECT name ... code = 'Muller': first Muller
... CONCAT(code) = 'Muller': second Müller, first Muller
DELETE ... code = 'Jose': Query OK, 1 row affected
... CONCAT(code) = 'Jose': José | second José <- still there
CHECK TABLE cust EXTENDED: status OK
ALTER TABLE cust REMOVE PARTITIONING:
ERROR 1062 (23000): Duplicate entry 'Café' for key 'cust.PRIMARY'
upsert: Query OK, 1 row affected; stock: (Café, 5), (Cafe, 10)
(expected 2 rows affected and one row (Cafe, 15))
tag: both INSERTs succeed; HEX(t): 65CC81, C3A9
ctl1 / ctl2 second INSERT: ERROR 1062 (23000): Duplicate entry 'Müller' for key 'ctl1.PRIMARY' / 'ctl2.PRIMARY'
Expected: the second spelling is rejected with ERROR 1062 (or, for the upsert,
updates the existing row), as without partitioning.
Suggested fix:
KEY partitioning computes the partition with
Partition_helper::ph_calculate_key_hash_value(), which calls field->hash():
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/pa...
(the call is at line 902). It is used for placing rows and for pruning
col = const.
Field_string (CHAR) does not override hash(); the base Field::hash() hashes
pack_length() bytes, i.e. the whole CHAR buffer of n * mbmaxlen bytes with its
pad spaces:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
For the NO PAD 0900 collations, my_hash_sort_uca_900() hashes every weight,
including the weights of the pad spaces:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/string...
so the hash depends on the number of pad bytes: in a CHAR(8) utf8mb4 buffer
(32 bytes) 'Muller' has 26 pad spaces and 'Müller' 25.
Comparisons strip the padding first: Field_string::cmp() ("Our CHAR default
behavior is to strip spaces. For PAD SPACE collations, this doesn't matter,
for but NO PAD, we need to do it ourselves here.") calls lengthsp() before
strnncollsp():
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
so =, the index lookup and the duplicate-key check treat the two values as
equal, but only inside one partition. The PAD SPACE hash function strips
trailing spaces (my_hash_sort_uca() calls lengthsp()), and
Field_varstring::hash() hashes only the data bytes, which is why those cases
are consistent:
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/string...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
A Field_string::hash() that strips the trailing spaces for NO PAD collations
(unless PAD_CHAR_TO_FULL_LENGTH is set), as cmp() does, would make the
partition consistent with the comparison. Since the hash decides where rows
are stored, existing KEY-partitioned tables with such columns would have to be
re-partitioned after such a change (and duplicate keys resolved first).
Description: With KEY partitioning (PARTITION BY KEY() over a CHAR primary key, KEY(c), LINEAR KEY(c), SUBPARTITION BY KEY(c)) on a CHAR(n) column whose collation is one of the NO PAD utf8mb4_0900 collations, including the default utf8mb4_0900_ai_ci, two values that the collation treats as equal but that have different lengths in bytes are put into different partitions. Examples: 'Muller' / 'Müller', 'Jose' / 'José', 'Cafe' / 'Café' under utf8mb4_0900_ai_ci, and a precomposed 'é' (C3A9) / decomposed 'e' + U+0301 (65CC81) under the accent- and case-sensitive utf8mb4_0900_as_cs. Since the uniqueness check runs inside one partition, and pruning sends a lookup to the partition of the constant: - the PRIMARY KEY holds both values (the same INSERTs into the same table without partitioning fail with ERROR 1062); - INSERT ... ON DUPLICATE KEY UPDATE inserts a second row instead of updating; - SELECT, UPDATE and DELETE ... WHERE code = 'Jose' read only one partition and miss 'José' (DELETE leaves it in the table); - CHECK TABLE ... EXTENDED reports OK, and ALTER TABLE ... REMOVE PARTITIONING fails with ERROR 1062, so the rows cannot be moved into a non-partitioned table. There is no error or warning when the rows are written. VARCHAR columns and CHAR columns with a PAD SPACE collation (utf8mb4_unicode_ci) reject the second value as they should. Which pairs end up in different partitions depends on the number of partitions (the hash values differ, but can still be equal modulo the partition count); with 8 partitions all three pairs below are split. The manual allows CHAR columns in KEY partitioning without a caveat ("it is possible to use columns of any valid MySQL data type other than TEXT or BLOB as partitioning keys, because the internal key-hashing functions produce the correct data type from these types", https://dev.mysql.com/doc/refman/26.10/en/partitioning-limitations.html). How to repeat: -- run with: mysql -uroot --table --force < file SET NAMES utf8mb4; CREATE DATABASE m220; USE m220; SELECT @@collation_database, 'Muller' = 'Müller', 'Jose' = 'José', 'Cafe' = 'Café'; -- without partitioning the second spelling is a duplicate key CREATE TABLE cust_np (code CHAR(8) NOT NULL PRIMARY KEY, name VARCHAR(20)); INSERT INTO cust_np VALUES ('Muller', 'first Muller'); INSERT INTO cust_np VALUES ('Müller', 'second Müller'); -- the same table with KEY partitioning CREATE TABLE cust (code CHAR(8) NOT NULL PRIMARY KEY, name VARCHAR(20)) PARTITION BY KEY() PARTITIONS 8; INSERT INTO cust VALUES ('Muller', 'first Muller'); INSERT INTO cust VALUES ('Müller', 'second Müller'); INSERT INTO cust VALUES ('Jose', 'first Jose'); INSERT INTO cust VALUES ('José', 'second José'); INSERT INTO cust VALUES ('Cafe', 'first Cafe'); INSERT INTO cust VALUES ('Café', 'second Café'); SELECT COUNT(*) FROM cust; EXPLAIN FORMAT=TRADITIONAL SELECT name FROM cust WHERE code = 'Jose'; EXPLAIN FORMAT=TRADITIONAL SELECT name FROM cust WHERE code = 'José'; SELECT name FROM cust WHERE code = 'Muller'; SELECT name FROM cust WHERE CONCAT(code) = 'Muller'; DELETE FROM cust WHERE code = 'Jose'; SELECT code, name FROM cust WHERE CONCAT(code) = 'Jose'; CHECK TABLE cust EXTENDED; ALTER TABLE cust REMOVE PARTITIONING; -- an upsert inserts a second row CREATE TABLE stock (sku CHAR(10) NOT NULL PRIMARY KEY, qty INT NOT NULL) PARTITION BY KEY() PARTITIONS 8; INSERT INTO stock VALUES ('Cafe', 10); INSERT INTO stock VALUES ('Café', 5) AS new ON DUPLICATE KEY UPDATE qty = stock.qty + new.qty; SELECT sku, qty FROM stock ORDER BY qty; -- NFC and NFD spellings of 'é' are equal under utf8mb4_0900_as_cs as well CREATE TABLE tag (t CHAR(4) COLLATE utf8mb4_0900_as_cs NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 7; INSERT INTO tag VALUES (_utf8mb4 X'C3A9'); INSERT INTO tag VALUES (_utf8mb4 X'65CC81'); SELECT HEX(t) FROM tag ORDER BY 1; -- controls: VARCHAR, and CHAR with a PAD SPACE collation CREATE TABLE ctl1 (code VARCHAR(8) NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 8; INSERT INTO ctl1 VALUES ('Muller'); INSERT INTO ctl1 VALUES ('Müller'); CREATE TABLE ctl2 (code CHAR(8) COLLATE utf8mb4_unicode_ci NOT NULL PRIMARY KEY) PARTITION BY KEY() PARTITIONS 8; INSERT INTO ctl2 VALUES ('Muller'); INSERT INTO ctl2 VALUES ('Müller'); Result on 26.10.0 (8.4.13 returns the same): SELECT @@collation_database, ...: utf8mb4_0900_ai_ci | 1 | 1 | 1 INSERT INTO cust_np ... 'Müller': ERROR 1062 (23000): Duplicate entry 'Müller' for key 'cust_np.PRIMARY' the six INSERTs into cust: Query OK, 1 row affected (each) SELECT COUNT(*) FROM cust: 6 EXPLAIN ... code = 'Jose': partitions p4 EXPLAIN ... code = 'José': partitions p5 SELECT name ... code = 'Muller': first Muller ... CONCAT(code) = 'Muller': second Müller, first Muller DELETE ... code = 'Jose': Query OK, 1 row affected ... CONCAT(code) = 'Jose': José | second José <- still there CHECK TABLE cust EXTENDED: status OK ALTER TABLE cust REMOVE PARTITIONING: ERROR 1062 (23000): Duplicate entry 'Café' for key 'cust.PRIMARY' upsert: Query OK, 1 row affected; stock: (Café, 5), (Cafe, 10) (expected 2 rows affected and one row (Cafe, 15)) tag: both INSERTs succeed; HEX(t): 65CC81, C3A9 ctl1 / ctl2 second INSERT: ERROR 1062 (23000): Duplicate entry 'Müller' for key 'ctl1.PRIMARY' / 'ctl2.PRIMARY' Expected: the second spelling is rejected with ERROR 1062 (or, for the upsert, updates the existing row), as without partitioning. Suggested fix: KEY partitioning computes the partition with Partition_helper::ph_calculate_key_hash_value(), which calls field->hash(): https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/pa... (the call is at line 902). It is used for placing rows and for pruning col = const. Field_string (CHAR) does not override hash(); the base Field::hash() hashes pack_length() bytes, i.e. the whole CHAR buffer of n * mbmaxlen bytes with its pad spaces: https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi... For the NO PAD 0900 collations, my_hash_sort_uca_900() hashes every weight, including the weights of the pad spaces: https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/string... so the hash depends on the number of pad bytes: in a CHAR(8) utf8mb4 buffer (32 bytes) 'Muller' has 26 pad spaces and 'Müller' 25. Comparisons strip the padding first: Field_string::cmp() ("Our CHAR default behavior is to strip spaces. For PAD SPACE collations, this doesn't matter, for but NO PAD, we need to do it ourselves here.") calls lengthsp() before strnncollsp(): https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi... so =, the index lookup and the duplicate-key check treat the two values as equal, but only inside one partition. The PAD SPACE hash function strips trailing spaces (my_hash_sort_uca() calls lengthsp()), and Field_varstring::hash() hashes only the data bytes, which is why those cases are consistent: https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/string... https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi... A Field_string::hash() that strips the trailing spaces for NO PAD collations (unless PAD_CHAR_TO_FULL_LENGTH is set), as cmp() does, would make the partition consistent with the comparison. Since the hash decides where rows are stored, existing KEY-partitioned tables with such columns would have to be re-partitioned after such a change (and duplicate keys resolved first).