Re: Aggregation results with json(b)_agg and array_agg in a SELECT with OUTER JOIN

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: sulfinu(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: Re: Aggregation results with json(b)_agg and array_agg in a SELECT with OUTER JOIN
Date: 2023-05-19 14:36:49
Message-ID: CAKFQuwazRkKKEe+rMZcXKcKWiO-QwcOQNviV5tf0KzGTx_KQ-A@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

On Fri, May 19, 2023 at 7:14 AM Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:

>
> > *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.
>

The OP is correct, the result for json_agg on an outer join where the input
is a composite column of the nullable-side of the join is a json array with
a single json null value. Likewise, for array_agg we produce a length one
array with a single SQL NULL.

I agree that, in at least the json_agg case, the json array that is
produced should be an json object with keys matching the names of the
fields of the composite. Absent that, representing "found no rows on the
nullable side of the join" should be represented by SQL NULL as the overall
result. Producing a value in the JSON array that isn't an object when the
input is a composite is a POLA violation.

That all said, it seems near impossible to change this behavior now. But
adding a note to the effect of: when aggregating a composite where the
fields are all null the simple null representation form will be used in the
resultant array instead of producing an object where all keys have null
values. (I haven't experimented with cases where there are matching rows in
the outer join but all the relevant columns actually end up with null
values in them)

David J.

In response to

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Tom Lane 2023-05-19 15:12:54 Re: Aggregation results with json(b)_agg and array_agg in a SELECT with OUTER JOIN
Previous Message Tom Lane 2023-05-19 14:14:02 Re: Aggregation results with json(b)_agg and array_agg in a SELECT with OUTER JOIN