Bug #97920 Aggregation function [sum()] return random numbers
Submitted: 8 Dec 2019 20:41 Modified: 4 Feb 11:31
Reporter: Wolf Heidler Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Optimizer Severity:S2 (Serious)
Version:8.0.18 OS:Any
Assigned to: CPU Architecture:Any
Tags: regression

[8 Dec 2019 20:41] Wolf Heidler
Description:
After an upgrade from MySQL Community Server 8.0.17 to 8.0.18 the aggregation function "SUM(xx)" together with a "GROUP BY" (I guess as well other functions) returns in some cases random numbers.

All tests (final exemple provided here) were performed on Windows 10 (1909 Build 18363.476).
Some strange numbers was seen as well on Windows Server 2016 Standard and Debian 9 (x64) but the systems was not used for further testing.

How to repeat:
CREATE TABLE t_test (
  id int(10) unsigned NOT NULL AUTO_INCREMENT,
  ym varchar(12) CHARACTER SET utf8 COLLATE utf8_general_ci DEFAULT NULL,
  vb double DEFAULT NULL,
  vc double DEFAULT NULL,
  PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

INSERT INTO t_test VALUES ('543', '201912', '235.0431370770208', null);
INSERT INTO t_test VALUES ('573', '201912', '76.5', '68.85000000000001');
INSERT INTO t_test VALUES ('582', '201912', '340', null);
INSERT INTO t_test VALUES ('608', '201912', '54.42', '48.978');
INSERT INTO t_test VALUES ('752', '201912', '140', null);
INSERT INTO t_test VALUES ('1205', '201912', '121.5', '109.35000000000001');
INSERT INTO t_test VALUES ('2988', '201912', null, null);
INSERT INTO t_test VALUES ('3168', '201912', null, null);
INSERT INTO t_test VALUES ('21299', '201912', '179.17000000000002', null);
INSERT INTO t_test VALUES ('66901', '201912', '21528.03', null);

Run a couple of times the select statement:
SELECT ym, ROUND(SUM(vb)), ROUND(SUM(vc)) FROM t_test GROUP BY ym;

Suggested fix:
A downgrade to version 8.0.17 has solved the problem for my use case.

As well it is possible to change the select statement to the following:
SELECT CAST(ym AS UNSIGNED), ROUND(SUM(vb)), ROUND(SUM(vc)) FROM t_test GROUP BY CAST(ym AS UNSIGNED);
[9 Dec 2019 3:57] MySQL Verification Team
Hello Wolf Heidler,

Thank you for the report and test case.
Observed that 8.0.18 is affected.

regards,
Umesh
[4 Feb 11:31] Erlend Dahl
Fixed in 8.0.20 under the heading of

Bug#30677984 UNINITIALISED VALUE ERROR WITH VALGRIND IN SQL_TMP_TABLE.CC
[6 Mar 9:27] Erlend Dahl
Bug#98825 sum() function result may be error in select query

was marked as a duplicate