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 1shsAM-00AbQO-SV for pgsql-bugs@arkaria.postgresql.org; Sat, 24 Aug 2024 14:54:58 +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 1shsAK-006KNa-88 for pgsql-bugs@arkaria.postgresql.org; Sat, 24 Aug 2024 14:54:56 +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 1shsAJ-006KNR-Vz for pgsql-bugs@lists.postgresql.org; Sat, 24 Aug 2024 14:54:56 +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 1shsAE-001ECn-9e for pgsql-bugs@lists.postgresql.org; Sat, 24 Aug 2024 14:54:55 +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 47OEsm482411055; Sat, 24 Aug 2024 10:54:48 -0400 From: Tom Lane To: "Plettenbacher, Tobias (LWF)" cc: "pgsql-bugs@lists.postgresql.org" Subject: Re: Bug or strange result of Max() on arrays containing NULL values In-reply-to: <325d78c062e945ebb286091f0eaa8b72@lwf.bayern.de> References: <325d78c062e945ebb286091f0eaa8b72@lwf.bayern.de> Comments: In-reply-to "Plettenbacher, Tobias (LWF)" message dated "Sat, 24 Aug 2024 12:54:26 -0000" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2411053.1724511288.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sat, 24 Aug 2024 10:54:48 -0400 Message-ID: <2411054.1724511288@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk "Plettenbacher, Tobias (LWF)" writes: > With Max(ARRAY[]) I get a strange result (in this case {NULL,7}): > SELECT Max(ARRAY[Val, ID]) FROM (VALUES (1, 5), (2, 6), (NULL, 7)) AS T(= Val, ID); This is the expected result, because =3D# select array[null, 7] > array[2, 6]; ?column? = ---------- t (1 row) When comparing array elements (or members of any container type), we treat two nulls as equal and a null as larger than any non-null. You might think that such a comparison should yield null, but if we did that then the comparisons would fail to provide a total order for the container type. That would, among other things, break the ability to build b-tree indexes on such types. regards, tom lane