From 2642df9fac09540c761441edd9bdd0a72c62f39c Mon Sep 17 00:00:00 2001 From: Dean Rasheed Date: Fri, 6 Aug 2021 21:29:15 +0100 Subject: Adjust the integer overflow tests in the numeric code. Formerly, the numeric code tested whether an integer value of a larger type would fit in a smaller type by casting it to the smaller type and then testing if the reverse conversion produced the original value. That's perfectly fine, except that it caused a test failure on buildfarm animal castoroides, most likely due to a compiler bug. Instead, do these tests by comparing against PG_INT16/32_MIN/MAX. That matches existing code in other places, such as int84(), which is more widely tested, and so is less likely to go wrong. While at it, add regression tests covering the numeric-to-int8/4/2 conversions, and adjust the recently added tests to the style of 434ddfb79a (on the v11 branch) to make failures easier to diagnose. Per buildfarm via Tom Lane, reviewed by Tom Lane. Discussion: https://postgr.es/m/2394813.1628179479%40sss.pgh.pa.us --- src/test/regress/sql/numeric.sql | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) (limited to 'src/test/regress/sql/numeric.sql') diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index e643752dd5..0418ff0524 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -773,6 +773,20 @@ INSERT INTO fract_only VALUES (11, '-Inf'); -- should fail SELECT * FROM fract_only; DROP TABLE fract_only; +-- Check conversion to integers +SELECT (-9223372036854775808.5)::int8; -- should fail +SELECT (-9223372036854775808.4)::int8; -- ok +SELECT 9223372036854775807.4::int8; -- ok +SELECT 9223372036854775807.5::int8; -- should fail +SELECT (-2147483648.5)::int4; -- should fail +SELECT (-2147483648.4)::int4; -- ok +SELECT 2147483647.4::int4; -- ok +SELECT 2147483647.5::int4; -- should fail +SELECT (-32768.5)::int2; -- should fail +SELECT (-32768.4)::int2; -- ok +SELECT 32767.4::int2; -- ok +SELECT 32767.5::int2; -- should fail + -- Check inf/nan conversion behavior SELECT 'NaN'::float8::numeric; SELECT 'Infinity'::float8::numeric; @@ -1133,12 +1147,12 @@ select 3.789 ^ 35; select 1.2 ^ 345; select 0.12 ^ (-20); select 1.000000000123 ^ (-2147483648); -select 0.9999999999 ^ 23300000000000 = 0 as rounds_to_zero; +select coalesce(nullif(0.9999999999 ^ 23300000000000, 0), 0) as rounds_to_zero; -- cases that used to error out select 0.12 ^ (-25); select 0.5678 ^ (-85); -select 0.9999999999 ^ 70000000000000 = 0 as underflows; +select coalesce(nullif(0.9999999999 ^ 70000000000000, 0), 0) as underflows; -- negative base to integer powers select (-1.0) ^ 2147483646; @@ -1188,8 +1202,8 @@ select exp(1.0::numeric(71,70)); select exp('nan'::numeric); select exp('inf'::numeric); select exp('-inf'::numeric); -select exp(-5000::numeric) = 0 as rounds_to_zero; -select exp(-10000::numeric) = 0 as underflows; +select coalesce(nullif(exp(-5000::numeric), 0), 0) as rounds_to_zero; +select coalesce(nullif(exp(-10000::numeric), 0), 0) as underflows; -- cases that used to generate inaccurate results select exp(32.999); -- cgit v1.2.1