public inbox for [email protected]  
help / color / mirror / Atom feed
From: Tom Lane <[email protected]>
To: Konstantin Knizhnik <[email protected]>
Cc: PostgreSQL Hackers <[email protected]>
Subject: Re: Contradictory behavior of array_agg(distinct) aggregate.
Date: Wed, 04 Dec 2024 02:03:17 -0500
Message-ID: <[email protected]> (raw)
In-Reply-To: <[email protected]>
References: <[email protected]>

Konstantin Knizhnik <[email protected]> writes:
> postgres=# create table t(x integer unique);
> CREATE TABLE
> postgres=# insert into t values (null),(null);
> INSERT 0 2
> postgres=# select count(distinct x) from t;
>   count
> -------
>       0
> (1 row)

> postgres=# select array_agg(distinct x) from t;
>   array_agg
> -----------
>   {NULL}
> (1 row)

> postgres=# select array_agg(x) from t;
>    array_agg
> -------------
>   {NULL,NULL}
> (1 row)

I see nothing contradictory here.  "array_agg(distinct x)"
combines the two NULLs into one, which is the normal
behavior of DISTINCT.  "count(distinct x)" does the same
thing --- but count() only counts non-null inputs, so
you end with zero.

			regards, tom lane






view thread (3+ messages)

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected]
  Subject: Re: Contradictory behavior of array_agg(distinct) aggregate.
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox