Bug #94612 | Wrong results are returned with "group by" | ||
---|---|---|---|
Submitted: | 10 Mar 2019 2:18 | Modified: | 10 Mar 2019 5:27 |
Reporter: | Hope Lee (OCA) | Email Updates: | |
Status: | Verified | Impact on me: | |
Category: | MySQL Server: Optimizer | Severity: | S2 (Serious) |
Version: | 8.0.15-rds-dev, 8.0.15, 5.7.25,5.6.43 | OS: | CentOS |
Assigned to: | CPU Architecture: | Any |
[10 Mar 2019 2:18]
Hope Lee
[10 Mar 2019 5:27]
MySQL Verification Team
Hello Lee, Thank you for the report and test case. regards, Umesh
[13 Dec 2020 18:00]
Vladimir Menkov
This is still an issue in MySQL 5.7.32. Example: create table Foo (t datetime); insert into Foo values('2020-01-01 00:00:01'); insert into Foo values('2020-01-02 00:00:01'); insert into Foo values('2020-01-03 00:00:01'); select t, to_seconds(t) from Foo; +---------------------+---------------+ | t | to_seconds(t) | +---------------------+---------------+ | 2020-01-01 00:00:01 | 63745056001 | | 2020-01-02 00:00:01 | 63745142401 | | 2020-01-03 00:00:01 | 63745228801 | +---------------------+---------------+ So far so good. Now, use group by: select t, to_seconds(t) from Foo group by t; +---------------------+---------------+ | t | to_seconds(t) | +---------------------+---------------+ | 2020-01-01 00:00:01 | 2147483647 | | 2020-01-02 00:00:01 | 2147483647 | | 2020-01-03 00:00:01 | 2147483647 | +---------------------+---------------+ So the to_seconds(t) value for each row becomes 2147483647, which is equal to 2^31-1, i.e. the largest positive value representable by a signed 4-byte integer. So this looks like some kind of data conversion problem inside the MySQL server, in a situation where it ought not occur. Interestingly, the problem is obviated if instead of to_second(t) we use to_second(max(t)), or max(to_second(t)) (which, theoretically, should produce the same result, since we group on t anyway): select t, to_seconds(t), max(t), to_seconds(max(t)), max(to_seconds(t)) from Foo group by t; +---------------------+---------------+---------------------+--------------------+--------------------+ | t | to_seconds(t) | max(t) | to_seconds(max(t)) | max(to_seconds(t)) | +---------------------+---------------+---------------------+--------------------+--------------------+ | 2020-01-01 00:00:01 | 2147483647 | 2020-01-01 00:00:01 | 63745056001 | 63745056001 | | 2020-01-02 00:00:01 | 2147483647 | 2020-01-02 00:00:01 | 63745142401 | 63745142401 | | 2020-01-03 00:00:01 | 2147483647 | 2020-01-03 00:00:01 | 63745228801 | 63745228801 | +---------------------+---------------+---------------------+--------------------+-