Bug #93254 | MAX treating Numbers as Strings when used in group by and JSON_UNQUOTE | ||
---|---|---|---|
Submitted: | 20 Nov 2018 3:33 | Modified: | 17 Apr 2019 22:22 |
Reporter: | Stanley Leong | Email Updates: | |
Status: | Closed | Impact on me: | |
Category: | MySQL Server: JSON | Severity: | S3 (Non-critical) |
Version: | 5.7.24, 8.0.13 | OS: | Ubuntu |
Assigned to: | CPU Architecture: | Other (x64) | |
Tags: | GROUP BY, INTEGER, JSON_QUOTE, MAX, number |
[20 Nov 2018 3:33]
Stanley Leong
[20 Nov 2018 3:47]
Stanley Leong
To prove that MySQL behaviour is inconsistent, mysql> INSERT INTO `billing`.`test` (`id`, `data`) VALUES ('5', '{\"group\": \"b\", \"number\": 9}'); Query OK, 1 row affected (0.02 sec) mysql> select * from test; +----+------------------------------+ | id | data | +----+------------------------------+ | 1 | {"group": "a", "number": 5} | | 2 | {"group": "a", "number": 10} | | 3 | {"group": "b", "number": 50} | | 4 | {"group": "b", "number": 1} | | 5 | {"group": "b", "number": 9} | +----+------------------------------+ 5 rows in set (0.00 sec) mysql> SELECT max(data->'$.number') FROM test; +-----------------------+ | max(data->'$.number') | +-----------------------+ | 50 | +-----------------------+ 1 row in set (0.00 sec) Without group by, the aggregator is performing in number's lexicological order correctly.
[20 Nov 2018 6:18]
MySQL Verification Team
Hello Stanley, Thank you for the report and test case. regards, Umesh
[4 Jan 2019 12:29]
Knut Anders Hatlen
In the cases where the aggregation is performed using filesort on disk-based temporary tables, such as when SQL_BIG_RESULT or WITH ROLLUP is specified, it seems to be able to compute the maximum value for group "a" correctly: mysql> SELECT SQL_BIG_RESULT data->>'$.group', max(data->'$.number') FROM test group by data->>'$.group'; +------------------+-----------------------+ | data->>'$.group' | max(data->'$.number') | +------------------+-----------------------+ | a | 10 | | b | 50 | +------------------+-----------------------+ 2 rows in set (0,00 sec) mysql> SELECT data->>'$.group', max(data->'$.number') FROM test group by data->>'$.group' WITH ROLLUP; +------------------+-----------------------+ | data->>'$.group' | max(data->'$.number') | +------------------+-----------------------+ | a | 10 | | b | 50 | | NULL | 50 | +------------------+-----------------------+ 3 rows in set (0,00 sec)
[17 Apr 2019 22:22]
Jon Stephens
Documented fix in the MySQL 8.0.17 changelog as follows: MAX() and MIN() used on expressions returning JSON sometimes compared these values as strings rather than JSON values, which caused unexpected results; this was especially evident when the JSON values were numbers. This was due to the fact that GROUP BY when using temporary tables with indexes did not compare JSON values correctly. Closed.