Description:
In a table partitioned BY KEY on a DOUBLE or FLOAT column (PARTITION BY KEY()
over the primary key, PARTITION BY KEY (c), or SUBPARTITION BY KEY (c)), the
partition of a row is computed from the bytes of the value. -0.0 and 0.0
have different bytes (the sign bit), so they can be placed in different
partitions (with 7 partitions: 0 in p1, -0 in p5), although they compare
equal everywhere else (-0e0 = 0e0 is 1, and InnoDB treats them as the same
key). Of the partition counts 2 to 16, 31 and 32, a DOUBLE KEY (c) table
split the two zeros with 7, 9, 11, 13, 14 and 31 partitions; LINEAR KEY kept
them together in all of these counts. A -0.0 is easy to get from ordinary SQL: ROUND(-0.4e0) is -0, as is
-1 * 0e0, and clients can send it.
Because a unique key is only checked inside one partition:
- a PRIMARY KEY (or UNIQUE key) of the partitioned table stores both 0 and
-0 (the same table without partitioning rejects the second with
ERROR 1062);
- INSERT ... ON DUPLICATE KEY UPDATE with -0 for an existing key 0 inserts a
second row (1 row affected) instead of updating the existing one;
- partition pruning for price = 0 reads only the partition of 0, so
SELECT ... WHERE price = 0 finds 1 of the 2 zero rows and
DELETE ... WHERE price = 0 leaves the -0 row;
- ALTER TABLE ... REMOVE PARTITIONING then fails with ERROR 1062.
CHECK TABLE ... EXTENDED reports OK, and none of the statements gives a
warning.
Bug #67978 reports signed zero for MEMORY hash indexes and GROUP BY; its fix
(GitHub PR #788, "Normalize signed zero in MEMORY hash keys") changes
storage/heap/hp_hash.cc, ha_heap.cc and key_cmp_if_same() in sql/key.cc, not
the partitioning hash, so it does not cover this case.
How to repeat:
-- run with: mysql -uroot --table --force < file
CREATE DATABASE m213;
USE m213;
CREATE TABLE price_level (price DOUBLE PRIMARY KEY, hits INT NOT NULL) PARTITION BY KEY() PARTITIONS 7;
INSERT INTO price_level VALUES (0, 1), (1.5, 1), (-2.25, 1);
SELECT ROUND(-0.4e0), ROUND(-0.4e0) = 0;
-- the key 0 exists, so this must update it (2 rows affected)
INSERT INTO price_level VALUES (ROUND(-0.4e0), 1) ON DUPLICATE KEY UPDATE hits = hits + 1;
SELECT ROW_COUNT();
SELECT price, hits FROM price_level ORDER BY price;
SELECT price FROM price_level PARTITION (p1);
SELECT price FROM price_level PARTITION (p5);
SELECT COUNT(*), SUM(hits) FROM price_level WHERE price = 0;
EXPLAIN FORMAT=TRADITIONAL SELECT * FROM price_level WHERE price = 0;
DELETE FROM price_level WHERE price = 0;
SELECT price, hits FROM price_level ORDER BY price;
CHECK TABLE price_level EXTENDED;
-- the PRIMARY KEY accepts both zeros directly
CREATE TABLE pl2 (price DOUBLE PRIMARY KEY, hits INT NOT NULL) PARTITION BY KEY() PARTITIONS 7;
INSERT INTO pl2 VALUES (0, 1), (ROUND(-0.2e0), 1);
SELECT price, hits FROM pl2 ORDER BY hits;
ALTER TABLE pl2 REMOVE PARTITIONING;
-- the same INSERT without partitioning
CREATE TABLE np (price DOUBLE PRIMARY KEY, hits INT NOT NULL);
INSERT INTO np VALUES (0, 1), (ROUND(-0.2e0), 1);
Result on 26.10.0 (8.4.13 gives the same):
ROUND(-0.4e0), ROUND(-0.4e0) = 0: -0, 1
ROW_COUNT() of the upsert: 1 <- 2 expected (update)
price_level: (-2.25, 1), (-0, 1), (0, 1), (1.5, 1) <- two rows with key 0
PARTITION (p1): 0
PARTITION (p5): -0, 1.5
COUNT(*), SUM(hits) WHERE price = 0: 1, 1 <- 1, 2 expected
EXPLAIN: partitions p1, type const, key PRIMARY
after DELETE ... WHERE price = 0: (-2.25, 1), (-0, 1), (1.5, 1) <- the -0 row is still there
CHECK TABLE price_level EXTENDED: status OK
pl2: (0, 1), (-0, 1) <- duplicate PRIMARY KEY accepted
ERROR 1062 (23000) at line 23: Duplicate entry '-0' for key 'pl2.PRIMARY' <- the ALTER TABLE
ERROR 1062 (23000) at line 27: Duplicate entry '-0' for key 'np.PRIMARY' <- correct, no partitioning
Expected: the partitioned table behaves like the table without
partitioning: the INSERT into pl2 fails with ERROR 1062, the upsert updates
the existing row (2 rows affected, hits = 2, three rows in the table),
WHERE price = 0 finds the row, and the DELETE removes it.
Suggested fix:
Partition_helper::ph_calculate_key_hash_value() computes the KEY partition
hash; for FLOAT/DOUBLE it hashes the field bytes, with my_charset_bin in the
5.1-compatible branch or through Field::hash() otherwise (FLOAT and DOUBLE
do not override Field::hash(), which hashes the raw bytes with the field's
binary sort charset):
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/pa...
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/fi...
It is used for row placement and for pruning (get_part_id_key() /
get_part_id_linear_key()):
https://github.com/mysql/mysql-server/blob/06bcb0eb0ad281e08dd2b4d81b99ad25491433e7/sql/sq...
Uniqueness of a key that contains the partitioning columns is checked only
inside one partition, which is only sound if equal values always hash to the
same partition. Hashing -0.0 as 0.0 (as PR #788 now does for MEMORY hash
keys) would fix new rows; tables that already hold -0.0 rows would need them
moved (the code notes that the hash must stay stable on disk).
Workaround: avoid FLOAT/DOUBLE columns in KEY partitioning, or normalize
the value before storing it (for example price + 0e0, which turns -0 into 0).