integers is a table which holds integers from 0 and larger.
SELECT i, floor(rand()*4)+1 r
FROM integers
WHERE i<=10
this query produces two columns, i and r with (i.e.) next result:
+----+---+
| i | r |
+----+---+
| 0 | 3 |
| 1 | 1 |
| 2 | 1 |
| 3 | 3 |
| 4 | 4 |
| 5 | 4 |
| 6 | 1 |
| 7 | 1 |
| 8 | 4 |
| 9 | 2 |
| 10 | 2 |
+----+---+
11 rows in set (0.00 sec)
Now, when i do this:
SELECT r, count(*)
FROM (
SELECT i, floor(rand()*4)+1 r
FROM integers
WHERE i<=10) x
GROUP BY x.r;
I am getting:
ERROR 1022 (23000): Can't write; duplicate key in table 'C:\WINDOWS\SERVIC~1\NETWOR~1\AppData\Local\Temp\#sql484c_181_2c'
expected was:
| r | count(*) |
|---|---|
| 1 | 4 |
| 2 | 2 |
| 3 | 2 |
| 4 | 3 |
EDIT: Version of MySQL is 8.0.23
The table integers is defined as:
CREATE TABLE `integers` (
`i` int NOT NULL,
`t1` varchar(36) DEFAULT NULL,
PRIMARY KEY (`i`),
KEY `integers_t1` (`t1`),
KEY `even` (((`i` % 2)))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
There are 2621442 records in that table, and the minimum and maximum value for i are 0 and 2621450.
And finally diskspace:
| disk | free |
|---|---|
| C | 118Gb |
| D | 586Gb |
The datadir for MySQL is on the D-drive.
EDIT2:
When doing the same on MariaDB, version 10.5.9-MariaDB, also on Windows, there is no error, and the expected result is returned.
EDIT3:
A DBFIDDLE showing the problem.
EDIT4:
I did create a bug report, see: https://bugs.mysql.com/bug.php?id=103632
This problem was also reproducible on "Ubuntu 20.04.2 LTS"