Re: Contradictory behavior of array_agg(distinct) aggregate.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Konstantin Knizhnik <knizhnik(at)garret(dot)ru>
Cc: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: Contradictory behavior of array_agg(distinct) aggregate.
Date: 2024-12-04 07:03:17
Message-ID: 2282300.1733295797@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Konstantin Knizhnik <knizhnik(at)garret(dot)ru> 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

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Konstantin Knizhnik 2024-12-04 07:20:24 Re: Contradictory behavior of array_agg(distinct) aggregate.
Previous Message Alexander Pyhalov 2024-12-04 06:59:10 Re: Partition-wise join with whole row vars