Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIjPv-00GBXR-Sj for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Dec 2024 07:03:23 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tIjPt-00FYhj-52 for pgsql-hackers@arkaria.postgresql.org; Wed, 04 Dec 2024 07:03:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIjPs-00FYhb-Rt for pgsql-hackers@lists.postgresql.org; Wed, 04 Dec 2024 07:03:22 +0000 Received: from sss.pgh.pa.us ([68.162.161.243]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tIjPr-000xsi-Qr for pgsql-hackers@postgresql.org; Wed, 04 Dec 2024 07:03:20 +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 4B473Hxm2282301; Wed, 4 Dec 2024 02:03:17 -0500 From: Tom Lane To: Konstantin Knizhnik cc: PostgreSQL Hackers Subject: Re: Contradictory behavior of array_agg(distinct) aggregate. In-reply-to: <7d3268df-abc2-4eb3-8e2b-59df084b579a@garret.ru> References: <7d3268df-abc2-4eb3-8e2b-59df084b579a@garret.ru> Comments: In-reply-to Konstantin Knizhnik message dated "Wed, 04 Dec 2024 08:44:07 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <2282299.1733295797.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Wed, 04 Dec 2024 02:03:17 -0500 Message-ID: <2282300.1733295797@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Konstantin Knizhnik 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