From: Andrew Gierth <andrew@tao11.riddles.org.uk>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: zzzzz.graf@gmail.com
Cc: pgsql-bugs@lists.postgresql.org
Subject: Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
Date: Wed, 27 Feb 2019 01:56:29 +0000
Message-ID: <87a7iif1l4.fsf@news-spur.riddles.org.uk> (raw)
In-Reply-To: <15647.1551221786@sss.pgh.pa.us>
References: <15658-10b53866d3800951@postgresql.org>
<15647.1551221786@sss.pgh.pa.us>
>>>>> "Tom" == Tom Lane <tgl@sss.pgh.pa.us> 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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: andrew@tao11.riddles.org.uk, tgl@sss.pgh.pa.us, zzzzz.graf@gmail.com
Subject: Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
In-Reply-To: <87a7iif1l4.fsf@news-spur.riddles.org.uk>
References: <15658-10b53866d3800951@postgresql.org> <15647.1551221786@sss.pgh.pa.us> <87a7iif1l4.fsf@news-spur.riddles.org.uk>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox