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 1q00ry-0007QX-IO for pgsql-bugs@arkaria.postgresql.org; Fri, 19 May 2023 14:14:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1q00rw-0000X4-Ad for pgsql-bugs@arkaria.postgresql.org; Fri, 19 May 2023 14:14:08 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1q00rw-0000Wv-2J for pgsql-bugs@lists.postgresql.org; Fri, 19 May 2023 14:14:08 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1q00rt-000gVk-CW for pgsql-bugs@lists.postgresql.org; Fri, 19 May 2023 14:14:06 +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 34JEE2cn035908; Fri, 19 May 2023 10:14:03 -0400 From: Tom Lane To: sulfinu@gmail.com cc: pgsql-bugs@lists.postgresql.org Subject: Re: Aggregation results with json(b)_agg and array_agg in a SELECT with OUTER JOIN In-reply-to: References: Comments: In-reply-to sulfinu@gmail.com message dated "Fri, 19 May 2023 15:46:35 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <35906.1684505642.1@sss.pgh.pa.us> Date: Fri, 19 May 2023 10:14:02 -0400 Message-ID: <35907.1684505642@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk sulfinu@gmail.com writes: > This problem was noticed in version *15.1*, as I don't have the latest > version at hand, but I've read the release notes for *15.2* and *15.3* and > there's no sign of any related change. It's about the outcome of a SELECT > statement which involves computing aggregate functions like json_agg, > jsonb_agg and array_agg over columns that originate from outer-joined > entities, when these entities are *missing*. I see no bug here. json_agg is defined as aggregating results equivalent to what to_json() would produce, and what to_json() would produce for NULL input is a NULL. > *I expect the aggregation to produce either > [{"name":null,"a":null}] or the SQL NULL for it, preferrably the latter.* AFAICS, it *does* produce the latter, so you are not making yourself very clear here. Note that Postgres does draw a distinction between a NULL of composite type and a value of composite type whose fields all happen to be NULL. The SQL spec is a bit squishy on this, in that they require a "composite_value IS NULL" test to return TRUE for both cases, but careful parsing of the spec seems to indicate that they aren't saying there is no distinction. > Returning a simple SQL NULL is the most appropriate choice, in line with the > general rule that aggregations over zero rows return NULL > . Um, you did *not* aggregate over zero rows: the FROM/GROUP BY construct definitely produced a row for a.id = 2. > Although there is a workaround for this problem, it implies checking a > stupid condition for absolutely every row in the JOIN result: > select a.*, json_agg(b) filter (where b.name is not null) from > a > left join b on b.a = a.id > group by a.id; json_agg_strict() is easier. regards, tom lane