Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jw95K-0008RL-QD for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Jul 2020 18:58:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jw95G-0000uY-U1 for pgsql-hackers@arkaria.postgresql.org; Thu, 16 Jul 2020 18:58:18 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jw95G-0000uR-In for pgsql-hackers@lists.postgresql.org; Thu, 16 Jul 2020 18:58:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jw95D-0001lw-UA for pgsql-hackers@lists.postgresql.org; Thu, 16 Jul 2020 18:58:17 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 06GIwDs33393131; Thu, 16 Jul 2020 14:58:14 -0400 From: Tom Lane To: pgsql-hackers@lists.postgresql.org cc: Dean Rasheed Subject: Wrong results from in_range() tests with infinite offset MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3393086.1594925837.0@sss.pgh.pa.us> Date: Thu, 16 Jul 2020 14:58:13 -0400 Message-ID: <3393130.1594925893@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3393086.1594925837.1@sss.pgh.pa.us> Dean Rasheed pointed out that in_range for float4/float8 seems to be doing the wrong thing for infinite offsets, and after some testing I concur that it is. For example, a sort key of '-infinity' should be considered to be in-range for a range specified as RANGE BETWEEN 'inf' PRECEDING AND 'inf' PRECEDING; but with the code as it stands, it isn't. I propose the attached patch, which probably should be back-patched. When the current row's value is +infinity, actual computation of base - offset would yield NaN, making it a bit unclear whether we should consider -infinity to be in-range. It seems to me that we should, as that gives more natural-looking results in the test cases, so that's how the patch does it. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="fix-infinite-in_range-cases.patch"; charset="us-ascii" Content-ID: <3393086.1594925837.2@sss.pgh.pa.us> Content-Description: fix-infinite-in_range-cases.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c index 50ec3d3dde..5c722d85c3 100644 --- a/src/backend/utils/adt/float.c +++ b/src/backend/utils/adt/float.c @@ -1090,11 +1090,18 @@ in_range_float8_float8(PG_FUNCTION_ARGS) /* * Deal with infinite offset (necessarily +inf, at this point). We must * special-case this because if base happens to be -inf, their sum would - * be NaN, which is an overflow-ish condition we should avoid. + * be NaN, which is an overflow-ish condition we must avoid. We take th= at + * sum to be +inf; that is, we consider base + inf to be +inf for any + * non-NaN base. Conversely, base - inf is taken to be -inf even if bas= e + * is +inf. These choices may seem arbitrary, but they give desirable + * windowing behavior with an infinite offset. */ if (isinf(offset)) { - PG_RETURN_BOOL(sub ? !less : less); + if (sub) + sum =3D -get_float8_infinity(); + else + sum =3D get_float8_infinity(); } = /* @@ -1103,7 +1110,7 @@ in_range_float8_float8(PG_FUNCTION_ARGS) * produce a suitably signed infinity, which will compare properly again= st * val whether or not that's infinity. */ - if (sub) + else if (sub) sum =3D base - offset; else sum =3D base + offset; @@ -1159,11 +1166,18 @@ in_range_float4_float8(PG_FUNCTION_ARGS) /* * Deal with infinite offset (necessarily +inf, at this point). We must * special-case this because if base happens to be -inf, their sum would - * be NaN, which is an overflow-ish condition we should avoid. + * be NaN, which is an overflow-ish condition we must avoid. We take th= at + * sum to be +inf; that is, we consider base + inf to be +inf for any + * non-NaN base. Conversely, base - inf is taken to be -inf even if bas= e + * is +inf. These choices may seem arbitrary, but they give desirable + * windowing behavior with an infinite offset. */ if (isinf(offset)) { - PG_RETURN_BOOL(sub ? !less : less); + if (sub) + sum =3D -get_float8_infinity(); + else + sum =3D get_float8_infinity(); } = /* @@ -1172,7 +1186,7 @@ in_range_float4_float8(PG_FUNCTION_ARGS) * produce a suitably signed infinity, which will compare properly again= st * val whether or not that's infinity. */ - if (sub) + else if (sub) sum =3D base - offset; else sum =3D base + offset; diff --git a/src/test/regress/expected/window.out b/src/test/regress/expec= ted/window.out index d5fd4045f9..4cb68ca42f 100644 --- a/src/test/regress/expected/window.out +++ b/src/test/regress/expected/window.out @@ -1936,6 +1936,42 @@ window w as (order by f_float4 range between 9 | NaN | 9 | 9 (10 rows) = +select id, f_float4, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float4 range between + 'inf' preceding and 'inf' preceding); + id | f_float4 | first_value | last_value = +----+-----------+-------------+------------ + 0 | -Infinity | 0 | 0 + 1 | -3 | 0 | 0 + 2 | -1 | 0 | 0 + 3 | 0 | 0 | 0 + 4 | 1.1 | 0 | 0 + 5 | 1.12 | 0 | 0 + 6 | 2 | 0 | 0 + 7 | 100 | 0 | 0 + 8 | Infinity | 0 | 0 + 9 | NaN | 9 | 9 +(10 rows) + +select id, f_float4, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float4 range between + 'inf' following and 'inf' following); + id | f_float4 | first_value | last_value = +----+-----------+-------------+------------ + 0 | -Infinity | 8 | 8 + 1 | -3 | 8 | 8 + 2 | -1 | 8 | 8 + 3 | 0 | 8 | 8 + 4 | 1.1 | 8 | 8 + 5 | 1.12 | 8 | 8 + 6 | 2 | 8 | 8 + 7 | 100 | 8 | 8 + 8 | Infinity | 8 | 8 + 9 | NaN | 9 | 9 +(10 rows) + select id, f_float4, first_value(id) over w, last_value(id) over w from numerics window w as (order by f_float4 range between @@ -1995,6 +2031,42 @@ window w as (order by f_float8 range between 9 | NaN | 9 | 9 (10 rows) = +select id, f_float8, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float8 range between + 'inf' preceding and 'inf' preceding); + id | f_float8 | first_value | last_value = +----+-----------+-------------+------------ + 0 | -Infinity | 0 | 0 + 1 | -3 | 0 | 0 + 2 | -1 | 0 | 0 + 3 | 0 | 0 | 0 + 4 | 1.1 | 0 | 0 + 5 | 1.12 | 0 | 0 + 6 | 2 | 0 | 0 + 7 | 100 | 0 | 0 + 8 | Infinity | 0 | 0 + 9 | NaN | 9 | 9 +(10 rows) + +select id, f_float8, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float8 range between + 'inf' following and 'inf' following); + id | f_float8 | first_value | last_value = +----+-----------+-------------+------------ + 0 | -Infinity | 8 | 8 + 1 | -3 | 8 | 8 + 2 | -1 | 8 | 8 + 3 | 0 | 8 | 8 + 4 | 1.1 | 8 | 8 + 5 | 1.12 | 8 | 8 + 6 | 2 | 8 | 8 + 7 | 100 | 8 | 8 + 8 | Infinity | 8 | 8 + 9 | NaN | 9 | 9 +(10 rows) + select id, f_float8, first_value(id) over w, last_value(id) over w from numerics window w as (order by f_float8 range between diff --git a/src/test/regress/sql/window.sql b/src/test/regress/sql/window= .sql index fe273aa31e..51ec0bac9a 100644 --- a/src/test/regress/sql/window.sql +++ b/src/test/regress/sql/window.sql @@ -524,6 +524,14 @@ window w as (order by f_float4 range between 'inf' preceding and 'inf' following); select id, f_float4, first_value(id) over w, last_value(id) over w from numerics +window w as (order by f_float4 range between + 'inf' preceding and 'inf' preceding); +select id, f_float4, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float4 range between + 'inf' following and 'inf' following); +select id, f_float4, first_value(id) over w, last_value(id) over w +from numerics window w as (order by f_float4 range between 1.1 preceding and 'NaN' following); -- error, NaN disallowe= d = @@ -541,6 +549,14 @@ window w as (order by f_float8 range between 'inf' preceding and 'inf' following); select id, f_float8, first_value(id) over w, last_value(id) over w from numerics +window w as (order by f_float8 range between + 'inf' preceding and 'inf' preceding); +select id, f_float8, first_value(id) over w, last_value(id) over w +from numerics +window w as (order by f_float8 range between + 'inf' following and 'inf' following); +select id, f_float8, first_value(id) over w, last_value(id) over w +from numerics window w as (order by f_float8 range between 1.1 preceding and 'NaN' following); -- error, NaN disallowe= d = ------- =_aaaaaaaaaa0--