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 1jYpAC-00087D-9o for pgsql-general@arkaria.postgresql.org; Wed, 13 May 2020 11:03:00 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jYpAA-0006vV-Nv for pgsql-general@arkaria.postgresql.org; Wed, 13 May 2020 11:02:58 +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 1jYpAA-0006vO-Fy for pgsql-general@lists.postgresql.org; Wed, 13 May 2020 11:02:58 +0000 Received: from balrog.mythic-beasts.com ([2a00:1098:0:82:1000:0:2:1]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jYpA7-0004KD-0e for pgsql-general@lists.postgresql.org; Wed, 13 May 2020 11:02:57 +0000 Received: from [2a00:1098:86:79::1] (port=32978 helo=cloudinit-builder) by balrog.mythic-beasts.com with esmtpsa (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92.3) (envelope-from ) id 1jYpA5-0004DU-Ad for pgsql-general@lists.postgresql.org; Wed, 13 May 2020 12:02:53 +0100 Received: from tom (uid 1000) (envelope-from tom-2018@jaguarpaw.co.uk) id 805a7 by cloudinit-builder (DragonFly Mail Agent v0.11); Wed, 13 May 2020 12:02:51 +0100 Date: Wed, 13 May 2020 12:02:51 +0100 From: Tom Ellis To: pgsql-general@lists.postgresql.org Subject: Why is it that =?utf-8?B?4oCcYWdncmVnYXRl?= =?utf-8?Q?_functions_are_not_allowed_in_FROM_clause_of_their_own_query_l?= =?utf-8?B?ZXZlbOKAnT8=?= Message-ID: <20200513110251.GC24083@cloudinit-builder> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Penguin-0: * .^. * X-Penguin-1: - /V\ - X-Penguin-2: * // \\ * X-Penguin-3: - /( )\ - X-Penguin-4: * ^^-^^ * User-Agent: Mutt/1.10.1 (2018-07-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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-from-clause-of-their-own Thanks, Tom