Bug #67978 Signed zero is handled incorrectly by HASH indexes and GROUP BY

Submitted: 27 Dec 2012 6:13 Modified: 29 Dec 2012 16:21
Reporter: Alexey Kopytov Email Updates:
Status: Documenting Impact on me:
None 
Category:MySQL Server: Memory storage engine Severity:S3 (Non-critical)
Version:5.1, 5.5, 5.6 OS:Any
Assigned to: CPU Architecture:Any

[27 Dec 2012 6:13] Alexey Kopytov
Description:
Negative and positive zeroes are considered different values by HASH
indexes in the MEMORY storage engine:

mysql> SELECT -0e0 = 0e0;
+------------+
| -0e0 = 0e0 |
+------------+
|          1 |
+------------+
1 row in set (0.00 sec)

mysql> CREATE TABLE h(a DOUBLE, UNIQUE KEY USING BTREE (a)) ENGINE=MEMORY;
Query OK, 0 rows affected (0.01 sec)

mysql> INSERT INTO h VALUES(-0e0), (0e0);
ERROR 1062 (23000): Duplicate entry '0' for key 'a'

mysql> ALTER TABLE h DROP KEY a, ADD UNIQUE KEY USING HASH (a);
Query OK, 1 row affected (1.24 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql> INSERT INTO h VALUES(0e0);
Query OK, 1 row affected (1.45 sec)

mysql> SELECT * FROM h;
+------+
| a    |
+------+
|   -0 |
|    0 |
+------+
2 rows in set (0.00 sec)

As a result, when GROUP BY is executed using a HEAP temporary table,
spurious groups occur in the results:

mysql> CREATE TABLE t(a DOUBLE);
Query OK, 0 rows affected (0.05 sec)

mysql> INSERT INTO t VALUES(-0e0), (0e0);
Query OK, 2 rows affected (1.93 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t GROUP BY a;
+------+
| a    |
+------+
|   -0 |
|    0 |
+------+
2 rows in set (2.42 sec)

Compare this with the case when a MyISAM temporary table is used:

mysql> ALTER TABLE t ADD COLUMN b BLOB;
Query OK, 2 rows affected (1.79 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t GROUP BY a;
+------+------+
| a    | b    |
+------+------+
|   -0 | NULL |
+------+------+
1 row in set (1.12 sec)

How to repeat:
See the description.
[27 Dec 2012 14:49] Valeriy Kravchuk
This is really easy to verify:

mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.29    |
+-----------+
1 row in set (0.14 sec)

mysql> SELECT -0e0 = 0e0;
+------------+
| -0e0 = 0e0 |
+------------+
|          1 |
+------------+
1 row in set (0.08 sec)

mysql> CREATE TABLE h(a DOUBLE, UNIQUE KEY USING BTREE (a)) ENGINE=MEMORY;
Query OK, 0 rows affected (0.17 sec)

mysql> INSERT INTO h VALUES(-0e0), (0e0);
ERROR 1062 (23000): Duplicate entry '0' for key 'a'
mysql> ALTER TABLE h DROP KEY a, ADD UNIQUE KEY USING HASH (a);
Query OK, 1 row affected (0.09 sec)
Records: 1  Duplicates: 0  Warnings: 0

mysql>  INSERT INTO h VALUES(0e0);
Query OK, 1 row affected (0.00 sec)

mysql> select * from h;
+------+
| a    |
+------+
|   -0 |
|    0 |
+------+
2 rows in set (0.20 sec)

mysql> drop table t;
ERROR 1051 (42S02): Unknown table 't'
mysql> CREATE TABLE t(a DOUBLE);
Query OK, 0 rows affected (0.17 sec)

mysql> INSERT INTO t VALUES(-0e0), (0e0);
Query OK, 2 rows affected (0.06 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t GROUP BY a;
+------+
| a    |
+------+
|   -0 |
|    0 |
+------+
2 rows in set (0.08 sec)

mysql> ALTER TABLE t ADD COLUMN b BLOB;
Query OK, 2 rows affected (0.67 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM t GROUP BY a;
+------+------+
| a    | b    |
+------+------+
|   -0 | NULL |
+------+------+
1 row in set (0.16 sec)
[29 Dec 2012 16:21] Shane Bester
Verified as described.
[13 Oct 7:19] Knut Anders Hatlen
https://github.com/mysql/mysql-server/pull/788 seems to fix this bug.
[13 Oct 10:21] Knut Anders Hatlen
Bug#114218, Bug#115599, Bug#115654, Bug#117485, Bug#117648, Bug#117945, Bug#119520, Bug#119708, Bug#119741, Bug#120146, and Bug#120273 have been marked as duplicates of this bug report.

Bug#118749 was previously marked as a duplicate of Bug#115654, which is now a duplicate of this report.

Bug#44372 is related: it concerns signed-zero comparison in the generic handler::index_next_same() implementation. It remains a separate report, but its reported case is also fixed by https://github.com/mysql/mysql-server/pull/788.
[13 Oct 14:59] OCA Admin
Contribution submitted via Github - Bug#120146: Normalize signed zero in MEMORY hash keys 
(*) Contribution by PR specific confirm no longer needed

Contribution: git_patch_4660202178.txt (text/plain), 25.50 KiB.

[22 Oct 14:48] MySQL Admin
Posted by developer: Bug status updated to 'Patch pending'
[26 Oct 9:48] MySQL Admin
Posted by developer: Bug status updated to 'Patch approved'
[26 Oct 21:48] MySQL Admin
Posted by developer: Bug status updated to 'Documenting'

Portions of this website are copyright © 2001, 2002 The PHP Group

Page generated in 0.073 sec. using MySQL 8.4.7-u6-cloud

Timestamp references displayed by the system are UTC.
Content reproduced on this site is the property of the respective copyright holders.
It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.