Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvMLw-00BTtb-Gq for pgsql-bugs@arkaria.postgresql.org; Fri, 12 Apr 2024 19:14:25 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1rvMLu-00HGgs-5o for pgsql-bugs@arkaria.postgresql.org; Fri, 12 Apr 2024 19:14:22 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvMLt-00HGgj-Te for pgsql-bugs@lists.postgresql.org; Fri, 12 Apr 2024 19:14:21 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1rvMLr-000b4K-BL for pgsql-bugs@lists.postgresql.org; Fri, 12 Apr 2024 19:14:21 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 43CJEFNW483377; Fri, 12 Apr 2024 15:14:15 -0400 From: Tom Lane To: Eric Atkin cc: Laurenz Albe , pgsql-bugs@lists.postgresql.org Subject: Re: BUG #18430: syntax error when using aggregate function in where clause of subquery In-reply-to: References: <18430-f06d523d31f29af7@postgresql.org> Comments: In-reply-to Eric Atkin message dated "Fri, 12 Apr 2024 12:35:46 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <483375.1712949255.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Fri, 12 Apr 2024 15:14:15 -0400 Message-ID: <483376.1712949255@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Eric Atkin writes: > It seems the aggregate function call should be evaluated at the outer la= yer > (where it would not be bad syntax) and then is a constant for the inner > query where clause where an array would be allowed. Correct, but the problem is not with the array_agg call, it's with your use of IN. In the first place, IN requires parens around its righthand side. But that only gets us past "syntax error": regression=3D# SELECT city, (SELECT count(*) FROM delivery WHERE driver_id IN (array_agg(driver.id= ))) AS deliveries FROM driver GROUP BY city ; ERROR: operator does not exist: integer =3D integer[] LINE 3: ... (SELECT count(*) FROM delivery WHERE driver_id IN (array_... ^ HINT: No operator matches the given name and argument types. You might ne= ed to add explicit type casts. That's because what's inside the parens is supposed to be either a sub-select or a list of things directly comparable to the LHS. To do what you're after, you need to use the "scalar =3D ANY(array)" construct: regression=3D# SELECT city, (SELECT count(*) FROM delivery WHERE driver_id =3D any (array_agg(driv= er.id))) AS deliveries FROM driver GROUP BY city ; city | deliveries = ------+------------ (0 rows) IN and =3DANY are more-or-less equivalent when the RHS is a sub-select, but not for an array RHS. There's nothing particularly wrong with Laurenz's construction, but it's not necessary to split it up like that. regards, tom lane