pingcap / tidb

TiDB - the open-source, cloud-native, distributed SQL database designed for modern applications.
https://pingcap.com
Apache License 2.0
37.26k stars 5.85k forks source link

All aggregate functions do not check for overflow, allowing production of Infinity #28489

Open kennytm opened 3 years ago

kennytm commented 3 years ago

Bug Report

Please answer these questions before submitting your issue. Thanks!

1. Minimal reproduce step (Required)

select sum(a) from (select 1e308 a union all select 1e308) _;
select avg(a) from (select 1e308 a union all select 1e308) _;
select var_pop(a) from (select 1e200 a union select -1e200) _;
select var_samp(a) from (select 1e200 a union select -1e200) _;
select stddev_pop(a) from (select 1e200 a union select -1e200) _;
select stddev_samp(a) from (select 1e200 a union select -1e200) _;

2. What did you expect to see? (Required)

some DOUBLE value is out of range error

3. What did you see instead (Required)

mysql> select sum(a) from (select 1e308 a union all select 1e308) _;
+--------+
| sum(a) |
+--------+
|      0 |
+--------+
1 row in set (0.29 sec)

Note that the result is not a zero, but an actual Infinity.

mysql> select sum(a) - 0 from (select 1e308 a union all select 1e308) _;
ERROR 1690 (22003): DOUBLE value is out of range in '(Column#4 - 0)'

MySQL 8.0 has got the same problem 🙃.

mysql> select sum(a) from (select 1e308 a union all select 1e308) _;
+--------+
| sum(a) |
+--------+
|      0 |
+--------+
1 row in set (0.00 sec)

mysql> select sum(a) - 0 from (select 1e308 a union all select 1e308) _;
ERROR 1690 (22003): DOUBLE value is out of range in '(sum(`_`.`a`) - 0)'

4. What is your TiDB version? (Required)

Release Version: v5.2.0
Edition: Community
Git Commit Hash: 05d2210647d6a1503a8d772477e43b14a024f609
Git Branch: heads/refs/tags/v5.2.0
UTC Build Time: 2021-08-27 05:57:10
GoVersion: go1.16.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false
kennytm commented 3 years ago
RDBMS Result
SQLite3 Infinity
MariaDB 0 (but is actually infinity)
PostgreSQL 22003 value out of range: overflow
MS SQL Arithmetic overflow error converting expression to data type float.
Oracle Inf (for BINARY_DOUBLE type)