• Tom Lane's avatar
    Fix mishandling of NaN counts in numeric_[avg_]combine. · 77a3be32
    Tom Lane authored
    When merging two NumericAggStates, the code missed adding the new
    state's NaNcount unless its N was also nonzero; since those counts
    are independent, this is wrong.
    
    This would only have visible effect if some partial aggregate scans
    found only NaNs while earlier ones found only non-NaNs; then we could
    end up falsely deciding that there were no NaNs and fail to return a
    NaN final result as expected.  That's pretty improbable, so it's no
    surprise this hasn't been reported from the field.  Still, it's a bug.
    
    I didn't try to produce a regression test that would show the bug,
    but I did notice that these functions weren't being reached at all
    in our regression tests, so I improved the tests to at least
    exercise them.  With these additions, I see pretty complete code
    coverage on the aggregation-related functions in numeric.c.
    
    Back-patch to 9.6 where this code was introduced.  (I only added
    the improved test case as far back as v10, though, since the
    relevant part of aggregates.sql isn't there at all in 9.6.)
    77a3be32
numeric.c 247 KB