Bug #90593 count(*) overflows and returns bogus
Submitted: 24 Apr 2018 7:23 Modified: 24 Apr 2018 7:49
Reporter: Shane Bester (Platinum Quality Contributor) Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server: DML Severity:S4 (Feature request)
Version:8.0.11 OS:Any (x64)
Assigned to: CPU Architecture:Any

[24 Apr 2018 7:23] Shane Bester
Description:
Doesn't make sense that count(*) returns a negative number.

mysql> select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s;
+----------------------+
| count(*)             |
+----------------------+
| -8446744073709551616 |
+----------------------+
1 row in set (0.00 sec)

How to repeat:
drop table if exists t;
create table t(a bigint unsigned not null auto_increment primary key)engine=innodb row_format=compressed key_block_size=2;
insert into t values (),(),(),(),(),(),(),(),(),();
select count(*) from t a,t b,t c,t d;
select count(*) from t a,t b,t c,t d,t e;
select count(*) from t a,t b,t c,t d,t e,t f;
select count(*) from t a,t b,t c,t d,t e,t f,t g;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t,t u;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t,t u,t v;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t,t u,t v,t x;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t,t u,t v,t x,t y;
select count(*) from t a,t b,t c,t d,t e,t f,t g,t h,t i,t j,t k,t l,t m,t n,t o,t p,t q,t r,t s,t t,t u,t v,t x,t y,t z;

Suggested fix:
don't overflow. 
use some big number math library?
[24 Apr 2018 7:58] Erlend Dahl
Posted by developer:
 
Tried to run the testcase on 5.7, but it takes forever :(
[30 Apr 2018 3:52] Sreeharsha Ramanavarapu
Posted by developer:
 
IMHO this isn't a bug. Current we use a long long variable to keep track of the count and project the result. So at some point the max value is hit and an overflow happens. 

This more of a limitation than bug.
[30 Apr 2018 4:57] MySQL Verification Team
changed to a FR.  please don't return incorrect results without warnings.