pg.ddx.io pgsql-bugs@postgresql.org mailing list archive
help / color / mirror / Atom feedBUG #15658: Window Function in a left join using AS or alias for the cloumn name
5+ messages / 4 participants
[nested] [flat]
* BUG #15658: Window Function in a left join using AS or alias for the cloumn name
@ 2019-02-26 18:55 PG Bug reporting form <noreply@postgresql.org>
2019-02-26 22:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Tom Lane <tgl@sss.pgh.pa.us>
0 siblings, 1 reply; 5+ messages in thread
From: PG Bug reporting form @ 2019-02-26 18:55 UTC (permalink / raw)
To: pgsql-bugs@lists.postgresql.org; +Cc: zzzzz.graf@gmail.com
The following bug has been logged on the website:
Bug reference: 15658
Logged by: Justin G
Email address: zzzzz.graf@gmail.com
PostgreSQL version: 11.2
Operating system: Debian 9
Description:
Hello postgresql developers
I believe I found an obscure bug with the window function
Postgresql version 11.2
OS Debian 9
PG Admin 4.1
Have 2 basic tables with a Parent child relationship, a One to Many
relationship. The child table also makes reference to a 3rd table which can
have a many to many relationship. This query returns count(*) of the 3rd
table’s keys in the child table grouped by Parent Table ID.
The base SQL statement works:
select calprorules_id cid , count(*) over
(PARTITION BY calprorules_calprorange_id, calprorules_calprohd_id ) as
howmany
from mcal.calprorules
The error occurs when I put the query into a LEFT JOIN:
select counts.count,
caldetail.*, calprorules_desired_value, calprorules_stdpreceision,
calprorules_mutpreceision ,
calprorange_description, calprorange_id, calprorange_from, calprorange_to
from mcal.caldetail
left join mcal.calprorules on calprorules_id = caldetail_calprorules_id
left join (select calprorules_id as cid , count(*) over
(PARTITION BY calprorules_calprorange_id, calprorules_calprohd_id ) as
howmany
from mcal.calprorules) counts
on counts.cid = caldetail_calprorules_id
left join mcal.calprorange on calprorange_id = calprorules_calprorange_id
where caldetail_calhead_id = 179
order by calprorange_description, caldetails_seqence
Postgresql returns
ERROR: column "caldetail.caldetail_id" must appear in the GROUP BY clause or
be used in an aggregate function LINE 2: caldetail.*,
calprorules_desired_value, calprorules_stdprec...
SQL state: 42803 Character: 24
Delete the AS howmany; it works.
I realized after writing this that I should have written the SQL like this,
deleting the LEFT JOIN and moving the window function into the select
statement:
select caldetail.*, calprorules_desired_value, calprorules_stdpreceision,
calprorules_mutpreceision ,
calprorange_description, calprorange_id, calprorange_from, calprorange_to
,
count(*) over (PARTITION BY calprorules_calprorange_id,
calprorules_calprohd_id )
from mcal.caldetail
left join mcal.calprorules on calprorules_id =
caldetail_calprorules_id
left join mcal.calprorange on calprorange_id =
calprorules_calprorange_id
where caldetail_calhead_id = 179
order by calprorange_description, caldetails_seqence
Clearly the second SQL statement is better, but i do not believe the window
function should error when put into a join
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
2019-02-26 18:55 BUG #15658: Window Function in a left join using AS or alias for the cloumn name PG Bug reporting form <noreply@postgresql.org>
@ 2019-02-26 22:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-02-27 01:15 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Justin <zzzzz.graf@gmail.com>
2019-02-27 01:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Andrew Gierth <andrew@tao11.riddles.org.uk>
0 siblings, 2 replies; 5+ messages in thread
From: Tom Lane @ 2019-02-26 22:56 UTC (permalink / raw)
To: zzzzz.graf@gmail.com; +Cc: pgsql-bugs@lists.postgresql.org
PG Bug reporting form <noreply@postgresql.org> writes:
> I believe I found an obscure bug with the window function
Hm, could we see a self-contained example, ie with the underlying
table/view definitions too? Probably don't need any sample data
for this type of problem, but I'm unexcited about trying to reverse-
engineer your table definitions enough to reproduce the problem.
> The error occurs when I put the query into a LEFT JOIN:
> select counts.count,
> caldetail.*, calprorules_desired_value, calprorules_stdpreceision,
I'm wondering why this didn't already fail at "counts.count", because with
the "AS howmany" in place, that sub-select isn't returning any column
named "count". I kind of suspect user error due to ambiguous column
names, but the details aren't obvious.
regards, tom lane
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
2019-02-26 18:55 BUG #15658: Window Function in a left join using AS or alias for the cloumn name PG Bug reporting form <noreply@postgresql.org>
2019-02-26 22:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-02-27 01:15 ` Justin <zzzzz.graf@gmail.com>
1 sibling, 0 replies; 5+ messages in thread
From: Justin @ 2019-02-27 01:15 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs@lists.postgresql.org
Hello Tom
attached is the SQL file for the 4 tables. if you need data not a problem
the data in the table is junk/test entries
also attached is the screen shot of the error in pgadmin then the same
command run without the AS
[image: image.png]
[image: image.png]
On Tue, Feb 26, 2019 at 5:56 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
> PG Bug reporting form <noreply@postgresql.org> writes:
> > I believe I found an obscure bug with the window function
>
> Hm, could we see a self-contained example, ie with the underlying
> table/view definitions too? Probably don't need any sample data
> for this type of problem, but I'm unexcited about trying to reverse-
> engineer your table definitions enough to reproduce the problem.
>
> > The error occurs when I put the query into a LEFT JOIN:
>
> > select counts.count,
> > caldetail.*, calprorules_desired_value, calprorules_stdpreceision,
>
> I'm wondering why this didn't already fail at "counts.count", because with
> the "AS howmany" in place, that sub-select isn't returning any column
> named "count". I kind of suspect user error due to ambiguous column
> names, but the details aren't obvious.
>
> regards, tom lane
>
Attachments:
image.png (image/png, 68.9K) download
view image
image.png (image/png, 80.0K) download
view image
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
2019-02-26 18:55 BUG #15658: Window Function in a left join using AS or alias for the cloumn name PG Bug reporting form <noreply@postgresql.org>
2019-02-26 22:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Tom Lane <tgl@sss.pgh.pa.us>
@ 2019-02-27 01:56 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-02-27 02:15 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Justin <zzzzz.graf@gmail.com>
1 sibling, 1 reply; 5+ messages in thread
From: Andrew Gierth @ 2019-02-27 01:56 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: zzzzz.graf@gmail.com; pgsql-bugs@lists.postgresql.org
>>>>> "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)
^ permalink raw reply [nested|flat] 5+ messages in thread
* Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name
2019-02-26 18:55 BUG #15658: Window Function in a left join using AS or alias for the cloumn name PG Bug reporting form <noreply@postgresql.org>
2019-02-26 22:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Tom Lane <tgl@sss.pgh.pa.us>
2019-02-27 01:56 ` Re: BUG #15658: Window Function in a left join using AS or alias for the cloumn name Andrew Gierth <andrew@tao11.riddles.org.uk>
@ 2019-02-27 02:15 ` Justin <zzzzz.graf@gmail.com>
0 siblings, 0 replies; 5+ messages in thread
From: Justin @ 2019-02-27 02:15 UTC (permalink / raw)
To: Andrew Gierth <andrew@tao11.riddles.org.uk>; +Cc: Tom Lane <tgl@sss.pgh.pa.us>; pgsql-bugs@lists.postgresql.org
Hi Andrew
I think I understand what your stating. I realized after re-reading Tom's
responds and playing with the SQL statement i see my mistake.
I just did not catch it when i was writing the SQL statement.
I figured out a work around pretty quickly and later realized i was being
an idiot putting the window function in a JOIN for this query
Then i wrote the bug report.. Thank you
On Tue, Feb 26, 2019 at 8:56 PM Andrew Gierth <andrew@tao11.riddles.org.uk>
wrote:
> >>>>> "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)
>
^ permalink raw reply [nested|flat] 5+ messages in thread
end of thread, other threads:[~2019-02-27 02:15 UTC | newest]
Thread overview: 5+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2019-02-26 18:55 BUG #15658: Window Function in a left join using AS or alias for the cloumn name PG Bug reporting form <noreply@postgresql.org>
2019-02-26 22:56 ` Tom Lane <tgl@sss.pgh.pa.us>
2019-02-27 01:15 ` Justin <zzzzz.graf@gmail.com>
2019-02-27 01:56 ` Andrew Gierth <andrew@tao11.riddles.org.uk>
2019-02-27 02:15 ` Justin <zzzzz.graf@gmail.com>
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