• unknown's avatar
    Fix for bug #34512: CAST( AVG( double ) AS DECIMAL ) · 8270d987
    unknown authored
                        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.
    8270d987
func_group.result 46.1 KB