mirror of
https://github.com/MariaDB/server.git
synced 2025-07-30 16:24:05 +03:00
Fix for bug #34512: CAST( AVG( double ) AS DECIMAL )
returns wrong results Casting AVG() to DECIMAL led to incorrect results when the arguments had a non-DECIMAL type, because in this case Item_sum_avg::val_decimal() performed the division by the number of arguments twice. Fixed by changing Item_sum_avg::val_decimal() to not rely on Item_sum_sum::val_decimal(), i.e. calculate sum and divide using DECIMAL arithmetics for DECIMAL arguments, and utilize val_real() with subsequent conversion to DECIMAL otherwise. mysql-test/r/func_group.result: Added a test case for bug #34512. mysql-test/t/func_group.test: Added a test case for bug #34512. sql/item_sum.cc: Do not use Item_sum_sum::val_decimal() in Item_sum_avg::val_decimal() because the first one, depending on the arguments type, may return either the sum of the arguments, or the average calculated by the virtual val_real() method of Item_sum_avg. Instead, do our own calculation based on the arguments type.
This commit is contained in:
@ -1419,4 +1419,10 @@ Note 1003 select (`test`.`t1`.`a` + 1) AS `y` from `test`.`t1` group by (`test`.
|
||||
DROP VIEW v1;
|
||||
DROP TABLE t1;
|
||||
SET SQL_MODE=DEFAULT;
|
||||
CREATE TABLE t1(a DOUBLE);
|
||||
INSERT INTO t1 VALUES (10), (20);
|
||||
SELECT AVG(a), CAST(AVG(a) AS DECIMAL) FROM t1;
|
||||
AVG(a) CAST(AVG(a) AS DECIMAL)
|
||||
15 15
|
||||
DROP TABLE t1;
|
||||
End of 5.0 tests
|
||||
|
Reference in New Issue
Block a user