Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQbf0-00CXCm-1d for pgsql-bugs@arkaria.postgresql.org; Wed, 03 Dec 2025 01:28:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.96) (envelope-from ) id 1vQbez-00ArYk-0G for pgsql-bugs@arkaria.postgresql.org; Wed, 03 Dec 2025 01:28:01 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQbey-00ArYc-2d for pgsql-bugs@lists.postgresql.org; Wed, 03 Dec 2025 01:28:01 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.96) (envelope-from ) id 1vQbew-002qM4-1d for pgsql-bugs@lists.postgresql.org; Wed, 03 Dec 2025 01:28:00 +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 5B31RuIP545891; Tue, 2 Dec 2025 20:27:56 -0500 From: Tom Lane To: Dean Rasheed cc: Oleg Ivanov , Laurenz Albe , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #19340: Wrong result from CORR() function In-reply-to: <531516.1764721052@sss.pgh.pa.us> References: <19340-6fb9f6637f562092@postgresql.org> <4ab9867066e9545dd1a7e835a480bb0ecbe1a00d.camel@cybertec.at> <375068.1764696127@sss.pgh.pa.us> <434484.1764707203@sss.pgh.pa.us> <513345.1764717860@sss.pgh.pa.us> <531516.1764721052@sss.pgh.pa.us> Comments: In-reply-to Tom Lane message dated "Tue, 02 Dec 2025 19:17:32 -0500" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <545889.1764725276.1@sss.pgh.pa.us> Date: Tue, 02 Dec 2025 20:27:56 -0500 Message-ID: <545890.1764725276@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk I wrote: > I'm coming around to the conclusion that your way is better, > though. It seems good that "any NaN in the input results in > NaN output", which your way does and mine doesn't. Poking further at this, I found that my v2 patch fails that principle in one case: regression=# SELECT corr( 0.1 , 'nan' ) FROM generate_series(1,1000) g; corr ------ (1 row) We see that Y is constant and therefore return NULL, despite the other NaN input. I think we can fix that along these lines: @@ -3776,8 +3776,12 @@ float8_corr(PG_FUNCTION_ARGS) if (N < 1.0) PG_RETURN_NULL(); - /* per spec, return NULL for horizontal and vertical lines */ - if (!isnan(commonX) || !isnan(commonY)) + /* + * per spec, return NULL for horizontal and vertical lines; but not if the + * result would otherwise be NaN + */ + if ((!isnan(commonX) || !isnan(commonY)) && + (!isnan(Sxx) && !isnan(Syy))) PG_RETURN_NULL(); /* at this point, Sxx and Syy cannot be zero or negative */ (don't think it should be necessary to also check Sxy) BTW, HEAD is inconsistent: it will return NaN for this example, but only because it's confused by roundoff error into thinking that Y isn't constant. With few enough inputs, it produces NULL too: regression=# SELECT corr( 0.1 , 'nan' ) FROM generate_series(1,3) g; corr ------ (1 row) regards, tom lane