[MDEV-8918] Wrong result for CAST(AVG(bigint_column) AS SIGNED) Created: 2015-10-08  Updated: 2015-10-22  Resolved: 2015-10-08

Status: Closed
Project: MariaDB Server
Component/s: OTHER
Affects Version/s: 10.0, 10.1
Fix Version/s: 10.1.8

Type: Bug Priority: Major
Reporter: Alexander Barkov Assignee: Alexander Barkov
Resolution: Fixed Votes: 0
Labels: None

Issue Links:
Relates
relates to MDEV-8921 Wrong result for CAST(AVG(double_colu... Open

 Description   

DROP TABLE IF EXISTS t1;
CREATE TABLE t1 (id INT, a BIGINT);
INSERT INTO t1 VALUES (1,0x7FFFFFFFFFFFFFFF),(2,0x7FFFFFFFFFFFFFFF);
SELECT id, AVG(a) AS avg, CAST(MIN(a) AS SIGNED) AS cast_min FROM t1 GROUP BY id HAVING avg!=123 ORDER BY id;

returns

+------+--------------------------+---------------------+
| id   | avg                      | cast_min            |
+------+--------------------------+---------------------+
|    1 | 9223372036854775807.0000 | 9223372036854775807 |
|    2 | 9223372036854775807.0000 | 9223372036854775807 |
+------+--------------------------+---------------------+

So far so good. Now if I change MIN to AVG:

SELECT id, AVG(a) AS avg, CAST(AVG(a) AS SIGNED) AS cast_avg FROM t1 GROUP BY id HAVING avg!=123 ORDER BY id;

it erroneously returns negative numbers:

+------+--------------------------+----------------------+
| id   | avg                      | cast_avg             |
+------+--------------------------+----------------------+
|    1 | 9223372036854775807.0000 | -9223372036854775808 |
|    2 | 9223372036854775807.0000 | -9223372036854775808 |
+------+--------------------------+----------------------+


Generated at Thu Feb 08 07:30:46 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.