List:Commits« Previous MessageNext Message »
From:Alexey Kopytov Date:March 6 2008 3:19pm
Subject:bk commit into 5.0 tree (kaa:1.2599) BUG#34512
View as plain text  
Below is the list of changes that have just been committed into a local
5.0 repository of kaa.  When kaa does a push these changes
will be propagated to the main repository and, within 24 hours after the
push, to the public repository.
For information on how to access the public repository
see http://dev.mysql.com/doc/mysql/en/installing-source-tree.html

ChangeSet@stripped, 2008-03-06 18:19:47+03:00, kaa@kaamos.(none) +3 -0
  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@stripped, 2008-03-06 18:19:47+03:00, kaa@kaamos.(none) +6 -0
    Added a test case for bug #34512.

  mysql-test/t/func_group.test@stripped, 2008-03-06 18:19:47+03:00, kaa@kaamos.(none) +10 -0
    Added a test case for bug #34512.

  sql/item_sum.cc@stripped, 2008-03-06 18:19:47+03:00, kaa@kaamos.(none) +9 -1
    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.

diff -Nrup a/mysql-test/r/func_group.result b/mysql-test/r/func_group.result
--- a/mysql-test/r/func_group.result	2008-01-09 17:49:11 +03:00
+++ b/mysql-test/r/func_group.result	2008-03-06 18:19:47 +03:00
@@ -1419,4 +1419,10 @@ Note	1003	select (`test`.`t1`.`a` + 1) A
 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
diff -Nrup a/mysql-test/t/func_group.test b/mysql-test/t/func_group.test
--- a/mysql-test/t/func_group.test	2008-01-09 17:49:11 +03:00
+++ b/mysql-test/t/func_group.test	2008-03-06 18:19:47 +03:00
@@ -916,5 +916,15 @@ DROP VIEW v1;
 DROP TABLE t1;
 SET SQL_MODE=DEFAULT;
 
+#
+# Bug #34512: CAST( AVG( double ) AS DECIMAL ) returns wrong results
+#
+
+CREATE TABLE t1(a DOUBLE);
+INSERT INTO t1 VALUES (10), (20);
+SELECT AVG(a), CAST(AVG(a) AS DECIMAL) FROM t1;
+
+DROP TABLE t1;
+
 ###
 --echo End of 5.0 tests
diff -Nrup a/sql/item_sum.cc b/sql/item_sum.cc
--- a/sql/item_sum.cc	2008-02-28 14:31:18 +03:00
+++ b/sql/item_sum.cc	2008-03-06 18:19:47 +03:00
@@ -1206,7 +1206,15 @@ my_decimal *Item_sum_avg::val_decimal(my
     null_value=1;
     return NULL;
   }
-  sum_dec= Item_sum_sum::val_decimal(&sum_buff);
+
+  /*
+    For non-DECIMAL hybrid_type the division will be done in
+    Item_sum_avg::val_real().
+  */
+  if (hybrid_type != DECIMAL_RESULT)
+    return val_decimal_from_real(val);
+
+  sum_dec= dec_buffs + curr_dec_buff;
   int2my_decimal(E_DEC_FATAL_ERROR, count, 0, &cnt);
   my_decimal_div(E_DEC_FATAL_ERROR, val, sum_dec, &cnt, prec_increment);
   return val;
Thread
bk commit into 5.0 tree (kaa:1.2599) BUG#34512Alexey Kopytov6 Mar