Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyoSZ-0000tl-Ox for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Feb 2019 01:56:35 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gyoSY-0001dH-Ah for pgsql-bugs@arkaria.postgresql.org; Wed, 27 Feb 2019 01:56:34 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyoSY-0001dA-4N for pgsql-bugs@lists.postgresql.org; Wed, 27 Feb 2019 01:56:34 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gyoSV-00021P-Pu for pgsql-bugs@lists.postgresql.org; Wed, 27 Feb 2019 01:56:33 +0000 Received: from [192.168.127.1] (port=50383 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1gyoST-0006dI-UB; Wed, 27 Feb 2019 01:56:29 +0000 Received: from [127.0.0.1] (port=15710 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1gyoST-000Eo8-99; Wed, 27 Feb 2019 01:56:29 +0000 From: Andrew Gierth To: Tom Lane Cc: zzzzz.graf@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name In-Reply-To: <15647.1551221786@sss.pgh.pa.us> (Tom Lane's message of "Tue, 26 Feb 2019 17:56:26 -0500") Message-ID: <87a7iif1l4.fsf@news-spur.riddles.org.uk> References: <15658-10b53866d3800951@postgresql.org> <15647.1551221786@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Wed, 27 Feb 2019 01:56:29 +0000 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Tom" == Tom Lane writes: >> The error occurs when I put the query into a LEFT JOIN: >> select counts.count, >> caldetail.*, calprorules_desired_value, calprorules_stdpreceision, Tom> I'm wondering why this didn't already fail at "counts.count", Because counts.count resolves as count(counts), obviously. That makes the query an aggregation query with implied GROUP BY (), hence the error. Justin: the problem is nothing to do with the join, but it _is_ to do with the AS alias. For historical compatibility reasons, PostgreSQL tries to treat x.y and y(x) as though they were somewhat equivalent; so you can do (under some conditions) x.function or columnname(table). Needless to say actually _using_ this facility is a very bad idea. So in this example, if you have a column called "count", then counts.count resolves to that column. But if there's no column called "count", then counts.count is resolved as count(counts) (which works because count() is one of the few functions that can take any parameter type), and since count() is an aggregate function, that forces the query to behave as if there were an implied GROUP BY (), just as doing something like select count(*) from table; does. So this is not a bug, just a historical landmine. -- Andrew (irc:RhodiumToad)