pg.ddx.io pgsql-general@postgresql.org mailing list archive
help / color / mirror / Atom feedWhy is it that “aggregate functions are not allowed in FROM clause of their own query level”?
3+ messages / 3 participants
[nested] [flat]
* Why is it that “aggregate functions are not allowed in FROM clause of their own query level”?
@ 2020-05-13 11:02 Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
0 siblings, 2 replies; 3+ messages in thread
From: Tom Ellis @ 2020-05-13 11:02 UTC (permalink / raw)
To: pgsql-general@lists.postgresql.org
Hello,
The code under 1 gives me the error message "aggregate functions are
not allowed in FROM clause of their own query level" whereas the code
under 2 is permitted. Unless I am much mistaken the latter is
equivalent to the former because it just makes a new "local" name for
`v`.
I'm writing a code generator and rewriting the former, natural form,
to the latter, artificial form, is somewhat fiddly, but ultimately
possible, I believe.
I have a few questions:
A. Am I right in thinking that the two forms are equivalent?
A1. And am I right to presume that it's always possibly to rewrite
more complicated examples that yield the same error to valid versions,
just by coming up with a local name for the problematic fields?
B. If they are indeed equivalent, what is the rationale for forbidding
the former? It seems like it would be more convenient to allow users
to write the former form.
1. Causes error:
SELECT * FROM
((SELECT 0 as "v") as "T1"
INNER JOIN LATERAL
(SELECT
SUM("v") as "r"
FROM (SELECT 0) as "T1") as "T2"
ON TRUE) as "T1"
2. Runs successfully
SELECT * FROM
((SELECT 0 as "v") as "T1"
INNER JOIN LATERAL
(SELECT
SUM("v_again") as "r"
FROM (SELECT "v" as "v_again") as "T1") as "T2"
ON TRUE) as "T1"
By the way, the only reference to this issue that I can find on the
entire internet is the following old mailing list post:
https://www.postgresql.org/message-id/1375925710.17807.13.camel%40vanquo.pezone.net
I also posted on DBA.StackExchange
https://dba.stackexchange.com/questions/266988/why-is-it-that-aggregate-functions-are-not-allowed-in...
Thanks,
Tom
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Why is it that “aggregate functions are not allowed in FROM clause of their own query level”?
@ 2020-05-13 13:43 David G. Johnston <david.g.johnston@gmail.com>
parent: Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
1 sibling, 0 replies; 3+ messages in thread
From: David G. Johnston @ 2020-05-13 13:43 UTC (permalink / raw)
To: Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>; +Cc: pgsql-general@lists.postgresql.org <pgsql-general@lists.postgresql.org>
On Wednesday, May 13, 2020, Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
wrote:
> Hello,
>
> The code under 1 gives me the error message "aggregate functions are
> not allowed in FROM clause of their own query level" whereas the code
> under 2 is permitted. Unless I am much mistaken the latter is
> equivalent to the former because it just makes a new "local" name for
> `v`.
>
https://www.postgresql.org/docs/12/sql-select.html
Because step 2 precedes step 4.
> A. Am I right in thinking that the two forms are equivalent?
In so far as if the first one could be executed it would provide the same
result, yes...i think
>
> A1. And am I right to presume that it's always possibly to rewrite
> more complicated examples that yield the same error to valid versions,
> just by coming up with a local name for the problematic fields?
Don’t feel like figuring out a counter-example, your given example is not
compelling enough
>
> B. If they are indeed equivalent, what is the rationale for forbidding
> the former? It seems like it would be more convenient to allow users
> to write the former form.
It wasn’t directly intentional. Lateral came long after from/group by.
But since it extends the from clause the processing order puts it before
aggregation.
> 1. Causes error:
>
> SELECT * FROM
> ((SELECT 0 as "v") as "T1"
> INNER JOIN LATERAL
> (SELECT
> SUM("v") as "r"
> FROM (SELECT 0) as "T1") as "T2"
> ON TRUE) as "T1"
>
> 2. Runs successfully
>
> SELECT * FROM
> ((SELECT 0 as "v") as "T1"
> INNER JOIN LATERAL
> (SELECT
> SUM("v_again") as "r"
> FROM (SELECT "v" as "v_again") as "T1") as "T2"
> ON TRUE) as "T1"
>
>
> By the way, the only reference to this issue that I can find on the
> entire internet is the following old mailing list post:
>
> https://www.postgresql.org/message-id/1375925710.17807.
> 13.camel%40vanquo.pezone.net
Yeah, because usually one just writes your example:
Select sum(“v”) from (select 0 as “v”) as t1 group by “v”;
> I also posted on DBA.StackExchange
> https://dba.stackexchange.com/questions/266988/why-is-it-
> that-aggregate-functions-are-not-allowed-in-from-clause-of-their-own
>
>
I did not.
David J.
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Why is it that “aggregate functions are not allowed in FROM clause of their own query level”?
@ 2020-05-13 14:57 Tom Lane <tgl@sss.pgh.pa.us>
parent: Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
1 sibling, 0 replies; 3+ messages in thread
From: Tom Lane @ 2020-05-13 14:57 UTC (permalink / raw)
To: Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>; +Cc: pgsql-general@lists.postgresql.org
Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk> writes:
> A. Am I right in thinking that the two forms are equivalent?
No. In the first case the SUM() aggregate does not use any variables
belonging to the "LATERAL (SELECT ..." query level, therefore, per
SQL standard, it is not an aggregate of that query but an aggregate
of the next query level up, whose variable(s) it does use. And
within the structure of *that* query level, it's in the wrong place.
> A1. And am I right to presume that it's always possibly to rewrite
> more complicated examples that yield the same error to valid versions,
> just by coming up with a local name for the problematic fields?
In the particular case here, the problem is to control which query level
the aggregate is deemed to belong to. I think the issue is less about
"can I rewrite the query" and more about "do I actually understand the
semantics this is asking for", so I'd be hesitant to let a tool think
that it can rearrange things like this.
regards, tom lane
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2020-05-13 14:57 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2020-05-13 11:02 Why is it that “aggregate functions are not allowed in FROM clause of their own query level”? Tom Ellis <tom-postgresql.org@jaguarpaw.co.uk>
2020-05-13 13:43 ` David G. Johnston <david.g.johnston@gmail.com>
2020-05-13 14:57 ` Tom Lane <tgl@sss.pgh.pa.us>
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