Re: Contradictory behavior of array_agg(distinct) aggregate. - Mailing list pgsql-hackers

From Tom Lane
Subject Re: Contradictory behavior of array_agg(distinct) aggregate.
Date
Msg-id 2282300.1733295797@sss.pgh.pa.us
Whole thread Raw
In response to Contradictory behavior of array_agg(distinct) aggregate.  (Konstantin Knizhnik <knizhnik@garret.ru>)
Responses Re: Contradictory behavior of array_agg(distinct) aggregate.
List pgsql-hackers
Konstantin Knizhnik <knizhnik@garret.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



pgsql-hackers by date:

Previous
From: Konstantin Knizhnik
Date:
Subject: Contradictory behavior of array_agg(distinct) aggregate.
Next
From: Konstantin Knizhnik
Date:
Subject: Re: Contradictory behavior of array_agg(distinct) aggregate.