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 1oqk50-0004f3-TH for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Nov 2022 23:57:02 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oqk4z-0002or-If for pgsql-bugs@arkaria.postgresql.org; Thu, 03 Nov 2022 23:57:01 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqk4z-0002oi-AH for pgsql-bugs@lists.postgresql.org; Thu, 03 Nov 2022 23:57:01 +0000 Received: from relay7-d.mail.gandi.net ([217.70.183.200]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oqk4x-0003gS-D9 for pgsql-bugs@postgresql.org; Thu, 03 Nov 2022 23:57:00 +0000 Received: (Authenticated sender: vik@postgresfriends.org) by mail.gandi.net (Postfix) with ESMTPSA id F250820002; Thu, 3 Nov 2022 23:56:56 +0000 (UTC) Message-ID: <309137b4-9b48-5952-c6e1-0fa7007d0419@postgresfriends.org> Date: Fri, 4 Nov 2022 00:56:56 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.2.2 Subject: Re: Wrong result with constant quals Content-Language: en-US To: Tom Lane Cc: pgsql-bugs References: <2193245.1667518580@sss.pgh.pa.us> From: Vik Fearing In-Reply-To: <2193245.1667518580@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/4/22 00:36, Tom Lane wrote: > Vik Fearing writes: >> The following query returns a wrong result, in my opinion. > >> postgres=# select 1 where false having true; >> ?column? >> ---------- >> 1 >> (1 row) > >> The correct result should be zero rows. > > No, I don't think so. The presence of HAVING without GROUP BY makes > this act like a query with an aggregate function and no GROUP BY: you > get a single grouped row, regardless of what the input is. There's a > reasonably clear specification of that in SQL92 7.8 : SQL92? wut? > 1) Let T be the result of the preceding , clause>, or . If that clause is not a by clause>, then T consists of a single group and does not have > a grouping column. > > "A single group" is not "no groups". > > Later SQL versions define this by reference to "GROUP BY ()", but > I think the effect is the same. I allowed for this by saying it could be a single group with no rows if you preferred to look at it that way. This does not explain why the WHERE FALSE is being ignored and producing rows. -- Vik Fearing